KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > joinpoint > management > SignatureFactory


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.joinpoint.management;
5
6 import java.lang.reflect.Constructor JavaDoc;
7 import java.lang.reflect.Field JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
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 /**
23  * Factory class for the signature hierarchy.
24  * The helper methods here are called by the JIT jp.
25  * <p/>
26  * TODO may be worth having a cache
27  *
28  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
29  * @author <a HREF="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
30  */

31 public final class SignatureFactory {
32
33   /**
34    * Method signature factory
35    *
36    * @param declaringClass
37    * @param joinPointHash
38    * @return
39    */

40   public static final MethodSignatureImpl newMethodSignature(final Class JavaDoc declaringClass, final int joinPointHash) {
41     AsmClassInfoRepository.getRepository(declaringClass.getClassLoader()).removeClassInfo(declaringClass.getName().replace('.', '/'));
42     Method JavaDoc[] methods = declaringClass.getDeclaredMethods();
43     for (int i = 0; i < methods.length; i++) {
44       Method JavaDoc method = methods[i];
45       if (ReflectHelper.calculateHash(method) == joinPointHash) {
46         return new MethodSignatureImpl(declaringClass, method);
47       }
48     }
49     // lookup in the hierarchy
50
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   /**
66    * Field signature factory
67    *
68    * @param declaringClass
69    * @param joinPointHash
70    * @return
71    */

72   public static final FieldSignatureImpl newFieldSignature(final Class JavaDoc declaringClass, final int joinPointHash) {
73     Field JavaDoc[] fields = declaringClass.getDeclaredFields();
74     for (int i = 0; i < fields.length; i++) {
75       Field JavaDoc field = fields[i];
76       if (ReflectHelper.calculateHash(field) == joinPointHash) {
77         return new FieldSignatureImpl(declaringClass, field);
78       }
79     }
80     // lookup in the hierarchy
81
if (declaringClass.getSuperclass() != null) {
82       return newFieldSignature(declaringClass.getSuperclass(), joinPointHash);
83     } else {
84       return null;
85     }
86   }
87
88   /**
89    * Constructor signature factory
90    *
91    * @param declaringClass
92    * @param joinPointHash
93    * @return
94    */

95   public static final ConstructorSignatureImpl newConstructorSignature(final Class JavaDoc declaringClass,
96                                                                        final int joinPointHash) {
97     for (int i = 0; i < declaringClass.getDeclaredConstructors().length; i++) {
98       Constructor JavaDoc c = declaringClass.getDeclaredConstructors()[i];
99       if (ReflectHelper.calculateHash(c) == joinPointHash) {
100         return new ConstructorSignatureImpl(declaringClass, c);
101       }
102     }
103     // lookup in the hierarchy
104
if (declaringClass.getSuperclass() != null) {
105       return newConstructorSignature(declaringClass.getSuperclass(), joinPointHash);
106     } else {
107       return null;
108     }
109   }
110
111   /**
112    * Handler signature factory
113    *
114    * @param exceptionClass
115    * @return
116    */

117   public static final CatchClauseSignatureImpl newCatchClauseSignature(final Class JavaDoc exceptionClass) {
118     return new CatchClauseSignatureImpl(exceptionClass);
119   }
120
121   /**
122    * Enclosing signature factory, wrapped behind an EnclosingStaticJoinPoint for syntax consistency
123    *
124    * @param declaringClass
125    * @param name
126    * @param description
127    * @return
128    */

129   public static EnclosingStaticJoinPoint newEnclosingStaticJoinPoint(
130           final Class JavaDoc declaringClass,
131           final String JavaDoc name,
132           final String JavaDoc 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       // regular method
145
return new EnclosingStaticJoinPointImpl(
146               newMethodSignature(declaringClass, AsmHelper.calculateMethodHash(name, description)),
147               JoinPointType.METHOD_EXECUTION
148       );
149     }
150   }
151
152   /**
153    * Static initialization factory
154    *
155    * @param declaringClass
156    * @return
157    */

158   public static StaticInitializerSignatureImpl newStaticInitializationSignature(final Class JavaDoc declaringClass) {
159     return new StaticInitializerSignatureImpl(declaringClass);
160   }
161
162 }
Popular Tags