[jboss-cvs] JBossAS SVN: r103906 - in projects/jboss-reflect/trunk/src: test/java/org/jboss/test/classinfo/test and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Apr 13 09:03:34 EDT 2010
Author: kabir.khan at jboss.com
Date: 2010-04-13 09:03:33 -0400 (Tue, 13 Apr 2010)
New Revision: 103906
Added:
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoClassWithNestedTypes.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericClassWithNestedTypes.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericInterfaceWithNestedTypes.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoInterfaceWithNestedTypes.java
Modified:
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistHelper.java
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoClassTest.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericMembersTest.java
Log:
[JBREFLECT-5] Take into account nested types when creating the generic information
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistHelper.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistHelper.java 2010-04-13 12:58:56 UTC (rev 103905)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistHelper.java 2010-04-13 13:03:33 UTC (rev 103906)
@@ -113,6 +113,27 @@
}
/**
+ * Get the fully qualified name for a generic type
+ *
+ * @param type the type
+ * @return the name
+ */
+ static String getClassNameForGenericType(ClassType type)
+ {
+ if (type.getDeclaringClass() == null)
+ return type.getName();
+
+ StringBuilder sb = new StringBuilder(type.getName());
+ while(type.getDeclaringClass() != null)
+ {
+ sb.insert(0, type.getDeclaringClass().getName() + "$");
+ type = type.getDeclaringClass();
+ }
+
+ return sb.toString();
+ }
+
+ /**
* Append the information of the type to the name builder
*
* @param type the type
@@ -127,7 +148,7 @@
if (type instanceof ClassType)
{
ClassType ctype = (ClassType)type;
- sb.append(ctype.getName());
+ sb.append(getClassNameForGenericType(ctype));
TypeArgument[] arguments = ctype.getTypeArguments();
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java 2010-04-13 12:58:56 UTC (rev 103905)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java 2010-04-13 13:03:33 UTC (rev 103906)
@@ -41,6 +41,7 @@
import javassist.bytecode.SignatureAttribute.ArrayType;
import javassist.bytecode.SignatureAttribute.BaseType;
import javassist.bytecode.SignatureAttribute.ClassType;
+import javassist.bytecode.SignatureAttribute.NestedClassType;
import javassist.bytecode.SignatureAttribute.ObjectType;
import javassist.bytecode.SignatureAttribute.TypeArgument;
@@ -307,9 +308,16 @@
private TypeInfo delegateToIntrospectionImplementation(ClassLoader cl, String name) throws ClassNotFoundException
{
//Check the class has been loaded
- cl.loadClass(name);
- IntrospectionTypeInfoFactory factory = new IntrospectionTypeInfoFactory();
- return factory.getTypeInfo(name, cl);
+ try
+ {
+ cl.loadClass(name);
+ IntrospectionTypeInfoFactory factory = new IntrospectionTypeInfoFactory();
+ return factory.getTypeInfo(name, cl);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw e;
+ }
}
/**
@@ -784,7 +792,7 @@
{
try
{
- return get(type.getName(), loader);
+ return get(JavassistHelper.getClassNameForGenericType(type), loader);
}
catch (ClassNotFoundException e)
{
@@ -806,7 +814,7 @@
//Create the parameterized type info
try
{
- ClassInfo delegate = (ClassInfo)get(type.getName(), loader);
+ ClassInfo delegate = (ClassInfo)get(JavassistHelper.getClassNameForGenericType(type), loader);
if (!isParameterized)
return delegate;
info = new JavassistParameterizedClassInfo(this, delegate, type.getTypeArguments(), spy);
Modified: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoClassTest.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoClassTest.java 2010-04-13 12:58:56 UTC (rev 103905)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoClassTest.java 2010-04-13 13:03:33 UTC (rev 103906)
@@ -23,6 +23,9 @@
import org.jboss.reflect.plugins.ClassInfoImpl;
import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.EnumInfo;
+import org.jboss.reflect.spi.InterfaceInfo;
+import org.jboss.reflect.spi.MethodInfo;
import org.jboss.reflect.spi.ModifierInfo;
import org.jboss.reflect.spi.TypeInfo;
import org.jboss.test.classinfo.support.ClassInfoAbstractMethodsClass;
@@ -117,8 +120,61 @@
testClass(ClassInfoConstructorParameterAnnotationClass.class);
}
- private void testClass(Class<?> clazz) throws Throwable
+ public void testClassWithNestedTypes() throws Throwable
{
+ testNestedTypes(ClassInfoClassWithNestedTypes.class);
+ }
+
+ public void testInterfaceWithNestedTypes() throws Throwable
+ {
+ testNestedTypes(ClassInfoInterfaceWithNestedTypes.class);
+ }
+
+ private void testNestedTypes(Class<?> clazz) throws Throwable
+ {
+ ClassInfo info = testClass(clazz);
+ MethodInfo[] minfos = info.getDeclaredMethods();
+
+ assertEquals(3, minfos.length);
+ MethodInfo enumMethod = null;
+ MethodInfo interfaceMethod = null;
+ MethodInfo classMethod = null;
+ for (MethodInfo current : minfos)
+ {
+ if (current.getName().equals("nestedClass"))
+ classMethod = current;
+ else if (current.getName().equals("nestedInterface"))
+ interfaceMethod = current;
+ else if (current.getName().equals("nestedEnum"))
+ enumMethod = current;
+ else
+ fail("Unknown method " + current);
+ }
+ assertNotNull(enumMethod);
+ assertNotNull(interfaceMethod);
+ assertNotNull(classMethod);
+
+ ClassInfo nestedClass = testNestedType(classMethod, ClassInfo.class);
+ InterfaceInfo nestedInterface = testNestedType(interfaceMethod, InterfaceInfo.class);
+ EnumInfo nestedEnum = testNestedType(enumMethod, EnumInfo.class);
+
+ }
+
+ private <T extends ClassInfo> T testNestedType(MethodInfo m, Class<T> type)
+ {
+ T nestedType = assertInstanceOf(m.getReturnType(), type);
+ assertEquals(1, m.getParameterTypes().length);
+ assertSame(nestedType, m.getParameterTypes()[0]);
+
+ //TODO JBREFLECT-117 implement getOwnerType()
+ //assertNotNull(nestedType.getOwnerType());
+ //assertSame(m.getDeclaringClass(), nestedType.getOwnerType());
+
+ return nestedType;
+ }
+
+ private ClassInfo testClass(Class<?> clazz) throws Throwable
+ {
ClassInfoImpl expected = new ClassInfoImpl(clazz.getName(), ModifierInfo.PUBLIC);
TypeInfo info = testBasics(clazz, expected);
@@ -128,5 +184,6 @@
ClassInfo classInfo = (ClassInfo) info;
assertClassInfo(classInfo, clazz);
+ return classInfo;
}
}
Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoClassWithNestedTypes.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoClassWithNestedTypes.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoClassWithNestedTypes.java 2010-04-13 13:03:33 UTC (rev 103906)
@@ -0,0 +1,58 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassInfoClassWithNestedTypes
+{
+ public static class NestedClass
+ {
+ }
+
+ public interface NestedInterface
+ {
+ }
+
+ public enum NestedEnum
+ {
+ TEST
+ }
+
+ public NestedClass nestedClass(NestedClass nc)
+ {
+ return nc;
+ }
+
+ public NestedInterface nestedInterface(NestedInterface ni)
+ {
+ return ni;
+ }
+
+ public NestedEnum nestedEnum(NestedEnum ne)
+ {
+ return ne;
+ }
+}
Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericClassWithNestedTypes.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericClassWithNestedTypes.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericClassWithNestedTypes.java 2010-04-13 13:03:33 UTC (rev 103906)
@@ -0,0 +1,60 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+import java.util.Collection;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassInfoGenericClassWithNestedTypes<T>
+{
+ public static class NestedClass<T>
+ {
+ }
+
+ public interface NestedInterface<T>
+ {
+ }
+
+ public enum NestedEnum
+ {
+ TEST
+ }
+
+ public NestedClass<String> nestedClass(NestedClass<String> nc, NestedClass<Object> nc2)
+ {
+ return nc;
+ }
+
+ public NestedInterface<String> nestedInterface(NestedInterface<String> ni, NestedInterface<Object> ni2)
+ {
+ return ni;
+ }
+
+ public Collection<String> nestedEnum(Collection<String > col, NestedEnum ne)
+ {
+ return col;
+ }
+}
Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericInterfaceWithNestedTypes.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericInterfaceWithNestedTypes.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericInterfaceWithNestedTypes.java 2010-04-13 13:03:33 UTC (rev 103906)
@@ -0,0 +1,53 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+import java.util.Collection;
+
+
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public interface ClassInfoGenericInterfaceWithNestedTypes<T>
+{
+ static class NestedClass<T>
+ {
+ }
+
+ interface NestedInterface<T>
+ {
+ }
+
+ enum NestedEnum
+ {
+ TEST
+ }
+
+ NestedClass<String> nestedClass(NestedClass<String> nc, NestedClass<Object> nc2);
+
+ NestedInterface<String> nestedInterface(NestedInterface<String> ni, NestedInterface<Object> ni2);
+
+ Collection<String> nestedEnum(Collection<String > col, NestedEnum ne);
+}
Modified: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericMembersTest.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericMembersTest.java 2010-04-13 12:58:56 UTC (rev 103905)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoGenericMembersTest.java 2010-04-13 13:03:33 UTC (rev 103906)
@@ -36,12 +36,17 @@
import org.jboss.reflect.spi.ArrayInfo;
import org.jboss.reflect.spi.ClassInfo;
import org.jboss.reflect.spi.ConstructorInfo;
+import org.jboss.reflect.spi.EnumInfo;
import org.jboss.reflect.spi.FieldInfo;
+import org.jboss.reflect.spi.InterfaceInfo;
import org.jboss.reflect.spi.MethodInfo;
import org.jboss.reflect.spi.TypeInfo;
import org.jboss.test.classinfo.support.ClassInfoTypeVariable;
import org.jboss.test.classinfo.support.ClassInfoTypeVariableBounded;
import org.jboss.test.classinfo.support.ClassInfoTypeVariableRecursiveBounded;
+import org.jboss.test.classinfo.test.ClassInfoGenericClassWithNestedTypes.NestedClass;
+import org.jboss.test.classinfo.test.ClassInfoGenericClassWithNestedTypes.NestedEnum;
+import org.jboss.test.classinfo.test.ClassInfoGenericClassWithNestedTypes.NestedInterface;
/**
*
@@ -327,6 +332,51 @@
testMethod(true, "complexGenerics");
}
+ public void testGenericNestedClassMethodInGenericClass() throws Throwable
+ {
+ MethodInfo minfo = testMethod(true, ClassInfoGenericClassWithNestedTypes.class, "nestedClass");
+ checkNestedType(minfo.getDeclaringClass(), minfo.getReturnType(), ClassInfo.class);
+ }
+
+ public void testGenericNestedInterfaceMethodInGenericClass() throws Throwable
+ {
+ MethodInfo minfo = testMethod(true, ClassInfoGenericClassWithNestedTypes.class, "nestedInterface");
+ checkNestedType(minfo.getDeclaringClass(), minfo.getReturnType(), InterfaceInfo.class);
+ }
+
+ public void testGenericNestedEnumMethodInGenericClass() throws Throwable
+ {
+ MethodInfo minfo = testMethod(true, ClassInfoGenericClassWithNestedTypes.class, "nestedEnum");
+ checkNestedType(minfo.getDeclaringClass(), minfo.getParameterTypes()[1], EnumInfo.class);
+ }
+
+ public void testGenericNestedClassMethodInGenericInterface() throws Throwable
+ {
+ MethodInfo minfo = testMethod(true, ClassInfoGenericInterfaceWithNestedTypes.class, "nestedClass");
+ checkNestedType(minfo.getDeclaringClass(), minfo.getReturnType(), ClassInfo.class);
+ }
+
+ public void testGenericNestedInterfaceMethodInGenericInterface() throws Throwable
+ {
+ MethodInfo minfo = testMethod(true, ClassInfoGenericInterfaceWithNestedTypes.class, "nestedInterface");
+ checkNestedType(minfo.getDeclaringClass(), minfo.getReturnType(), InterfaceInfo.class);
+ }
+
+ public void testGenericNestedEnumMethodInGenericInterface() throws Throwable
+ {
+ MethodInfo minfo = testMethod(true, ClassInfoGenericInterfaceWithNestedTypes.class, "nestedEnum");
+ checkNestedType(minfo.getDeclaringClass(), minfo.getParameterTypes()[1], EnumInfo.class);
+ }
+
+ private <T extends ClassInfo> void checkNestedType(ClassInfo expectedOwner, TypeInfo info, Class<T> expectedNestedType)
+ {
+ T nestedInfo = assertInstanceOf(info, expectedNestedType);
+
+ //TODO JBREFLECT-117 implement getOwnerType()
+ //assertNotNull(nestedInfo.getOwnerType());
+ //assertSame(expectedOwner, nestedInfo.getOwnerType());
+ }
+
public void testNotGenericField() throws Throwable
{
testField("notGeneric");
@@ -372,7 +422,7 @@
testMethod(generic, ClassInfoGenericMembersTest.class, name);
}
- private void testMethod(boolean generic, Class<?> clazz, String name) throws Throwable
+ private MethodInfo testMethod(boolean generic, Class<?> clazz, String name) throws Throwable
{
ClassInfo info = getClassInfo(clazz);
Method m = getMethod(clazz, name);
@@ -400,6 +450,7 @@
{
assertNotSame(parameterInfos[0], parameterInfos[1]);
}
+ return minfo;
}
private void testConstructor(boolean generic, Class<?> id) throws Throwable
Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoInterfaceWithNestedTypes.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoInterfaceWithNestedTypes.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoInterfaceWithNestedTypes.java 2010-04-13 13:03:33 UTC (rev 103906)
@@ -0,0 +1,50 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public interface ClassInfoInterfaceWithNestedTypes
+{
+ public static class NestedClass
+ {
+ }
+
+ public interface NestedInterface
+ {
+ }
+
+ public enum NestedEnum
+ {
+ TEST
+ }
+
+ public NestedClass nestedClass(NestedClass nc);
+
+ public NestedInterface nestedInterface(NestedInterface ni);
+
+ public NestedEnum nestedEnum(NestedEnum ne);
+}
More information about the jboss-cvs-commits
mailing list