View Javadoc

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.reflect.impl.java;
9   
10  import org.codehaus.aspectwerkz.annotation.Annotations;
11  import org.codehaus.aspectwerkz.reflect.ClassInfo;
12  import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
13  import org.codehaus.aspectwerkz.transform.ReflectHelper;
14  
15  import java.lang.reflect.Constructor;
16  import java.util.List;
17  
18  /***
19   * Implementation of the ConstructorInfo interface for java.lang.reflect.*.
20   * 
21   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
22   */
23  public class JavaConstructorInfo extends JavaMemberInfo implements ConstructorInfo {
24      /***
25       * A list with the parameter types.
26       */
27      private ClassInfo[] m_parameterTypes = null;
28  
29      /***
30       * A list with the exception types.
31       */
32      private ClassInfo[] m_exceptionTypes = null;
33  
34      /***
35       * Creates a new method meta data instance.
36       * 
37       * @param constructor
38       * @param declaringType
39       */
40      JavaConstructorInfo(final Constructor constructor, final JavaClassInfo declaringType) {
41          super(constructor, declaringType);
42      }
43  
44      /***
45       * Returns the constructor info for the constructor specified.
46       * 
47       * @param constructor the constructor
48       * @return the constructor info
49       */
50      public static ConstructorInfo getConstructorInfo(final Constructor constructor) {
51          Class declaringClass = constructor.getDeclaringClass();
52          JavaClassInfoRepository repository = JavaClassInfoRepository.getRepository(declaringClass.getClassLoader());
53          ClassInfo classInfo = repository.getClassInfo(declaringClass.getName());
54          if (classInfo == null) {
55              classInfo = JavaClassInfo.getClassInfo(declaringClass);
56          }
57          return classInfo.getConstructor(ReflectHelper.calculateHash(constructor));
58      }
59  
60      /***
61       * Returns the attributes.
62       * 
63       * @return the attributes
64       * @TODO: fix constructor annotations
65       */
66      public List getAnnotations() {
67          if (m_annotations == null) {
68              m_annotations = Annotations.getAnnotationInfos((Constructor) m_member);
69          }
70          return m_annotations;
71      }
72  
73      /***
74       * Returns the parameter types.
75       * 
76       * @return the parameter types
77       */
78      public ClassInfo[] getParameterTypes() {
79          if (m_parameterTypes == null) {
80              Class[] parameterTypes = ((Constructor) m_member).getParameterTypes();
81              m_parameterTypes = new ClassInfo[parameterTypes.length];
82              for (int i = 0; i < parameterTypes.length; i++) {
83                  Class parameterType = parameterTypes[i];
84                  ClassInfo metaData;
85                  if (m_classInfoRepository.hasClassInfo(parameterType.getName())) {
86                      metaData = m_classInfoRepository.getClassInfo(parameterType.getName());
87                  } else {
88                      metaData = JavaClassInfo.getClassInfo(parameterType);
89                      m_classInfoRepository.addClassInfo(metaData);
90                  }
91                  m_parameterTypes[i] = metaData;
92              }
93          }
94          return m_parameterTypes;
95      }
96  
97      /***
98       * Returns the exception types.
99       * 
100      * @return the exception types
101      */
102     public ClassInfo[] getExceptionTypes() {
103         if (m_exceptionTypes == null) {
104             Class[] exceptionTypes = ((Constructor) m_member).getExceptionTypes();
105             m_exceptionTypes = new ClassInfo[exceptionTypes.length];
106             for (int i = 0; i < exceptionTypes.length; i++) {
107                 Class exceptionType = exceptionTypes[i];
108                 ClassInfo metaData;
109                 if (m_classInfoRepository.hasClassInfo(exceptionType.getName())) {
110                     metaData = m_classInfoRepository.getClassInfo(exceptionType.getName());
111                 } else {
112                     metaData = JavaClassInfo.getClassInfo(exceptionType);
113                     m_classInfoRepository.addClassInfo(metaData);
114                 }
115                 m_exceptionTypes[i] = metaData;
116             }
117         }
118         return m_exceptionTypes;
119     }
120 
121     public boolean equals(Object o) {
122         if (this == o) {
123             return true;
124         }
125         if (!(o instanceof ConstructorInfo)) {
126             return false;
127         }
128         ConstructorInfo constructorInfo = (ConstructorInfo) o;
129         if (!m_declaringType.getName().equals(constructorInfo.getDeclaringType().getName())) {
130             return false;
131         }
132         if (!m_member.getName().equals(constructorInfo.getName())) {
133             return false;
134         }
135         Class[] parameterTypes1 = ((Constructor) m_member).getParameterTypes();
136         ClassInfo[] parameterTypes2 = constructorInfo.getParameterTypes();
137         if (parameterTypes1.length != parameterTypes2.length) {
138             return false;
139         }
140         for (int i = 0; i < parameterTypes1.length; i++) {
141             if (!parameterTypes1[i].getName().equals(parameterTypes2[i].getName())) {
142                 return false;
143             }
144         }
145         return true;
146     }
147 
148     public int hashCode() {
149         int result = 29;
150         result = (29 * result) + m_declaringType.getName().hashCode();
151         result = (29 * result) + m_member.getName().hashCode();
152         Class[] parameterTypes = ((Constructor) m_member).getParameterTypes();
153         for (int i = 0; i < parameterTypes.length; i++) {
154             result = (29 * result) + parameterTypes[i].getName().hashCode();
155         }
156         return result;
157     }
158 }