KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

130   protected void createJoinPointInvocation(final MethodVisitor cv) {
131
132     // load the target instance member field unless calleeMember is static
133
if (!Modifier.isStatic(m_calleeMemberModifiers)) {
134       cv.visitVarInsn(ALOAD, 0);
135       cv.visitFieldInsn(GETFIELD, m_joinPointClassName, CALLEE_INSTANCE_FIELD_NAME, m_calleeClassSignature);
136     }
137
138     loadArguments(cv);
139
140     // do we have a public field ? If so don't use the wrappers
141
if (Modifier.isPublic(m_calleeMemberModifiers)) {
142       if (Modifier.isStatic(m_calleeMemberModifiers)) {
143         cv.visitFieldInsn(PUTSTATIC, m_calleeClassName, m_calleeMemberName, m_calleeMemberDesc);
144       } else {
145         cv.visitFieldInsn(PUTFIELD, m_calleeClassName, m_calleeMemberName, m_calleeMemberDesc);
146       }
147     } else {
148       String JavaDoc joinPointName = TransformationUtil.getWrapperMethodName(
149               m_calleeMemberName,
150               m_calleeMemberDesc,
151               m_calleeClassName,
152               PUTFIELD_WRAPPER_METHOD_PREFIX
153       );
154       StringBuffer JavaDoc putFieldWrapperDesc = new StringBuffer JavaDoc();
155       putFieldWrapperDesc.append('(');
156       putFieldWrapperDesc.append(m_calleeMemberDesc);
157       putFieldWrapperDesc.append(')');
158       putFieldWrapperDesc.append('V');
159       if (Modifier.isStatic(m_calleeMemberModifiers)) {
160         cv.visitMethodInsn(INVOKESTATIC, m_calleeClassName, joinPointName, putFieldWrapperDesc.toString());
161       } else {
162         cv.visitMethodInsn(INVOKEVIRTUAL, m_calleeClassName, joinPointName, putFieldWrapperDesc.toString());
163       }
164     }
165
166     AsmHelper.addDefaultValue(cv, m_argumentTypes[0]);
167   }
168
169   /**
170    * Returns the join points return type.
171    *
172    * @return
173    */

174   protected Type getJoinPointReturnType() {
175     return Type.getType(m_calleeMemberDesc);
176   }
177
178   /**
179    * Returns the join points argument type(s).
180    *
181    * @return
182    */

183   protected Type[] getJoinPointArgumentTypes() {
184     return new Type[]{Type.getType(m_calleeMemberDesc)};
185   }
186
187   /**
188    * Creates the getRtti method
189    */

190   protected void createGetRttiMethod() {
191     MethodVisitor cv = m_cw.visitMethod(ACC_PUBLIC, GET_RTTI_METHOD_NAME, GET_RTTI_METHOD_SIGNATURE, null, null);
192
193     // new FieldRttiImpl( .. )
194
cv.visitTypeInsn(NEW, FIELD_RTTI_IMPL_CLASS_NAME);
195     cv.visitInsn(DUP);
196     cv.visitFieldInsn(GETSTATIC, m_joinPointClassName, SIGNATURE_FIELD_NAME, FIELD_SIGNATURE_IMPL_CLASS_SIGNATURE);
197     cv.visitVarInsn(ALOAD, 0);
198     cv.visitFieldInsn(GETFIELD, m_joinPointClassName, CALLER_INSTANCE_FIELD_NAME, m_callerClassSignature);
199     cv.visitVarInsn(ALOAD, 0);
200     cv.visitFieldInsn(GETFIELD, m_joinPointClassName, CALLEE_INSTANCE_FIELD_NAME, m_calleeClassSignature);
201     cv.visitMethodInsn(
202             INVOKESPECIAL, FIELD_RTTI_IMPL_CLASS_NAME, INIT_METHOD_NAME, FIELD_RTTI_IMPL_INIT_SIGNATURE
203     );
204
205     // set the value
206
cv.visitInsn(DUP);
207     if (AsmHelper.isPrimitive(m_returnType)) {
208       AsmHelper.prepareWrappingOfPrimitiveType(cv, m_returnType);
209       cv.visitVarInsn(ALOAD, 0);
210       cv.visitFieldInsn(GETFIELD, m_joinPointClassName, RETURN_VALUE_FIELD_NAME, m_returnType.getDescriptor());
211       AsmHelper.wrapPrimitiveType(cv, m_returnType);
212     } else {
213       cv.visitVarInsn(ALOAD, 0);
214       cv.visitFieldInsn(GETFIELD, m_joinPointClassName, RETURN_VALUE_FIELD_NAME, m_returnType.getDescriptor());
215     }
216     cv.visitMethodInsn(
217             INVOKEVIRTUAL,
218             FIELD_RTTI_IMPL_CLASS_NAME,
219             SET_FIELD_VALUE_METHOD_NAME,
220             SET_FIELD_VALUE_METHOD_SIGNATURE
221     );
222
223     cv.visitInsn(ARETURN);
224     cv.visitMaxs(0, 0);
225   }
226
227   /**
228    * Creates the getSignature method.
229    */

230   protected void createGetSignatureMethod() {
231     MethodVisitor cv = m_cw.visitMethod(
232             ACC_PUBLIC,
233             GET_SIGNATURE_METHOD_NAME,
234             GET_SIGNATURE_METHOD_SIGNATURE,
235             null,
236             null
237     );
238     cv.visitFieldInsn(
239             GETSTATIC, m_joinPointClassName,
240             SIGNATURE_FIELD_NAME, FIELD_SIGNATURE_IMPL_CLASS_SIGNATURE
241     );
242     cv.visitInsn(ARETURN);
243     cv.visitMaxs(0, 0);
244   }
245 }
Popular Tags