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