[jboss-cvs] JBossAS SVN: r103273 - 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 Mar 30 13:25:32 EDT 2010


Author: kabir.khan at jboss.com
Date: 2010-03-30 13:25:30 -0400 (Tue, 30 Mar 2010)
New Revision: 103273

Added:
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/JavassistProxyMethodHandler.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/JdkProxyInvocationHandler.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/ProxyInterface.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoProxyTest.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionClassInfoProxyTestCase.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistClassInfoProxyTestCase.java
Modified:
   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-112] When looking up dynamic proxies and other classes for which there is no resource in the classloader in the javassist type info factory, fall back to the introspection implementation

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-03-30 16:34:29 UTC (rev 103272)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java	2010-03-30 17:25:30 UTC (rev 103273)
@@ -49,6 +49,7 @@
 import org.jboss.reflect.plugins.AnnotationValueImpl;
 import org.jboss.reflect.plugins.EnumConstantInfoImpl;
 import org.jboss.reflect.plugins.GenericsUtil;
+import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
 import org.jboss.reflect.plugins.javassist.classpool.ClassPoolFactory;
 import org.jboss.reflect.plugins.javassist.classpool.DefaultClassPoolFactory;
 import org.jboss.reflect.spi.AnnotationInfo;
@@ -284,11 +285,33 @@
       }
       catch(NotFoundException nfe)
       {
-         throw new ClassNotFoundException(nfe.getMessage());
+         return delegateToIntrospectionImplementation(cl, name);
       }
    }
    
    /**
+    * Proxies, whether
+    * <ul>
+    * <li>JDK dynamic proxies</li>
+    * <li>javassist ProxyFactory proxies - created by calling ClassLoader.defineClass()</li>
+    * </ul> 
+    * are not visible to the javassist classpools, and neither will classes generated by cglib or other
+    * frameworks, so try to load up the class from the reflect implementation.
+    * 
+    * @param cl the classloader
+    * @param name the name of the class
+    * @return the info
+    * @throws ClassNotFoundException when the class cannot be found
+    */
+   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);
+   }
+   
+   /**
     * Get the information for a class
     * 
     * 

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/JavassistProxyMethodHandler.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/JavassistProxyMethodHandler.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/JavassistProxyMethodHandler.java	2010-03-30 17:25:30 UTC (rev 103273)
@@ -0,0 +1,49 @@
+/*
+* 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;
+
+import java.lang.reflect.Method;
+
+import javassist.util.proxy.MethodHandler;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class JavassistProxyMethodHandler implements MethodHandler
+{
+   public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
+   {
+      String name = thisMethod.getName();
+      if (name.equals("echo"))
+         return args[0];
+      if (name.equals("equals"))
+         return Boolean.FALSE;
+      if (name.equals("hashCode"))
+         return 6;
+      if (name.equals("toString"))
+         return this.toString();
+      return null;
+   }
+   
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/JdkProxyInvocationHandler.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/JdkProxyInvocationHandler.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/JdkProxyInvocationHandler.java	2010-03-30 17:25:30 UTC (rev 103273)
@@ -0,0 +1,49 @@
+/*
+* 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;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class JdkProxyInvocationHandler implements InvocationHandler
+{
+
+   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+   {
+      String name = method.getName();
+      if (name.equals("echo"))
+         return args[0];
+      if (name.equals("equals"))
+         return Boolean.FALSE;
+      if (name.equals("hashCode"))
+         return 6;
+      if (name.equals("toString"))
+         return this.toString();
+      return null;
+   }
+
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/ProxyInterface.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/ProxyInterface.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/ProxyInterface.java	2010-03-30 17:25:30 UTC (rev 103273)
@@ -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 ProxyInterface
+{
+   String echo(String s);
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoProxyTest.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoProxyTest.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoProxyTest.java	2010-03-30 17:25:30 UTC (rev 103273)
@@ -0,0 +1,116 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.Proxy;
+
+import javassist.util.proxy.ProxyFactory;
+
+import org.jboss.reflect.plugins.ClassInfoImpl;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.MethodInfo;
+import org.jboss.reflect.spi.ModifierInfo;
+import org.jboss.reflect.spi.TypeInfo;
+import org.jboss.test.classinfo.support.ClassInfoGenericFieldsInterface;
+import org.jboss.test.classinfo.support.JavassistProxyMethodHandler;
+import org.jboss.test.classinfo.support.JdkProxyInvocationHandler;
+import org.jboss.test.classinfo.support.ProxyInterface;
+
+/**
+ * ClassInfoProxyTest.
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class ClassInfoProxyTest extends AbstractClassInfoTest
+{
+   public ClassInfoProxyTest(String name)
+   {
+      super(name);
+   }
+
+   public void testJdkDynamicProxy() throws Throwable
+   {
+      Object o = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] {ProxyInterface.class}, new JdkProxyInvocationHandler());
+      checkProxy(o);
+   }
+   
+   public void testJavassistProxy() throws Throwable
+   {
+      ProxyFactory proxyFactory = new ProxyFactory();
+      proxyFactory.setInterfaces(new Class[] {ProxyInterface.class});
+      proxyFactory.setHandler(new JavassistProxyMethodHandler());
+      Class<?> clazz = proxyFactory.createClass();
+      Object o = clazz.newInstance();
+      checkProxy(o);
+   }
+   
+   /**
+    * The javassist implementation now tries to load the class from the introspection implementation if it can
+    * not be found. This is just a sanity check to make sure that classes that are definitely not there 
+    * fail
+    */
+   public void testUnknownClass() throws Throwable
+   {
+      try
+      {
+         getTypeInfoFactory().getTypeInfo("does.not.Exist", Thread.currentThread().getContextClassLoader());
+         fail("Should not have found the class");
+      }
+      catch (Exception expected)
+      {
+      }
+   }
+   
+   private void checkProxy(Object proxyObject) throws Throwable
+   {
+      ProxyInterface proxy = assertInstanceOf(proxyObject, ProxyInterface.class);
+      assertEquals("test", proxy.echo("test"));
+      
+      TypeInfo type = getTypeInfoFactory().getTypeInfo(proxy.getClass());
+      assertNotNull(type);
+      ClassInfo info = assertInstanceOf(type, ClassInfo.class);
+      MethodInfo echo = null;
+      for (MethodInfo minfo : info.getDeclaredMethods())
+      {
+         if (minfo.getName().equals("echo"))
+         {
+            echo = minfo;
+            break;
+         }
+      }
+      assertEquals("test", echo.invoke(proxy, new Object[] {"test"}));
+   }
+   
+   private void testGenericInterface(Class<?> clazz) throws Throwable
+   {
+      ClassInfoImpl expected = new ClassInfoImpl(clazz.getName(), ModifierInfo.PUBLIC);
+      TypeInfo info = testBasics(clazz, expected);
+      
+      assertFalse(info.isArray());
+      assertFalse(info.isEnum());
+      assertFalse(info.isPrimitive());
+      
+      ClassInfo classInfo = (ClassInfo) info;
+      assertClassInfo(classInfo, clazz);
+   }
+}

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-03-30 16:34:29 UTC (rev 103272)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoTestSuite.java	2010-03-30 17:25:30 UTC (rev 103273)
@@ -66,6 +66,8 @@
       suite.addTest(MethodAccessRestrictionTestCase.suite());
       suite.addTest(IntrospectionClassInfoGenericMembersTestCase.suite());
       suite.addTest(JavassistClassInfoGenericMembersTestCase.suite());
+      suite.addTest(IntrospectionClassInfoProxyTestCase.suite());
+      suite.addTest(JavassistClassInfoProxyTestCase.suite());
 
       return suite;
    }

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionClassInfoProxyTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionClassInfoProxyTestCase.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionClassInfoProxyTestCase.java	2010-03-30 17:25:30 UTC (rev 103273)
@@ -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 IntrospectionClassInfoProxyTestCase extends ClassInfoProxyTest
+{
+   public static Test suite()
+   {
+      return suite(IntrospectionClassInfoProxyTestCase.class);
+   }
+
+   public IntrospectionClassInfoProxyTestCase(String name)
+   {
+      super(name);
+   }
+
+   @Override
+   protected TypeInfoFactory getTypeInfoFactory()
+   {
+      return new IntrospectionTypeInfoFactory();
+   }
+
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistClassInfoProxyTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistClassInfoProxyTestCase.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistClassInfoProxyTestCase.java	2010-03-30 17:25:30 UTC (rev 103273)
@@ -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.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 JavassistClassInfoProxyTestCase extends ClassInfoProxyTest
+{
+   public static Test suite()
+   {
+      return suite(JavassistClassInfoProxyTestCase.class);
+   }
+
+   public JavassistClassInfoProxyTestCase(String name)
+   {
+      super(name);
+   }
+
+   @Override
+   protected TypeInfoFactory getTypeInfoFactory()
+   {
+      return new JavassistTypeInfoFactory();
+   }
+
+}




More information about the jboss-cvs-commits mailing list