KickJava   Java API By Example, From Geeks To Geeks.

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


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.Label;
7 import com.tc.asm.MethodVisitor;
8 import com.tc.asm.Type;
9
10 import com.tc.aspectwerkz.aspect.management.NoAspectBoundException;
11
12 /**
13  * Factory compiler for perThis perTarget and perInstance (lazy like) models.
14  * All factories rely on HasInstanceLevelAspect interface.
15  *
16  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
17  */

18 public class PerObjectFactoryCompiler extends AbstractAspectFactoryCompiler {
19
20   public PerObjectFactoryCompiler(String JavaDoc uuid, String JavaDoc aspectClassName, String JavaDoc aspectQualifiedName, String JavaDoc containerClassName, String JavaDoc rawParameters, ClassLoader JavaDoc loader) {
21     super(uuid, aspectClassName, aspectQualifiedName, containerClassName, rawParameters, loader);
22   }
23
24   protected void createAspectOf() {
25     MethodVisitor cv = m_cw.visitMethod(
26             ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
27             FACTORY_ASPECTOF_METHOD_NAME,
28             "(Ljava/lang/Object;)" + m_aspectClassSignature,
29             null,
30             null
31     );
32
33     // instanceOf check
34
cv.visitVarInsn(ALOAD, 0);// object
35
cv.visitTypeInsn(INSTANCEOF, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
36     Label ifInstanceOf = new Label();
37     cv.visitJumpInsn(IFNE, ifInstanceOf);
38     cv.visitTypeInsn(NEW, Type.getInternalName(NoAspectBoundException.class));
39     cv.visitInsn(DUP);
40     cv.visitLdcInsn("Unimplemented interface");
41     cv.visitLdcInsn(m_aspectQualifiedName);
42     cv.visitMethodInsn(
43             INVOKESPECIAL,
44             NO_ASPECT_BOUND_EXCEPTION_CLASS_NAME,
45             INIT_METHOD_NAME,
46             "(Ljava/lang/String;Ljava/lang/String;)V"
47     );
48     cv.visitInsn(ATHROW);
49     cv.visitLabel(ifInstanceOf);
50
51     cv.visitVarInsn(ALOAD, 0);
52     cv.visitTypeInsn(CHECKCAST, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
53     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, FACTORY_CLASS_FIELD_NAME, CLASS_CLASS_SIGNATURE);
54     cv.visitMethodInsn(
55             INVOKEINTERFACE,
56             HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME,
57             INSTANCE_LEVEL_GETASPECT_METHOD_NAME,
58             INSTANCE_LEVEL_GETASPECT_METHOD_SIGNATURE
59     );
60     cv.visitVarInsn(ASTORE, 1);
61     cv.visitVarInsn(ALOAD, 1);
62     Label ifBound = new Label();
63     cv.visitJumpInsn(IFNONNULL, ifBound);
64     cv.visitTypeInsn(NEW, NO_ASPECT_BOUND_EXCEPTION_CLASS_NAME);
65     cv.visitInsn(DUP);
66     cv.visitLdcInsn("Not bound");
67     cv.visitLdcInsn(m_aspectQualifiedName);
68     cv.visitMethodInsn(
69             INVOKESPECIAL,
70             NO_ASPECT_BOUND_EXCEPTION_CLASS_NAME,
71             INIT_METHOD_NAME,
72             "(Ljava/lang/String;Ljava/lang/String;)V"
73     );
74     cv.visitInsn(ATHROW);
75
76     cv.visitLabel(ifBound);
77     cv.visitVarInsn(ALOAD, 1);
78     cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
79     cv.visitInsn(ARETURN);
80     cv.visitMaxs(0, 0);
81   }
82
83   protected void createHasAspect() {
84     MethodVisitor cv = m_cw.visitMethod(
85             ACC_PUBLIC + ACC_STATIC,
86             FACTORY_HASASPECT_METHOD_NAME,
87             "(Ljava/lang/Object;)Z",
88             null,
89             null
90     );
91
92     // instanceOf check
93
cv.visitVarInsn(ALOAD, 0);// object
94
cv.visitTypeInsn(INSTANCEOF, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
95     Label ifInstanceOf = new Label();
96     cv.visitJumpInsn(IFNE, ifInstanceOf);
97     cv.visitInsn(ICONST_0);
98     cv.visitInsn(IRETURN);
99     cv.visitLabel(ifInstanceOf);
100
101     cv.visitVarInsn(ALOAD, 0);
102     cv.visitTypeInsn(CHECKCAST, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
103     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, FACTORY_CLASS_FIELD_NAME, CLASS_CLASS_SIGNATURE);
104     cv.visitMethodInsn(
105             INVOKEINTERFACE,
106             HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME,
107             INSTANCE_LEVEL_HASASPECT_METHOD_NAME,
108             INSTANCE_LEVEL_HASASPECT_METHOD_SIGNATURE
109     );
110     cv.visitInsn(IRETURN);
111     cv.visitMaxs(0, 0);
112   }
113
114   protected void createOtherArtifacts() {
115     createBindMethod();
116   }
117
118   private void createBindMethod() {
119     MethodVisitor cv = m_cw.visitMethod(
120             ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
121             "bind",
122             "(Ljava/lang/Object;)Ljava/lang/Object;",
123             null,
124             null
125     );
126
127     // instanceOf check
128
cv.visitVarInsn(ALOAD, 0);// object
129
cv.visitTypeInsn(INSTANCEOF, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
130     Label ifInstanceOf = new Label();
131     cv.visitJumpInsn(IFNE, ifInstanceOf);
132     cv.visitTypeInsn(NEW, NO_ASPECT_BOUND_EXCEPTION_CLASS_NAME);
133     cv.visitInsn(DUP);
134     cv.visitLdcInsn("Unimplemented interface");
135     cv.visitLdcInsn(m_aspectQualifiedName);
136     cv.visitMethodInsn(
137             INVOKESPECIAL,
138             NO_ASPECT_BOUND_EXCEPTION_CLASS_NAME,
139             INIT_METHOD_NAME,
140             "(Ljava/lang/String;Ljava/lang/String;)V"
141     );
142     cv.visitInsn(ATHROW);
143     cv.visitLabel(ifInstanceOf);
144
145     cv.visitVarInsn(ALOAD, 0);//object
146
cv.visitTypeInsn(CHECKCAST, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
147     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, FACTORY_CLASS_FIELD_NAME, CLASS_CLASS_SIGNATURE);
148     cv.visitMethodInsn(
149             INVOKEINTERFACE,
150             HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME,
151             INSTANCE_LEVEL_GETASPECT_METHOD_NAME,
152             INSTANCE_LEVEL_GETASPECT_METHOD_SIGNATURE
153     );
154     cv.visitVarInsn(ASTORE, 1);
155     cv.visitVarInsn(ALOAD, 1);
156     Label ifAlreadyBound = new Label();
157     cv.visitJumpInsn(IFNONNULL, ifAlreadyBound);
158
159     // target instance and arg0 for bind call
160
cv.visitVarInsn(ALOAD, 0);//object
161
cv.visitTypeInsn(CHECKCAST, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
162     cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, FACTORY_CLASS_FIELD_NAME, CLASS_CLASS_SIGNATURE);
163
164     if (m_hasAspectContainer) {
165       cv.visitFieldInsn(
166               GETSTATIC, m_aspectFactoryClassName, FACTORY_CONTAINER_FIELD_NAME, ASPECT_CONTAINER_CLASS_SIGNATURE
167       );
168       cv.visitVarInsn(ALOAD, 0);//associated object
169
cv.visitMethodInsn(
170               INVOKEINTERFACE, ASPECT_CONTAINER_CLASS_NAME, ASPECT_CONTAINER_ASPECTOF_METHOD_NAME, "(Ljava/lang/Object;)Ljava/lang/Object;"
171       );
172       cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
173     } else {
174       cv.visitTypeInsn(NEW, m_aspectClassName);
175       cv.visitInsn(DUP);
176       cv.visitMethodInsn(INVOKESPECIAL, m_aspectClassName, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
177     }
178     cv.visitMethodInsn(
179             INVOKEINTERFACE,
180             HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME,
181             INSTANCE_LEVEL_BINDASPECT_METHOD_NAME,
182             INSTANCE_LEVEL_BINDASPECT_METHOD_SIGNATURE
183     );
184     cv.visitInsn(ARETURN);
185
186     cv.visitLabel(ifAlreadyBound);
187     cv.visitVarInsn(ALOAD, 1);
188     cv.visitInsn(ARETURN);
189
190     cv.visitMaxs(0, 0);
191   }
192
193   public static class PerInstanceFactoryCompiler extends PerObjectFactoryCompiler {
194
195     public PerInstanceFactoryCompiler(String JavaDoc uuid, String JavaDoc aspectClassName, String JavaDoc aspectQualifiedName, String JavaDoc containerClassName, String JavaDoc rawParameters, ClassLoader JavaDoc loader) {
196       super(uuid, aspectClassName, aspectQualifiedName, containerClassName, rawParameters, loader);
197     }
198
199     protected void createAspectOf() {
200       MethodVisitor cv = m_cw.visitMethod(
201               ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
202               FACTORY_ASPECTOF_METHOD_NAME,
203               "(Ljava/lang/Object;)" + m_aspectClassSignature,
204               null,
205               null
206       );
207
208       // instanceOf check
209
cv.visitVarInsn(ALOAD, 0);// object
210
cv.visitTypeInsn(INSTANCEOF, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
211       Label ifInstanceOf = new Label();
212       cv.visitJumpInsn(IFNE, ifInstanceOf);
213       cv.visitTypeInsn(NEW, Type.getInternalName(NoAspectBoundException.class));
214       cv.visitInsn(DUP);
215       cv.visitLdcInsn("Unimplemented interface");
216       cv.visitLdcInsn(m_aspectQualifiedName);
217       cv.visitMethodInsn(
218               INVOKESPECIAL,
219               NO_ASPECT_BOUND_EXCEPTION_CLASS_NAME,
220               INIT_METHOD_NAME,
221               "(Ljava/lang/String;Ljava/lang/String;)V"
222       );
223       cv.visitInsn(ATHROW);
224       cv.visitLabel(ifInstanceOf);
225
226       cv.visitVarInsn(ALOAD, 0);
227       cv.visitTypeInsn(CHECKCAST, HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME);
228       cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName, FACTORY_CLASS_FIELD_NAME, CLASS_CLASS_SIGNATURE);
229       cv.visitMethodInsn(
230               INVOKEINTERFACE,
231               HAS_INSTANCE_LEVEL_ASPECT_INTERFACE_NAME,
232               INSTANCE_LEVEL_GETASPECT_METHOD_NAME,
233               INSTANCE_LEVEL_GETASPECT_METHOD_SIGNATURE
234       );
235       cv.visitVarInsn(ASTORE, 1);
236       cv.visitVarInsn(ALOAD, 1);
237       Label ifBound = new Label();
238       cv.visitJumpInsn(IFNONNULL, ifBound);
239       // no aspect bound yet - since perInstance is lazy delegate to bind
240
cv.visitVarInsn(ALOAD, 0);
241       cv.visitMethodInsn(
242               INVOKESTATIC,
243               m_aspectFactoryClassName,
244               "bind",
245               "(Ljava/lang/Object;)Ljava/lang/Object;"
246       );
247       cv.visitVarInsn(ASTORE, 1);
248       cv.visitLabel(ifBound);
249       cv.visitVarInsn(ALOAD, 1);
250       cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
251       cv.visitInsn(ARETURN);
252       cv.visitMaxs(0, 0);
253     }
254   }
255 }
256
Popular Tags