KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 public class JavaMethodInfo extends JavaMemberInfo implements MethodInfo {
19
20   /**
21    * The return type.
22    */

23   private ClassInfo m_returnType = null;
24
25   /**
26    * A list with the parameter types.
27    */

28   private ClassInfo[] m_parameterTypes = null;
29
30   /**
31    * A list with the exception types.
32    */

33   private ClassInfo[] m_exceptionTypes = null;
34
35   /**
36    * The signature of the method.
37    */

38   private String JavaDoc m_signature;
39
40   /**
41    * Creates a new method meta data instance.
42    *
43    * @param method
44    * @param declaringType
45    */

46   JavaMethodInfo(final Method JavaDoc method, final JavaClassInfo declaringType) {
47     super(method, declaringType);
48     m_signature = ReflectHelper.getMethodSignature(method);
49   }
50
51   /**
52    * Returns the method info for the method specified.
53    *
54    * @param method the method
55    * @return the method info
56    */

57   public static MethodInfo getMethodInfo(final Method JavaDoc method) {
58     Class JavaDoc declaringClass = method.getDeclaringClass();
59     JavaClassInfoRepository repository = JavaClassInfoRepository.getRepository(declaringClass.getClassLoader());
60     ClassInfo classInfo = repository.getClassInfo(declaringClass.getName());
61     if (classInfo == null) {
62       classInfo = JavaClassInfo.getClassInfo(declaringClass);
63     }
64     return classInfo.getMethod(ReflectHelper.calculateHash(method));
65   }
66
67   /**
68    * Returns the signature for the element.
69    *
70    * @return the signature for the element
71    */

72   public String JavaDoc getSignature() {
73     return m_signature;
74   }
75   
76   public String JavaDoc getGenericsSignature() {
77     // XXX implement
78
throw new RuntimeException JavaDoc();
79   }
80
81   /**
82    * Returns the annotations.
83    *
84    * @return the annotations
85    */

86   public AnnotationElement.Annotation[] getAnnotations() {
87     return getDeclaringType().getAnnotationReader().getMethodAnnotationElements(m_member.getName(), m_signature);
88   }
89
90   /**
91    * Returns the return type.
92    *
93    * @return the return type
94    */

95   public ClassInfo getReturnType() {
96     if (m_returnType == null) {
97       Class JavaDoc returnTypeClass = ((Method JavaDoc) m_member).getReturnType();
98       if (m_classInfoRepository.hasClassInfo(returnTypeClass.getName())) {
99         m_returnType = m_classInfoRepository.getClassInfo(returnTypeClass.getName());
100       } else {
101         m_returnType = JavaClassInfo.getClassInfo(returnTypeClass);
102         m_classInfoRepository.addClassInfo(m_returnType);
103       }
104     }
105     return m_returnType;
106   }
107
108   /**
109    * Returns the parameter types.
110    *
111    * @return the parameter types
112    */

113   public ClassInfo[] getParameterTypes() {
114     if (m_parameterTypes == null) {
115       Class JavaDoc[] parameterTypes = ((Method JavaDoc) m_member).getParameterTypes();
116       m_parameterTypes = new ClassInfo[parameterTypes.length];
117       for (int i = 0; i < parameterTypes.length; i++) {
118         Class JavaDoc parameterType = parameterTypes[i];
119         ClassInfo metaData;
120         if (m_classInfoRepository.hasClassInfo(parameterType.getName())) {
121           metaData = m_classInfoRepository.getClassInfo(parameterType.getName());
122         } else {
123           metaData = JavaClassInfo.getClassInfo(parameterType);
124           m_classInfoRepository.addClassInfo(metaData);
125         }
126         m_parameterTypes[i] = metaData;
127       }
128     }
129     return m_parameterTypes;
130   }
131
132   /**
133    * Returns the parameter names as they appear in the source code.
134    * <p/>
135    * This information is not available from Reflect.
136    * We may use ASM to grab it - is that needed ?
137    *
138    * @return null / not supported for now.
139    */

140   public String JavaDoc[] getParameterNames() {
141     return null;
142   }
143
144   /**
145    * Returns the exception types.
146    *
147    * @return the exception types
148    */

149   public ClassInfo[] getExceptionTypes() {
150     if (m_exceptionTypes == null) {
151       Class JavaDoc[] exceptionTypes = ((Method JavaDoc) m_member).getExceptionTypes();
152       m_exceptionTypes = new ClassInfo[exceptionTypes.length];
153       for (int i = 0; i < exceptionTypes.length; i++) {
154         Class JavaDoc exceptionType = exceptionTypes[i];
155         ClassInfo metaData;
156         if (m_classInfoRepository.hasClassInfo(exceptionType.getName())) {
157           metaData = m_classInfoRepository.getClassInfo(exceptionType.getName());
158         } else {
159           metaData = JavaClassInfo.getClassInfo(exceptionType);
160           m_classInfoRepository.addClassInfo(metaData);
161         }
162         m_exceptionTypes[i] = metaData;
163       }
164     }
165     return m_exceptionTypes;
166   }
167
168   public boolean equals(Object JavaDoc o) {
169     if (this == o) {
170       return true;
171     }
172     if (!(o instanceof MethodInfo)) {
173       return false;
174     }
175     MethodInfo methodInfo = (MethodInfo) o;
176     if (!m_declaringType.getName().equals(methodInfo.getDeclaringType().getName())) {
177       return false;
178     }
179     if (!m_member.getName().equals(methodInfo.getName())) {
180       return false;
181     }
182     Class JavaDoc[] parameterTypes1 = ((Method JavaDoc) m_member).getParameterTypes();
183     ClassInfo[] parameterTypes2 = methodInfo.getParameterTypes();
184     if (parameterTypes1.length != parameterTypes2.length) {
185       return false;
186     }
187     for (int i = 0; i < parameterTypes1.length; i++) {
188       if (!parameterTypes1[i].getName().equals(parameterTypes2[i].getName())) {
189         return false;
190       }
191     }
192     return true;
193   }
194
195   public int hashCode() {
196     int result = 29;
197     result = (29 * result) + m_declaringType.getName().hashCode();
198     result = (29 * result) + m_member.getName().hashCode();
199     Class JavaDoc[] parameterTypes = ((Method JavaDoc) m_member).getParameterTypes();
200     for (int i = 0; i < parameterTypes.length; i++) {
201       result = (29 * result) + parameterTypes[i].getName().hashCode();
202     }
203     return result;
204   }
205
206   public String JavaDoc toString() {
207     return m_member.toString();
208   }
209 }
Popular Tags