[jboss-cvs] JBossAS SVN: r82699 - in projects/aop/trunk/aop/src: test/org/jboss/test/aop and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jan 8 09:23:54 EST 2009


Author: stalep
Date: 2009-01-08 09:23:54 -0500 (Thu, 08 Jan 2009)
New Revision: 82699

Added:
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/JavassistAopTypeInfoFactoryImplTestCase.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/POJO.java
Modified:
   projects/aop/trunk/aop/src/main/org/jboss/aop/reflectprototype/JavassistAopTypeInfoFactoryImpl.java
Log:
[JBAOP-684]
JavassistAopTypeInfoFactoryImplTestCase should not load the class now when trying
to create a ClassInfo object.
Added a simple test to verify the changes


Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/reflectprototype/JavassistAopTypeInfoFactoryImpl.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/reflectprototype/JavassistAopTypeInfoFactoryImpl.java	2009-01-08 14:23:45 UTC (rev 82698)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/reflectprototype/JavassistAopTypeInfoFactoryImpl.java	2009-01-08 14:23:54 UTC (rev 82699)
@@ -22,12 +22,26 @@
 package org.jboss.aop.reflectprototype;
 
 import java.lang.ref.WeakReference;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 
 import javassist.CtClass;
+import javassist.CtField;
+import javassist.CtMethod;
 import javassist.NotFoundException;
 
+import org.jboss.reflect.plugins.AnnotationAttributeImpl;
+import org.jboss.reflect.plugins.EnumConstantInfoImpl;
+import org.jboss.reflect.plugins.javassist.JavassistAnnotationInfo;
+import org.jboss.reflect.plugins.javassist.JavassistArrayInfoImpl;
+import org.jboss.reflect.plugins.javassist.JavassistEnumInfo;
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfo;
 import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactoryImpl;
+import org.jboss.reflect.spi.AnnotationValue;
+import org.jboss.reflect.spi.NumberInfo;
+import org.jboss.reflect.spi.PrimitiveInfo;
+import org.jboss.reflect.spi.TypeInfo;
 
 /**
  * A JavassistAopTypeInfoFactoryImpl.
@@ -90,7 +104,7 @@
       }
 
 //      how do we call instantiate?
-      Object result = null; //instantiate(clazz);
+      Object result = instantiate(clazz);
       
 
       weak = new WeakReference(result);
@@ -101,5 +115,181 @@
       
       return result;
    }
+   
+   /**
+    * Get the information for a class
+    * 
+    * @param clazz the class
+    * @return the info
+    */
+   @Override
+   public Object get(Class clazz)
+   {
+      throw new RuntimeException("This method is void!!!");
+   }
+   
+   @SuppressWarnings("unchecked")
+   protected Object instantiate(CtClass ctClass)
+   {
+      try
+      {
+         if (ctClass.isArray())
+         {
+            TypeInfo componentType = getTypeInfo(ctClass.getComponentType());
+            
+            Class[] types = new Class[] { JavassistTypeInfoFactoryImpl.class, CtClass.class, Class.class, TypeInfo.class };
+            Constructor con = JavassistArrayInfoImpl.class.getDeclaredConstructor(types);
+            
+            Object[] args = new Object[] {this, ctClass, null, componentType};
+            con.setAccessible(true);
+            return con.newInstance(args);
+         }
 
+         if (ctClass.isAnnotation())
+         {
+            JavassistAnnotationInfo result = new JavassistAnnotationInfo(this, ctClass, null);
+            CtMethod[] methods = ctClass.getDeclaredMethods();
+            AnnotationAttributeImpl[] atttributes = new AnnotationAttributeImpl[methods.length];
+            for (int i = 0 ; i < methods.length ; i++)
+            {
+               atttributes[i] = new AnnotationAttributeImpl(methods[i].getName(), getTypeInfo(methods[i].getReturnType()), null);
+            }
+            result.setAttributes(atttributes);
+            return result;
+
+         }
+         else if (ctClass.isEnum())
+         {
+            JavassistEnumInfo enumInfo = new JavassistEnumInfo(this, ctClass, null);
+            CtField[] fields = ctClass.getFields();
+            EnumConstantInfoImpl[] constants = new EnumConstantInfoImpl[fields.length];
+            int i = 0;
+            for (CtField field : fields)
+            {
+               AnnotationValue[] annotations = getAnnotations(field);
+               constants[i++] = new EnumConstantInfoImpl(field.getName(), enumInfo, annotations);
+            }
+            enumInfo.setEnumConstants(constants);
+            return enumInfo;
+         }
+
+         Class[] types = new Class[] { JavassistTypeInfoFactoryImpl.class, CtClass.class, Class.class};
+         Constructor con = JavassistTypeInfo.class.getDeclaredConstructor(types);
+         Object[] args = new Object[] {this, ctClass, null};
+         con.setAccessible(true);
+         return  con.newInstance(args);
+
+      }
+      catch (NotFoundException e)
+      {
+         throw new RuntimeException(e);
+      }
+      catch (IllegalArgumentException e)
+      {
+         e.printStackTrace();
+      }
+      catch (InstantiationException e)
+      {
+         e.printStackTrace();
+      }
+      catch (IllegalAccessException e)
+      {
+         e.printStackTrace();
+      }
+      catch (InvocationTargetException e)
+      {
+         e.printStackTrace();
+      }
+      catch (SecurityException e)
+      {
+         e.printStackTrace();
+      }
+      catch (NoSuchMethodException e)
+      {
+         e.printStackTrace();
+      }
+      
+      return null;
+   }
+
+   @Override
+   public TypeInfo getTypeInfo(String name, ClassLoader cl) throws ClassNotFoundException
+   {
+      if (name == null)
+         throw new IllegalArgumentException("Null class name");
+      if (cl == null)
+         cl = Thread.currentThread().getContextClassLoader();
+
+      TypeInfo primitive = PrimitiveInfo.valueOf(name);
+      if (primitive != null)
+         return primitive;
+
+      NumberInfo number = NumberInfo.valueOf(name);
+      if (number != null)
+      {
+         synchronized (number)
+         {
+            if (number.getPhase() != NumberInfo.Phase.INITIALIZING)
+            {
+               if (number.getPhase() != NumberInfo.Phase.COMPLETE)
+               {
+                  number.initializing();
+                  try
+                  {
+                     number.setDelegate((TypeInfo)get(poolFactory.getPoolForLoader(cl).get(name)));
+                  }
+                  catch (NotFoundException e)
+                  {
+                     throw new ClassNotFoundException(e.getMessage());
+                  }
+//                  number.setDelegate((TypeInfo)get(Class.forName(name, false, cl)));
+               }
+               return number;
+            }
+         }
+      }
+
+//      Class<?> clazz = Class.forName(name, false, cl);
+      CtClass clazz = null;
+      try
+      {
+         clazz = poolFactory.getPoolForLoader(cl).get(name);
+      }
+      catch (NotFoundException e)
+      {
+         throw new ClassNotFoundException(e.getMessage());
+      }
+      return getTypeInfo(clazz);
+   }
+   
+   @Override
+   public TypeInfo getTypeInfo(CtClass clazz)
+   {
+      if (clazz == null)
+         throw new IllegalArgumentException("Null class");
+
+      TypeInfo primitive = PrimitiveInfo.valueOf(clazz.getName());
+      if (primitive != null)
+         return primitive;
+
+      NumberInfo number = NumberInfo.valueOf(clazz.getName());
+      if (number != null)
+      {
+         synchronized (number)
+         {
+            if (number.getPhase() != NumberInfo.Phase.INITIALIZING)
+            {
+               if (number.getPhase() != NumberInfo.Phase.COMPLETE)
+               {
+                  number.initializing();
+                  number.setDelegate((TypeInfo)get(clazz));
+               }
+               return number;
+            }
+         }
+      }
+
+      return (TypeInfo) get(clazz);
+   }
+
 }

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/JavassistAopTypeInfoFactoryImplTestCase.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/JavassistAopTypeInfoFactoryImplTestCase.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/JavassistAopTypeInfoFactoryImplTestCase.java	2009-01-08 14:23:54 UTC (rev 82699)
@@ -0,0 +1,106 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, 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.aop.reflectprototype;
+
+import javassist.CannotCompileException;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtMethod;
+import javassist.CtNewMethod;
+import javassist.NotFoundException;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.jboss.aop.reflectprototype.JavassistAopTypeInfoFactoryImpl;
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfo;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.MethodInfo;
+import org.jboss.test.aop.AOPTestWithSetup;
+
+/**
+ * A JavassistAopTypeInfoFactoryImplTestCase.
+ * 
+ * @author <a href="stalep at gmail.com">Stale W. Pedersen</a>
+ * @version $Revision: 1.1 $
+ */
+public class JavassistAopTypeInfoFactoryImplTestCase extends AOPTestWithSetup
+{
+
+   public JavassistAopTypeInfoFactoryImplTestCase(String name)
+   {
+      super(name);
+   }
+   
+   public static Test suite()
+   {
+      TestSuite suite = new TestSuite("JavassistAopTypeInfoFactoryImplTestCase");
+      suite.addTestSuite(JavassistAopTypeInfoFactoryImplTestCase.class);
+      return suite;
+   }
+   
+
+   @Override
+   public void setUp() throws Exception
+   {
+      super.setUp();
+   }
+   
+   public void testJavassistCtClass()
+   {
+      ClassPool pool = ClassPool.getDefault();
+      try
+      {
+         CtClass pojo = pool.get("org.jboss.test.aop.reflectprototype.POJO");
+         
+         assertFalse("POJO shouldnt be frozen", pojo.isFrozen());
+         
+         JavassistAopTypeInfoFactoryImpl typeInfoFactory = new JavassistAopTypeInfoFactoryImpl();
+         ClassInfo pojoTypeInfo = (JavassistTypeInfo) typeInfoFactory.get(pojo);
+         
+         MethodInfo[] methodInfos = pojoTypeInfo.getDeclaredMethods();
+         assertEquals("It should be one method in POJO", methodInfos.length, 1);
+         
+//         CtClass pojo2 = pool.get("org.jboss.test.aop.reflectprototype.POJO");
+         try
+         {
+            CtMethod bar = CtNewMethod.make("public void bar() {}", pojo);
+            pojo.addMethod(bar);
+         }
+         catch (CannotCompileException e)
+         {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+         }
+         
+         ClassInfo pojoTypeInfo2 = (JavassistTypeInfo) typeInfoFactory.get(pojo);
+         assertEquals("It should be two methods in POJO", pojoTypeInfo2.getDeclaredMethods().length, 2);
+         
+         assertFalse("POJO shouldnt be frozen", pojo.isFrozen());
+         
+      }
+      catch (NotFoundException e)
+      {
+         // TODO Auto-generated catch block
+         e.printStackTrace();
+      }
+   }
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/POJO.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/POJO.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/reflectprototype/POJO.java	2009-01-08 14:23:54 UTC (rev 82699)
@@ -0,0 +1,37 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, 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.aop.reflectprototype;
+
+/**
+ * A POJO.
+ * 
+ * @author <a href="stalep at gmail.com">Stale W. Pedersen</a>
+ * @version $Revision: 1.1 $
+ */
+public class POJO
+{
+
+   public void foo()
+   {
+      
+   }
+}




More information about the jboss-cvs-commits mailing list