KickJava   Java API By Example, From Geeks To Geeks.

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


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
7 import com.tc.asm.MethodVisitor;
8 import com.tc.asm.Type;
9
10
11 import com.tc.aspectwerkz.transform.TransformationUtil;
12
13 /**
14  * A compiler that compiles/generates a class that represents a specific join
15  * point, a class which invokes the advices and the target join point
16  * statically.
17  *
18  * @author <a HREF="mailto:the_mindstorm@evolva.ro">Alex Popescu </a>
19  */

20 public class StaticInitializationJoinPointCompiler extends AbstractJoinPointCompiler {
21   private static final Type[] ARG_TYPES = new Type[0];
22
23   /**
24    * Creates a new join point compiler instance.
25    *
26    * @param model
27    */

28   StaticInitializationJoinPointCompiler(final CompilationInfo.Model model) {
29     super(model);
30   }
31
32   /**
33    * Creates join point specific fields.
34    */

35   protected void createJoinPointSpecificFields() {
36     m_fieldNames = new String JavaDoc[0];
37
38     m_cw.visitField(ACC_PRIVATE + ACC_STATIC,
39             SIGNATURE_FIELD_NAME,
40             STATICINITIALIZATION_SIGNATURE_IMPL_CLASS_SIGNATURE,
41             null,
42             null);
43   }
44
45   /**
46    * Creates the signature for the join point.
47    *
48    * @param cv
49    */

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

74   protected void createInlinedJoinPointInvocation(final MethodVisitor cv,
75                                                   final CompilerInput input) {
76     String JavaDoc joinPointName = TransformationUtil.getPrefixedOriginalClinitName(m_calleeClassName);
77
78     cv.visitMethodInsn(INVOKESTATIC, m_calleeClassName, joinPointName, m_calleeMemberDesc);
79   }
80
81   /**
82    * Creates a call to the target join point, the parameter(s) to the join
83    * point are retrieved from the invocation local join point instance.
84    *
85    * @param cv
86    */

87   protected void createJoinPointInvocation(final MethodVisitor cv) {
88
89     // load the target instance member field unless calleeMember is static
90
String JavaDoc joinPointName = TransformationUtil.getPrefixedOriginalClinitName(m_calleeClassName);
91     cv.visitMethodInsn(INVOKESTATIC, m_calleeClassName, joinPointName, m_calleeMemberDesc);
92   }
93
94   /**
95    * Returns the join points return type.
96    *
97    * @return
98    */

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

108   protected Type[] getJoinPointArgumentTypes() {
109     return ARG_TYPES;
110   }
111
112   /**
113    * Creates the getRtti method
114    */

115   protected void createGetRttiMethod() {
116     MethodVisitor cv = m_cw.visitMethod(ACC_PUBLIC,
117             GET_RTTI_METHOD_NAME,
118             GET_RTTI_METHOD_SIGNATURE,
119             null,
120             null
121     );
122
123     // new StaticInitializationRttiImpl
124
cv.visitTypeInsn(NEW, STATICINITIALIZATION_RTTI_IMPL_CLASS_NAME);
125     cv.visitInsn(DUP);
126     cv.visitFieldInsn(GETSTATIC,
127             m_joinPointClassName,
128             SIGNATURE_FIELD_NAME,
129             STATICINITIALIZATION_SIGNATURE_IMPL_CLASS_SIGNATURE);
130     cv.visitMethodInsn(INVOKESPECIAL,
131             STATICINITIALIZATION_RTTI_IMPL_CLASS_NAME,
132             INIT_METHOD_NAME,
133             STATICINITIALIZATION_RTTI_IMPL_INIT_SIGNATURE
134     );
135
136     cv.visitInsn(ARETURN);
137     cv.visitMaxs(0, 0);
138   }
139
140   /**
141    * Creates the getSignature method.
142    */

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