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.asm;
008:
009: import org.codehaus.aspectwerkz.reflect.ClassInfo;
010: import org.codehaus.aspectwerkz.reflect.MethodInfo;
011: import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
012: import org.codehaus.aspectwerkz.annotation.instrumentation.asm.AsmAnnotationHelper;
013: import org.codehaus.aspectwerkz.exception.DefinitionException;
014: import org.codehaus.aspectwerkz.proxy.ProxyCompiler;
015: import org.objectweb.asm.Type;
016: import org.objectweb.asm.ClassReader;
017: import org.objectweb.asm.attrs.Attributes;
018:
019: import java.util.List;
020: import java.util.ArrayList;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.lang.reflect.Modifier;
024:
025: /**
026: * ASM implementation of the MethodInfo interface.
027: *
028: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
029: */
030: public class AsmMethodInfo extends AsmMemberInfo implements MethodInfo {
031:
032: /**
033: * The return type name.
034: */
035: private String m_returnTypeName = null;
036:
037: /**
038: * A list with the parameter names as they appear in the source code.
039: * This information may not be available.
040: */
041: protected String[] m_parameterNames = null;
042:
043: /**
044: * A list with the parameter type names.
045: */
046: private String[] m_parameterTypeNames = null;
047:
048: /**
049: * A list with the exception type names.
050: */
051: private String[] m_exceptionTypeNames = null;
052:
053: /**
054: * The return type.
055: */
056: private ClassInfo m_returnType = null;
057:
058: /**
059: * A list with the parameter types.
060: */
061: private ClassInfo[] m_parameterTypes = null;
062:
063: /**
064: * A list with the exception types.
065: */
066: private ClassInfo[] m_exceptionTypes = null;
067:
068: /**
069: * Creates a new method info instance.
070: *
071: * @param method
072: * @param declaringType
073: * @param loader
074: */
075: AsmMethodInfo(final MethodStruct method,
076: final String declaringType, final ClassLoader loader) {
077: super (method, declaringType, loader);
078:
079: m_returnTypeName = Type.getReturnType(method.desc)
080: .getClassName();
081: Type[] argTypes = Type.getArgumentTypes(method.desc);
082: m_parameterTypeNames = new String[argTypes.length];
083: for (int i = 0; i < argTypes.length; i++) {
084: m_parameterTypeNames[i] = argTypes[i].getClassName();
085: }
086: // FIXME: how to do exceptions? needed?
087: m_exceptionTypeNames = new String[] {};
088: }
089:
090: /**
091: * Returns the method info for the method specified.
092: *
093: * @param methodName
094: * @param methodDesc
095: * @param bytecode
096: * @param loader
097: * @return the method info
098: */
099: public static MethodInfo getMethodInfo(final String methodName,
100: final String methodDesc, final byte[] bytecode,
101: final ClassLoader loader) {
102: String className = AsmClassInfo
103: .retrieveClassNameFromBytecode(bytecode);
104: AsmClassInfoRepository repository = AsmClassInfoRepository
105: .getRepository(loader);
106: ClassInfo classInfo = repository.getClassInfo(className);
107: if (classInfo == null) {
108: classInfo = AsmClassInfo.getClassInfo(bytecode, loader);
109: }
110: return classInfo.getMethod(AsmHelper.calculateMethodHash(
111: methodName, methodDesc));
112: }
113:
114: /**
115: * Returns the signature for the element.
116: *
117: * @return the signature for the element
118: */
119: public String getSignature() {
120: return AsmHelper.getMethodDescriptor(this );
121: }
122:
123: /**
124: * Returns the return type.
125: *
126: * @return the return type
127: */
128: public ClassInfo getReturnType() {
129: if (m_returnType == null) {
130: m_returnType = AsmClassInfo.getClassInfo(m_returnTypeName,
131: (ClassLoader) m_loaderRef.get());
132: }
133: return m_returnType;
134: }
135:
136: /**
137: * Returns the parameter types.
138: *
139: * @return the parameter types
140: */
141: public ClassInfo[] getParameterTypes() {
142: if (m_parameterTypes == null) {
143: m_parameterTypes = new ClassInfo[m_parameterTypeNames.length];
144: for (int i = 0; i < m_parameterTypeNames.length; i++) {
145: m_parameterTypes[i] = AsmClassInfo.getClassInfo(
146: m_parameterTypeNames[i],
147: (ClassLoader) m_loaderRef.get());
148: }
149: }
150: return m_parameterTypes;
151: }
152:
153: /**
154: * Returns the parameter names as they appear in the source code.
155: * This information is available only when class are compiled with javac -g (debug info), but is required
156: * for Aspect that are using args() and target()/this() bindings.
157: * <p/>
158: * It returns null if not available.
159: *
160: * @return
161: */
162: public String[] getParameterNames() {
163: return m_parameterNames;
164: }
165:
166: /**
167: * Returns the exception types.
168: *
169: * @return the exception types
170: */
171: public ClassInfo[] getExceptionTypes() {
172: if (m_exceptionTypes == null) {
173: m_exceptionTypes = new ClassInfo[m_exceptionTypeNames.length];
174: for (int i = 0; i < m_exceptionTypeNames.length; i++) {
175: m_exceptionTypes[i] = AsmClassInfo.getClassInfo(
176: m_exceptionTypeNames[i],
177: (ClassLoader) m_loaderRef.get());
178: }
179: }
180: return m_exceptionTypes;
181: }
182:
183: /**
184: * Returns the annotations.
185: *
186: * @return the annotations
187: */
188: public List getAnnotations() {
189: if (m_annotations == null) {
190: try {
191: InputStream in = null;
192: ClassReader cr = null;
193: try {
194: if ((ClassLoader) m_loaderRef.get() != null) {
195: in = ((ClassLoader) m_loaderRef.get())
196: .getResourceAsStream(m_declaringTypeName
197: .replace('.', '/')
198: + ".class");
199: } else {
200: in = ClassLoader.getSystemClassLoader()
201: .getResourceAsStream(
202: m_declaringTypeName.replace(
203: '.', '/')
204: + ".class");
205: }
206: if (in == null) {
207: in = ProxyCompiler.getProxyResourceAsStream(
208: (ClassLoader) m_loaderRef.get(),
209: m_declaringTypeName);
210: }
211: cr = new ClassReader(in);
212: } finally {
213: try {
214: in.close();
215: } catch (Exception e) {
216: ;
217: }
218: }
219: List annotations = new ArrayList();
220: cr
221: .accept(
222: new AsmAnnotationHelper.MethodAnnotationExtractor(
223: annotations, m_member.name,
224: m_member.desc,
225: (ClassLoader) m_loaderRef.get()),
226: Attributes.getDefaultAttributes(), true);
227: m_annotations = annotations;
228: } catch (IOException e) {
229: // unlikely to occur since ClassInfo relies on getResourceAsStream
230: System.err.println("WARN - could not load "
231: + m_declaringTypeName
232: + " as a resource to retrieve annotations");
233: m_annotations = AsmClassInfo.EMPTY_LIST;
234: }
235: }
236: return m_annotations;
237: }
238:
239: public boolean equals(Object o) {
240: if (this == o) {
241: return true;
242: }
243: if (!(o instanceof MethodInfo)) {
244: return false;
245: }
246: MethodInfo methodInfo = (MethodInfo) o;
247: if (!m_declaringTypeName.equals(methodInfo.getDeclaringType()
248: .getName())) {
249: return false;
250: }
251: if (!m_member.name.equals(methodInfo.getName())) {
252: return false;
253: }
254: ClassInfo[] parameterTypes = methodInfo.getParameterTypes();
255: if (m_parameterTypeNames.length != parameterTypes.length) {//check on names length for lazyness optim
256: return false;
257: }
258: for (int i = 0; i < m_parameterTypeNames.length; i++) {
259: if (!m_parameterTypeNames[i].equals(parameterTypes[i]
260: .getName())) {
261: return false;
262: }
263: }
264: return true;
265: }
266:
267: public int hashCode() {
268: int result = 29;
269: result = (29 * result) + m_declaringTypeName.hashCode();
270: result = (29 * result) + m_member.name.hashCode();
271: for (int i = 0; i < m_parameterTypeNames.length; i++) {
272: result = (29 * result) + m_parameterTypeNames[i].hashCode();
273: }
274: return result;
275: }
276:
277: public String toString() {
278: StringBuffer sb = new StringBuffer(m_declaringTypeName);
279: sb.append('.').append(m_member.name);
280: sb.append(m_member.desc);
281: return sb.toString();
282: }
283:
284: /**
285: * Update the parameter name given the parameter information
286: * the index is the one from the register ie a long or double will needs 2 register
287: *
288: * @param registerIndex
289: * @param parameterName
290: */
291: public void pushParameterNameFromRegister(int registerIndex,
292: String parameterName) {
293: int registerStart = 1;
294: if (Modifier.isStatic(m_member.modifiers)) {
295: registerStart = 0;
296: }
297: // assume we have a stack starting at the first parameter
298: int registerIndexFrom0 = registerIndex - registerStart;
299: Type[] parameters = Type.getArgumentTypes(m_member.desc);
300: int typeIndex = AsmHelper.getTypeIndexOf(parameters,
301: registerIndexFrom0);
302: if (typeIndex >= 0 && typeIndex < m_parameterNames.length) {
303: m_parameterNames[typeIndex] = parameterName;
304: } else {
305: throw new DefinitionException(
306: "Could not register parameter named "
307: + parameterName + " from register "
308: + registerIndex + " for " + m_member.name
309: + "." + m_member.desc);
310: }
311: }
312: }
|