| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.tracing; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.io.File; |
| 37 | | | import java.io.FileInputStream; |
| 38 | | | import java.io.FileOutputStream; |
| 39 | | | import java.io.FileReader; |
| 40 | | | import java.io.IOException; |
| 41 | | | import java.io.LineNumberReader; |
| 42 | | | import java.util.ArrayList; |
| 43 | | | import java.util.Iterator; |
| 44 | | | import java.util.List; |
| 45 | | | import java.util.logging.Level; |
| 46 | | | import java.util.logging.Logger; |
| 47 | | | |
| 48 | | | import org.jcoderz.commons.util.FileUtils; |
| 49 | | | import org.jcoderz.commons.util.IoUtil; |
| 50 | | | import org.jcoderz.commons.util.StringUtil; |
| 51 | | | import org.objectweb.asm.ClassReader; |
| 52 | | | import org.objectweb.asm.ClassWriter; |
| 53 | | | import org.objectweb.asm.Type; |
| 54 | | | import org.objectweb.asm.tree.ClassNode; |
| 55 | | | import org.objectweb.asm.tree.MethodNode; |
| 56 | | | |
| 57 | | | |
| 58 | | | |
| 59 | | (1) | |
| 60 | | (2) | |
| 61 | | (3) | {@link } |
| 62 | | | |
| 63 | | (4) | |
| 64 | | | |
| 65 | | | |
| 66 | | (5) | |
| 67 | | | |
| 68 | | | |
| 69 | | | @author |
| 70 | | | |
| 71 | 0 | (6) | public class TracingInjector |
| 72 | | | { |
| 73 | 0 | | static final Type THROWABLE_TYPE = Type.getType(Throwable.class); |
| 74 | | | static final String PREFIX = "LGI$"; |
| 75 | | | static final String LOCAL_PREFIX = "lgi$"; |
| 76 | | | static final String STATIC_LOGGER_FIELD_NAME |
| 77 | | | = PREFIX + "LGR"; |
| 78 | | | static final String IS_LOGGABLE_FIELD_NAME |
| 79 | | | = LOCAL_PREFIX + "isLoggable"; |
| 80 | | | static final String RESULT_VAR_FIELD_NAME |
| 81 | | | = LOCAL_PREFIX + "result"; |
| 82 | | | |
| 83 | 0 | | private static final String CLASSNAME = TracingInjector.class.getName(); |
| 84 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | @author |
| 89 | | | |
| 90 | | | public interface Matcher |
| 91 | | | { |
| 92 | | | |
| 93 | | | |
| 94 | | | @param |
| 95 | | | @return |
| 96 | | | |
| 97 | | (7)(8)(9)(10) | public boolean matches (ClassNode cn); |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | @param |
| 102 | | | @return |
| 103 | | | |
| 104 | | (11)(12)(13)(14) | public boolean matches (MethodNode mn); |
| 105 | | | |
| 106 | | | |
| 107 | | | |
| 108 | | | @param |
| 109 | | | @param |
| 110 | | | @return |
| 111 | | | |
| 112 | | (15)(16)(17)(18)(19) | public boolean matches (ClassNode cn, MethodNode mn); |
| 113 | | | } |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | @author |
| 118 | | | |
| 119 | 0 | | public static class MethodMatcher implements Matcher |
| 120 | | | { |
| 121 | | (20)(21) | public final static String COMMENT = "#"; |
| 122 | | (22)(23) | public final static String COMMENT2 = "//"; |
| 123 | 0 | (24) | final List mPatterString = new ArrayList(); |
| 124 | 0 | (25) | final List mPatterns = new ArrayList(); |
| 125 | | | |
| 126 | | | |
| 127 | | | |
| 128 | | | |
| 129 | | | @param |
| 130 | | | @throws |
| 131 | | | |
| 132 | | (26) | public MethodMatcher (File inFile) |
| 133 | | (27) | throws IOException |
| 134 | 0 | | { |
| 135 | 0 | | LineNumberReader reader = null; |
| 136 | 0 | | final FileReader fr = new FileReader(inFile); |
| 137 | | | try |
| 138 | | | { |
| 139 | 0 | | reader = new LineNumberReader(fr); |
| 140 | 0 | | String line = reader.readLine(); |
| 141 | 0 | | while (line != null) |
| 142 | | | { |
| 143 | 0 | | if (!StringUtil.isEmptyOrNull(line) && !line.startsWith(COMMENT) |
| 144 | | | && !line.startsWith(COMMENT2)) |
| 145 | | | { |
| 146 | 0 | | mPatterString.add(line.trim()); |
| 147 | 0 | | final AspectPattern aspectPattern = new AspectPattern(line.trim()); |
| 148 | 0 | | mPatterns.add(aspectPattern); |
| 149 | | | } |
| 150 | 0 | | line = reader.readLine(); |
| 151 | | | } |
| 152 | | | } |
| 153 | | | finally |
| 154 | | | { |
| 155 | 0 | | IoUtil.close(reader); |
| 156 | 0 | | IoUtil.close(fr); |
| 157 | 0 | | } |
| 158 | 0 | | } |
| 159 | | | |
| 160 | | | {@inheritDoc} |
| 161 | | | public String toString () |
| 162 | | | { |
| 163 | 0 | | return mPatterns.toString(); |
| 164 | | | } |
| 165 | | | |
| 166 | | | |
| 167 | | | @param |
| 168 | | | @return |
| 169 | | | |
| 170 | | (28)(29)(30)(31) | public boolean matches(ClassNode cn) |
| 171 | | | { |
| 172 | 0 | | final Iterator i = mPatterns.iterator(); |
| 173 | 0 | | boolean matched = false; |
| 174 | 0 | | while (i.hasNext() && !matched) |
| 175 | | | { |
| 176 | 0 | | final AspectPattern pattern = (AspectPattern) i.next(); |
| 177 | 0 | | matched |= pattern.matchClass(cn.name); |
| 178 | | (32) | |
| 179 | | | |
| 180 | 0 | | } |
| 181 | 0 | | return matched; |
| 182 | | | } |
| 183 | | | |
| 184 | | | |
| 185 | | | @param |
| 186 | | | @return |
| 187 | | | |
| 188 | | (33)(34)(35)(36) | public boolean matches(MethodNode mn) |
| 189 | | | { |
| 190 | 0 | (37) | throw new UnsupportedOperationException("TODO"); |
| 191 | | | } |
| 192 | | | |
| 193 | | | |
| 194 | | | @param |
| 195 | | | @param |
| 196 | | | @return |
| 197 | | | |
| 198 | | (38)(39)(40)(41)(42) | public boolean matches(ClassNode cn, MethodNode mn) |
| 199 | | | { |
| 200 | 0 | | final Iterator i = mPatterns.iterator(); |
| 201 | 0 | | boolean matched = false; |
| 202 | 0 | | while (i.hasNext() && !matched) |
| 203 | | | { |
| 204 | 0 | | final AspectPattern pattern = (AspectPattern) i.next(); |
| 205 | 0 | | if (pattern.matchClass(cn.name)) |
| 206 | | (43) | |
| 207 | | | { |
| 208 | 0 | | matched |= |
| 209 | | | pattern.matchAccess(mn.access) |
| 210 | | | && pattern.matchMethod(mn.desc); |
| 211 | | | } |
| 212 | 0 | | } |
| 213 | 0 | | return matched; |
| 214 | | | } |
| 215 | | | } |
| 216 | | | |
| 217 | | | |
| 218 | | | |
| 219 | | | @param |
| 220 | | | @param |
| 221 | | | @param |
| 222 | | | @param |
| 223 | | | @param |
| 224 | | | @throws |
| 225 | | | |
| 226 | | (44)(45)(46) | public static void inject (File inFile, File outFile, Matcher matcher, |
| 227 | | (47)(48) | boolean java5, boolean pai) |
| 228 | | (49) | throws IOException |
| 229 | | | { |
| 230 | 0 | | final ClassNode cn = new ClassNode(); |
| 231 | 0 | | final FileInputStream is = new FileInputStream(inFile); |
| 232 | | | try |
| 233 | | | { |
| 234 | 0 | | final ClassReader cr = new ClassReader(is); |
| 235 | 0 | | cr.accept(cn, 0); |
| 236 | | | } |
| 237 | | | finally |
| 238 | | | { |
| 239 | 0 | | IoUtil.close(is); |
| 240 | 0 | | } |
| 241 | 0 | | if (matcher.matches(cn)) |
| 242 | | | { |
| 243 | 0 | | if (logger.isLoggable(Level.FINE)) |
| 244 | | | { |
| 245 | 0 | | logger.fine("Will inject tracing into: " + cn.name); |
| 246 | | | } |
| 247 | 0 | | new ClassTracingInjector(cn, java5, pai).inject(matcher); |
| 248 | 0 | | final FileOutputStream os = new FileOutputStream(outFile); |
| 249 | | | try |
| 250 | | | { |
| 251 | 0 | | final ClassWriter cw = new ClassWriter( |
| 252 | | | ClassWriter.COMPUTE_MAXS); |
| 253 | 0 | | cn.accept(cw); |
| 254 | 0 | | os.write(cw.toByteArray()); |
| 255 | | | } |
| 256 | | | finally |
| 257 | | | { |
| 258 | 0 | | IoUtil.close(os); |
| 259 | 0 | | } |
| 260 | 0 | | } |
| 261 | | | else |
| 262 | | | { |
| 263 | 0 | | if (logger.isLoggable(Level.FINE)) |
| 264 | | | { |
| 265 | 0 | | logger.fine("Direct copy for: " + cn.name + " (no match)"); |
| 266 | | | } |
| 267 | 0 | | FileUtils.copy(inFile, outFile); |
| 268 | | | } |
| 269 | 0 | | } |
| 270 | | | } |