1 8 package org.codehaus.aspectwerkz.joinpoint.management; 9 10 import java.lang.reflect.Constructor ; 11 import java.lang.reflect.Field ; 12 import java.lang.reflect.Method ; 13 14 import org.codehaus.aspectwerkz.joinpoint.EnclosingStaticJoinPoint; 15 import org.codehaus.aspectwerkz.joinpoint.impl.CatchClauseSignatureImpl; 16 import org.codehaus.aspectwerkz.joinpoint.impl.ConstructorSignatureImpl; 17 import org.codehaus.aspectwerkz.joinpoint.impl.EnclosingStaticJoinPointImpl; 18 import org.codehaus.aspectwerkz.joinpoint.impl.FieldSignatureImpl; 19 import org.codehaus.aspectwerkz.joinpoint.impl.MethodSignatureImpl; 20 import org.codehaus.aspectwerkz.joinpoint.impl.StaticInitializerSignatureImpl; 21 import org.codehaus.aspectwerkz.reflect.ReflectHelper; 22 import org.codehaus.aspectwerkz.transform.TransformationConstants; 23 import org.codehaus.aspectwerkz.transform.inlining.AsmHelper; 24 25 34 public final class SignatureFactory { 35 36 43 public static final MethodSignatureImpl newMethodSignature(final Class declaringClass, final int joinPointHash) { 44 Method [] methods = declaringClass.getDeclaredMethods(); 45 for (int i = 0; i < methods.length; i++) { 46 Method method = methods[i]; 47 if (ReflectHelper.calculateHash(method) == joinPointHash) { 48 return new MethodSignatureImpl(declaringClass, method); 49 } 50 } 51 MethodSignatureImpl signature = null; 53 for (int i = 0; i < declaringClass.getInterfaces().length; i++) { 54 signature = newMethodSignature(declaringClass.getInterfaces()[i], joinPointHash); 55 if (signature != null) { 56 return signature; 57 } 58 } 59 if (declaringClass.getSuperclass() != null) { 60 signature = newMethodSignature(declaringClass.getSuperclass(), joinPointHash); 61 } else { 62 return null; 63 } 64 return signature; 65 } 66 67 74 public static final FieldSignatureImpl newFieldSignature(final Class declaringClass, final int joinPointHash) { 75 Field [] fields = declaringClass.getDeclaredFields(); 76 for (int i = 0; i < fields.length; i++) { 77 Field field = fields[i]; 78 if (ReflectHelper.calculateHash(field) == joinPointHash) { 79 return new FieldSignatureImpl(declaringClass, field); 80 } 81 } 82 if (declaringClass.getSuperclass() != null) { 84 return newFieldSignature(declaringClass.getSuperclass(), joinPointHash); 85 } else { 86 return null; 87 } 88 } 89 90 97 public static final ConstructorSignatureImpl newConstructorSignature(final Class declaringClass, 98 final int joinPointHash) { 99 Constructor constructor = null; 100 for (int i = 0; i < declaringClass.getDeclaredConstructors().length; i++) { 101 Constructor c = declaringClass.getDeclaredConstructors()[i]; 102 if (ReflectHelper.calculateHash(c) == joinPointHash) { 103 return new ConstructorSignatureImpl(declaringClass, c); 104 } 105 } 106 if (declaringClass.getSuperclass() != null) { 108 return newConstructorSignature(declaringClass.getSuperclass(), joinPointHash); 109 } else { 110 return null; 111 } 112 } 113 114 120 public static final CatchClauseSignatureImpl newCatchClauseSignature(final Class exceptionClass) { 121 return new CatchClauseSignatureImpl(exceptionClass); 122 } 123 124 132 public static EnclosingStaticJoinPoint newEnclosingStaticJoinPoint( 133 final Class declaringClass, 134 final String name, 135 final String description) { 136 if (TransformationConstants.CLINIT_METHOD_NAME.equals(name)) { 137 return new EnclosingStaticJoinPointImpl( 138 new StaticInitializerSignatureImpl(declaringClass), 139 JoinPointType.STATIC_INITIALIZATION 140 ); 141 } else if (TransformationConstants.INIT_METHOD_NAME.equals(name)) { 142 return new EnclosingStaticJoinPointImpl( 143 newConstructorSignature(declaringClass, AsmHelper.calculateConstructorHash(description)), 144 JoinPointType.CONSTRUCTOR_EXECUTION 145 ); 146 } else { 147 return new EnclosingStaticJoinPointImpl( 149 newMethodSignature(declaringClass, AsmHelper.calculateMethodHash(name, description)), 150 JoinPointType.METHOD_EXECUTION 151 ); 152 } 153 } 154 155 161 public static StaticInitializerSignatureImpl newStaticInitializationSignature(final Class declaringClass) { 162 return new StaticInitializerSignatureImpl(declaringClass); 163 } 164 165 } | Popular Tags |