KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > aspectwerkz > ExpressionHelper


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.object.bytecode.aspectwerkz;
5
6 import com.tc.aspectwerkz.expression.ExpressionVisitor;
7 import com.tc.aspectwerkz.expression.ExpressionInfo;
8 import com.tc.aspectwerkz.expression.ExpressionContext;
9 import com.tc.aspectwerkz.expression.PointcutType;
10 import com.tc.aspectwerkz.reflect.MemberInfo;
11 import com.tc.aspectwerkz.reflect.ClassInfo;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * Helper class for dealing with Aspectwerkz expressions
18  */

19 public class ExpressionHelper {
20
21   private final Map JavaDoc expressionInfoCache = new HashMap JavaDoc();
22
23   public ExpressionVisitor[] createExpressionVisitors(String JavaDoc[] expressions) {
24     ExpressionVisitor[] rv = null;
25     if (expressions == null) {
26       rv = new ExpressionVisitor[] {};
27     } else {
28       rv = new ExpressionVisitor[expressions.length];
29       for (int i = 0; i < expressions.length; i++) {
30         rv[i] = createExpressionVisitor(expressions[i]);
31       }
32     }
33     return rv;
34   }
35
36   /**
37    * Creates and returns ExpressionVisitor. An ExpressionVisitor can be used to match ClassInfo, MethodInfo, etc.
38    * against.
39    */

40   public ExpressionVisitor createExpressionVisitor(String JavaDoc expression) {
41     return createExpressionInfo(expression).getExpression();
42   }
43
44   /**
45    * Creates and returns ExpressionInfo from the given expression. <p/>Since the expression namespace doesn't appear to
46    * be used, this is probably the version of this method you're looking for.
47    */

48   public ExpressionInfo createExpressionInfo(String JavaDoc expression) {
49     return createExpressionInfo(expression, "__tc_default");
50   }
51
52   /**
53    * Creates and returns ExpressionInfo object from the given expression and namespace. <p/>I'm not sure what namespace
54    * is for. As far as I can tell from examining the aspectwerkz code, it's not actually used.
55    */

56   public ExpressionInfo createExpressionInfo(String JavaDoc expression, String JavaDoc namespace) {
57     ExpressionInfo info;
58     synchronized (expressionInfoCache) {
59       info = (ExpressionInfo) expressionInfoCache.get(expression);
60       if (info == null) {
61         info = new ExpressionInfo(expression, namespace);
62         expressionInfoCache.put(expression, info);
63       }
64     }
65     return info;
66   }
67
68   /**
69    * Converts an array of raw expression to an array of within(&lt;expression&gt;) expressions
70    */

71   public static String JavaDoc[] expressionPatterns2WithinExpressions(String JavaDoc[] expressions) {
72     String JavaDoc[] rv = null;
73     if (expressions == null) {
74       rv = new String JavaDoc[0];
75     } else {
76       rv = new String JavaDoc[expressions.length];
77       for (int i=0; i<expressions.length; i++) {
78         rv[i] = expressionPattern2WithinExpression(expressions[i]);
79       }
80     }
81     return rv;
82   }
83
84   /**
85    * Converts a raw expression to a within(&lt;expression&gt;) expression
86    */

87   public static String JavaDoc expressionPattern2WithinExpression(String JavaDoc expression) {
88     return "within(" + expression + ")";
89   }
90
91   public static String JavaDoc expressionPattern2ExecutionExpression(String JavaDoc expression) {
92     return "execution(" + expression + ")";
93   }
94
95   /**
96    * Creates a within expression context for testing to see if a class is within a certain
97    * class expression.
98    */

99   public ExpressionContext createWithinExpressionContext(ClassInfo classInfo) {
100     // TODO: Cache me
101
ExpressionContext ctxt = new ExpressionContext(PointcutType.WITHIN, classInfo, classInfo);
102     return ctxt;
103   }
104
105   /**
106    * Creates an execution expression context for testing to see of a method matches the execution of a
107    * certain method expression
108    */

109   public ExpressionContext createExecutionExpressionContext(MemberInfo methodInfo) {
110     // TODO: Cache me
111
ExpressionContext ctxt = new ExpressionContext(PointcutType.EXECUTION, methodInfo, methodInfo);
112     return ctxt;
113   }
114 }
Popular Tags