1 4 package com.tc.aspectwerkz.transform; 5 6 import com.tc.asm.Label; 7 8 import com.tc.aspectwerkz.definition.SystemDefinitionContainer; 9 import com.tc.aspectwerkz.proxy.ProxyDelegationStrategy; 10 import com.tc.aspectwerkz.proxy.ProxySubclassingStrategy; 11 import com.tc.aspectwerkz.transform.inlining.EmittedJoinPoint; 12 13 import java.io.File ; 14 import java.io.FileOutputStream ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.ArrayList ; 19 import java.util.Set ; 20 21 22 28 public class InstrumentationContext { 29 32 private final String m_className; 33 34 37 private final byte[] m_initialBytecode; 38 39 42 private byte[] m_currentBytecode; 43 44 47 private final ClassLoader m_loader; 48 49 52 private boolean m_advised = false; 53 54 57 private boolean m_madeAdvisable = false; 58 59 62 private boolean m_isProxy = false; 63 64 67 private boolean m_readOnly = false; 68 69 72 private Map m_metaData = new HashMap (); 73 74 77 private final Set m_definitions; 78 79 82 private final List m_emittedJoinPoints = new ArrayList (); 83 84 88 private final HashMap m_labelTolineNumbers = new HashMap (); 89 90 private long m_serialVerUid; 91 92 95 public InstrumentationContext(final String className, final byte[] bytecode, final ClassLoader loader, final Set definitions) { 96 m_className = className.replace('.', '/'); 97 m_loader = loader; 98 m_initialBytecode = bytecode; 99 m_currentBytecode = bytecode; 100 m_definitions = definitions; 101 if (isAWProxy(className) || isCGLIBProxy(className) || isDynamicProxy(className)) { 102 markAsProxy(); 103 } 104 } 105 106 109 public InstrumentationContext(final String className, final byte[] bytecode, final ClassLoader loader) { 110 this(className, bytecode, loader, SystemDefinitionContainer.getDefinitionsFor(loader)); 111 } 112 113 public String getClassName() { 114 return m_className; 115 } 116 117 122 public byte[] getInitialBytecode() { 123 return m_initialBytecode; 124 } 125 126 131 public byte[] getCurrentBytecode() { 132 return m_currentBytecode; 133 } 134 135 140 public void setCurrentBytecode(final byte[] bytecode) { 141 m_currentBytecode = bytecode; 142 } 143 144 149 public ClassLoader getLoader() { 150 return m_loader; 151 } 152 153 158 public Set getDefinitions() { 159 return m_definitions; 160 } 161 162 165 public void markAsAdvised() { 166 m_advised = true; 167 } 168 169 172 public void markMadeAdvisable() { 173 m_madeAdvisable = true; 174 } 175 176 179 public void resetAdvised() { 180 m_advised = false; 181 } 182 183 186 public boolean isProxy() { 187 return m_isProxy; 188 } 189 190 193 public void markAsProxy() { 194 m_isProxy = true; 195 } 196 197 202 public boolean isAdvised() { 203 return m_advised; 204 } 205 206 211 public boolean isMadeAdvisable() { 212 return m_madeAdvisable; 213 } 214 215 218 public void markAsReadOnly() { 219 m_readOnly = true; 220 } 221 222 227 public boolean isReadOnly() { 228 return m_readOnly; 229 } 230 231 237 public Object getMetaData(final Object key) { 238 return m_metaData.get(key); 239 } 240 241 247 public void addMetaData(final Object key, final Object value) { 248 if (m_readOnly) { 249 throw new IllegalStateException ("context is read only"); 250 } 251 m_metaData.put(key, value); 252 } 253 254 259 public void dump(final String dumpDir) { 260 try { 261 int lastSegmentIndex = m_className.lastIndexOf('/'); 262 if (lastSegmentIndex < 0) { 263 lastSegmentIndex = 0; 264 } 265 File dir = new File (dumpDir + File.separator + m_className.substring(0, lastSegmentIndex)); 266 dir.mkdirs(); 267 FileOutputStream os = new FileOutputStream ( 268 dumpDir 269 + File.separator 270 + m_className.replace('.', '/') 271 + ".class" 272 ); 273 os.write(m_currentBytecode); 274 os.close(); 275 } catch (Exception e) { 276 System.err.println("failed to dump " + m_className); 277 e.printStackTrace(); 278 } 279 } 280 281 286 public void addEmittedJoinPoint(final EmittedJoinPoint jp) { 287 m_emittedJoinPoints.add(jp); 288 } 289 290 295 public List getEmittedJoinPoints() { 296 return m_emittedJoinPoints; 297 } 298 299 public void setSerialVerUid(long initialSerialVerUid) { 300 m_serialVerUid = initialSerialVerUid; 301 } 302 303 public long getSerialVerUid() { 304 return m_serialVerUid; 305 } 306 307 public void addLineNumberInfo(Label label, int lineNumber) { 308 m_labelTolineNumbers.put(label, new Integer (lineNumber)); 309 } 310 311 317 public int resolveLineNumberInfo(Label label) { 318 Integer info = (Integer ) m_labelTolineNumbers.get(label); 319 return info==null ? 0 : info.intValue(); 320 } 321 322 public static boolean isAWProxy(final String className) { 323 return className.indexOf(ProxySubclassingStrategy.PROXY_SUFFIX) != -1 324 || className.indexOf(ProxyDelegationStrategy.PROXY_SUFFIX) != -1; 325 } 326 327 public static boolean isCGLIBProxy(final String className) { 328 return className.indexOf("$$EnhancerByCGLIB$$") != -1 329 || className.indexOf("$$FastClassByCGLIB$$") != -1; 330 } 331 332 private boolean isDynamicProxy(final String className) { 333 return className.startsWith("$Proxy"); 334 } 335 } | Popular Tags |