[jboss-cvs] JBossAS SVN: r104270 - in projects/jboss-reflect/trunk/src: test/java/org/jboss/test/classinfo/support and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Apr 27 12:48:44 EDT 2010


Author: kabir.khan at jboss.com
Date: 2010-04-27 12:48:43 -0400 (Tue, 27 Apr 2010)
New Revision: 104270

Added:
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/GenericInterface.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/GenericSuperclass.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/AbstractParameterizedClassLoaderTest.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionParameterizedClassInfoClassLoaderArgumentsTestCase.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionParameterizedClassInfoClassLoaderCachingTestCase.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistParameterizedClassInfoClassLoaderArgumentsTestCase.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistParameterizedClassInfoClassLoaderCachingTestCase.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ParameterizedClassInfoClassLoaderArgumentsTest.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ParameterizedClassInfoClassLoaderCachingTest.java
Modified:
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistParameterizedClassInfo.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/ClassInfoTestSuite.java
Log:
[JBREFLECT-5] Add tests for parameterized types with different classloaders - the caching ones still fail

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistParameterizedClassInfo.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistParameterizedClassInfo.java	2010-04-27 15:37:42 UTC (rev 104269)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistParameterizedClassInfo.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -59,6 +59,9 @@
    /** Utility to determine the actual bounds of generic type variables */
    private final JavassistTypeVariableSpy spy;
    
+   /** The classloader used to create this parameterized type, it is used to load up the generic info */
+   private final ClassLoader classLoader;
+   
    /**
     * Constructor
     * 
@@ -67,9 +70,9 @@
     * @param typeArguments the javassist generic type arguments
     * @param spy used to determine the actual bounds of generic type variables
     */
-   JavassistParameterizedClassInfo(JavassistTypeInfoFactoryImpl factory, ClassInfo delegate, TypeArgument[] typeArguments, JavassistTypeVariableSpy spy)
+   JavassistParameterizedClassInfo(JavassistTypeInfoFactoryImpl factory, ClassInfo delegate, ClassLoader cl, TypeArgument[] typeArguments, JavassistTypeVariableSpy spy)
    {
-      this(factory, delegate, typeArguments, spy, ClassInfoImpl.UNKNOWN_TYPES);
+      this(factory, delegate, cl, typeArguments, spy, ClassInfoImpl.UNKNOWN_TYPES);
    }
    
    /**
@@ -81,16 +84,17 @@
     */
    public JavassistParameterizedClassInfo(JavassistTypeInfoFactoryImpl factory, ClassInfo delegate, TypeInfo[] typeArgumentInfos)
    {
-      this(factory, delegate, null, null, typeArgumentInfos);
+      this(factory, delegate, null, null, null, typeArgumentInfos);
    }
    
-   private JavassistParameterizedClassInfo(JavassistTypeInfoFactoryImpl factory, ClassInfo delegate, TypeArgument[] typeArguments, JavassistTypeVariableSpy spy, TypeInfo[] typeArgumentInfos)
+   private JavassistParameterizedClassInfo(JavassistTypeInfoFactoryImpl factory, ClassInfo delegate, ClassLoader cl, TypeArgument[] typeArguments, JavassistTypeVariableSpy spy, TypeInfo[] typeArgumentInfos)
    {
       super(delegate);
       this.factory = factory;
       this.typeArguments = typeArguments;
       this.spy = spy;
       this.typeArgumentInfos = typeArgumentInfos;
+      this.classLoader = cl;
    }
    
    @Override
@@ -100,6 +104,12 @@
    }
 
    @Override
+   public ClassLoader getClassLoader()
+   {
+      return classLoader;
+   }
+
+   @Override
    public TypeInfo[] getActualTypeArguments()
    {
       if (typeArgumentInfos == ClassInfoImpl.UNKNOWN_TYPES && typeArguments != null && typeArguments.length > 0)
@@ -107,12 +117,11 @@
          if (delegate instanceof JavassistTypeInfo == false)
             throw new IllegalStateException("Delegate is not a javassist one");
 
-         ClassLoader cl = ((JavassistTypeInfo)delegate).getClassLoaderInternal();
          TypeInfo[] infos = new TypeInfo[typeArguments.length];
          
          for (int i = 0 ; i < typeArguments.length ; i++)
          {
-            infos[i] = factory.createTypeInfoForTypeArgument(typeArguments[i], cl, spy);
+            infos[i] = factory.createTypeInfoForTypeArgument(typeArguments[i], classLoader, spy);
          }
          typeArgumentInfos = infos;
       }

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-27 15:37:42 UTC (rev 104269)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -28,6 +28,8 @@
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
 import java.lang.reflect.WildcardType;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -549,7 +551,10 @@
             throw new RuntimeException(e);
       }
 
-      
+      ClassLoader realLoader = getClassLoader(result);
+      if (realLoader != cl)
+         classLoaderCache = getClassLoaderCache(realLoader);
+         
       WeakReference<TypeInfo>weak = new WeakReference<TypeInfo>(result);
       classLoaderCache.put(name, weak);
       
@@ -558,6 +563,23 @@
       
       return result;
    }
+   
+   private ClassLoader getClassLoader(final TypeInfo info)
+   {
+      if (System.getSecurityManager() == null)
+         return info.getClassLoader();
+      
+      if (info instanceof JavassistTypeInfo)
+         return ((JavassistTypeInfo)info).getClassLoaderInternal();
+      
+      return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+      {
+         public ClassLoader run()
+         {
+            return info.getClassLoader();
+         }
+      });
+   }
 
    /**
     * Proxies, whether
@@ -614,8 +636,15 @@
 
       //Extra code - END
       
-      IntrospectionTypeInfoFactory factory = new IntrospectionTypeInfoFactory();
-      return factory.getTypeInfo(name, cl);
+      try
+      {
+         IntrospectionTypeInfoFactory factory = new IntrospectionTypeInfoFactory();
+         return factory.getTypeInfo(name, cl);
+      }
+      catch (ClassNotFoundException e)
+      {
+         throw e;
+      }
    }
    
    /**
@@ -813,7 +842,8 @@
    /**
     * Gets the type info for a javassist generic class type
     * 
-    * @param loader the class loader
+    * @param loader the class loader of the raw class
+    * @param parameterloaders the class of the
     * @param type the class type
     * @param spy used to determine actual types of type variables
     * @return the type info
@@ -851,7 +881,7 @@
          ClassInfo delegate = (ClassInfo)get(JavassistHelper.getClassNameForGenericType(type), loader);
          if (!isParameterized)
             return delegate;
-         info = new JavassistParameterizedClassInfo(this, delegate, type.getTypeArguments(), spy);
+         info = new JavassistParameterizedClassInfo(this, delegate, loader, type.getTypeArguments(), spy);
       }
       catch (ClassNotFoundException e)
       {

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/GenericInterface.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/GenericInterface.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/GenericInterface.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,32 @@
+/*
+* 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.support;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public interface GenericInterface<T>
+{
+
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/GenericSuperclass.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/GenericSuperclass.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/GenericSuperclass.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,32 @@
+/*
+* 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.support;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class GenericSuperclass<T>
+{
+
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/AbstractParameterizedClassLoaderTest.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/AbstractParameterizedClassLoaderTest.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/AbstractParameterizedClassLoaderTest.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,338 @@
+/*
+* 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.io.File;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtConstructor;
+import javassist.CtField;
+import javassist.CtMethod;
+import javassist.LoaderClassPath;
+import javassist.Modifier;
+import javassist.bytecode.SignatureAttribute;
+
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactoryImpl;
+import org.jboss.reflect.plugins.javassist.classpool.ClassPoolFactory;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.TypeInfo;
+import org.jboss.reflect.spi.TypeInfoFactory;
+import org.jboss.test.AbstractTestDelegate;
+import org.jboss.test.ContainerTest;
+import org.jboss.test.classinfo.support.GenericInterface;
+import org.jboss.test.classinfo.support.GenericSuperclass;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class AbstractParameterizedClassLoaderTest extends ContainerTest
+{
+   static final String FIELD_NAME = "field";
+
+   static final String METHOD_NAME = "signature";
+   
+   File file;
+   ClassLoader loader;
+   ClassPool pool;
+
+   public AbstractParameterizedClassLoaderTest(String name)
+   {
+      super(name);
+   }
+
+   public static AbstractTestDelegate getDelegate(Class<?> clazz) throws Exception
+   {
+      AbstractTestDelegate delegate = new ClassPoolTestDelegate(clazz);
+      return delegate;
+   }
+
+   @Override
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      file = createTempDir();
+      loader = new URLClassLoader(new URL[] {file.toURI().toURL()}, ClassLoader.getSystemClassLoader());
+      pool = MockClassPoolFactory.INSTANCE.registerClassLoader(loader);
+   }
+
+   @Override
+   protected void tearDown() throws Exception
+   {
+      deleteFile(file);
+      super.tearDown();
+   }
+   
+   protected Class<?> assertSingleActualTypeArgument(ParameterizedType type)
+   {
+      Type[] args = type.getActualTypeArguments();
+      assertNotNull(args);
+      assertEquals(1, args.length);
+      assertNotNull(args[0]);
+      return assertInstanceOf(args[0], Class.class);
+   }
+   
+   protected ClassInfo assertSingleActualTypeArgument(ClassInfo info)
+   {
+      TypeInfo[] args = info.getActualTypeArguments();  
+      assertNotNull(args);
+      assertEquals(1, args.length);
+      assertNotNull(args[0]);
+      return assertInstanceOf(args[0], ClassInfo.class);
+   }
+
+   protected File createTempDir() throws IOException
+   {
+      File file = File.createTempFile("temp", Long.toString(System.nanoTime()));
+
+      if(!(file.delete()))
+          throw new IOException("Could not delete temp file: " + file.getAbsolutePath());
+      if(!(file.mkdir()))
+          throw new IOException("Could not create temp directory: " + file.getAbsolutePath());
+
+      return file;
+   }
+   
+   protected void deleteFile(File file)
+   {
+      if (file == null)
+         return;
+      if (file.isDirectory())
+      {
+         String[] files = file.list();
+         if (files != null)
+         {
+            for (String child : files)
+            {
+               deleteFile(new File(file, child));
+            }
+         }
+      }
+      if (!file.delete())
+         getLog().warn("Could not delete file " + file.getAbsolutePath());
+   }
+   
+   protected Class<?> createTestClasses(ClassPool pool) throws Exception
+   {
+      CtClass valueClass = pool.makeClass(getValueClassName());
+      valueClass.toClass();
+      
+      CtClass set = SystemClassPool.INSTANCE.get(Set.class.getName());
+      
+      CtClass signatureClass = pool.makeClass(getSignatureClassName());
+      
+      signatureClass.setSuperclass(pool.get(GenericSuperclass.class.getName()));
+      signatureClass.setInterfaces(new CtClass[] {pool.get(GenericInterface.class.getName())});
+      SignatureAttribute attribute = new SignatureAttribute(signatureClass.getClassFile().getConstPool(), getClassSignature());
+      signatureClass.getClassFile().addAttribute(attribute);
+      
+      
+      CtMethod method = new CtMethod(set, METHOD_NAME, new CtClass[] {set}, signatureClass);
+      method.setModifiers(Modifier.PUBLIC);
+      method.setBody("{return null;}");
+      attribute = new SignatureAttribute(method.getMethodInfo().getConstPool(), getMethodSignature());
+      method.getMethodInfo().addAttribute(attribute);
+      signatureClass.addMethod(method);
+      
+      CtConstructor ctor = new CtConstructor(new CtClass[] {set}, signatureClass);
+      ctor.setModifiers(Modifier.PUBLIC);
+      ctor.setBody("{}");
+      attribute = new SignatureAttribute(ctor.getMethodInfo().getConstPool(), getConstructorSignature());
+      ctor.getMethodInfo().addAttribute(attribute);
+      signatureClass.addConstructor(ctor);
+      
+      CtField field = new CtField(set, FIELD_NAME, signatureClass);
+      field.setModifiers(Modifier.PUBLIC);
+      attribute = new SignatureAttribute(field.getFieldInfo().getConstPool(), getFieldSignature());
+      field.getFieldInfo().addAttribute(attribute);
+      signatureClass.addField(field);
+      
+      //signatureClass.debugWriteFile();
+      Class<?> clazz = signatureClass.toClass();
+
+      return clazz;
+   }
+
+   protected Method getMethod(Class<?> clazz) throws Exception
+   {
+      if (!clazz.getName().equals(getSignatureClassName()))
+         fail("Wrong class passed in");
+      return clazz.getDeclaredMethod(METHOD_NAME, Set.class);
+   }
+   
+   protected Field getField(Class<?> clazz) throws Exception
+   {
+      if (!clazz.getName().equals(getSignatureClassName()))
+         fail("Wrong class passed in");
+      return clazz.getDeclaredField(FIELD_NAME);
+   }
+   
+   protected Constructor<?> getConstructor(Class<?> clazz) throws Exception
+   {
+      if (!clazz.getName().equals(getSignatureClassName()))
+         fail("Wrong class passed in");
+      return clazz.getDeclaredConstructor(Set.class);
+   }
+   
+   protected String getValueClassName()
+   {
+      return this.getClass().getName() + "Value";
+   }
+   
+   protected String getSignatureClassName()
+   {
+      return this.getClass().getName() + "WithSignature";
+   }
+
+   protected String getClassSignature()
+   {
+      return "L" + GenericSuperclass.class.getName().replace('.', '/') + 
+      "<L" + getValueClassName().replace('.', '/') + ";>;" +
+      "L" + GenericInterface.class.getName().replace('.', '/') + 
+      "<L" + getValueClassName().replace('.', '/') + ";>;";
+   }
+
+   protected String getMethodSignature()
+   {
+      String parameterizedSet = getParameterizedSetForSignature();
+      String sig = "(" + parameterizedSet + ")" + parameterizedSet;
+      return sig;
+   }
+   
+   private String getConstructorSignature()
+   {
+      return "(" + getParameterizedSetForSignature() + ")V"; 
+   }
+   
+   private String getFieldSignature()
+   {
+      return getParameterizedSetForSignature();
+   }
+   
+   private String getParameterizedSetForSignature()
+   {
+      return "Ljava/util/Set<L" + getValueClassName().replace('.', '/') + ";>;";
+   }
+   
+   protected abstract TypeInfoFactory getTypeInfoFactory();
+   
+   protected static class MockClassPoolFactory implements ClassPoolFactory
+   {
+      static final MockClassPoolFactory INSTANCE = new MockClassPoolFactory();
+      
+      Map<ClassLoader, ClassPool> pools = new HashMap<ClassLoader, ClassPool>();
+      
+      ClassPool registerClassLoader(ClassLoader cl)
+      {
+         ClassPool pool = new ClassLoaderAwareClassPool(SystemClassPool.INSTANCE, cl);
+         pools.put(cl, pool);
+         return pool;
+      }
+      
+      public ClassPool getPoolForLoader(ClassLoader classLoader)
+      {
+         ClassPool pool = pools.get(classLoader);
+         if (pool != null)
+            return pool;
+         return SystemClassPool.INSTANCE;
+      }
+   }
+
+   protected static class SystemClassPool extends ClassPool
+   {
+      private static final SystemClassPool INSTANCE = new SystemClassPool();
+      
+      public static final SystemClassPool getInstance()
+      {
+         return INSTANCE;
+      }
+      
+      private SystemClassPool()
+      {
+         super(null);
+         appendSystemPath();
+      }
+      
+      @Override
+      public ClassLoader getClassLoader()
+      {
+         return ClassLoader.getSystemClassLoader();
+      }
+   }
+   
+   
+   protected static class ClassLoaderAwareClassPool extends ClassPool
+   {
+      ClassLoader cl;
+      public ClassLoaderAwareClassPool(ClassPool parent, ClassLoader cl)
+      {
+         super(parent);
+         this.cl = cl;
+         appendClassPath(new LoaderClassPath(cl));
+      }
+
+      public ClassLoader getClassLoader()
+      {
+         return cl;
+      }
+   }
+   
+   protected static class ClassPoolTestDelegate extends AbstractTestDelegate
+   {
+      ClassPoolFactory factory = JavassistTypeInfoFactoryImpl.getPoolFactory();
+      
+      public ClassPoolTestDelegate(Class<?> clazz)
+      {
+         super(clazz);
+      }
+
+      @Override
+      public void setUp() throws Exception
+      {
+         super.setUp();
+         JavassistTypeInfoFactoryImpl.setPoolFactory(MockClassPoolFactory.INSTANCE);
+      }
+
+      @Override
+      public void tearDown() throws Exception
+      {
+         if (factory == null)
+            JavassistTypeInfoFactoryImpl.setPoolFactory(factory);
+         super.tearDown();
+      }
+      
+   }
+
+}

Modified: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoTestSuite.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoTestSuite.java	2010-04-27 15:37:42 UTC (rev 104269)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoTestSuite.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -68,6 +68,10 @@
       suite.addTest(JavassistClassInfoGenericMembersTestCase.suite());
       suite.addTest(IntrospectionClassInfoProxyTestCase.suite());
       suite.addTest(JavassistClassInfoProxyTestCase.suite());
+      suite.addTest(IntrospectionParameterizedClassInfoClassLoaderCachingTestCase.suite());
+      suite.addTest(JavassistParameterizedClassInfoClassLoaderCachingTestCase.suite());
+      suite.addTest(IntrospectionParameterizedClassInfoClassLoaderArgumentsTestCase.suite());
+      suite.addTest(JavassistParameterizedClassInfoClassLoaderArgumentsTestCase.suite());
 
       return suite;
    }

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionParameterizedClassInfoClassLoaderArgumentsTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionParameterizedClassInfoClassLoaderArgumentsTestCase.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionParameterizedClassInfoClassLoaderArgumentsTestCase.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,55 @@
+/*
+* 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 junit.framework.Test;
+
+import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
+import org.jboss.reflect.spi.TypeInfoFactory;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class IntrospectionParameterizedClassInfoClassLoaderArgumentsTestCase
+      extends
+         ParameterizedClassInfoClassLoaderArgumentsTest
+{
+
+   public IntrospectionParameterizedClassInfoClassLoaderArgumentsTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(IntrospectionParameterizedClassInfoClassLoaderArgumentsTestCase.class);
+   }
+
+   @Override
+   protected TypeInfoFactory getTypeInfoFactory()
+   {
+      return new IntrospectionTypeInfoFactory();
+   }
+
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionParameterizedClassInfoClassLoaderCachingTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionParameterizedClassInfoClassLoaderCachingTestCase.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionParameterizedClassInfoClassLoaderCachingTestCase.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,52 @@
+/*
+* 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 junit.framework.Test;
+
+import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
+import org.jboss.reflect.spi.TypeInfoFactory;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class IntrospectionParameterizedClassInfoClassLoaderCachingTestCase extends ParameterizedClassInfoClassLoaderCachingTest
+{
+   public IntrospectionParameterizedClassInfoClassLoaderCachingTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(IntrospectionParameterizedClassInfoClassLoaderCachingTestCase.class);
+   }
+
+   @Override
+   protected TypeInfoFactory getTypeInfoFactory()
+   {
+      return new IntrospectionTypeInfoFactory();
+   }
+
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistParameterizedClassInfoClassLoaderArgumentsTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistParameterizedClassInfoClassLoaderArgumentsTestCase.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistParameterizedClassInfoClassLoaderArgumentsTestCase.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,55 @@
+/*
+* 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 junit.framework.Test;
+
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactory;
+import org.jboss.reflect.spi.TypeInfoFactory;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class JavassistParameterizedClassInfoClassLoaderArgumentsTestCase
+      extends
+         ParameterizedClassInfoClassLoaderArgumentsTest
+{
+
+   public JavassistParameterizedClassInfoClassLoaderArgumentsTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(JavassistParameterizedClassInfoClassLoaderArgumentsTestCase.class);
+   }
+
+   @Override
+   protected TypeInfoFactory getTypeInfoFactory()
+   {
+      return new JavassistTypeInfoFactory();
+   }
+
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistParameterizedClassInfoClassLoaderCachingTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistParameterizedClassInfoClassLoaderCachingTestCase.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistParameterizedClassInfoClassLoaderCachingTestCase.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,51 @@
+/*
+* 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 junit.framework.Test;
+
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactory;
+import org.jboss.reflect.spi.TypeInfoFactory;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class JavassistParameterizedClassInfoClassLoaderCachingTestCase extends ParameterizedClassInfoClassLoaderCachingTest
+{
+   public JavassistParameterizedClassInfoClassLoaderCachingTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(JavassistParameterizedClassInfoClassLoaderCachingTestCase.class);
+   }
+
+   @Override
+   protected TypeInfoFactory getTypeInfoFactory()
+   {
+      return new JavassistTypeInfoFactory();
+   }
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ParameterizedClassInfoClassLoaderArgumentsTest.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ParameterizedClassInfoClassLoaderArgumentsTest.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ParameterizedClassInfoClassLoaderArgumentsTest.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,150 @@
+/*
+* 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.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Set;
+
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.ConstructorInfo;
+import org.jboss.reflect.spi.FieldInfo;
+import org.jboss.reflect.spi.MethodInfo;
+import org.jboss.reflect.spi.TypeInfo;
+import org.jboss.test.AbstractTestDelegate;
+import org.jboss.test.classinfo.support.GenericInterface;
+import org.jboss.test.classinfo.support.GenericSuperclass;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class ParameterizedClassInfoClassLoaderArgumentsTest extends AbstractParameterizedClassLoaderTest
+{
+   public ParameterizedClassInfoClassLoaderArgumentsTest(String name)
+   {
+      super(name);
+   }
+
+   public static AbstractTestDelegate getDelegate(Class<?> clazz) throws Exception
+   {
+      AbstractTestDelegate delegate = new ClassPoolTestDelegate(clazz);
+      return delegate;
+   }
+
+   public void testParameterizedClassInfoWithArgumentsFromChildLoader() throws Throwable
+   {
+      Class<?> clazz = createTestClasses(pool);
+      assertRawReflect(clazz);
+      
+      ClassInfo info = assertInstanceOf(getTypeInfoFactory().getTypeInfo(clazz), ClassInfo.class);
+
+      TypeInfo valueClass = getTypeInfoFactory().getTypeInfo(getValueClassName(), loader);
+      
+      //Check superclass and interfaces
+      assertParameterizedTypeInfo(info.getGenericSuperclass(), getTypeInfoFactory().getTypeInfo(GenericSuperclass.class));
+      assertEquals(1, info.getGenericInterfaces().length);
+      assertParameterizedTypeInfo(info.getGenericInterfaces()[0], getTypeInfoFactory().getTypeInfo(GenericInterface.class));
+      
+      //Check method
+      TypeInfo[] params = new TypeInfo[] {getTypeInfoFactory().getTypeInfo(Set.class)};
+      MethodInfo minfo = info.getDeclaredMethod(METHOD_NAME, params);  
+      assertNotNull(minfo);
+      assertParameterizedTypeInfo(minfo.getReturnType(), getTypeInfoFactory().getTypeInfo(Set.class));
+      assertParameterizedParameters(minfo.getParameterTypes());
+      
+      //Check constructor
+      ConstructorInfo cinfo = info.getDeclaredConstructor(params);
+      assertNotNull(cinfo);
+      assertParameterizedParameters(cinfo.getParameterTypes());
+      
+      //Check field
+      FieldInfo finfo = info.getDeclaredField(FIELD_NAME);
+      assertNotNull(finfo);
+      assertParameterizedTypeInfo(finfo.getType(), getTypeInfoFactory().getTypeInfo(Set.class));
+   }
+   
+   private void assertParameterizedParameters(TypeInfo[] params) throws Exception
+   {
+      assertEquals(1, params.length);
+      assertParameterizedTypeInfo(params[0], getTypeInfoFactory().getTypeInfo(Set.class));
+   }
+   
+   private void assertParameterizedTypeInfo(TypeInfo actual, TypeInfo expectedRawType) throws Exception
+   {
+      ClassInfo actualClassInfo = assertInstanceOf(actual, ClassInfo.class);
+      ClassInfo raw = assertInstanceOf(actualClassInfo.getRawType(), ClassInfo.class);
+      assertSame(expectedRawType, raw);
+      if (raw.getClassLoader() != null)
+         assertSame(ClassLoader.getSystemClassLoader(), raw.getClassLoader());
+      
+      ClassInfo arg = assertSingleActualTypeArgument(actualClassInfo);
+      assertEquals(getValueClassName(), arg.getName());
+      assertSame(getTypeInfoFactory().getTypeInfo(getValueClassName(), loader), arg);
+      assertSame(loader, arg.getClassLoader());
+      
+      assertNotSame(arg.getClassLoader(), raw.getClassLoader());
+   }
+   
+   private void assertRawReflect(Class<?> clazz) throws Exception
+   {
+      Method method = getMethod(clazz);
+      Constructor<?> ctor = getConstructor(clazz);
+      Field field = getField(clazz);
+      
+      assertRawParameterizedType(clazz.getGenericSuperclass(), GenericSuperclass.class);
+      assertEquals(1, clazz.getGenericInterfaces().length);
+      assertRawParameterizedType(clazz.getGenericInterfaces()[0], GenericInterface.class);
+      
+      assertRawParameterizedType(method.getGenericReturnType(), Set.class);
+      assertRawParameters(ctor.getGenericParameterTypes());
+      assertRawParameters(ctor.getGenericParameterTypes());
+      assertRawParameterizedType(field.getGenericType(), Set.class);
+   }
+   
+   private void assertRawParameters(Type[] parameters) throws Exception
+   {
+      assertEquals(1, parameters.length);
+      assertRawParameterizedType(parameters[0], Set.class);
+   }
+   
+   private void assertRawParameterizedType(Type type, Class<?> expectedRawType) throws Exception
+   {
+      ParameterizedType ptype = assertInstanceOf(type, ParameterizedType.class);
+      Class<?> raw = assertInstanceOf(ptype.getRawType(), Class.class);
+      assertSame(expectedRawType, raw);
+      if (raw.getClassLoader() != null)
+         assertSame(ClassLoader.getSystemClassLoader(), raw.getClassLoader());
+      
+      Class<?> arg = assertSingleActualTypeArgument(ptype);
+      assertEquals(getValueClassName(), arg.getName());
+      assertSame(loader.loadClass(getValueClassName()), arg);
+      assertSame(loader, arg.getClassLoader());
+      
+      assertNotSame(arg.getClassLoader(), raw.getClassLoader());
+      
+   }
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ParameterizedClassInfoClassLoaderCachingTest.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ParameterizedClassInfoClassLoaderCachingTest.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ParameterizedClassInfoClassLoaderCachingTest.java	2010-04-27 16:48:43 UTC (rev 104270)
@@ -0,0 +1,110 @@
+/*
+* 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.io.File;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Set;
+
+import javassist.ClassPool;
+
+import org.jboss.reflect.spi.ClassInfo;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class ParameterizedClassInfoClassLoaderCachingTest extends AbstractParameterizedClassLoaderTest
+{   
+   File fileB;
+   ClassLoader loaderB;
+   ClassPool poolB;
+   
+   public ParameterizedClassInfoClassLoaderCachingTest(String name)
+   {
+      super(name);
+   }
+
+   @Override
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      fileB = createTempDir();
+      loaderB = new URLClassLoader(new URL[] {fileB.toURI().toURL()}, ClassLoader.getSystemClassLoader());
+      poolB = MockClassPoolFactory.INSTANCE.registerClassLoader(loaderB);
+   }
+
+   @Override
+   protected void tearDown() throws Exception
+   {
+      deleteFile(file);
+      deleteFile(fileB);
+      super.tearDown();
+   }
+
+   public void testParameterizedClassInfoWithArgumentsFromChildLoader() throws Throwable
+   {
+      Method methodA = getTestMethod(pool);
+      Type typeA = methodA.getGenericReturnType();
+      ParameterizedType ptypeA = assertInstanceOf(typeA, ParameterizedType.class);
+      
+      Method methodB = getTestMethod(poolB);
+      Type typeB = methodB.getGenericReturnType();
+      ParameterizedType ptypeB = assertInstanceOf(typeB, ParameterizedType.class);
+      
+      assertRawReflect(ptypeA, ptypeB);
+      
+      ClassInfo classInfoA = (ClassInfo)getTypeInfoFactory().getTypeInfo(ptypeA);
+      ClassInfo classInfoB = (ClassInfo)getTypeInfoFactory().getTypeInfo(ptypeB);
+      
+      assertNotNull(classInfoA.getRawType());
+      assertSame(getTypeInfoFactory().getTypeInfo(Set.class), classInfoA.getRawType());
+      assertSame(classInfoA.getRawType(), classInfoB.getRawType());
+
+      ClassInfo argA = assertSingleActualTypeArgument(classInfoA);
+      ClassInfo argB = assertSingleActualTypeArgument(classInfoB);
+      assertEquals(loader, argA.getClassLoader());
+      assertEquals(loaderB, argB.getClassLoader());
+      assertNotSame(argA, argB);
+   }
+   
+   private Method getTestMethod(ClassPool pool) throws Exception
+   {
+      return getMethod(createTestClasses(pool));
+   }
+   
+   private void assertRawReflect(ParameterizedType ptypeA, ParameterizedType ptypeB)
+   {
+      assertNotSame(ptypeA, ptypeB);
+      assertSame(ptypeA.getRawType(), ptypeB.getRawType());
+      Class<?> clazzA = assertSingleActualTypeArgument(ptypeA);
+      Class<?> clazzB = assertSingleActualTypeArgument(ptypeB);
+      assertEquals(loader, clazzA.getClassLoader());
+      assertEquals(loaderB, clazzB.getClassLoader());
+      assertNotSame(clazzA, clazzB);
+   }
+}




More information about the jboss-cvs-commits mailing list