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.aspectwerkz.joinpoint.management;
005:
006: import java.lang.reflect.Constructor;
007: import java.lang.reflect.Field;
008: import java.lang.reflect.Method;
009:
010: import com.tc.aspectwerkz.joinpoint.EnclosingStaticJoinPoint;
011: import com.tc.aspectwerkz.joinpoint.impl.CatchClauseSignatureImpl;
012: import com.tc.aspectwerkz.joinpoint.impl.ConstructorSignatureImpl;
013: import com.tc.aspectwerkz.joinpoint.impl.EnclosingStaticJoinPointImpl;
014: import com.tc.aspectwerkz.joinpoint.impl.FieldSignatureImpl;
015: import com.tc.aspectwerkz.joinpoint.impl.MethodSignatureImpl;
016: import com.tc.aspectwerkz.joinpoint.impl.StaticInitializerSignatureImpl;
017: import com.tc.aspectwerkz.reflect.impl.asm.AsmClassInfoRepository;
018: import com.tc.aspectwerkz.reflect.ReflectHelper;
019: import com.tc.aspectwerkz.transform.TransformationConstants;
020: import com.tc.aspectwerkz.transform.inlining.AsmHelper;
021:
022: /**
023: * Factory class for the signature hierarchy.
024: * The helper methods here are called by the JIT jp.
025: * <p/>
026: * TODO may be worth having a cache
027: *
028: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
029: * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
030: */
031: public final class SignatureFactory {
032:
033: /**
034: * Method signature factory
035: *
036: * @param declaringClass
037: * @param joinPointHash
038: * @return
039: */
040: public static final MethodSignatureImpl newMethodSignature(
041: final Class declaringClass, final int joinPointHash) {
042: AsmClassInfoRepository.getRepository(
043: declaringClass.getClassLoader()).removeClassInfo(
044: declaringClass.getName().replace('.', '/'));
045: Method[] methods = declaringClass.getDeclaredMethods();
046: for (int i = 0; i < methods.length; i++) {
047: Method method = methods[i];
048: if (ReflectHelper.calculateHash(method) == joinPointHash) {
049: return new MethodSignatureImpl(declaringClass, method);
050: }
051: }
052: // lookup in the hierarchy
053: MethodSignatureImpl signature = null;
054: for (int i = 0; i < declaringClass.getInterfaces().length; i++) {
055: signature = newMethodSignature(declaringClass
056: .getInterfaces()[i], joinPointHash);
057: if (signature != null) {
058: return signature;
059: }
060: }
061: if (declaringClass.getSuperclass() != null) {
062: signature = newMethodSignature(declaringClass
063: .getSuperclass(), joinPointHash);
064: } else {
065: return null;
066: }
067: return signature;
068: }
069:
070: /**
071: * Field signature factory
072: *
073: * @param declaringClass
074: * @param joinPointHash
075: * @return
076: */
077: public static final FieldSignatureImpl newFieldSignature(
078: final Class declaringClass, final int joinPointHash) {
079: Field[] fields = declaringClass.getDeclaredFields();
080: for (int i = 0; i < fields.length; i++) {
081: Field field = fields[i];
082: if (ReflectHelper.calculateHash(field) == joinPointHash) {
083: return new FieldSignatureImpl(declaringClass, field);
084: }
085: }
086: // lookup in the hierarchy
087: if (declaringClass.getSuperclass() != null) {
088: return newFieldSignature(declaringClass.getSuperclass(),
089: joinPointHash);
090: } else {
091: return null;
092: }
093: }
094:
095: /**
096: * Constructor signature factory
097: *
098: * @param declaringClass
099: * @param joinPointHash
100: * @return
101: */
102: public static final ConstructorSignatureImpl newConstructorSignature(
103: final Class declaringClass, final int joinPointHash) {
104: for (int i = 0; i < declaringClass.getDeclaredConstructors().length; i++) {
105: Constructor c = declaringClass.getDeclaredConstructors()[i];
106: if (ReflectHelper.calculateHash(c) == joinPointHash) {
107: return new ConstructorSignatureImpl(declaringClass, c);
108: }
109: }
110: // lookup in the hierarchy
111: if (declaringClass.getSuperclass() != null) {
112: return newConstructorSignature(declaringClass
113: .getSuperclass(), joinPointHash);
114: } else {
115: return null;
116: }
117: }
118:
119: /**
120: * Handler signature factory
121: *
122: * @param exceptionClass
123: * @return
124: */
125: public static final CatchClauseSignatureImpl newCatchClauseSignature(
126: final Class exceptionClass) {
127: return new CatchClauseSignatureImpl(exceptionClass);
128: }
129:
130: /**
131: * Enclosing signature factory, wrapped behind an EnclosingStaticJoinPoint for syntax consistency
132: *
133: * @param declaringClass
134: * @param name
135: * @param description
136: * @return
137: */
138: public static EnclosingStaticJoinPoint newEnclosingStaticJoinPoint(
139: final Class declaringClass, final String name,
140: final String description) {
141: if (TransformationConstants.CLINIT_METHOD_NAME.equals(name)) {
142: return new EnclosingStaticJoinPointImpl(
143: new StaticInitializerSignatureImpl(declaringClass),
144: JoinPointType.STATIC_INITIALIZATION);
145: } else if (TransformationConstants.INIT_METHOD_NAME
146: .equals(name)) {
147: return new EnclosingStaticJoinPointImpl(
148: newConstructorSignature(declaringClass, AsmHelper
149: .calculateConstructorHash(description)),
150: JoinPointType.CONSTRUCTOR_EXECUTION);
151: } else {
152: // regular method
153: return new EnclosingStaticJoinPointImpl(newMethodSignature(
154: declaringClass, AsmHelper.calculateMethodHash(name,
155: description)),
156: JoinPointType.METHOD_EXECUTION);
157: }
158: }
159:
160: /**
161: * Static initialization factory
162: *
163: * @param declaringClass
164: * @return
165: */
166: public static StaticInitializerSignatureImpl newStaticInitializationSignature(
167: final Class declaringClass) {
168: return new StaticInitializerSignatureImpl(declaringClass);
169: }
170:
171: }
|