KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > yapbaop > core > Yapbaop


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

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 JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
30  */

31 public class Yapbaop extends AopAllianceAspectModel {//extend not required but wants to acess constants
32

33     // register the AOP Alliance model (do not require the -D option of AW ext container)
34
static {
35         System.setProperty("aspectwerkz.extension.aspectmodels", AopAllianceAspectModel.class.getName().replace('/', '.'));
36         AspectModelManager.getModels();
37     }
38
39     // the AOP Alliance aspect for method
40
private final static Method JavaDoc AOP_ALLIANCE_METHOD_AROUND;
41     static {
42         try {
43             AOP_ALLIANCE_METHOD_AROUND = MethodInterceptor.class.getDeclaredMethod(
44                     "invoke",
45                     new Class JavaDoc[]{MethodInvocation.class}
46             );
47         } catch (Throwable JavaDoc t) {
48             throw new Error JavaDoc(t.toString());
49         }
50     }
51
52     public static Handle bindAspect(Class JavaDoc aopAllianceAspect, String JavaDoc pointcut) {
53         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
54         ClassInfo aspectInfo = JavaClassInfo.getClassInfo(aopAllianceAspect);
55
56         // check some
57
if (aspectInfo.getMethod(ReflectHelper.calculateHash(AOP_ALLIANCE_METHOD_AROUND)) == null) {
58             throw new RuntimeException JavaDoc("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",// as per AOPAlliance
65
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         // make it an AOP Alliance aspect for the compiler
75
aspectDef.setAspectModel(ASPECT_MODEL_TYPE);
76         aspectDef.setContainerClassName(ASPECT_CONTAINER_CLASS_NAME);
77
78         // add the advice
79
aspectDef.addAroundAdviceDefinition(adviceDef);
80
81         // add the aspect
82
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