1 8 package yapbaop.core; 9 10 import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer; 11 import org.codehaus.aspectwerkz.definition.SystemDefinition; 12 import org.codehaus.aspectwerkz.definition.AspectDefinition; 13 import org.codehaus.aspectwerkz.definition.AdviceDefinition; 14 import org.codehaus.aspectwerkz.reflect.ClassInfo; 15 import org.codehaus.aspectwerkz.reflect.ReflectHelper; 16 import org.codehaus.aspectwerkz.reflect.impl.asm.AsmClassInfo; 17 import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo; 18 import org.codehaus.aspectwerkz.aspect.AdviceType; 19 import org.codehaus.aspectwerkz.transform.inlining.AsmHelper; 20 import org.codehaus.aspectwerkz.transform.inlining.AspectModelManager; 21 import org.codehaus.aspectwerkz.transform.aopalliance.AopAllianceAspectModel; 22 import org.codehaus.aspectwerkz.org.objectweb.asm.Type; 23 import org.aopalliance.intercept.MethodInterceptor; 24 import org.aopalliance.intercept.MethodInvocation; 25 26 import java.lang.reflect.Method ; 27 28 31 public class Yapbaop extends AopAllianceAspectModel { 33 static { 35 System.setProperty("aspectwerkz.extension.aspectmodels", AopAllianceAspectModel.class.getName().replace('/', '.')); 36 AspectModelManager.getModels(); 37 } 38 39 private final static Method AOP_ALLIANCE_METHOD_AROUND; 41 static { 42 try { 43 AOP_ALLIANCE_METHOD_AROUND = MethodInterceptor.class.getDeclaredMethod( 44 "invoke", 45 new Class []{MethodInvocation.class} 46 ); 47 } catch (Throwable t) { 48 throw new Error (t.toString()); 49 } 50 } 51 52 public static Handle bindAspect(Class aopAllianceAspect, String pointcut) { 53 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 54 ClassInfo aspectInfo = JavaClassInfo.getClassInfo(aopAllianceAspect); 55 56 if (aspectInfo.getMethod(ReflectHelper.calculateHash(AOP_ALLIANCE_METHOD_AROUND)) == null) { 58 throw new RuntimeException ("Aspect is not AOP Alliance compatible - " + aopAllianceAspect.getName()); 59 } 60 61 SystemDefinition def = SystemDefinitionContainer.getVirtualDefinitionAt(cl); 62 AspectDefinition aspectDef = new AspectDefinition(aspectInfo.getName(), aspectInfo, def); 63 AdviceDefinition adviceDef = AdviceDefinition.newInstance( 64 "invoke", AdviceType.AROUND, 66 "execution("+pointcut+")", 67 null, 68 aspectInfo.getName(), 69 aspectInfo.getName(), 70 aspectInfo.getMethod(ReflectHelper.calculateHash(AOP_ALLIANCE_METHOD_AROUND)), 71 aspectDef 72 ); 73 74 aspectDef.setAspectModel(ASPECT_MODEL_TYPE); 76 aspectDef.setContainerClassName(ASPECT_CONTAINER_CLASS_NAME); 77 78 aspectDef.addAroundAdviceDefinition(adviceDef); 80 81 def.addAspect(aspectDef); 83 84 return new Handle(adviceDef); 85 } 86 87 public static class Handle { 88 private AdviceDefinition m_definition; 89 private Handle(AdviceDefinition def) { 90 m_definition = def; 91 } 92 public void unbind() { 93 m_definition.setExpressionInfo(null); 94 } 95 } 96 } 97 | Popular Tags |