1 4 package com.tc.aspectwerkz.transform.inlining.compiler; 5 6 import java.util.Map ; 7 import java.util.Iterator ; 8 import java.util.WeakHashMap ; 9 import java.util.HashSet ; 10 import java.util.Set ; 11 12 import com.tc.aspectwerkz.joinpoint.management.JoinPointType; 13 import com.tc.aspectwerkz.reflect.impl.asm.AsmClassInfo; 14 import com.tc.aspectwerkz.reflect.ClassInfo; 15 import com.tc.aspectwerkz.reflect.MethodInfo; 16 import com.tc.aspectwerkz.transform.inlining.AsmHelper; 17 import com.tc.aspectwerkz.transform.inlining.EmittedJoinPoint; 18 import com.tc.aspectwerkz.expression.PointcutType; 19 import com.tc.aspectwerkz.expression.ExpressionContext; 20 import com.tc.aspectwerkz.expression.ExpressionInfo; 21 22 25 public class CompilerHelper { 26 27 30 private static final Map COMPILATION_INFO_REPOSITORY = new WeakHashMap (); 31 32 39 public static Class compileJoinPointAndAttachToClassLoader(final CompilationInfo.Model model, 40 final ClassLoader loader) { 41 return attachToClassLoader(model.getJoinPointClassName(), loader, compileJoinPoint(model)); 42 } 43 44 52 public static Class attachToClassLoader(final String joinpointClassName, 53 final ClassLoader loader, 54 final byte[] bytecode) { 55 return AsmHelper.defineClass(loader, bytecode, joinpointClassName); 56 } 57 58 64 public static void addCompilationInfo(final Class clazz, final CompilationInfo compilationInfo) { 65 COMPILATION_INFO_REPOSITORY.put(clazz, compilationInfo); 66 } 67 68 74 public static byte[] compileJoinPoint(final CompilationInfo.Model model) { 75 switch (model.getEmittedJoinPoint().getJoinPointType()) { 76 case JoinPointType.METHOD_EXECUTION_INT: 77 return new MethodExecutionJoinPointCompiler(model).compile(); 78 case JoinPointType.METHOD_CALL_INT: 79 return new MethodCallJoinPointCompiler(model).compile(); 80 case JoinPointType.CONSTRUCTOR_EXECUTION_INT: 81 return new ConstructorExecutionJoinPointCompiler(model).compile(); 82 case JoinPointType.CONSTRUCTOR_CALL_INT: 83 return new ConstructorCallJoinPointCompiler(model).compile(); 84 case JoinPointType.FIELD_SET_INT: 85 return new FieldSetJoinPointCompiler(model).compile(); 86 case JoinPointType.FIELD_GET_INT: 87 return new FieldGetJoinPointCompiler(model).compile(); 88 case JoinPointType.HANDLER_INT: 89 return new HandlerJoinPointCompiler(model).compile(); 90 case JoinPointType.STATIC_INITIALIZATION_INT: 91 return new StaticInitializationJoinPointCompiler(model).compile(); 92 default: 93 throw new UnsupportedOperationException ( 94 "join point type is not supported: " + model.getEmittedJoinPoint().getJoinPointType() 95 ); 96 } 97 } 98 99 105 public static byte[] redefineJoinPoint(final CompilationInfo compilationInfo) { 106 switch (compilationInfo.getInitialModel().getEmittedJoinPoint().getJoinPointType()) { 107 case JoinPointType.METHOD_EXECUTION_INT: 108 return new MethodExecutionJoinPointRedefiner(compilationInfo).compile(); 109 case JoinPointType.METHOD_CALL_INT: 110 return new MethodCallJoinPointRedefiner(compilationInfo).compile(); 111 case JoinPointType.CONSTRUCTOR_EXECUTION_INT: 112 return new ConstructorExecutionJoinPointRedefiner(compilationInfo).compile(); 113 case JoinPointType.CONSTRUCTOR_CALL_INT: 114 return new ConstructorCallJoinPointRedefiner(compilationInfo).compile(); 115 case JoinPointType.FIELD_SET_INT: 116 return new FieldSetJoinPointRedefiner(compilationInfo).compile(); 117 case JoinPointType.FIELD_GET_INT: 118 return new FieldGetJoinPointRedefiner(compilationInfo).compile(); 119 case JoinPointType.HANDLER_INT: 120 return new HandlerJoinPointRedefiner(compilationInfo).compile(); 121 default: 122 throw new UnsupportedOperationException ( 123 "join point type is not supported: " + 124 compilationInfo.getInitialModel().getEmittedJoinPoint().getJoinPointType() 125 ); 126 } 127 } 128 129 138 public static Set getJoinPointsMatching(final ExpressionInfo expression) { 139 final Set matchingJoinPointInfos = new HashSet (); 140 for (Iterator it = COMPILATION_INFO_REPOSITORY.entrySet().iterator(); it.hasNext();) { 141 final Map.Entry entry = (Map.Entry ) it.next(); 142 143 final Class clazz = (Class ) entry.getKey(); 144 final CompilationInfo compilationInfo = (CompilationInfo) entry.getValue(); 145 final EmittedJoinPoint joinPoint = (EmittedJoinPoint) compilationInfo. 146 getInitialModel().getEmittedJoinPoint(); 147 final ClassLoader loader = clazz.getClassLoader(); 148 149 final ClassInfo calleeClassInfo = AsmClassInfo.getClassInfo(joinPoint.getCalleeClassName(), loader); 150 final ClassInfo callerClassInfo = AsmClassInfo.getClassInfo(joinPoint.getCallerClassName(), loader); 151 final MethodInfo callerMethodInfo = getCallerMethodInfo(callerClassInfo, joinPoint); 152 153 ExpressionContext ctx = null; 154 switch (joinPoint.getJoinPointType()) { 155 case JoinPointType.METHOD_EXECUTION_INT: 156 ctx = new ExpressionContext( 157 PointcutType.EXECUTION, 158 calleeClassInfo.getMethod(joinPoint.getJoinPointHash()), 159 callerMethodInfo 160 ); 161 break; 162 case JoinPointType.METHOD_CALL_INT: 163 ctx = new ExpressionContext( 164 PointcutType.CALL, 165 calleeClassInfo.getMethod(joinPoint.getJoinPointHash()), 166 callerMethodInfo 167 ); 168 break; 169 case JoinPointType.CONSTRUCTOR_EXECUTION_INT: 170 ctx = new ExpressionContext( 171 PointcutType.EXECUTION, 172 calleeClassInfo.getConstructor(joinPoint.getJoinPointHash()), 173 callerMethodInfo 174 ); 175 break; 176 case JoinPointType.CONSTRUCTOR_CALL_INT: 177 ctx = new ExpressionContext( 178 PointcutType.CALL, 179 calleeClassInfo.getConstructor(joinPoint.getJoinPointHash()), 180 callerMethodInfo 181 ); 182 break; 183 case JoinPointType.FIELD_SET_INT: 184 ctx = new ExpressionContext( 185 PointcutType.SET, 186 calleeClassInfo.getField(joinPoint.getJoinPointHash()), 187 callerMethodInfo 188 ); 189 break; 190 case JoinPointType.FIELD_GET_INT: 191 ctx = new ExpressionContext( 192 PointcutType.GET, 193 calleeClassInfo.getField(joinPoint.getJoinPointHash()), 194 callerMethodInfo 195 ); 196 break; 197 case JoinPointType.HANDLER_INT: 198 ctx = new ExpressionContext( 199 PointcutType.HANDLER, 200 AsmClassInfo.getClassInfo(joinPoint.getCalleeClassName(), loader), 201 callerMethodInfo 202 ); 203 break; 204 case JoinPointType.STATIC_INITIALIZATION_INT: 205 ctx = new ExpressionContext( 206 PointcutType.STATIC_INITIALIZATION, 207 calleeClassInfo.staticInitializer(), 208 calleeClassInfo 209 ); 210 } 211 if (expression.getExpression().match(ctx)) { 212 matchingJoinPointInfos.add(new MatchingJoinPointInfo(clazz, compilationInfo, ctx)); 213 } 214 } 215 return matchingJoinPointInfos; 216 } 217 218 224 public static EmittedJoinPoint getEmittedJoinPoint(final Class clazz) { 225 return (EmittedJoinPoint) COMPILATION_INFO_REPOSITORY.get(clazz); 226 } 227 228 235 private static MethodInfo getCallerMethodInfo(final ClassInfo callerClassInfo, 236 final EmittedJoinPoint emittedJoinPoint) { 237 MethodInfo callerMethodInfo = null; 238 MethodInfo[] callerMethods = callerClassInfo.getMethods(); 239 for (int i = 0; i < callerMethods.length; i++) { 240 MethodInfo method = callerMethods[i]; 241 if (method.getName().equals(emittedJoinPoint.getCallerMethodName()) && 242 method.getSignature().equals(emittedJoinPoint.getCallerMethodDesc())) { 243 callerMethodInfo = method; 244 break; 245 } 246 } 247 return callerMethodInfo; 248 } 249 } 250 | Popular Tags |