1 4 package com.tc.aspectwerkz.joinpoint.management; 5 6 import java.lang.reflect.Constructor ; 7 import java.lang.reflect.Field ; 8 import java.lang.reflect.Method ; 9 10 import com.tc.aspectwerkz.joinpoint.EnclosingStaticJoinPoint; 11 import com.tc.aspectwerkz.joinpoint.impl.CatchClauseSignatureImpl; 12 import com.tc.aspectwerkz.joinpoint.impl.ConstructorSignatureImpl; 13 import com.tc.aspectwerkz.joinpoint.impl.EnclosingStaticJoinPointImpl; 14 import com.tc.aspectwerkz.joinpoint.impl.FieldSignatureImpl; 15 import com.tc.aspectwerkz.joinpoint.impl.MethodSignatureImpl; 16 import com.tc.aspectwerkz.joinpoint.impl.StaticInitializerSignatureImpl; 17 import com.tc.aspectwerkz.reflect.impl.asm.AsmClassInfoRepository; 18 import com.tc.aspectwerkz.reflect.ReflectHelper; 19 import com.tc.aspectwerkz.transform.TransformationConstants; 20 import com.tc.aspectwerkz.transform.inlining.AsmHelper; 21 22 31 public final class SignatureFactory { 32 33 40 public static final MethodSignatureImpl newMethodSignature(final Class declaringClass, final int joinPointHash) { 41 AsmClassInfoRepository.getRepository(declaringClass.getClassLoader()).removeClassInfo(declaringClass.getName().replace('.', '/')); 42 Method [] methods = declaringClass.getDeclaredMethods(); 43 for (int i = 0; i < methods.length; i++) { 44 Method method = methods[i]; 45 if (ReflectHelper.calculateHash(method) == joinPointHash) { 46 return new MethodSignatureImpl(declaringClass, method); 47 } 48 } 49 MethodSignatureImpl signature = null; 51 for (int i = 0; i < declaringClass.getInterfaces().length; i++) { 52 signature = newMethodSignature(declaringClass.getInterfaces()[i], joinPointHash); 53 if (signature != null) { 54 return signature; 55 } 56 } 57 if (declaringClass.getSuperclass() != null) { 58 signature = newMethodSignature(declaringClass.getSuperclass(), joinPointHash); 59 } else { 60 return null; 61 } 62 return signature; 63 } 64 65 72 public static final FieldSignatureImpl newFieldSignature(final Class declaringClass, final int joinPointHash) { 73 Field [] fields = declaringClass.getDeclaredFields(); 74 for (int i = 0; i < fields.length; i++) { 75 Field field = fields[i]; 76 if (ReflectHelper.calculateHash(field) == joinPointHash) { 77 return new FieldSignatureImpl(declaringClass, field); 78 } 79 } 80 if (declaringClass.getSuperclass() != null) { 82 return newFieldSignature(declaringClass.getSuperclass(), joinPointHash); 83 } else { 84 return null; 85 } 86 } 87 88 95 public static final ConstructorSignatureImpl newConstructorSignature(final Class declaringClass, 96 final int joinPointHash) { 97 for (int i = 0; i < declaringClass.getDeclaredConstructors().length; i++) { 98 Constructor c = declaringClass.getDeclaredConstructors()[i]; 99 if (ReflectHelper.calculateHash(c) == joinPointHash) { 100 return new ConstructorSignatureImpl(declaringClass, c); 101 } 102 } 103 if (declaringClass.getSuperclass() != null) { 105 return newConstructorSignature(declaringClass.getSuperclass(), joinPointHash); 106 } else { 107 return null; 108 } 109 } 110 111 117 public static final CatchClauseSignatureImpl newCatchClauseSignature(final Class exceptionClass) { 118 return new CatchClauseSignatureImpl(exceptionClass); 119 } 120 121 129 public static EnclosingStaticJoinPoint newEnclosingStaticJoinPoint( 130 final Class declaringClass, 131 final String name, 132 final String description) { 133 if (TransformationConstants.CLINIT_METHOD_NAME.equals(name)) { 134 return new EnclosingStaticJoinPointImpl( 135 new StaticInitializerSignatureImpl(declaringClass), 136 JoinPointType.STATIC_INITIALIZATION 137 ); 138 } else if (TransformationConstants.INIT_METHOD_NAME.equals(name)) { 139 return new EnclosingStaticJoinPointImpl( 140 newConstructorSignature(declaringClass, AsmHelper.calculateConstructorHash(description)), 141 JoinPointType.CONSTRUCTOR_EXECUTION 142 ); 143 } else { 144 return new EnclosingStaticJoinPointImpl( 146 newMethodSignature(declaringClass, AsmHelper.calculateMethodHash(name, description)), 147 JoinPointType.METHOD_EXECUTION 148 ); 149 } 150 } 151 152 158 public static StaticInitializerSignatureImpl newStaticInitializationSignature(final Class declaringClass) { 159 return new StaticInitializerSignatureImpl(declaringClass); 160 } 161 162 } | Popular Tags |