KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > reflect > impl > java > JavaConstructorInfo


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.java;
5
6 import com.tc.aspectwerkz.reflect.ClassInfo;
7 import com.tc.aspectwerkz.reflect.ConstructorInfo;
8 import com.tc.aspectwerkz.reflect.ReflectHelper;
9 import com.tc.backport175.bytecode.AnnotationElement;
10
11 import java.lang.reflect.Constructor JavaDoc;
12
13 /**
14  * Implementation of the ConstructorInfo interface for java.lang.reflect.*.
15  *
16  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17  */

18 public class JavaConstructorInfo extends JavaMemberInfo implements ConstructorInfo {
19   /**
20    * A list with the parameter types.
21    */

22   private ClassInfo[] m_parameterTypes = null;
23
24   /**
25    * A list with the exception types.
26    */

27   private ClassInfo[] m_exceptionTypes = null;
28
29   /**
30    * The signature of the class.
31    */

32   private String JavaDoc m_signature;
33
34   /**
35    * Creates a new method meta data instance.
36    *
37    * @param constructor
38    * @param declaringType
39    */

40   JavaConstructorInfo(final Constructor JavaDoc constructor, final JavaClassInfo declaringType) {
41     super(constructor, declaringType);
42     m_signature = ReflectHelper.getConstructorSignature(constructor);
43   }
44
45   /**
46    * Returns the constructor info for the constructor specified.
47    *
48    * @param constructor the constructor
49    * @return the constructor info
50    */

51   public static ConstructorInfo getConstructorInfo(final Constructor JavaDoc constructor) {
52     Class JavaDoc declaringClass = constructor.getDeclaringClass();
53     JavaClassInfoRepository repository = JavaClassInfoRepository.getRepository(declaringClass.getClassLoader());
54     ClassInfo classInfo = repository.getClassInfo(declaringClass.getName());
55     if (classInfo == null) {
56       classInfo = JavaClassInfo.getClassInfo(declaringClass);
57     }
58     return classInfo.getConstructor(ReflectHelper.calculateHash(constructor));
59   }
60
61   /**
62    * Returns the signature for the element.
63    *
64    * @return the signature for the element
65    */

66   public String JavaDoc getSignature() {
67     return m_signature;
68   }
69   
70   public String JavaDoc getGenericsSignature() {
71     // XXX implement
72
throw new RuntimeException JavaDoc();
73   }
74
75   /**
76    * Returns the attributes.
77    *
78    * @return the attributes
79    */

80   public AnnotationElement.Annotation[] getAnnotations() {
81     return getDeclaringType().getAnnotationReader().getConstructorAnnotationElements(m_signature);
82   }
83
84   /**
85    * Returns the parameter types.
86    *
87    * @return the parameter types
88    */

89   public ClassInfo[] getParameterTypes() {
90     if (m_parameterTypes == null) {
91       Class JavaDoc[] parameterTypes = ((Constructor JavaDoc) m_member).getParameterTypes();
92       m_parameterTypes = new ClassInfo[parameterTypes.length];
93       for (int i = 0; i < parameterTypes.length; i++) {
94         Class JavaDoc parameterType = parameterTypes[i];
95         ClassInfo metaData;
96         if (m_classInfoRepository.hasClassInfo(parameterType.getName())) {
97           metaData = m_classInfoRepository.getClassInfo(parameterType.getName());
98         } else {
99           metaData = JavaClassInfo.getClassInfo(parameterType);
100           m_classInfoRepository.addClassInfo(metaData);
101         }
102         m_parameterTypes[i] = metaData;
103       }
104     }
105     return m_parameterTypes;
106   }
107
108   /**
109    * Returns the exception types.
110    *
111    * @return the exception types
112    */

113   public ClassInfo[] getExceptionTypes() {
114     if (m_exceptionTypes == null) {
115       Class JavaDoc[] exceptionTypes = ((Constructor JavaDoc) m_member).getExceptionTypes();
116       m_exceptionTypes = new ClassInfo[exceptionTypes.length];
117       for (int i = 0; i < exceptionTypes.length; i++) {
118         Class JavaDoc exceptionType = exceptionTypes[i];
119         ClassInfo metaData;
120         if (m_classInfoRepository.hasClassInfo(exceptionType.getName())) {
121           metaData = m_classInfoRepository.getClassInfo(exceptionType.getName());
122         } else {
123           metaData = JavaClassInfo.getClassInfo(exceptionType);
124           m_classInfoRepository.addClassInfo(metaData);
125         }
126         m_exceptionTypes[i] = metaData;
127       }
128     }
129     return m_exceptionTypes;
130   }
131
132   public boolean equals(Object JavaDoc o) {
133     if (this == o) {
134       return true;
135     }
136     if (!(o instanceof ConstructorInfo)) {
137       return false;
138     }
139     ConstructorInfo constructorInfo = (ConstructorInfo) o;
140     if (!m_declaringType.getName().equals(constructorInfo.getDeclaringType().getName())) {
141       return false;
142     }
143     if (!m_member.getName().equals(constructorInfo.getName())) {
144       return false;
145     }
146     Class JavaDoc[] parameterTypes1 = ((Constructor JavaDoc) m_member).getParameterTypes();
147     ClassInfo[] parameterTypes2 = constructorInfo.getParameterTypes();
148     if (parameterTypes1.length != parameterTypes2.length) {
149       return false;
150     }
151     for (int i = 0; i < parameterTypes1.length; i++) {
152       if (!parameterTypes1[i].getName().equals(parameterTypes2[i].getName())) {
153         return false;
154       }
155     }
156     return true;
157   }
158
159   public int hashCode() {
160     int result = 29;
161     result = (29 * result) + m_declaringType.getName().hashCode();
162     result = (29 * result) + m_member.getName().hashCode();
163     Class JavaDoc[] parameterTypes = ((Constructor JavaDoc) m_member).getParameterTypes();
164     for (int i = 0; i < parameterTypes.length; i++) {
165       result = (29 * result) + parameterTypes[i].getName().hashCode();
166     }
167     return result;
168   }
169 }
Popular Tags