1 8 package org.codehaus.aspectwerkz.transform.inlining; 9 10 import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer; 11 import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor; 12 import org.codehaus.aspectwerkz.transform.Context; 13 import org.objectweb.asm.Label; 14 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.util.HashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.ArrayList ; 21 import java.util.Set ; 22 23 import gnu.trove.TObjectIntHashMap; 24 25 31 public class ContextImpl implements Context { 32 35 private final String m_className; 36 37 40 private final byte[] m_initialBytecode; 41 42 45 private byte[] m_currentBytecode; 46 47 50 private final ClassLoader m_loader; 51 52 55 private boolean m_advised = false; 56 57 60 private boolean m_madeAdvisable = false; 61 62 65 private boolean m_readOnly = false; 66 67 70 private Map m_metaData = new HashMap (); 71 72 75 private final Set m_definitions; 76 77 80 private final List m_emittedJoinPoints = new ArrayList (); 81 82 86 private final TObjectIntHashMap m_labelTolineNumbers = new TObjectIntHashMap(); 87 88 private long m_serialVerUid; 89 94 public ContextImpl(final String className, final byte[] bytecode, final ClassLoader loader) { 95 m_className = className.replace('.', '/'); 96 m_loader = loader; 97 m_initialBytecode = bytecode; 98 m_currentBytecode = bytecode; 99 m_definitions = SystemDefinitionContainer.getDefinitionsFor(m_loader); 100 } 101 102 public String getClassName() { 103 return m_className; 104 } 105 106 111 public byte[] getInitialBytecode() { 112 return m_initialBytecode; 113 } 114 115 120 public byte[] getCurrentBytecode() { 121 return m_currentBytecode; 122 } 123 124 129 public void setCurrentBytecode(final byte[] bytecode) { 130 m_currentBytecode = bytecode; 131 } 132 133 138 public ClassLoader getLoader() { 139 return m_loader; 140 } 141 142 147 public Set getDefinitions() { 148 return m_definitions; 149 } 150 151 154 public void markAsAdvised() { 155 m_advised = true; 156 } 157 158 161 public void markMadeAdvisable() { 162 m_madeAdvisable = true; 163 } 164 165 168 public void resetAdvised() { 169 m_advised = false; 170 } 171 172 177 public boolean isAdvised() { 178 return m_advised; 179 } 180 181 186 public boolean isMadeAdvisable() { 187 return m_madeAdvisable; 188 } 189 190 193 public void markAsReadOnly() { 194 m_readOnly = true; 195 } 196 197 202 public boolean isReadOnly() { 203 return m_readOnly; 204 } 205 206 212 public Object getMetaData(final Object key) { 213 return m_metaData.get(key); 214 } 215 216 222 public void addMetaData(final Object key, final Object value) { 223 if (m_readOnly) { 224 throw new IllegalStateException ("context is read only"); 225 } 226 m_metaData.put(key, value); 227 } 228 229 234 public void dump(final String dumpDir) { 235 try { 236 int lastSegmentIndex = m_className.lastIndexOf('/'); 237 if (lastSegmentIndex < 0) { 238 lastSegmentIndex = 0; 239 } 240 File dir = new File (dumpDir + File.separator + m_className.substring(0, lastSegmentIndex)); 241 dir.mkdirs(); 242 FileOutputStream os = new FileOutputStream ( 243 dumpDir 244 + File.separator 245 + m_className.replace('.', '/') 246 + ".class" 247 ); 248 os.write(m_currentBytecode); 249 os.close(); 250 } catch (Exception e) { 251 AspectWerkzPreProcessor.log("failed to dump " + m_className); 252 e.printStackTrace(); 253 } 254 } 255 256 261 public void addEmittedJoinPoint(final EmittedJoinPoint jp) { 262 m_emittedJoinPoints.add(jp); 263 } 264 265 270 public List getEmittedJoinPoints() { 271 return m_emittedJoinPoints; 272 } 273 274 public void setSerialVerUid(long initialSerialVerUid) { 275 m_serialVerUid = initialSerialVerUid; 276 } 277 278 public long getSerialVerUid() { 279 return m_serialVerUid; 280 } 281 282 public void addLineNumberInfo(Label label, int lineNumber) { 283 m_labelTolineNumbers.put(label, lineNumber); 284 } 285 286 292 public int resolveLineNumberInfo(Label label) { 293 if (m_labelTolineNumbers.containsKey(label)) { 294 return m_labelTolineNumbers.get(label); 295 } else { 296 return 0; 297 } 298 } 299 300 } | Popular Tags |