KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > PatternHelper


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 org.terracotta.dso;
5
6 import org.apache.commons.lang.StringUtils;
7 import org.eclipse.jdt.core.Flags;
8 import org.eclipse.jdt.core.ICompilationUnit;
9 import org.eclipse.jdt.core.IJavaElement;
10 import org.eclipse.jdt.core.IMethod;
11 import org.eclipse.jdt.core.IPackageDeclaration;
12 import org.eclipse.jdt.core.IPackageFragment;
13 import org.eclipse.jdt.core.IType;
14 import org.eclipse.jdt.core.JavaModelException;
15 import org.eclipse.jdt.core.Signature;
16 import org.eclipse.jdt.core.dom.IMethodBinding;
17 import org.eclipse.jdt.core.dom.MethodDeclaration;
18
19 import com.tc.aspectwerkz.expression.ExpressionContext;
20 import com.tc.aspectwerkz.expression.ExpressionVisitor;
21 import com.tc.aspectwerkz.reflect.ClassInfo;
22 import com.tc.aspectwerkz.reflect.MemberInfo;
23 import com.tc.aspectwerkz.reflect.MethodInfo;
24 import com.tc.object.bytecode.aspectwerkz.AsmMethodInfo;
25 import com.tc.object.bytecode.aspectwerkz.ClassInfoFactory;
26 import com.tc.object.bytecode.aspectwerkz.ExpressionHelper;
27
28 /**
29  * Utility singleton for bridging the gap between Eclipse internal
30  * Java parser constructs (MethodDeclaration) and Aspectwerks expressions.
31  */

32
33 public class PatternHelper {
34   private static PatternHelper m_helper = new PatternHelper();
35   private ExpressionHelper m_expressionHelper;
36   private ClassInfoFactory m_classInfoFactory;
37
38   public static final PatternHelper getHelper() {
39     return m_helper;
40   }
41   
42   private PatternHelper() {
43     m_expressionHelper = new ExpressionHelper();
44     m_classInfoFactory = new ClassInfoFactory();
45   }
46
47   public boolean matchesMethod(String JavaDoc expression, final IMethod method) {
48     MethodInfo methodInfo = method != null ? getMethodInfo(method) : null;
49     
50     int parentIndex = expression.indexOf('(');
51     if(parentIndex > 0){
52       String JavaDoc tmp = expression.substring(parentIndex);
53       tmp = StringUtils.replaceChars(tmp, '$', '.');
54       expression = expression.substring(0, parentIndex)+tmp;
55     }
56     
57     return methodInfo != null && matchesMember(expression, methodInfo);
58   }
59   
60   public boolean matchesMethod(
61     final String JavaDoc expression,
62     final MethodDeclaration methodDecl)
63   {
64     return matchesMember(expression, getMethodInfo(methodDecl));
65   }
66   
67   public ExpressionContext createExecutionExpressionContext(final IMethod method) {
68     return createExecutionExpressionContext(getMethodInfo(method));
69   }
70   
71   public ExpressionContext createExecutionExpressionContext(final MemberInfo methodInfo) {
72     return m_expressionHelper.createExecutionExpressionContext(methodInfo);
73   }
74   
75   public void testValidateMethodExpression(final String JavaDoc expr) throws Exception JavaDoc {
76     String JavaDoc execExpr = ExpressionHelper.expressionPattern2ExecutionExpression(expr);
77     m_expressionHelper.createExpressionVisitor(execExpr);
78   }
79   
80   public boolean matchesMethod(final String JavaDoc expr, final ExpressionContext exprCntx) {
81     try {
82       String JavaDoc execExpr = ExpressionHelper.expressionPattern2ExecutionExpression(expr);
83       ExpressionVisitor visitor = m_expressionHelper.createExpressionVisitor(execExpr);
84     
85       return visitor.match(exprCntx);
86     } catch(Exception JavaDoc e) {
87       return false;
88     }
89   }
90   
91   public boolean matchesMember(String JavaDoc expr, final MemberInfo methodInfo) {
92     int parentIndex = expr.indexOf('(');
93     if(parentIndex > 0){
94       String JavaDoc tmp = expr.substring(parentIndex);
95       tmp = StringUtils.replaceChars(tmp, '$', '.');
96       expr = expr.substring(0, parentIndex)+tmp;
97     }
98     
99     return matchesMethod(expr, m_expressionHelper.createExecutionExpressionContext(methodInfo));
100   }
101
102   public static String JavaDoc getFullyQualifiedName(IType type) {
103     return type.getFullyQualifiedName('$');
104   }
105   
106   public ExpressionContext createWithinExpressionContext(final IType type) {
107     return createWithinExpressionContext(getFullyQualifiedName(type));
108   }
109
110   public ExpressionContext createWithinExpressionContext(final IPackageDeclaration packageDecl) {
111     return createWithinExpressionContext(packageDecl.getElementName());
112   }
113   
114   public ExpressionContext createWithinExpressionContext(final IPackageFragment fragment) {
115     return createWithinExpressionContext(fragment.getElementName());
116   }
117
118   public ExpressionContext createWithinExpressionContext(final String JavaDoc className) {
119     return createWithinExpressionContext(m_classInfoFactory.getClassInfo(className));
120   }
121   
122   public ExpressionContext createWithinExpressionContext(final ClassInfo classInfo) {
123     return m_expressionHelper.createWithinExpressionContext(classInfo);
124   }
125   
126   public boolean matchesClass(final String JavaDoc expr, final ExpressionContext exprCntx) {
127     try {
128       String JavaDoc withinExpr = ExpressionHelper.expressionPattern2WithinExpression(expr);
129       ExpressionVisitor visitor = m_expressionHelper.createExpressionVisitor(withinExpr);
130     
131       return visitor.match(exprCntx);
132     } catch(Exception JavaDoc e) {
133       return false;
134     }
135   }
136   
137   public boolean matchesClass(final String JavaDoc expression, final String JavaDoc className) {
138     return matchesClass(expression, m_classInfoFactory.getClassInfo(className));
139   }
140   
141   public boolean matchesClass(final String JavaDoc expr, final ClassInfo classInfo) {
142     return matchesClass(expr, m_expressionHelper.createWithinExpressionContext(classInfo));
143   }
144   
145   public boolean matchesType(final String JavaDoc expr, final IType type) {
146     return matchesClass(expr, createWithinExpressionContext(type));
147   }
148   
149   public boolean matchesPackageFragment(final String JavaDoc expr, final IPackageFragment fragment) {
150     return matchesClass(expr, createWithinExpressionContext(fragment));
151   }
152
153   public boolean matchesPackageDeclaration(final String JavaDoc expr, final IPackageDeclaration packageDecl) {
154     return matchesClass(expr, createWithinExpressionContext(packageDecl));
155   }
156
157   public static String JavaDoc getSignature(IMethod method)
158     throws JavaModelException
159   {
160     IType dType = method.getDeclaringType();
161     String JavaDoc[] pTypes = method.getParameterTypes();
162     String JavaDoc rType = method.getReturnType();
163     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("(");
164     
165     rType = Signature.getTypeErasure(rType);
166     
167     for(int i = 0; i < pTypes.length; i++) {
168       pTypes[i] = Signature.getTypeErasure(pTypes[i]);
169       if(pTypes[i].charAt(0) == 'T') {
170         pTypes[i] = "Ljava.lang.Object;";
171       } else if(pTypes[i].charAt(0) == '[' && pTypes[i].charAt(1) == 'T') {
172         pTypes[i] = "[Ljava.lang.Object;";
173       }
174       JdtUtils.resolveTypeName(pTypes[i], dType, sb);
175     }
176     sb.append(')');
177     if(rType.charAt(0) == 'T') {
178       rType = "Ljava.lang.Object;";
179     } else if(rType.charAt(0) == '[' && rType.charAt(1) == 'T') {
180       rType = "[Ljava.lang.Object;";
181     }
182     JdtUtils.resolveTypeName(rType, dType, sb);
183
184     String JavaDoc result = sb.toString().replace('.', '/');
185
186     return result;
187   }
188   
189   public static String JavaDoc getFullName(IMethod method) {
190     IType declaringType = method.getDeclaringType();
191     return getFullyQualifiedName(declaringType)+"."+method.getElementName();
192   }
193   
194   public static boolean isVarargs(IMethod method) {
195     try {
196       return Flags.isVarargs(method.getFlags());
197     } catch(JavaModelException jme) {
198       return false;
199     }
200   }
201   
202   /**
203    * Returns a full method signature, compatible with the forms required
204    * by the DSO config format.
205    */

206   public static String JavaDoc getJavadocSignature(IMethod method)
207     throws JavaModelException
208   {
209     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
210     IType declaringType = method.getDeclaringType();
211     boolean isVararg = isVarargs(method);
212     String JavaDoc[] params = method.getParameterTypes();
213     int lastParam = params.length - 1;
214     int dim;
215     
216     try {
217       String JavaDoc returnType = method.getReturnType();
218
219       dim = Signature.getArrayCount(returnType);
220       returnType = Signature.getTypeErasure(returnType);
221       sb.append(JdtUtils.getResolvedTypeFileName(returnType, declaringType));
222       while(dim > 0) {
223         sb.append("[]");
224         dim--;
225       }
226     } catch(JavaModelException jme) {
227       sb.append("*");
228     }
229     
230     sb.append(" ");
231     sb.append(getFullyQualifiedName(declaringType));
232     sb.append(".");
233     sb.append(method.isConstructor() ? "__INIT__" : method.getElementName());
234     sb.append("(");
235
236     for(int i = 0; i < params.length; i++) {
237       if(i != 0) {
238         sb.append(", ");
239       }
240
241       params[i] = Signature.getTypeErasure(params[i]);
242       sb.append(JdtUtils.getResolvedTypeFileName(params[i], declaringType));
243       dim = Signature.getArrayCount(params[i]);
244     
245       if(i == lastParam && isVararg) {
246         dim--;
247       }
248       while (dim > 0) {
249         sb.append("[]");
250         dim--;
251       }
252       if(i == lastParam && isVararg) {
253         sb.append("...");
254       }
255     }
256     
257     sb.append(")");
258     
259     return sb.toString();
260   }
261   
262   public MethodInfo getMethodInfo(IMethod method) {
263     MethodInfo info = null;
264     
265     if(method != null) {
266       try {
267         String JavaDoc className = getFullyQualifiedName(method.getDeclaringType());
268         String JavaDoc methodName = method.isConstructor() ? "__INIT__" : method.getElementName();
269         String JavaDoc desc = getSignature(method);
270         String JavaDoc[] excepts = method.getExceptionTypes();
271         int access = method.getFlags();
272   
273         info = getMethodInfo(access, className, methodName, desc, excepts);
274       } catch(JavaModelException jme) {/**/}
275         catch(NullPointerException JavaDoc npe) {/**/}
276     }
277     
278     return info;
279   }
280   
281   public MethodInfo getMethodInfo(MethodDeclaration methodDecl) {
282     return getMethodInfo(methodDecl2IMethod(methodDecl));
283   }
284   
285   public static IMethod methodDecl2IMethod(MethodDeclaration methodDecl) {
286     IMethodBinding binding = methodDecl.resolveBinding();
287     IJavaElement elem = binding != null ? binding.getJavaElement() : null;
288     
289     if(elem instanceof IMethod) {
290       return (IMethod)elem;
291     }
292
293     return null;
294   }
295   
296   public MethodInfo getMethodInfo(
297     int modifiers,
298     String JavaDoc className,
299     String JavaDoc methodName,
300     String JavaDoc description,
301     String JavaDoc[] exceptions)
302   {
303     return new AsmMethodInfo(m_classInfoFactory,
304                              modifiers,
305                              className,
306                              methodName,
307                              description,
308                              exceptions);
309   }
310   
311   public static String JavaDoc getExecutionPattern(IJavaElement element) {
312     if(element instanceof IMethod) {
313       return getExecutionPattern((IMethod)element);
314     } else if(element instanceof IType) {
315       return getExecutionPattern((IType)element);
316     } else if(element instanceof IPackageFragment) {
317       return getExecutionPattern((IPackageFragment)element);
318     } else if(element instanceof ICompilationUnit) {
319       return getExecutionPattern(((ICompilationUnit)element).findPrimaryType());
320     } else if(element instanceof IPackageDeclaration) {
321       return getExecutionPattern((IPackageDeclaration)element);
322     }
323     return null;
324   }
325
326   public static String JavaDoc getExecutionPattern(IMethod method) {
327     try {
328       if(!method.getOpenable().isOpen()) {
329         method.getOpenable().open(null);
330       }
331       return getJavadocSignature(method);
332     } catch(JavaModelException jme) {
333       IType type = method.getDeclaringType();
334       String JavaDoc typeName = getFullyQualifiedName(type);
335
336       return "* "+typeName+"."+method.getElementName()+"(..)";
337     }
338   }
339   
340   public static String JavaDoc getExecutionPattern(IType type) {
341     return "* "+getWithinPattern(type)+"(..)";
342   }
343   
344   public static String JavaDoc getWithinPattern(IType type) {
345     return getFullyQualifiedName(type)+".*";
346   }
347   
348   public static String JavaDoc getExecutionPattern(IPackageFragment fragment) {
349     return "* "+getWithinPattern(fragment)+"(..)";
350   }
351   
352   public static String JavaDoc getExecutionPattern(IPackageDeclaration packageDecl) {
353     return "* "+getWithinPattern(packageDecl)+"(..)";
354   }
355
356   public static String JavaDoc getWithinPattern(IPackageFragment fragment) {
357     return fragment.getElementName()+"..*";
358   }
359
360   public static String JavaDoc getWithinPattern(IPackageDeclaration packageDecl) {
361     return packageDecl.getElementName()+"..*";
362   }
363 }
364
365
Popular Tags