1 /***************************************************************************************
2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package test;
9
10 import junit.framework.TestCase;
11 import org.codehaus.aspectwerkz.reflect.impl.asm.AsmClassInfo;
12 import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
13 import org.codehaus.aspectwerkz.reflect.ClassInfo;
14 import org.codehaus.aspectwerkz.reflect.MethodInfo;
15 import org.codehaus.aspectwerkz.reflect.ReflectHelper;
16 import org.codehaus.aspectwerkz.reflect.FieldInfo;
17 import org.codehaus.aspectwerkz.annotation.instrumentation.asm.AsmAnnotationHelper;
18 import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
19
20 import java.lang.reflect.Modifier;
21 import java.lang.reflect.Method;
22 import java.util.SortedSet;
23
24 /***
25 * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
26 */
27 public class ClassInfoTest extends TestCase {
28
29 public void method(int i, long l, String[] s, int[][] i2) {
30
31 for (int a = 0; a < 3; a++) {
32 for (long b = 0; b < 2; b++) {
33 ;
34 }
35 }
36 }
37
38 public static long[][][] smethod(long[][] l2, ClassInfoTest test, ClassInfoTest[][] test2) {
39 return null;
40 }
41
42 public void testMethodInfo() {
43 ClassInfo ci = AsmClassInfo.getClassInfo("test.ClassInfoTest", ClassLoader.getSystemClassLoader());
44 MethodInfo[] methods = ci.getMethods();
45
46 assertTrue(methods.length >= 2);
47
48 for (int i = 0; i < methods.length; i++) {
49 MethodInfo method = methods[i];
50 if (method.getName().equals("method")) {
51 checkMethod(method);
52 } else if (method.getName().equals("smethod")) {
53 checkStaticMethod(method);
54 }
55 }
56 }
57
58 private void checkMethod(MethodInfo method) {
59 try {
60 assertEquals("method", method.getName());
61 assertTrue(!Modifier.isStatic(method.getModifiers()));
62
63 assertEquals("i", method.getParameterNames()[0]);
64 assertEquals("l", method.getParameterNames()[1]);
65 assertEquals("s", method.getParameterNames()[2]);
66 assertEquals("i2", method.getParameterNames()[3]);
67
68 assertEquals("int", method.getParameterTypes()[0].getName());
69 assertEquals("long", method.getParameterTypes()[1].getName());
70 assertEquals("java.lang.String[]", method.getParameterTypes()[2].getName());
71 assertEquals("int[][]", method.getParameterTypes()[3].getName());
72
73 assertEquals("void", method.getReturnType().getName());
74 } catch (Throwable t) {
75 fail(t.toString());
76 }
77 }
78
79 private void checkStaticMethod(MethodInfo method) {
80 try {
81 assertEquals("smethod", method.getName());
82 assertTrue(Modifier.isStatic(method.getModifiers()));
83
84 assertEquals("l2", method.getParameterNames()[0]);
85 assertEquals("test", method.getParameterNames()[1]);
86 assertEquals("test2", method.getParameterNames()[2]);
87
88 assertEquals("long[][]", method.getParameterTypes()[0].getName());
89 assertEquals("[[J", method.getParameterTypes()[0].getSignature());
90 assertEquals("test.ClassInfoTest", method.getParameterTypes()[1].getName());
91 assertEquals("Ltest/ClassInfoTest;", method.getParameterTypes()[1].getSignature());
92 assertEquals("test.ClassInfoTest[][]", method.getParameterTypes()[2].getName());
93 assertEquals("[[Ltest/ClassInfoTest;", method.getParameterTypes()[2].getSignature());
94
95 assertEquals("long[][][]", method.getReturnType().getName());
96 assertEquals("[[[J", method.getReturnType().getSignature());
97 } catch (Throwable t) {
98 fail(t.toString());
99 }
100 }
101
102 public void testGetMethods() throws Exception {
103 Class intfClazz = SortedSet.class;
104 Method inIntfMethod = intfClazz.getMethod("first", new Class[0]);
105 assertNotNull("first() is declared in java.util.SortedSet", inIntfMethod);
106
107 Method inSuperMethod = intfClazz.getMethod("isEmpty", new Class[0]);
108 assertNotNull("isEmpty() is declared in java.util.Set", inSuperMethod);
109
110 int inIntfMethodHash = ReflectHelper.calculateHash(inIntfMethod);
111 int inSuperMethodHash = ReflectHelper.calculateHash(inSuperMethod);
112
113 ClassInfo clazzInfo = AsmClassInfo.getClassInfo("java.util.SortedSet", ClassInfoTest.class.getClassLoader());
114 assertNotNull("java.util.SortedSet should be found", clazzInfo);
115
116 MethodInfo inIntfMethodInfo = clazzInfo.getMethod(inIntfMethodHash);
117 assertNotNull("first() method info should be found directly", inIntfMethodInfo);
118
119 MethodInfo inSuperMethodInfo = clazzInfo.getMethod(inSuperMethodHash);
120
121
122 ClassInfo clazzInfo2 = JavaClassInfo.getClassInfo(java.util.SortedSet.class);
123 assertNotNull("java.util.SortedSet should be found", clazzInfo);
124
125 MethodInfo inIntfMethodInfo2 = clazzInfo2.getMethod(inIntfMethodHash);
126 assertNotNull("first() method info should be found directly", inIntfMethodInfo2);
127
128 MethodInfo inSuperMethodInfo2 = clazzInfo2.getMethod(inSuperMethodHash);
129 assertNotNull("isEmpty() method info from super interface", inSuperMethodInfo2);
130 }
131
132
133 public static void main(String[] args) {
134 junit.textui.TestRunner.run(suite());
135 }
136
137 public static junit.framework.Test suite() {
138 return new junit.framework.TestSuite(ClassInfoTest.class);
139 }
140
141 }