KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > transform > TransformationUtil


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;
5
6 import java.lang.reflect.Modifier JavaDoc;
7
8 import com.tc.aspectwerkz.joinpoint.management.JoinPointType;
9
10 /**
11  * Utility method used by the transformers.
12  *
13  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
14  */

15 public final class TransformationUtil {
16
17   /**
18    * Return the prefixed clinit method name
19    *
20    * @param className
21    * @return
22    */

23   public static String JavaDoc getPrefixedOriginalClinitName(final String JavaDoc className) {
24     return getPrefixedOriginalMethodName(
25             TransformationConstants.STATICINITIALIZER_WRAPPER_METHOD_KEY,//need to not clash with a user method named "clinit"
26
className
27     );
28   }
29
30   /**
31    * Returns the prefixed method name.
32    *
33    * @param methodName the method name
34    * @param className the class name
35    * @return the name of the join point
36    */

37   public static String JavaDoc getPrefixedOriginalMethodName(final String JavaDoc methodName,
38                                                      final String JavaDoc className) {
39     final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
40     buf.append(TransformationConstants.ORIGINAL_METHOD_PREFIX);
41     buf.append(methodName);
42     buf.append(TransformationConstants.DELIMITER);
43     buf.append(className.replace('.', '_').replace('/', '_'));
44     return buf.toString();
45   }
46
47   /**
48    * Returns the prefixed method name.
49    *
50    * @param methodName the method name
51    * @param methodDesc the method desc
52    * @param className the class name
53    * @return the name of the join point
54    */

55   public static String JavaDoc getWrapperMethodName(final String JavaDoc methodName,
56                                             final String JavaDoc methodDesc,
57                                             final String JavaDoc className,
58                                             final String JavaDoc prefix) {
59     final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
60     //FIXME: double check me
61
// we use the javaC convention for hidden synthetic method
62
// is the methodSequence enough ?
63
// [ Alex: looks like it will change between each RW since tied to ctx match ]
64
buf.append(TransformationConstants.WRAPPER_METHOD_PREFIX);
65     buf.append(prefix);
66     buf.append(methodName.replace('<', '$').replace('>', '$'));// can be <init> for ctor wrapping
67
buf.append(methodDesc.hashCode());//??
68
buf.append(className.replace('.', '_').replace('/', '_'));
69     return buf.toString().replace('-', '_');
70   }
71
72   /**
73    * Build the join point invoke method descriptor for code (method or constructor) join points.
74    * Depends if the target method is static or not.
75    *
76    * @param codeModifiers
77    * @param codeDesc
78    * @param callerTypeName
79    * @param calleeTypeName
80    * @return
81    */

82   public static String JavaDoc getInvokeSignatureForCodeJoinPoints(final int codeModifiers,
83                                                            final String JavaDoc codeDesc,
84                                                            final String JavaDoc callerTypeName,
85                                                            final String JavaDoc calleeTypeName) {
86     StringBuffer JavaDoc sig = new StringBuffer JavaDoc("(");
87     if (!Modifier.isStatic(codeModifiers)) {
88       // callee is arg0 for non static target method invoke call
89
// else it is skept
90
sig.append('L');
91       sig.append(calleeTypeName);
92       sig.append(';');
93     }
94     int index = codeDesc.lastIndexOf(')');
95     sig.append(codeDesc.substring(1, index));
96     sig.append('L');
97     sig.append(callerTypeName);
98     sig.append(';');
99     sig.append(codeDesc.substring(index, codeDesc.length()));
100     return sig.toString();
101   }
102
103   /**
104    * Build the join point invoke method descriptor for field join points.
105    * Depends if the target field is static or not.
106    *
107    * @param fieldModifiers
108    * @param fieldDesc
109    * @param callerTypeName
110    * @param calleeTypeName
111    * @return the signature
112    */

113   public static String JavaDoc getInvokeSignatureForFieldJoinPoints(final int fieldModifiers,
114                                                             final String JavaDoc fieldDesc,
115                                                             final String JavaDoc callerTypeName,
116                                                             final String JavaDoc calleeTypeName) {
117     StringBuffer JavaDoc sig = new StringBuffer JavaDoc("(");
118     if (!Modifier.isStatic(fieldModifiers)) {
119       // callee is arg0 for non static target method invoke call
120
// else it is skept
121
sig.append('L');
122       sig.append(calleeTypeName);
123       sig.append(';');
124     }
125     sig.append(fieldDesc);
126     sig.append('L');
127     sig.append(callerTypeName);
128     sig.append(';');
129     sig.append(')');
130     sig.append(fieldDesc);
131     return sig.toString();
132   }
133
134   /**
135    * Build the join point invoke method descriptor for handler join points.
136    * "Exception invoke(Exception, WithinInstance[can be null])"
137    *
138    * @param withinTypeName
139    * @param exceptionTypeName
140    * @return the signature
141    */

142   public static String JavaDoc getInvokeSignatureForHandlerJoinPoints(final String JavaDoc withinTypeName,
143                                                               final String JavaDoc exceptionTypeName) {
144     StringBuffer JavaDoc sig = new StringBuffer JavaDoc("(");
145     sig.append('L');
146     sig.append(exceptionTypeName);
147     sig.append(';');
148     sig.append('L');//TODO check me [callee + arg0 or just arg0?]
149
sig.append(exceptionTypeName);
150     sig.append(';');
151     sig.append('L');
152     sig.append(withinTypeName);
153     sig.append(';');
154     sig.append(')');
155     sig.append('L');
156     sig.append(exceptionTypeName);
157     sig.append(';');
158     return sig.toString();
159   }
160
161   /**
162    * Build the join point invoke method descriptor for ctor call join points.
163    *
164    * @param calleeConstructorDesc
165    * @param callerTypeName
166    * @param calleeTypeName
167    * @return the signature
168    */

169   public static String JavaDoc getInvokeSignatureForConstructorCallJoinPoints(final String JavaDoc calleeConstructorDesc,
170                                                                       final String JavaDoc callerTypeName,
171                                                                       final String JavaDoc calleeTypeName) {
172     StringBuffer JavaDoc sig = new StringBuffer JavaDoc("(");
173     int index = calleeConstructorDesc.lastIndexOf(')');
174     // callee ctor args
175
sig.append(calleeConstructorDesc.substring(1, index));
176     // caller
177
sig.append('L');
178     sig.append(callerTypeName);
179     sig.append(';');
180     sig.append(")L");
181     sig.append(calleeTypeName);
182     sig.append(';');
183     return sig.toString();
184   }
185
186   /**
187    * Returns the method name used for constructor body
188    *
189    * @param calleeTypeName
190    * @return
191    */

192   public static String JavaDoc getConstructorBodyMethodName(final String JavaDoc calleeTypeName) {
193     final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
194     buf.append(TransformationConstants.ORIGINAL_METHOD_PREFIX);
195     buf.append("init");
196     buf.append(TransformationConstants.DELIMITER);
197     buf.append(calleeTypeName.replace('.', '_').replace('/', '_'));
198     return buf.toString();
199   }
200
201   /**
202    * Returns the method used for constructor body signature
203    * The callee type name is prepended to the constructor signature
204    *
205    * @param ctorDesc
206    * @param calleeTypeName
207    * @return
208    */

209   public static String JavaDoc getConstructorBodyMethodSignature(final String JavaDoc ctorDesc, final String JavaDoc calleeTypeName) {
210     StringBuffer JavaDoc sig = new StringBuffer JavaDoc("(L");
211     sig.append(calleeTypeName);
212     sig.append(";");
213     sig.append(ctorDesc.substring(1));
214     return sig.toString();
215   }
216
217   /**
218    * Computes the joinpoint classname : "caller/class_type_hash_suffix"
219    * For constructor call joinpoints, the hash of callee name is used as well.
220    *
221    * @param thisClassName
222    * @param thisMemberName
223    * @param thisMemberDesc
224    * @param targetClassName
225    * @param joinPointType
226    * @param joinPointHash
227    * @return the JIT joinpoint classname
228    */

229   public static String JavaDoc getJoinPointClassName(final String JavaDoc thisClassName,
230                                              final String JavaDoc thisMemberName,
231                                              final String JavaDoc thisMemberDesc,
232                                              final String JavaDoc targetClassName,
233                                              final int joinPointType,
234                                              final int joinPointHash) {
235     StringBuffer JavaDoc classNameBuf = new StringBuffer JavaDoc(thisClassName);
236     // TODO: INNER CLASS OR NOT?
237
// classNameBuf.append("$");
238
classNameBuf.append('_');
239     classNameBuf.append(joinPointType);
240     classNameBuf.append('_');
241     // whithincode support
242
classNameBuf.append((thisMemberName + thisMemberDesc).hashCode());
243     classNameBuf.append('_');
244     classNameBuf.append(joinPointHash);
245     //FIXME needed for other jp ? f.e. Handler ??
246
if (joinPointType == JoinPointType.CONSTRUCTOR_CALL_INT || joinPointType == JoinPointType.METHOD_CALL_INT
247             || joinPointType == JoinPointType.FIELD_GET_INT
248             || joinPointType == JoinPointType.FIELD_SET_INT
249             ) {
250       classNameBuf.append('_').append(targetClassName.hashCode());
251     }
252     classNameBuf.append(TransformationConstants.JOIN_POINT_CLASS_SUFFIX);
253
254     //replace minus signs on m_joinPointHash
255
return classNameBuf.toString().replace('-', '_').replace('.', '/');
256   }
257
258 }
Popular Tags