001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.bytecode.aspectwerkz;
005:
006: import com.tc.aspectwerkz.expression.ExpressionVisitor;
007: import com.tc.aspectwerkz.expression.ExpressionInfo;
008: import com.tc.aspectwerkz.expression.ExpressionContext;
009: import com.tc.aspectwerkz.expression.PointcutType;
010: import com.tc.aspectwerkz.reflect.MemberInfo;
011: import com.tc.aspectwerkz.reflect.ClassInfo;
012:
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: /**
017: * Helper class for dealing with Aspectwerkz expressions
018: */
019: public class ExpressionHelper {
020:
021: private final Map expressionInfoCache = new HashMap();
022:
023: public ExpressionVisitor[] createExpressionVisitors(
024: String[] expressions) {
025: ExpressionVisitor[] rv = null;
026: if (expressions == null) {
027: rv = new ExpressionVisitor[] {};
028: } else {
029: rv = new ExpressionVisitor[expressions.length];
030: for (int i = 0; i < expressions.length; i++) {
031: rv[i] = createExpressionVisitor(expressions[i]);
032: }
033: }
034: return rv;
035: }
036:
037: /**
038: * Creates and returns ExpressionVisitor. An ExpressionVisitor can be used to match ClassInfo, MethodInfo, etc.
039: * against.
040: */
041: public ExpressionVisitor createExpressionVisitor(String expression) {
042: return createExpressionInfo(expression).getExpression();
043: }
044:
045: /**
046: * Creates and returns ExpressionInfo from the given expression. <p/>Since the expression namespace doesn't appear to
047: * be used, this is probably the version of this method you're looking for.
048: */
049: public ExpressionInfo createExpressionInfo(String expression) {
050: return createExpressionInfo(expression, "__tc_default");
051: }
052:
053: /**
054: * Creates and returns ExpressionInfo object from the given expression and namespace. <p/>I'm not sure what namespace
055: * is for. As far as I can tell from examining the aspectwerkz code, it's not actually used.
056: */
057: public ExpressionInfo createExpressionInfo(String expression,
058: String namespace) {
059: ExpressionInfo info;
060: synchronized (expressionInfoCache) {
061: info = (ExpressionInfo) expressionInfoCache.get(expression);
062: if (info == null) {
063: info = new ExpressionInfo(expression, namespace);
064: expressionInfoCache.put(expression, info);
065: }
066: }
067: return info;
068: }
069:
070: /**
071: * Converts an array of raw expression to an array of within(<expression>) expressions
072: */
073: public static String[] expressionPatterns2WithinExpressions(
074: String[] expressions) {
075: String[] rv = null;
076: if (expressions == null) {
077: rv = new String[0];
078: } else {
079: rv = new String[expressions.length];
080: for (int i = 0; i < expressions.length; i++) {
081: rv[i] = expressionPattern2WithinExpression(expressions[i]);
082: }
083: }
084: return rv;
085: }
086:
087: /**
088: * Converts a raw expression to a within(<expression>) expression
089: */
090: public static String expressionPattern2WithinExpression(
091: String expression) {
092: return "within(" + expression + ")";
093: }
094:
095: public static String expressionPattern2ExecutionExpression(
096: String expression) {
097: return "execution(" + expression + ")";
098: }
099:
100: /**
101: * Creates a within expression context for testing to see if a class is within a certain
102: * class expression.
103: */
104: public ExpressionContext createWithinExpressionContext(
105: ClassInfo classInfo) {
106: // TODO: Cache me
107: ExpressionContext ctxt = new ExpressionContext(
108: PointcutType.WITHIN, classInfo, classInfo);
109: return ctxt;
110: }
111:
112: /**
113: * Creates an execution expression context for testing to see of a method matches the execution of a
114: * certain method expression
115: */
116: public ExpressionContext createExecutionExpressionContext(
117: MemberInfo methodInfo) {
118: // TODO: Cache me
119: ExpressionContext ctxt = new ExpressionContext(
120: PointcutType.EXECUTION, methodInfo, methodInfo);
121: return ctxt;
122: }
123: }
|