001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.bytecode.aspectwerkz;
005:
006: import org.apache.commons.lang.builder.ToStringBuilder;
007:
008: import com.tc.asm.Type;
009: import com.tc.aspectwerkz.reflect.ClassInfo;
010: import com.tc.aspectwerkz.reflect.MethodInfo;
011: import com.tc.backport175.bytecode.AnnotationElement;
012: import com.tc.exception.ImplementMe;
013:
014: import java.lang.reflect.Method;
015:
016: /**
017: * Converts Asm method descriptions to Aspectwerkz MethodInfo
018: */
019: public class AsmMethodInfo implements MethodInfo {
020: private int modifiers;
021: private ClassInfo declaringType;
022: private String name;
023: private ClassInfo returnTypeInfo;
024: private ClassInfo[] parameterTypeInfos;
025: private ClassInfo[] exceptionTypeInfos;
026: private ClassInfoFactory classInfoFactory;
027:
028: public AsmMethodInfo(ClassInfoFactory classInfoFactory,
029: int modifiers, String className, String methodName,
030: String desc, String[] exceptions) {
031: this .classInfoFactory = classInfoFactory;
032: // handle modifiers
033: this .modifiers = modifiers;
034: // handle declaring type
035: this .declaringType = classInfoFactory.getClassInfo(className);//new SimpleClassInfo(className);
036: // handle method name
037: this .name = methodName.equals("<init>") ? "__INIT__"
038: : methodName;
039: // handle return type.
040: this .returnTypeInfo = type2ClassInfo(Type.getReturnType(desc));
041: // handle parameter types
042: Type[] parameterTypes = Type.getArgumentTypes(desc);
043: this .parameterTypeInfos = types2ClassInfos(parameterTypes);
044: // handle exception types
045: this .exceptionTypeInfos = classNames2ClassInfos(exceptions);
046: }
047:
048: public String toString() {
049: return ToStringBuilder.reflectionToString(this );
050: }
051:
052: private ClassInfo[] classNames2ClassInfos(String[] classNames) {
053: if (classNames == null)
054: return null;
055: ClassInfo[] rv = new ClassInfo[classNames.length];
056: for (int i = 0; i < classNames.length; i++) {
057: rv[i] = className2ClassInfo(classNames[i]);
058: }
059: return rv;
060: }
061:
062: private ClassInfo[] types2ClassInfos(Type[] types) {
063: if (types == null)
064: return null;
065: ClassInfo[] rv = new ClassInfo[types.length];
066: for (int i = 0; i < types.length; i++) {
067: rv[i] = type2ClassInfo(types[i]);
068: }
069: return rv;
070: }
071:
072: private ClassInfo className2ClassInfo(String className) {
073: return classInfoFactory.getClassInfo(className);//new SimpleClassInfo(className);
074: }
075:
076: private ClassInfo type2ClassInfo(Type type) {
077: return className2ClassInfo(type.getClassName());
078: }
079:
080: public ClassInfo getReturnType() {
081: return this .returnTypeInfo;
082: }
083:
084: public ClassInfo[] getParameterTypes() {
085: return this .parameterTypeInfos;
086: }
087:
088: public String[] getParameterNames() {
089: return new String[0]; //To change body of implemented methods use File | Settings | File Templates.
090: }
091:
092: public ClassInfo[] getExceptionTypes() {
093: return this .exceptionTypeInfos;
094: }
095:
096: public ClassInfo getDeclaringType() {
097: return this .declaringType;
098: }
099:
100: public String getName() {
101: return this .name;
102: }
103:
104: public String getSignature() {
105: return null;
106: }
107:
108: public String getGenericsSignature() {
109: return null;
110: }
111:
112: public int getModifiers() {
113: return modifiers;
114: }
115:
116: public AnnotationElement.Annotation[] getAnnotations() {
117: throw new ImplementMe();
118: }
119:
120: /**
121: * Creates a new AsmMethodInfo from the given Method. This is only used for testing.
122: */
123: public static AsmMethodInfo createNewAsmMethodInfo(Method method) {
124: int modifiers = method.getModifiers();
125: String className = method.getDeclaringClass().getName();
126: String methodName = method.getName();
127: String desc = Type.getMethodDescriptor(method);
128: Class[] exceptionTypes = method.getExceptionTypes();
129: String[] exceptionTypeNames = new String[exceptionTypes.length];
130: for (int i = 0; i < exceptionTypes.length; i++) {
131: exceptionTypeNames[i] = exceptionTypes[i].getName();
132: }
133: return new AsmMethodInfo(new ClassInfoFactory(), modifiers,
134: className, methodName, desc, exceptionTypeNames);
135: }
136:
137: }
|