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 test;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.reflect.impl.asm.AsmClassInfo;
011: import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
012: import org.codehaus.aspectwerkz.reflect.ClassInfo;
013: import org.codehaus.aspectwerkz.reflect.MethodInfo;
014: import org.codehaus.aspectwerkz.reflect.ReflectHelper;
015: import org.codehaus.aspectwerkz.reflect.FieldInfo;
016: import org.codehaus.aspectwerkz.annotation.instrumentation.asm.AsmAnnotationHelper;
017: import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
018:
019: import java.lang.reflect.Modifier;
020: import java.lang.reflect.Method;
021: import java.util.SortedSet;
022:
023: /**
024: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
025: */
026: public class ClassInfoTest extends TestCase {
027:
028: public void method(int i, long l, String[] s, int[][] i2) {
029: // some code to influence local variables
030: for (int a = 0; a < 3; a++) {
031: for (long b = 0; b < 2; b++) {
032: ;
033: }
034: }
035: }
036:
037: public static long[][][] smethod(long[][] l2, ClassInfoTest test,
038: ClassInfoTest[][] test2) {
039: return null;
040: }
041:
042: public void testMethodInfo() {
043: ClassInfo ci = AsmClassInfo.getClassInfo("test.ClassInfoTest",
044: ClassLoader.getSystemClassLoader());
045: MethodInfo[] methods = ci.getMethods();
046:
047: assertTrue(methods.length >= 2);
048:
049: for (int i = 0; i < methods.length; i++) {
050: MethodInfo method = methods[i];
051: if (method.getName().equals("method")) {
052: checkMethod(method);
053: } else if (method.getName().equals("smethod")) {
054: checkStaticMethod(method);
055: }
056: }
057: }
058:
059: private void checkMethod(MethodInfo method) {
060: try {
061: assertEquals("method", method.getName());
062: assertTrue(!Modifier.isStatic(method.getModifiers()));
063:
064: assertEquals("i", method.getParameterNames()[0]);
065: assertEquals("l", method.getParameterNames()[1]);
066: assertEquals("s", method.getParameterNames()[2]);
067: assertEquals("i2", method.getParameterNames()[3]);
068:
069: assertEquals("int", method.getParameterTypes()[0].getName());
070: assertEquals("long", method.getParameterTypes()[1]
071: .getName());
072: assertEquals("java.lang.String[]", method
073: .getParameterTypes()[2].getName());
074: assertEquals("int[][]", method.getParameterTypes()[3]
075: .getName());
076:
077: assertEquals("void", method.getReturnType().getName());
078: } catch (Throwable t) {
079: fail(t.toString());
080: }
081: }
082:
083: private void checkStaticMethod(MethodInfo method) {
084: try {
085: assertEquals("smethod", method.getName());
086: assertTrue(Modifier.isStatic(method.getModifiers()));
087:
088: assertEquals("l2", method.getParameterNames()[0]);
089: assertEquals("test", method.getParameterNames()[1]);
090: assertEquals("test2", method.getParameterNames()[2]);
091:
092: assertEquals("long[][]", method.getParameterTypes()[0]
093: .getName());
094: assertEquals("[[J", method.getParameterTypes()[0]
095: .getSignature());
096: assertEquals("test.ClassInfoTest", method
097: .getParameterTypes()[1].getName());
098: assertEquals("Ltest/ClassInfoTest;", method
099: .getParameterTypes()[1].getSignature());
100: assertEquals("test.ClassInfoTest[][]", method
101: .getParameterTypes()[2].getName());
102: assertEquals("[[Ltest/ClassInfoTest;", method
103: .getParameterTypes()[2].getSignature());
104:
105: assertEquals("long[][][]", method.getReturnType().getName());
106: assertEquals("[[[J", method.getReturnType().getSignature());
107: } catch (Throwable t) {
108: fail(t.toString());
109: }
110: }
111:
112: public void testGetMethods() throws Exception {
113: Class intfClazz = SortedSet.class;
114: Method inIntfMethod = intfClazz
115: .getMethod("first", new Class[0]);
116: assertNotNull("first() is declared in java.util.SortedSet",
117: inIntfMethod);
118:
119: Method inSuperMethod = intfClazz.getMethod("isEmpty",
120: new Class[0]);
121: assertNotNull("isEmpty() is declared in java.util.Set",
122: inSuperMethod);
123:
124: int inIntfMethodHash = ReflectHelper
125: .calculateHash(inIntfMethod);
126: int inSuperMethodHash = ReflectHelper
127: .calculateHash(inSuperMethod);
128:
129: ClassInfo clazzInfo = AsmClassInfo.getClassInfo(
130: "java.util.SortedSet", ClassInfoTest.class
131: .getClassLoader());
132: assertNotNull("java.util.SortedSet should be found", clazzInfo);
133:
134: MethodInfo inIntfMethodInfo = clazzInfo
135: .getMethod(inIntfMethodHash);
136: assertNotNull("first() method info should be found directly",
137: inIntfMethodInfo);
138:
139: MethodInfo inSuperMethodInfo = clazzInfo
140: .getMethod(inSuperMethodHash);
141: // assertNotNull("isEmpty() method info from super interface", inSuperMethodInfo);
142:
143: ClassInfo clazzInfo2 = JavaClassInfo
144: .getClassInfo(java.util.SortedSet.class);
145: assertNotNull("java.util.SortedSet should be found", clazzInfo);
146:
147: MethodInfo inIntfMethodInfo2 = clazzInfo2
148: .getMethod(inIntfMethodHash);
149: assertNotNull("first() method info should be found directly",
150: inIntfMethodInfo2);
151:
152: MethodInfo inSuperMethodInfo2 = clazzInfo2
153: .getMethod(inSuperMethodHash);
154: assertNotNull("isEmpty() method info from super interface",
155: inSuperMethodInfo2);
156: }
157:
158: //-- JUnit
159: public static void main(String[] args) {
160: junit.textui.TestRunner.run(suite());
161: }
162:
163: public static junit.framework.Test suite() {
164: return new junit.framework.TestSuite(ClassInfoTest.class);
165: }
166:
167: }
|