KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.tc.aspectwerkz.transform.TransformationUtil;
11 import com.tc.aspectwerkz.transform.inlining.AsmHelper;
12
13 import java.lang.reflect.Modifier JavaDoc;
14
15 /**
16  * A compiler that compiles/generates a class that represents a specific join point, a class which invokes the advices
17  * and the target join point statically.
18  *
19  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
20  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur </a>
21  */

22 public class FieldGetJoinPointCompiler extends AbstractJoinPointCompiler {
23
24   /**
25    * Creates a new join point compiler instance.
26    *
27    * @param model
28    */

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

36   protected void createJoinPointSpecificFields() {
37     String JavaDoc[] fieldNames = null;
38     // create the field argument field
39
Type fieldType = Type.getType(m_calleeMemberDesc);
40     fieldNames = new String JavaDoc[1];
41     String JavaDoc fieldName = ARGUMENT_FIELD + 0;
42     fieldNames[0] = fieldName;
43     m_cw.visitField(ACC_PRIVATE, fieldName, fieldType.getDescriptor(), null, null);
44     m_fieldNames = fieldNames;
45
46     m_cw.visitField(
47             ACC_PRIVATE + ACC_STATIC,
48             SIGNATURE_FIELD_NAME,
49             FIELD_SIGNATURE_IMPL_CLASS_SIGNATURE,
50             null,
51             null
52     );
53   }
54
55   /**
56    * Creates the signature for the join point.
57    * <p/>
58    * FIXME signature field should NOT be of type Signature but of the specific type (update all refs as well)
59    *
60    * @param cv
61    */

62   protected void createSignature(final MethodVisitor cv) {
63     cv.visitFieldInsn(GETSTATIC, m_joinPointClassName, TARGET_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE);
64     cv.visitLdcInsn(new Integer JavaDoc(m_joinPointHash));
65
66     cv.visitMethodInsn(
67             INVOKESTATIC,
68             SIGNATURE_FACTORY_CLASS,
69             NEW_FIELD_SIGNATURE_METHOD_NAME,
70             NEW_FIELD_SIGNATURE_METHOD_SIGNATURE
71     );
72     cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, SIGNATURE_FIELD_NAME, FIELD_SIGNATURE_IMPL_CLASS_SIGNATURE);
73   }
74
75   /**
76    * Optimized implementation that does not retrieve the parameters from the join point instance but is passed
77    * directly to the method from the input parameters in the 'invoke' method. Can only be used if no around advice
78    * exists.
79    *
80    * @param cv
81    * @param input
82    */

83   protected void createInlinedJoinPointInvocation(final MethodVisitor cv, final CompilerInput input) {
84
85     // load the target instance (arg0 else not available for static target)
86
if (!Modifier.isStatic(m_calleeMemberModifiers)) {
87       cv.visitVarInsn(ALOAD, 0);
88     }
89
90     // do we have a public field ? If so don't use the wrappers
91
if (Modifier.isPublic(m_calleeMemberModifiers)) {
92       if (Modifier.isStatic(m_calleeMemberModifiers)) {
93         cv.visitFieldInsn(GETSTATIC, m_calleeClassName, m_calleeMemberName, m_calleeMemberDesc);
94       } else {
95         cv.visitFieldInsn(GETFIELD, m_calleeClassName, m_calleeMemberName, m_calleeMemberDesc);
96       }
97     } else {
98       // use the wrapper
99
String JavaDoc joinPointName = TransformationUtil.getWrapperMethodName(
100               m_calleeMemberName,
101               m_calleeMemberDesc,
102               m_calleeClassName,
103               GETFIELD_WRAPPER_METHOD_PREFIX
104       );
105       StringBuffer JavaDoc getFieldWrapperDesc = new StringBuffer JavaDoc();
106       getFieldWrapperDesc.append('(');
107       getFieldWrapperDesc.append(')');
108       getFieldWrapperDesc.append(m_calleeMemberDesc);
109       if (Modifier.isStatic(m_calleeMemberModifiers)) {
110         cv.visitMethodInsn(INVOKESTATIC, m_calleeClassName, joinPointName, getFieldWrapperDesc.toString());
111       } else {
112         cv.visitMethodInsn(INVOKEVIRTUAL, m_calleeClassName, joinPointName, getFieldWrapperDesc.toString());
113       }
114     }
115   }
116
117   /**
118    * Creates a call to the target join point, the parameter(s) to the join point are retrieved from the invocation
119    * local join point instance.
120    *
121    * @param cv
122    */

123   protected void createJoinPointInvocation(final MethodVisitor cv) {
124
125     // load the target instance member field unless calleeMember is static
126
if (!Modifier.isStatic(m_calleeMemberModifiers)) {
127       cv.visitVarInsn(ALOAD, 0);
128       cv.visitFieldInsn(GETFIELD, m_joinPointClassName, CALLEE_INSTANCE_FIELD_NAME, m_calleeClassSignature);
129     }
130
131     // do we have a public field ? If so don't use the wrappers
132
if (Modifier.isPublic(m_calleeMemberModifiers)) {
133       if (Modifier.isStatic(m_calleeMemberModifiers)) {
134         cv.visitFieldInsn(GETSTATIC, m_calleeClassName, m_calleeMemberName, m_calleeMemberDesc);
135       } else {
136         cv.visitFieldInsn(GETFIELD, m_calleeClassName, m_calleeMemberName, m_calleeMemberDesc);
137       }
138     } else {
139       String JavaDoc joinPointName = TransformationUtil.getWrapperMethodName(
140               m_calleeMemberName,
141               m_calleeMemberDesc,
142               m_calleeClassName,
143               GETFIELD_WRAPPER_METHOD_PREFIX
144       );
145       StringBuffer JavaDoc getFieldWrapperDesc = new StringBuffer JavaDoc();
146       getFieldWrapperDesc.append('(');
147       getFieldWrapperDesc.append(')');
148       getFieldWrapperDesc.append(m_calleeMemberDesc);
149       if (Modifier.isStatic(m_calleeMemberModifiers)) {
150         cv.visitMethodInsn(INVOKESTATIC, m_calleeClassName, joinPointName, getFieldWrapperDesc.toString());
151       } else {
152         cv.visitMethodInsn(INVOKEVIRTUAL, m_calleeClassName, joinPointName, getFieldWrapperDesc.toString());
153       }
154     }
155   }
156
157   /**
158    * Returns the join points return type.
159    *
160    * @return
161    */

162   protected Type getJoinPointReturnType() {
163     return Type.getType(m_calleeMemberDesc);
164   }
165
166   /**
167    * Returns the join points argument type(s).
168    *
169    * @return
170    */

171   protected Type[] getJoinPointArgumentTypes() {
172     return new Type[]{Type.getType(m_calleeMemberDesc)};
173   }
174
175   /**
176    * Creates the getRtti method
177    */

178   protected void createGetRttiMethod() {
179     MethodVisitor cv = m_cw.visitMethod(ACC_PUBLIC, GET_RTTI_METHOD_NAME, GET_RTTI_METHOD_SIGNATURE, null, null);
180
181     // new FieldRttiImpl( .. )
182
cv.visitTypeInsn(NEW, FIELD_RTTI_IMPL_CLASS_NAME);
183     cv.visitInsn(DUP);
184     cv.visitFieldInsn(GETSTATIC, m_joinPointClassName, SIGNATURE_FIELD_NAME, FIELD_SIGNATURE_IMPL_CLASS_SIGNATURE);
185     cv.visitVarInsn(ALOAD, 0);
186     cv.visitFieldInsn(GETFIELD, m_joinPointClassName, CALLER_INSTANCE_FIELD_NAME, m_callerClassSignature);
187     cv.visitVarInsn(ALOAD, 0);
188     cv.visitFieldInsn(GETFIELD, m_joinPointClassName, CALLEE_INSTANCE_FIELD_NAME, m_calleeClassSignature);
189     cv.visitMethodInsn(
190             INVOKESPECIAL, FIELD_RTTI_IMPL_CLASS_NAME, INIT_METHOD_NAME, FIELD_RTTI_IMPL_INIT_SIGNATURE
191     );
192
193     // set the value
194
cv.visitInsn(DUP);
195     if (AsmHelper.isPrimitive(m_returnType)) {
196       AsmHelper.prepareWrappingOfPrimitiveType(cv, m_returnType);
197       cv.visitVarInsn(ALOAD, 0);
198       cv.visitFieldInsn(GETFIELD, m_joinPointClassName, RETURN_VALUE_FIELD_NAME, m_returnType.getDescriptor());
199       AsmHelper.wrapPrimitiveType(cv, m_returnType);
200     } else {
201       cv.visitVarInsn(ALOAD, 0);
202       cv.visitFieldInsn(GETFIELD, m_joinPointClassName, RETURN_VALUE_FIELD_NAME, m_returnType.getDescriptor());
203     }
204     cv.visitMethodInsn(
205             INVOKEVIRTUAL,
206             FIELD_RTTI_IMPL_CLASS_NAME,
207             SET_FIELD_VALUE_METHOD_NAME,
208             SET_FIELD_VALUE_METHOD_SIGNATURE
209     );
210
211     cv.visitInsn(ARETURN);
212     cv.visitMaxs(0, 0);
213   }
214
215   /**
216    * Creates the getSignature method.
217    */

218   protected void createGetSignatureMethod() {
219     MethodVisitor cv = m_cw.visitMethod(
220             ACC_PUBLIC,
221             GET_SIGNATURE_METHOD_NAME,
222             GET_SIGNATURE_METHOD_SIGNATURE,
223             null,
224             null
225     );
226     cv.visitFieldInsn(
227             GETSTATIC, m_joinPointClassName,
228             SIGNATURE_FIELD_NAME, FIELD_SIGNATURE_IMPL_CLASS_SIGNATURE
229     );
230     cv.visitInsn(ARETURN);
231     cv.visitMaxs(0, 0);
232   }
233 }
Popular Tags