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.MethodInfo;
012: import org.codehaus.aspectwerkz.reflect.ReflectHelper;
013:
014: import java.lang.reflect.Method;
015: import java.util.List;
016:
017: /**
018: * Implementation of the MethodInfo interface for java.lang.reflect.*.
019: *
020: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
021: */
022: public class JavaMethodInfo extends JavaMemberInfo implements
023: MethodInfo {
024:
025: /**
026: * The return type.
027: */
028: private ClassInfo m_returnType = null;
029:
030: /**
031: * A list with the parameter types.
032: */
033: private ClassInfo[] m_parameterTypes = null;
034:
035: /**
036: * A list with the exception types.
037: */
038: private ClassInfo[] m_exceptionTypes = null;
039:
040: /**
041: * The signature of the method.
042: */
043: private String m_signature;
044:
045: /**
046: * Creates a new method meta data instance.
047: *
048: * @param method
049: * @param declaringType
050: */
051: JavaMethodInfo(final Method method,
052: final JavaClassInfo declaringType) {
053: super (method, declaringType);
054: m_signature = ReflectHelper.getMethodSignature(method);
055: }
056:
057: /**
058: * Returns the method info for the method specified.
059: *
060: * @param method the method
061: * @return the method info
062: */
063: public static MethodInfo getMethodInfo(final Method method) {
064: Class declaringClass = method.getDeclaringClass();
065: JavaClassInfoRepository repository = JavaClassInfoRepository
066: .getRepository(declaringClass.getClassLoader());
067: ClassInfo classInfo = repository.getClassInfo(declaringClass
068: .getName());
069: if (classInfo == null) {
070: classInfo = JavaClassInfo.getClassInfo(declaringClass);
071: }
072: return classInfo.getMethod(ReflectHelper.calculateHash(method));
073: }
074:
075: /**
076: * Returns the signature for the element.
077: *
078: * @return the signature for the element
079: */
080: public String getSignature() {
081: return m_signature;
082: }
083:
084: /**
085: * Returns the annotations.
086: *
087: * @return the annotations
088: */
089: public List getAnnotations() {
090: if (m_annotations == null) {
091: m_annotations = Annotations
092: .getAnnotationInfos((Method) m_member);
093: }
094: return m_annotations;
095: }
096:
097: /**
098: * Returns the return type.
099: *
100: * @return the return type
101: */
102: public ClassInfo getReturnType() {
103: if (m_returnType == null) {
104: Class returnTypeClass = ((Method) m_member).getReturnType();
105: if (m_classInfoRepository.hasClassInfo(returnTypeClass
106: .getName())) {
107: m_returnType = m_classInfoRepository
108: .getClassInfo(returnTypeClass.getName());
109: } else {
110: m_returnType = JavaClassInfo
111: .getClassInfo(returnTypeClass);
112: m_classInfoRepository.addClassInfo(m_returnType);
113: }
114: }
115: return m_returnType;
116: }
117:
118: /**
119: * Returns the parameter types.
120: *
121: * @return the parameter types
122: */
123: public ClassInfo[] getParameterTypes() {
124: if (m_parameterTypes == null) {
125: Class[] parameterTypes = ((Method) m_member)
126: .getParameterTypes();
127: m_parameterTypes = new ClassInfo[parameterTypes.length];
128: for (int i = 0; i < parameterTypes.length; i++) {
129: Class parameterType = parameterTypes[i];
130: ClassInfo metaData;
131: if (m_classInfoRepository.hasClassInfo(parameterType
132: .getName())) {
133: metaData = m_classInfoRepository
134: .getClassInfo(parameterType.getName());
135: } else {
136: metaData = JavaClassInfo
137: .getClassInfo(parameterType);
138: m_classInfoRepository.addClassInfo(metaData);
139: }
140: m_parameterTypes[i] = metaData;
141: }
142: }
143: return m_parameterTypes;
144: }
145:
146: /**
147: * Returns the parameter names as they appear in the source code.
148: * <p/>
149: * This information is not available from Reflect.
150: * We may use ASM to grab it - is that needed ?
151: *
152: * @return null / not supported for now.
153: */
154: public String[] getParameterNames() {
155: return null;
156: }
157:
158: /**
159: * Returns the exception types.
160: *
161: * @return the exception types
162: */
163: public ClassInfo[] getExceptionTypes() {
164: if (m_exceptionTypes == null) {
165: Class[] exceptionTypes = ((Method) m_member)
166: .getExceptionTypes();
167: m_exceptionTypes = new ClassInfo[exceptionTypes.length];
168: for (int i = 0; i < exceptionTypes.length; i++) {
169: Class exceptionType = exceptionTypes[i];
170: ClassInfo metaData;
171: if (m_classInfoRepository.hasClassInfo(exceptionType
172: .getName())) {
173: metaData = m_classInfoRepository
174: .getClassInfo(exceptionType.getName());
175: } else {
176: metaData = JavaClassInfo
177: .getClassInfo(exceptionType);
178: m_classInfoRepository.addClassInfo(metaData);
179: }
180: m_exceptionTypes[i] = metaData;
181: }
182: }
183: return m_exceptionTypes;
184: }
185:
186: public boolean equals(Object o) {
187: if (this == o) {
188: return true;
189: }
190: if (!(o instanceof MethodInfo)) {
191: return false;
192: }
193: MethodInfo methodInfo = (MethodInfo) o;
194: if (!m_declaringType.getName().equals(
195: methodInfo.getDeclaringType().getName())) {
196: return false;
197: }
198: if (!m_member.getName().equals(methodInfo.getName())) {
199: return false;
200: }
201: Class[] parameterTypes1 = ((Method) m_member)
202: .getParameterTypes();
203: ClassInfo[] parameterTypes2 = methodInfo.getParameterTypes();
204: if (parameterTypes1.length != parameterTypes2.length) {
205: return false;
206: }
207: for (int i = 0; i < parameterTypes1.length; i++) {
208: if (!parameterTypes1[i].getName().equals(
209: parameterTypes2[i].getName())) {
210: return false;
211: }
212: }
213: return true;
214: }
215:
216: public int hashCode() {
217: int result = 29;
218: result = (29 * result) + m_declaringType.getName().hashCode();
219: result = (29 * result) + m_member.getName().hashCode();
220: Class[] parameterTypes = ((Method) m_member)
221: .getParameterTypes();
222: for (int i = 0; i < parameterTypes.length; i++) {
223: result = (29 * result)
224: + parameterTypes[i].getName().hashCode();
225: }
226: return result;
227: }
228:
229: public String toString() {
230: return m_member.toString();
231: }
232: }
|