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