View Javadoc

1   /***************************************************************************************
2    * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package org.codehaus.aspectwerkz.transform;
9   
10  import java.lang.reflect.Modifier;
11  
12  /***
13   * Utility method used by the transformers.
14   *
15   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
16   */
17  public final class TransformationUtil {
18  
19      /***
20       * Returns the prefixed method name.
21       *
22       * @param methodName     the method name
23       * @param methodSequence the method sequence
24       * @param className      the class name
25       * @return the name of the join point
26       */
27      public static String getPrefixedOriginalMethodName(final String methodName,
28                                                         final int methodSequence,
29                                                         final String className) {
30          final StringBuffer buf = new StringBuffer();
31          buf.append(TransformationConstants.ORIGINAL_METHOD_PREFIX);
32          buf.append(methodName);
33          buf.append(TransformationConstants.DELIMITER);
34          buf.append(methodSequence);
35          buf.append(TransformationConstants.DELIMITER);
36          buf.append(className.replace('.', '_').replace('/', '_'));
37          return buf.toString();
38      }
39  
40      /***
41       * Returns the prefixed method name.
42       *
43       * @param methodName     the method name
44       * @param methodSequence the method sequence
45       * @param className      the class name
46       * @return the name of the join point
47       */
48      public static String getWrapperMethodName(final String methodName,
49                                                      final int methodSequence,
50                                                      final String className,
51                                                      final String prefix) {
52          final StringBuffer buf = new StringBuffer();
53          //FIXME: double check me
54          // we use the javaC convention for hidden synthetic method
55          // is the methodSequence enough ?
56          // [ Alex: looks like it will change between each RW since tied to ctx match ]
57          buf.append(TransformationConstants.WRAPPER_METHOD_PREFIX);
58          buf.append(methodSequence);
59          buf.append("$");
60          buf.append(prefix);
61          buf.append(methodName);
62          buf.append(TransformationConstants.DELIMITER);
63          buf.append(methodSequence);
64          buf.append(TransformationConstants.DELIMITER);
65          buf.append(className.replace('.', '_').replace('/', '_'));
66          return buf.toString();
67      }
68  
69      /***
70       * Build the join point invoke method descriptor for code (method or constructor) join points.
71       * Depends if the target method is static or not.
72       *
73       * @param codeModifiers
74       * @param codeDesc
75       * @param callerTypeName
76       * @param calleeTypeName
77       * @return
78       */
79      public static String getInvokeSignatureForCodeJoinPoints(final int codeModifiers,
80                                                               final String codeDesc,
81                                                               final String callerTypeName,
82                                                               final String calleeTypeName) {
83          StringBuffer sig = new StringBuffer("(");
84          if (!Modifier.isStatic(codeModifiers)) {
85              // callee is arg0 for non static target method invoke call
86              // else it is skept
87              sig.append('L');
88              sig.append(calleeTypeName);
89              sig.append(';');
90          }
91          int index = codeDesc.lastIndexOf(')');
92          sig.append(codeDesc.substring(1, index));
93          sig.append('L');
94          sig.append(callerTypeName);
95          sig.append(';');
96          sig.append(codeDesc.substring(index, codeDesc.length()));
97          return sig.toString();
98      }
99  
100     /***
101      * Build the join point invoke method descriptor for field join points.
102      * Depends if the target field is static or not.
103      *
104      * @param fieldModifiers
105      * @param fieldDesc
106      * @param callerTypeName
107      * @param calleeTypeName
108      * @return
109      */
110     public static String getInvokeSignatureForFieldJoinPoints(final int fieldModifiers,
111                                                               final String fieldDesc,
112                                                               final String callerTypeName,
113                                                               final String calleeTypeName) {
114         StringBuffer sig = new StringBuffer("(");
115         if (!Modifier.isStatic(fieldModifiers)) {
116             // callee is arg0 for non static target method invoke call
117             // else it is skept
118             sig.append('L');
119             sig.append(calleeTypeName);
120             sig.append(';');
121         }
122         sig.append(fieldDesc);
123         sig.append('L');
124         sig.append(callerTypeName);
125         sig.append(';');
126         sig.append(')');
127         sig.append(fieldDesc);
128         return sig.toString();
129     }
130 
131     /***
132      * Returns the method name used for constructor body
133      *
134      * @param calleeTypeName
135      * @return
136      */
137     public static String getConstructorBodyMethodName(final String calleeTypeName) {
138         return TransformationConstants.ASPECTWERKZ_PREFIX;
139     }
140 
141     /***
142      * Returns the method used for constructor body signature
143      * The callee type name is prepended to the constructor signature
144      * 
145      * @param ctorDesc
146      * @param calleeTypeName
147      * @return
148      */
149     public static String getConstructorBodyMethodSignature(final String ctorDesc, final String calleeTypeName) {
150         StringBuffer sig = new StringBuffer("(L");
151         sig.append(calleeTypeName);
152         sig.append(";");
153         sig.append(ctorDesc.substring(1));
154         return sig.toString();
155     }
156 }