KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > joinpoint > management > SignatureFactory


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.joinpoint.management;
9
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.lang.reflect.Field JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
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 /**
26  * Factory class for the signature hierarchy.
27  * The helper methods here are called by the JIT jp.
28  *
29  * TODO may be worth having a cache
30  *
31  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér </a>
32  * @author <a HREF="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
33  */

34 public final class SignatureFactory {
35
36     /**
37      * Method signature factory
38      *
39      * @param declaringClass
40      * @param joinPointHash
41      * @return
42      */

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

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

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

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

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

161     public static StaticInitializerSignatureImpl newStaticInitializationSignature(final Class JavaDoc declaringClass) {
162         return new StaticInitializerSignatureImpl(declaringClass);
163     }
164     
165 }
Popular Tags