001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.config;
006:
007: import com.tc.aspectwerkz.expression.ExpressionContext;
008: import com.tc.aspectwerkz.expression.ExpressionVisitor;
009: import com.tc.aspectwerkz.expression.PointcutType;
010: import com.tc.aspectwerkz.reflect.ClassInfo;
011: import com.tc.aspectwerkz.reflect.FieldInfo;
012: import com.tc.object.bytecode.aspectwerkz.ExpressionHelper;
013:
014: public class Root {
015:
016: private final String className;
017: private final String fieldNameOrExpression;
018: private final String rootName;
019: private final DsoFinal dsoFinal;
020: private final Type type;
021:
022: public Root(String rootExpr) {
023: this (null, rootExpr, null, DsoFinal.NOT_SET, Type.FIELD_EXPR);
024: }
025:
026: public Root(String rootExpr, String rootName) {
027: this (null, rootExpr, rootName, DsoFinal.NOT_SET,
028: Type.FIELD_EXPR);
029: }
030:
031: public Root(String className, String fieldName, String rootName) {
032: this (className, fieldName, rootName, DsoFinal.NOT_SET,
033: Type.FIELD_NAME);
034: }
035:
036: public Root(String className, String fieldName, String rootName,
037: boolean dsoFinal) {
038: this (className, fieldName, rootName, (dsoFinal ? DsoFinal.TRUE
039: : DsoFinal.FALSE), Type.FIELD_NAME);
040: }
041:
042: private Root(String className, String fieldName, String rootName,
043: DsoFinal dsoFinal, Type type) {
044: this .className = className;
045: this .fieldNameOrExpression = fieldName;
046: this .rootName = rootName;
047: this .dsoFinal = dsoFinal;
048: this .type = type;
049: }
050:
051: public boolean isExpression() {
052: return (type == Type.FIELD_EXPR);
053: }
054:
055: public String getClassName() {
056: if (type != Type.FIELD_NAME) {
057: throw new IllegalStateException();
058: }
059: return this .className;
060: }
061:
062: public String getFieldName() {
063: if (type != Type.FIELD_NAME) {
064: throw new IllegalStateException();
065: }
066: return this .fieldNameOrExpression;
067: }
068:
069: public String getFieldExpression() {
070: if (type != Type.FIELD_EXPR) {
071: throw new IllegalStateException();
072: }
073: return this .fieldNameOrExpression;
074: }
075:
076: public String getRootName(FieldInfo fieldInfo) {
077: return rootName == null ? fieldInfo.getDeclaringType()
078: .getName()
079: + "." + fieldInfo.getName() : rootName;
080: }
081:
082: public boolean isDsoFinal(boolean isPrimitive) {
083: if (dsoFinal != DsoFinal.NOT_SET) {
084: return (dsoFinal == DsoFinal.TRUE);
085: } else {
086: return !isPrimitive;
087: }
088: }
089:
090: private boolean isDsoFinal() {
091: return (dsoFinal == DsoFinal.TRUE);
092: }
093:
094: public boolean matches(ClassInfo ci,
095: ExpressionHelper expressionHelper) {
096: if (type == Type.FIELD_EXPR) {
097: ExpressionContext ctxt = expressionHelper
098: .createWithinExpressionContext(ci);
099: ExpressionVisitor visitor = expressionHelper
100: .createExpressionVisitor(fieldNameOrExpression);
101: return visitor.match(ctxt);
102: } else if (type == Type.FIELD_NAME) {
103: return ci.getName().equals(className);
104: }
105: throw new AssertionError();
106: }
107:
108: public boolean matches(FieldInfo fi,
109: ExpressionHelper expressionHelper) {
110: if (type == Type.FIELD_EXPR) {
111: ExpressionVisitor visitor = expressionHelper
112: .createExpressionVisitor("get("
113: + fieldNameOrExpression + ")");
114: return visitor.match(new ExpressionContext(
115: PointcutType.GET, fi, fi));
116: } else if (type == Type.FIELD_NAME) {
117: //
118: return fi.getDeclaringType().getName().equals(className)
119: && fi.getName().equals(fieldNameOrExpression);
120: }
121:
122: throw new AssertionError();
123: }
124:
125: public String toString() {
126: return getClass().getName() + "[className=" + className
127: + ", fieldNameOrExpression=" + fieldNameOrExpression
128: + ", rootName=" + rootName + ", dsoFinal="
129: + isDsoFinal() + "]";
130: }
131:
132: private static class Type {
133: static final Type FIELD_NAME = new Type();
134: static final Type FIELD_EXPR = new Type();
135: }
136:
137: private static class DsoFinal {
138: private final String s;
139:
140: public DsoFinal(String s) {
141: this .s = s;
142: }
143:
144: public String toString() {
145: return s;
146: }
147:
148: static final DsoFinal NOT_SET = new DsoFinal("not set");
149: static final DsoFinal TRUE = new DsoFinal("true");
150: static final DsoFinal FALSE = new DsoFinal("false");
151: }
152:
153: }
|