KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > aspectwerkz > AsmMethodInfo


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.object.bytecode.aspectwerkz;
5
6 import org.apache.commons.lang.builder.ToStringBuilder;
7
8 import com.tc.asm.Type;
9 import com.tc.aspectwerkz.reflect.ClassInfo;
10 import com.tc.aspectwerkz.reflect.MethodInfo;
11 import com.tc.backport175.bytecode.AnnotationElement;
12 import com.tc.exception.ImplementMe;
13
14 import java.lang.reflect.Method JavaDoc;
15
16 /**
17  * Converts Asm method descriptions to Aspectwerkz MethodInfo
18  */

19 public class AsmMethodInfo implements MethodInfo {
20   private int modifiers;
21   private ClassInfo declaringType;
22   private String JavaDoc name;
23   private ClassInfo returnTypeInfo;
24   private ClassInfo[] parameterTypeInfos;
25   private ClassInfo[] exceptionTypeInfos;
26   private ClassInfoFactory classInfoFactory;
27
28   public AsmMethodInfo(ClassInfoFactory classInfoFactory, int modifiers, String JavaDoc className, String JavaDoc methodName,
29                        String JavaDoc desc, String JavaDoc[] exceptions) {
30     this.classInfoFactory = classInfoFactory;
31     // handle modifiers
32
this.modifiers = modifiers;
33     // handle declaring type
34
this.declaringType = classInfoFactory.getClassInfo(className);//new SimpleClassInfo(className);
35
// handle method name
36
this.name = methodName.equals("<init>") ? "__INIT__" : methodName;
37     // handle return type.
38
this.returnTypeInfo = type2ClassInfo(Type.getReturnType(desc));
39     // handle parameter types
40
Type[] parameterTypes = Type.getArgumentTypes(desc);
41     this.parameterTypeInfos = types2ClassInfos(parameterTypes);
42     // handle exception types
43
this.exceptionTypeInfos = classNames2ClassInfos(exceptions);
44   }
45
46   public String JavaDoc toString() {
47     return ToStringBuilder.reflectionToString(this);
48   }
49
50   private ClassInfo[] classNames2ClassInfos(String JavaDoc[] classNames) {
51     if (classNames == null) return null;
52     ClassInfo[] rv = new ClassInfo[classNames.length];
53     for (int i = 0; i < classNames.length; i++) {
54       rv[i] = className2ClassInfo(classNames[i]);
55     }
56     return rv;
57   }
58
59   private ClassInfo[] types2ClassInfos(Type[] types) {
60     if (types == null) return null;
61     ClassInfo[] rv = new ClassInfo[types.length];
62     for (int i = 0; i < types.length; i++) {
63       rv[i] = type2ClassInfo(types[i]);
64     }
65     return rv;
66   }
67
68   private ClassInfo className2ClassInfo(String JavaDoc className) {
69     return classInfoFactory.getClassInfo(className);//new SimpleClassInfo(className);
70
}
71
72   private ClassInfo type2ClassInfo(Type type) {
73     return className2ClassInfo(type.getClassName());
74   }
75
76   public ClassInfo getReturnType() {
77     return this.returnTypeInfo;
78   }
79
80   public ClassInfo[] getParameterTypes() {
81     return this.parameterTypeInfos;
82   }
83
84   public String JavaDoc[] getParameterNames() {
85     return new String JavaDoc[0]; //To change body of implemented methods use File | Settings | File Templates.
86
}
87
88   public ClassInfo[] getExceptionTypes() {
89     return this.exceptionTypeInfos;
90   }
91
92   public ClassInfo getDeclaringType() {
93     return this.declaringType;
94   }
95
96   public String JavaDoc getName() {
97     return this.name;
98   }
99
100   public String JavaDoc getSignature() {
101     return null;
102   }
103   
104   public String JavaDoc getGenericsSignature() {
105     return null;
106   }
107
108   public int getModifiers() {
109     return modifiers;
110   }
111
112   public AnnotationElement.Annotation[] getAnnotations() {
113     throw new ImplementMe();
114   }
115
116   /**
117    * Creates a new AsmMethodInfo from the given Method. This is only used for testing.
118    */

119   public static AsmMethodInfo createNewAsmMethodInfo(Method JavaDoc method) {
120     int modifiers = method.getModifiers();
121     String JavaDoc className = method.getDeclaringClass().getName();
122     String JavaDoc methodName = method.getName();
123     String JavaDoc desc = Type.getMethodDescriptor(method);
124     Class JavaDoc[] exceptionTypes = method.getExceptionTypes();
125     String JavaDoc[] exceptionTypeNames = new String JavaDoc[exceptionTypes.length];
126     for (int i = 0; i < exceptionTypes.length; i++) {
127       exceptionTypeNames[i] = exceptionTypes[i].getName();
128     }
129     return new AsmMethodInfo(new ClassInfoFactory(), modifiers, className, methodName, desc, exceptionTypeNames);
130   }
131
132 }
Popular Tags