KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 /**
10  * Abstract class for lazy model that instantiate the aspect as soon as aspectOf is called
11  * (f.e. perThread, perClass)
12  *
13  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
14  */

15 public abstract class LazyPerXFactoryCompiler extends AbstractAspectFactoryCompiler {
16
17   public LazyPerXFactoryCompiler(String JavaDoc uuid, String JavaDoc aspectClassName, String JavaDoc aspectQualifiedName, String JavaDoc containerClassName, String JavaDoc rawParameters, ClassLoader JavaDoc loader) {
18     super(uuid, aspectClassName, aspectQualifiedName, containerClassName, rawParameters, loader);
19   }
20
21   protected abstract String JavaDoc getXSignature();
22
23   protected void createAspectOf() {
24     m_cw.visitField(
25             ACC_PRIVATE + ACC_STATIC,
26             FACTORY_ASPECTS_FIELD_NAME,
27             MAP_CLASS_SIGNATURE,
28             null,
29             null
30     );
31
32     m_clinit.visitTypeInsn(NEW, "java/util/WeakHashMap");
33     m_clinit.visitInsn(DUP);
34     m_clinit.visitMethodInsn(INVOKESPECIAL, "java/util/WeakHashMap", INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
35     m_clinit.visitFieldInsn(PUTSTATIC, m_aspectFactoryClassName, FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE);
36
37     MethodVisitor cv = m_cw.visitMethod(
38             ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
39             FACTORY_ASPECTOF_METHOD_NAME,
40             "(" + getXSignature() + ")" + m_aspectClassSignature,
41             null,
42             null
43     );
44
45     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE);
46     cv.visitVarInsn(ALOAD, 0);//Class
47
cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, "getDefault", "(Ljava/lang/Object;)Ljava/lang/Object;");
48     cv.visitVarInsn(ASTORE, 1);
49     cv.visitVarInsn(ALOAD, 1);
50     Label ifBound = new Label();
51     cv.visitJumpInsn(IFNONNULL, ifBound);
52     if (m_hasAspectContainer) {
53       cv.visitFieldInsn(
54               GETSTATIC, m_aspectFactoryClassName, FACTORY_CONTAINER_FIELD_NAME, ASPECT_CONTAINER_CLASS_SIGNATURE
55       );
56       cv.visitMethodInsn(
57               INVOKEINTERFACE,
58               ASPECT_CONTAINER_CLASS_NAME,
59               ASPECT_CONTAINER_ASPECTOF_METHOD_NAME,
60               "(" + getXSignature() + ")Ljava/lang/Object;"
61       );
62       cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
63     } else {
64       cv.visitTypeInsn(NEW, m_aspectClassName);
65       cv.visitInsn(DUP);
66       cv.visitMethodInsn(INVOKESPECIAL, m_aspectClassName, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
67     }
68     cv.visitVarInsn(ASTORE, 2);
69     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE);
70     cv.visitVarInsn(ALOAD, 0);
71     cv.visitVarInsn(ALOAD, 2);
72     cv.visitMethodInsn(
73             INVOKEINTERFACE, MAP_CLASS_NAME, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"
74     );
75     cv.visitVarInsn(ALOAD, 2);
76     cv.visitInsn(ARETURN);
77
78     cv.visitLabel(ifBound);
79     cv.visitVarInsn(ALOAD, 1);
80     cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
81     cv.visitInsn(ARETURN);
82     cv.visitMaxs(0, 0);
83   }
84
85   protected void createHasAspect() {
86     MethodVisitor cv = m_cw.visitMethod(
87             ACC_STATIC + ACC_PUBLIC + ACC_FINAL,
88             FACTORY_HASASPECT_METHOD_NAME,
89             "(" + getXSignature() + ")Z",
90             null,
91             null
92     );
93
94     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE);
95     cv.visitVarInsn(ALOAD, 0);
96     cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, "containsKey", "(Ljava/lang/Object;)Z");
97     cv.visitInsn(IRETURN);
98     cv.visitMaxs(0, 0);
99   }
100
101   protected void createOtherArtifacts() {
102     ;
103   }
104
105   static class PerClassAspectFactoryCompiler extends LazyPerXFactoryCompiler {
106     public PerClassAspectFactoryCompiler(String JavaDoc uuid, String JavaDoc aspectClassName, String JavaDoc aspectQualifiedName, String JavaDoc containerClassName, String JavaDoc rawParameters, ClassLoader JavaDoc loader) {
107       super(uuid, aspectClassName, aspectQualifiedName, containerClassName, rawParameters, loader);
108     }
109
110     protected String JavaDoc getXSignature() {
111       return "Ljava/lang/Class;";
112     }
113   }
114
115   static class PerThreadAspectFactoryCompiler extends LazyPerXFactoryCompiler {
116     public PerThreadAspectFactoryCompiler(String JavaDoc uuid, String JavaDoc aspectClassName, String JavaDoc aspectQualifiedName, String JavaDoc containerClassName, String JavaDoc rawParameters, ClassLoader JavaDoc loader) {
117       super(uuid, aspectClassName, aspectQualifiedName, containerClassName, rawParameters, loader);
118     }
119
120     protected String JavaDoc getXSignature() {
121       return "Ljava/lang/Thread;";
122     }
123   }
124
125 }
126
Popular Tags