001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.reflect.impl.java;
005:
006: import com.tc.aspectwerkz.reflect.ClassInfo;
007: import com.tc.aspectwerkz.reflect.ConstructorInfo;
008: import com.tc.aspectwerkz.reflect.ReflectHelper;
009: import com.tc.backport175.bytecode.AnnotationElement;
010:
011: import java.lang.reflect.Constructor;
012:
013: /**
014: * Implementation of the ConstructorInfo interface for java.lang.reflect.*.
015: *
016: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
017: */
018: public class JavaConstructorInfo extends JavaMemberInfo implements
019: ConstructorInfo {
020: /**
021: * A list with the parameter types.
022: */
023: private ClassInfo[] m_parameterTypes = null;
024:
025: /**
026: * A list with the exception types.
027: */
028: private ClassInfo[] m_exceptionTypes = null;
029:
030: /**
031: * The signature of the class.
032: */
033: private String m_signature;
034:
035: /**
036: * Creates a new method meta data instance.
037: *
038: * @param constructor
039: * @param declaringType
040: */
041: JavaConstructorInfo(final Constructor constructor,
042: final JavaClassInfo declaringType) {
043: super (constructor, declaringType);
044: m_signature = ReflectHelper
045: .getConstructorSignature(constructor);
046: }
047:
048: /**
049: * Returns the constructor info for the constructor specified.
050: *
051: * @param constructor the constructor
052: * @return the constructor info
053: */
054: public static ConstructorInfo getConstructorInfo(
055: final Constructor constructor) {
056: Class declaringClass = constructor.getDeclaringClass();
057: JavaClassInfoRepository repository = JavaClassInfoRepository
058: .getRepository(declaringClass.getClassLoader());
059: ClassInfo classInfo = repository.getClassInfo(declaringClass
060: .getName());
061: if (classInfo == null) {
062: classInfo = JavaClassInfo.getClassInfo(declaringClass);
063: }
064: return classInfo.getConstructor(ReflectHelper
065: .calculateHash(constructor));
066: }
067:
068: /**
069: * Returns the signature for the element.
070: *
071: * @return the signature for the element
072: */
073: public String getSignature() {
074: return m_signature;
075: }
076:
077: public String getGenericsSignature() {
078: // XXX implement
079: throw new RuntimeException();
080: }
081:
082: /**
083: * Returns the attributes.
084: *
085: * @return the attributes
086: */
087: public AnnotationElement.Annotation[] getAnnotations() {
088: return getDeclaringType().getAnnotationReader()
089: .getConstructorAnnotationElements(m_signature);
090: }
091:
092: /**
093: * Returns the parameter types.
094: *
095: * @return the parameter types
096: */
097: public ClassInfo[] getParameterTypes() {
098: if (m_parameterTypes == null) {
099: Class[] parameterTypes = ((Constructor) m_member)
100: .getParameterTypes();
101: m_parameterTypes = new ClassInfo[parameterTypes.length];
102: for (int i = 0; i < parameterTypes.length; i++) {
103: Class parameterType = parameterTypes[i];
104: ClassInfo metaData;
105: if (m_classInfoRepository.hasClassInfo(parameterType
106: .getName())) {
107: metaData = m_classInfoRepository
108: .getClassInfo(parameterType.getName());
109: } else {
110: metaData = JavaClassInfo
111: .getClassInfo(parameterType);
112: m_classInfoRepository.addClassInfo(metaData);
113: }
114: m_parameterTypes[i] = metaData;
115: }
116: }
117: return m_parameterTypes;
118: }
119:
120: /**
121: * Returns the exception types.
122: *
123: * @return the exception types
124: */
125: public ClassInfo[] getExceptionTypes() {
126: if (m_exceptionTypes == null) {
127: Class[] exceptionTypes = ((Constructor) m_member)
128: .getExceptionTypes();
129: m_exceptionTypes = new ClassInfo[exceptionTypes.length];
130: for (int i = 0; i < exceptionTypes.length; i++) {
131: Class exceptionType = exceptionTypes[i];
132: ClassInfo metaData;
133: if (m_classInfoRepository.hasClassInfo(exceptionType
134: .getName())) {
135: metaData = m_classInfoRepository
136: .getClassInfo(exceptionType.getName());
137: } else {
138: metaData = JavaClassInfo
139: .getClassInfo(exceptionType);
140: m_classInfoRepository.addClassInfo(metaData);
141: }
142: m_exceptionTypes[i] = metaData;
143: }
144: }
145: return m_exceptionTypes;
146: }
147:
148: public boolean equals(Object o) {
149: if (this == o) {
150: return true;
151: }
152: if (!(o instanceof ConstructorInfo)) {
153: return false;
154: }
155: ConstructorInfo constructorInfo = (ConstructorInfo) o;
156: if (!m_declaringType.getName().equals(
157: constructorInfo.getDeclaringType().getName())) {
158: return false;
159: }
160: if (!m_member.getName().equals(constructorInfo.getName())) {
161: return false;
162: }
163: Class[] parameterTypes1 = ((Constructor) m_member)
164: .getParameterTypes();
165: ClassInfo[] parameterTypes2 = constructorInfo
166: .getParameterTypes();
167: if (parameterTypes1.length != parameterTypes2.length) {
168: return false;
169: }
170: for (int i = 0; i < parameterTypes1.length; i++) {
171: if (!parameterTypes1[i].getName().equals(
172: parameterTypes2[i].getName())) {
173: return false;
174: }
175: }
176: return true;
177: }
178:
179: public int hashCode() {
180: int result = 29;
181: result = (29 * result) + m_declaringType.getName().hashCode();
182: result = (29 * result) + m_member.getName().hashCode();
183: Class[] parameterTypes = ((Constructor) m_member)
184: .getParameterTypes();
185: for (int i = 0; i < parameterTypes.length; i++) {
186: result = (29 * result)
187: + parameterTypes[i].getName().hashCode();
188: }
189: return result;
190: }
191: }
|