KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > transform > inlining > compiler > HandlerJoinPointCompiler


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.transform.inlining.compiler;
5
6 import com.tc.asm.MethodVisitor;
7 import com.tc.asm.Type;
8
9 /**
10  * A compiler that compiles/generates a class that represents a specific join point, a class which invokes the advices
11  * and the target join point statically.
12  * <p/>
13  * In this case, CALLEE is the catched exception instance itself.
14  *
15  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur </a>
17  */

18 public class HandlerJoinPointCompiler extends AbstractJoinPointCompiler {
19
20   /**
21    * Creates a new join point compiler instance.
22    *
23    * @param model
24    */

25   HandlerJoinPointCompiler(final CompilationInfo.Model model) {
26     super(model);
27   }
28
29   /**
30    * Creates join point specific fields.
31    */

32   protected void createJoinPointSpecificFields() {
33     // create the field argument field
34
String JavaDoc[] fieldNames = null;
35     Type fieldType = Type.getType(m_calleeClassSignature);
36     fieldNames = new String JavaDoc[1];
37     String JavaDoc fieldName = ARGUMENT_FIELD + 0;
38     fieldNames[0] = fieldName;
39     m_cw.visitField(ACC_PRIVATE, fieldName, fieldType.getDescriptor(), null, null);
40     m_fieldNames = fieldNames;
41     m_cw.visitField(
42             ACC_PRIVATE + ACC_STATIC,
43             SIGNATURE_FIELD_NAME,
44             HANDLER_SIGNATURE_IMPL_CLASS_SIGNATURE,
45             null,
46             null
47     );
48   }
49
50   /**
51    * Creates the signature for the join point.
52    *
53    * @param cv
54    */

55   protected void createSignature(final MethodVisitor cv) {
56     cv.visitFieldInsn(GETSTATIC, m_joinPointClassName, TARGET_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE);
57
58     cv.visitMethodInsn(
59             INVOKESTATIC,
60             SIGNATURE_FACTORY_CLASS,
61             NEW_CATCH_CLAUSE_SIGNATURE_METHOD_NAME,
62             NEW_HANDLER_SIGNATURE_METHOD_SIGNATURE
63     );
64     cv.visitFieldInsn(
65             PUTSTATIC, m_joinPointClassName, SIGNATURE_FIELD_NAME, HANDLER_SIGNATURE_IMPL_CLASS_SIGNATURE
66     );
67
68   }
69
70   /**
71    * Optimized implementation that does not retrieve the parameters from the join point instance but is passed
72    * directly to the method from the input parameters in the 'invoke' method. Can only be used if no around advice
73    * exists.
74    *
75    * @param cv
76    * @param input
77    */

78   protected void createInlinedJoinPointInvocation(final MethodVisitor cv,
79                                                   final CompilerInput input) {
80     // load the exception
81
cv.visitVarInsn(ALOAD, 0);//TODO if changed perhaps load CALLEE instead that host the exception ?
82
}
83
84   /**
85    * Creates a call to the target join point, the parameter(s) to the join point are retrieved from the invocation
86    * local join point instance.
87    *
88    * @param cv
89    */

90   protected void createJoinPointInvocation(final MethodVisitor cv) {
91     cv.visitInsn(ACONST_NULL);
92   }
93
94   /**
95    * Returns the join points return type.
96    *
97    * @return
98    */

99   protected Type getJoinPointReturnType() {
100     return Type.getType(m_calleeClassSignature);
101   }
102
103   /**
104    * Returns the join points argument type(s).
105    *
106    * @return
107    */

108   protected Type[] getJoinPointArgumentTypes() {
109     return new Type[]{Type.getType(m_calleeClassSignature)};//TODO should callee be arg instead ? to bind it later ?
110
}
111
112   /**
113    * Creates the getRtti method
114    */

115   protected void createGetRttiMethod() {
116     MethodVisitor cv = m_cw.visitMethod(ACC_PUBLIC, GET_RTTI_METHOD_NAME, GET_RTTI_METHOD_SIGNATURE, null, null);
117
118     // new CtorRttiImpl( .. )
119
cv.visitTypeInsn(NEW, HANDLER_RTTI_IMPL_CLASS_NAME);
120     cv.visitInsn(DUP);
121     cv.visitFieldInsn(
122             GETSTATIC, m_joinPointClassName, SIGNATURE_FIELD_NAME, HANDLER_SIGNATURE_IMPL_CLASS_SIGNATURE
123     );
124     cv.visitVarInsn(ALOAD, 0);
125     cv.visitFieldInsn(GETFIELD, m_joinPointClassName, CALLER_INSTANCE_FIELD_NAME, m_callerClassSignature);
126     cv.visitVarInsn(ALOAD, 0);
127     cv.visitFieldInsn(GETFIELD, m_joinPointClassName, CALLEE_INSTANCE_FIELD_NAME, m_calleeClassSignature);
128     cv.visitMethodInsn(
129             INVOKESPECIAL, HANDLER_RTTI_IMPL_CLASS_NAME, INIT_METHOD_NAME, HANDLER_RTTI_IMPL_INIT_SIGNATURE
130     );
131
132     cv.visitInsn(ARETURN);
133     cv.visitMaxs(0, 0);
134   }
135
136   /**
137    * Creates the getSignature method.
138    */

139   protected void createGetSignatureMethod() {
140     MethodVisitor cv = m_cw.visitMethod(
141             ACC_PUBLIC,
142             GET_SIGNATURE_METHOD_NAME,
143             GET_SIGNATURE_METHOD_SIGNATURE,
144             null,
145             null
146     );
147     cv.visitFieldInsn(
148             GETSTATIC, m_joinPointClassName,
149             SIGNATURE_FIELD_NAME, HANDLER_SIGNATURE_IMPL_CLASS_SIGNATURE
150     );
151     cv.visitInsn(ARETURN);
152     cv.visitMaxs(0, 0);
153   }
154 }
Popular Tags