KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > reflect > impl > asm > AsmConstructorInfo


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.reflect.impl.asm;
5
6 import com.tc.backport175.bytecode.AnnotationElement;
7 import com.tc.asm.Type;
8
9 import com.tc.aspectwerkz.transform.inlining.AsmHelper;
10 import com.tc.aspectwerkz.reflect.ClassInfo;
11 import com.tc.aspectwerkz.reflect.ConstructorInfo;
12
13 /**
14  * ASM implementation of the ConstructorInfo interface.
15  *
16  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17  */

18 public class AsmConstructorInfo extends AsmMemberInfo implements ConstructorInfo {
19
20     /**
21      * A list with the parameter type names.
22      */

23     private String JavaDoc[] m_parameterTypeNames = null;
24
25     /**
26      * A list with the exception type names.
27      */

28     private String JavaDoc[] m_exceptionTypeNames = null;
29
30     /**
31      * A list with the parameter types.
32      */

33     private ClassInfo[] m_parameterTypes = null;
34
35     /**
36      * A list with the exception types.
37      */

38     private ClassInfo[] m_exceptionTypes = null;
39
40     /**
41      * Creates a new method meta data instance.
42      *
43      * @param method
44      * @param declaringType
45      * @param loader
46      */

47     AsmConstructorInfo(final MethodStruct method, final String JavaDoc declaringType, final ClassLoader JavaDoc loader) {
48         super(method, declaringType, loader);
49         Type[] argTypes = Type.getArgumentTypes(method.desc);
50         m_parameterTypeNames = new String JavaDoc[argTypes.length];
51         for (int i = 0; i < argTypes.length; i++) {
52             m_parameterTypeNames[i] = argTypes[i].getClassName();
53         }
54         // TODO how to do exceptions?
55
m_exceptionTypeNames = new String JavaDoc[]{};
56     }
57
58     /**
59      * Returns the constructor info for the constructor specified.
60      *
61      * @param constructorDesc
62      * @param bytecode
63      * @param loader
64      * @return the constructor info
65      */

66     public static ConstructorInfo getConstructorInfo(final String JavaDoc constructorDesc,
67                                                      final byte[] bytecode,
68                                                      final ClassLoader JavaDoc loader) {
69         String JavaDoc className = AsmClassInfo.retrieveClassNameFromBytecode(bytecode);
70         AsmClassInfoRepository repository = AsmClassInfoRepository.getRepository(loader);
71         ClassInfo classInfo = repository.getClassInfo(className);
72         if (classInfo == null) {
73             classInfo = AsmClassInfo.getClassInfo(bytecode, loader);
74         }
75         return classInfo.getConstructor(AsmHelper.calculateConstructorHash(constructorDesc));
76     }
77
78     /**
79      * Returns the signature for the element.
80      *
81      * @return the signature for the element
82      */

83     public String JavaDoc getSignature() {
84         return AsmHelper.getConstructorDescriptor(this);
85     }
86     
87     public String JavaDoc getGenericsSignature() {
88       return m_member.signature;
89     }
90
91     /**
92      * Returns the parameter types.
93      *
94      * @return the parameter types
95      */

96     public synchronized ClassInfo[] getParameterTypes() {
97         if (m_parameterTypes == null) {
98             m_parameterTypes = new ClassInfo[m_parameterTypeNames.length];
99             for (int i = 0; i < m_parameterTypeNames.length; i++) {
100                 m_parameterTypes[i] = AsmClassInfo.getClassInfo(
101                         m_parameterTypeNames[i],
102                         (ClassLoader JavaDoc) m_loaderRef.get()
103                 );
104             }
105         }
106         return m_parameterTypes;
107     }
108
109     /**
110      * Returns the exception types.
111      *
112      * @return the exception types
113      */

114     public synchronized ClassInfo[] getExceptionTypes() {
115         if (m_exceptionTypes == null) {
116             m_exceptionTypes = new ClassInfo[m_exceptionTypeNames.length];
117             for (int i = 0; i < m_exceptionTypeNames.length; i++) {
118                 m_exceptionTypes[i] = AsmClassInfo.getClassInfo(
119                         m_exceptionTypeNames[i],
120                         (ClassLoader JavaDoc) m_loaderRef.get()
121                 );
122             }
123         }
124         return m_exceptionTypes;
125     }
126
127     /**
128      * Returns the annotations.
129      *
130      * @return the annotations
131      */

132     public AnnotationElement.Annotation[] getAnnotations() {
133         return getDeclaringType().getAnnotationReader().getConstructorAnnotationElements(m_member.desc);
134     }
135
136     public boolean equals(Object JavaDoc o) {
137         if (this == o) {
138             return true;
139         }
140         if (!(o instanceof ConstructorInfo)) {
141             return false;
142         }
143         ConstructorInfo constructorInfo = (ConstructorInfo) o;
144         if (!m_declaringTypeName.equals(constructorInfo.getDeclaringType().getName())) {
145             return false;
146         }
147         if (!m_member.name.equals(constructorInfo.getName())) {
148             return false;
149         }
150         ClassInfo[] parameterTypes = constructorInfo.getParameterTypes();
151         if (m_parameterTypeNames.length != parameterTypes.length) {//check on names length for lazyness optim
152
return false;
153         }
154         for (int i = 0; i < m_parameterTypeNames.length; i++) {
155             if (!m_parameterTypeNames[i].equals(parameterTypes[i].getName())) {
156                 return false;
157             }
158         }
159         return true;
160     }
161
162     public int hashCode() {
163         int result = 29;
164         result = (29 * result) + m_declaringTypeName.hashCode();
165         result = (29 * result) + m_member.name.hashCode();
166         for (int i = 0; i < m_parameterTypeNames.length; i++) {
167             result = (29 * result) + m_parameterTypeNames[i].hashCode();
168         }
169         return result;
170     }
171
172     public String JavaDoc toString() {
173         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(m_declaringTypeName);
174         sb.append('.').append(m_member.name);
175         sb.append(m_member.desc);
176         return sb.toString();
177     }
178 }
179
Popular Tags