1 4 package com.tc.aspectwerkz.proxy; 5 6 import java.lang.reflect.Field ; 7 import java.util.HashMap ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.Set ; 11 12 import com.tc.backport175.bytecode.AnnotationReader; 13 import com.tc.backport175.bytecode.spi.BytecodeProvider; 14 15 import com.tc.aspectwerkz.definition.SystemDefinition; 16 import com.tc.aspectwerkz.definition.SystemDefinitionContainer; 17 import com.tc.aspectwerkz.exception.WrappedRuntimeException; 18 import com.tc.aspectwerkz.joinpoint.management.JoinPointManager; 19 import com.tc.aspectwerkz.transform.InstrumentationContext; 20 import com.tc.aspectwerkz.transform.TransformationConstants; 21 import com.tc.aspectwerkz.transform.WeavingStrategy; 22 import com.tc.aspectwerkz.transform.inlining.AsmHelper; 23 import com.tc.aspectwerkz.transform.inlining.EmittedJoinPoint; 24 import com.tc.aspectwerkz.transform.inlining.ProxyWeavingStrategy; 25 26 29 public class ProxyCompilerHelper { 30 31 34 private final static WeavingStrategy s_weavingStrategy = new ProxyWeavingStrategy(); 35 36 45 public static Class weaveAndDefineProxyClass(final byte[] bytes, 46 final ClassLoader loader, 47 final String proxyClassName, 48 final SystemDefinition definition) { 49 50 AnnotationReader.setBytecodeProviderFor(proxyClassName, loader, new BytecodeProvider() { 53 public byte[] getBytecode(String className, ClassLoader l) throws Exception { 54 return bytes; 55 } 56 }); 57 58 61 final Set definitions; 62 if (definition == null) { 63 definitions = SystemDefinitionContainer.getDefinitionsFor(loader); 64 } else { 65 definitions = new HashSet (); 66 definitions.add(definition); 67 68 for (Iterator it = SystemDefinitionContainer.getDefinitionsFor(loader).iterator(); it.hasNext();) { 69 definitions.add(it.next()); 70 } 71 } 72 73 final InstrumentationContext ctx = new InstrumentationContext( 74 proxyClassName, 75 bytes, 76 loader, 77 definitions); 78 ctx.markAsProxy(); 79 80 s_weavingStrategy.transform(proxyClassName, ctx); 81 82 byte[] transformedBytes = ctx.getCurrentBytecode(); 83 84 return AsmHelper.defineClass(loader, transformedBytes, proxyClassName); 86 } 87 88 93 public static void compileJoinPoint(final Class proxyClass, SystemDefinition definition) { 94 try { 95 Field field = proxyClass.getDeclaredField(TransformationConstants.EMITTED_JOINPOINTS_FIELD_NAME); 96 field.setAccessible(true); 97 98 HashMap emittedJoinPoints = (HashMap ) field.get(null); 99 100 for (Iterator it = emittedJoinPoints.values().iterator(); it.hasNext();) { 104 EmittedJoinPoint emittedJoinPoint = (EmittedJoinPoint) it.next(); 105 JoinPointManager.loadJoinPoint( 106 emittedJoinPoint.getJoinPointType(), 107 proxyClass, 108 emittedJoinPoint.getCallerMethodName(), 109 emittedJoinPoint.getCallerMethodDesc(), 110 emittedJoinPoint.getCallerMethodModifiers(), 111 emittedJoinPoint.getCalleeClassName(), 112 emittedJoinPoint.getCalleeMemberName(), 113 emittedJoinPoint.getCalleeMemberDesc(), 114 emittedJoinPoint.getCalleeMemberModifiers(), 115 emittedJoinPoint.getJoinPointHash(), 116 emittedJoinPoint.getJoinPointClassName(), 117 definition); 118 } 119 } catch (NoSuchFieldException e) { 120 } catch (Exception e) { 122 throw new WrappedRuntimeException(e); 123 } 124 } 125 } 126 | Popular Tags |