KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > aspect > container > PerCflowXAspectFactoryCompiler


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.aspect.container;
5
6 import com.tc.asm.MethodVisitor;
7 import com.tc.asm.Label;
8 import com.tc.asm.Type;
9
10 import com.tc.aspectwerkz.aspect.AspectContainer;
11 import com.tc.aspectwerkz.aspect.management.NoAspectBoundException;
12
13 /**
14  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
15  */

16 public class PerCflowXAspectFactoryCompiler extends LazyPerXFactoryCompiler {
17
18   public PerCflowXAspectFactoryCompiler(String JavaDoc uuid, String JavaDoc aspectClassName, String JavaDoc aspectQualifiedName, String JavaDoc containerClassName, String JavaDoc rawParameters, ClassLoader JavaDoc loader) {
19     super(uuid, aspectClassName, aspectQualifiedName, containerClassName, rawParameters, loader);
20   }
21
22   protected String JavaDoc getXSignature() {
23     return "Ljava/lang/Thread;";
24   }
25
26   /**
27    * Overrides the aspectOf() method to not do lazy aspect instantiation since controlled by bind()/unbind()
28    */

29   protected void createAspectOf() {
30     m_cw.visitField(
31             ACC_PRIVATE + ACC_STATIC,
32             "ASPECTS",
33             MAP_CLASS_SIGNATURE,
34             null,
35             null
36     );
37
38     m_clinit.visitTypeInsn(NEW, MAP_CLASS_NAME);
39     m_clinit.visitInsn(DUP);
40     m_clinit.visitMethodInsn(INVOKESPECIAL, "java/util/WeakHashMap", INIT_METHOD_NAME, "()V");
41     m_clinit.visitFieldInsn(PUTSTATIC, m_aspectFactoryClassName, "ASPECTS", MAP_CLASS_SIGNATURE);
42
43     MethodVisitor cv = m_cw.visitMethod(
44             ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
45             "aspectOf",
46             "(" + getXSignature() + ")" + m_aspectClassSignature,
47             null,
48             null
49     );
50
51     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, "ASPECTS", MAP_CLASS_SIGNATURE);
52     cv.visitVarInsn(ALOAD, 0);//Thread
53
cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, "getDefault", "(Ljava/lang/Object;)Ljava/lang/Object;");
54     cv.visitVarInsn(ASTORE, 1);
55     cv.visitVarInsn(ALOAD, 1);
56     Label ifBound = new Label();
57     cv.visitJumpInsn(IFNONNULL, ifBound);
58     cv.visitTypeInsn(NEW, Type.getInternalName(NoAspectBoundException.class));
59     cv.visitInsn(DUP);
60     cv.visitLdcInsn("Not bound");
61     cv.visitLdcInsn(m_aspectQualifiedName);
62     cv.visitMethodInsn(
63             INVOKESPECIAL,
64             NO_ASPECT_BOUND_EXCEPTION_CLASS_NAME,
65             INIT_METHOD_NAME,
66             "(Ljava/lang/String;Ljava/lang/String;)V"
67     );
68     cv.visitInsn(ATHROW);
69
70     cv.visitLabel(ifBound);
71     cv.visitVarInsn(ALOAD, 1);
72     cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
73     cv.visitInsn(ARETURN);
74
75     // create an implicit Thread.currentThread no arg aspectOf() method
76
cv = m_cw.visitMethod(
77             ACC_STATIC + ACC_PUBLIC + ACC_FINAL,
78             "aspectOf",
79             "()" + m_aspectClassSignature,
80             null,
81             null
82     );
83     cv.visitMethodInsn(
84             INVOKESTATIC, Type.getInternalName(Thread JavaDoc.class), "currentThread", "()Ljava/lang/Thread;"
85     );
86     cv.visitMethodInsn(
87             INVOKESTATIC, m_aspectFactoryClassName, "aspectOf", "(Ljava/lang/Thread;)" + getXSignature()
88     );
89     cv.visitInsn(ARETURN);
90     cv.visitMaxs(0, 0);
91   }
92
93   protected void createOtherArtifacts() {
94     createBindMethod();
95     createUnbindMethod();
96   }
97
98   private void createBindMethod() {
99     //FIXME AVF do not bind if already present
100
MethodVisitor cv = m_cw.visitMethod(
101             ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
102             "bind",
103             "(" + getXSignature() + ")V",
104             null,
105             null
106     );
107
108     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, "ASPECTS", MAP_CLASS_SIGNATURE);
109     if (m_hasAspectContainer) {
110       cv.visitFieldInsn(
111               GETSTATIC, m_aspectFactoryClassName, "CONTAINER", Type.getDescriptor(AspectContainer.class)
112       );
113       cv.visitMethodInsn(
114               INVOKEINTERFACE,
115               Type.getInternalName(AspectContainer.class),
116               "aspectOf",
117               "(" + getXSignature() + ")Ljava/lang/Object;"
118       );
119       cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
120     } else {
121       cv.visitTypeInsn(NEW, m_aspectClassName);
122       cv.visitInsn(DUP);
123       cv.visitMethodInsn(INVOKESPECIAL, m_aspectClassName, INIT_METHOD_NAME, "()V");
124     }
125     cv.visitVarInsn(ALOAD, 0);//Thread
126
cv.visitMethodInsn(
127             INVOKEINTERFACE,
128             MAP_CLASS_NAME,
129             "put",
130             "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"
131     );
132     cv.visitInsn(POP);
133     cv.visitInsn(RETURN);
134     cv.visitMaxs(0, 0);
135   }
136
137   private void createUnbindMethod() {
138     MethodVisitor cv = m_cw.visitMethod(
139             ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
140             "unbind",
141             "(" + getXSignature() + ")V",
142             null,
143             null
144     );
145
146     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, "ASPECTS", MAP_CLASS_SIGNATURE);
147     cv.visitVarInsn(ALOAD, 0);//Thread
148
cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, "remove", "(Ljava/lang/Object;)Ljava/lang/Object;");
149     cv.visitInsn(POP);
150     cv.visitInsn(RETURN);
151     cv.visitMaxs(0, 0);
152   }
153 }
154
Popular Tags