[jboss-cvs] JBossAS SVN: r85899 - projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Mar 16 07:35:50 EDT 2009


Author: stalep
Date: 2009-03-16 07:35:50 -0400 (Mon, 16 Mar 2009)
New Revision: 85899

Modified:
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java
Log:
[JBREFLECT-49]
Tried to make JavassistTypeInfo and JavassistTypeInfoFactoryImpl mutable
compliant. Note that this breaks the current testsuite since
JavassisitTypeInfo.getType() do not any longer return any object.

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java	2009-03-16 11:35:37 UTC (rev 85898)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java	2009-03-16 11:35:50 UTC (rev 85899)
@@ -323,14 +323,16 @@
 
    public boolean isArray()
    {
-      return getType().isArray();
+      return getCtClass().isArray();
    }
 
+ //TODO: need to change the use of getType() here
    public boolean isCollection()
    {
       return Collection.class.isAssignableFrom(getType());
    }
 
+   //TODO: need to change the use of getType() here
    public boolean isMap()
    {
       return Map.class.isAssignableFrom(getType());
@@ -338,17 +340,17 @@
 
    public boolean isAnnotation()
    {
-      return getType().isAnnotation();
+      return getCtClass().isAnnotation();
    }
 
    public boolean isEnum()
    {
-      return getType().isEnum();
+      return getCtClass().isEnum();
    }
 
    public boolean isPrimitive()
    {
-      return getType().isPrimitive();
+      return getCtClass().isPrimitive();
    }
 
    /**
@@ -700,4 +702,9 @@
       }
       return attachments.getAttachment(name);
    }
+   
+   protected CtClass getCtClass()
+   {
+      return ctClass;
+   }
 }

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	2009-03-16 11:35:37 UTC (rev 85898)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java	2009-03-16 11:35:50 UTC (rev 85899)
@@ -22,7 +22,9 @@
 package org.jboss.reflect.plugins.javassist;
 
 import java.lang.annotation.Annotation;
+import java.lang.ref.WeakReference;
 import java.lang.reflect.Type;
+import java.util.Map;
 
 import javassist.ClassPool;
 import javassist.CtClass;
@@ -38,6 +40,8 @@
 import org.jboss.reflect.plugins.EnumConstantInfoImpl;
 import org.jboss.reflect.spi.AnnotationInfo;
 import org.jboss.reflect.spi.AnnotationValue;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.ClassPoolFactory;
 import org.jboss.reflect.spi.NumberInfo;
 import org.jboss.reflect.spi.PrimitiveInfo;
 import org.jboss.reflect.spi.TypeInfo;
@@ -52,7 +56,8 @@
  */
 public class JavassistTypeInfoFactoryImpl extends WeakClassCache implements TypeInfoFactory, AnnotationHelper
 {
-   static final ClassPool pool = ClassPool.getDefault();
+   //TODO: Need to change this to a usable CPF.
+   static final ClassPoolFactory poolFactory = new DummyClassPoolFactory();
 
    static final AnnotationValue[] NO_ANNOTATIONS = new AnnotationValue[0];
    /**
@@ -167,8 +172,147 @@
          throw new RuntimeException(e);
       }
    }
+   
+   @SuppressWarnings("unchecked")
+   protected Object instantiate(CtClass ctClass)
+   {
+      try
+      {
 
+         if (ctClass.isArray())
+         {
+            TypeInfo componentType = getTypeInfo(ctClass.getComponentType());
+            return new JavassistArrayInfoImpl(this, ctClass, null, componentType);
+         }
+
+         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;
+         }
+
+
+         return new JavassistTypeInfo(this, ctClass, null);
+      }
+      catch (NotFoundException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+   
    /**
+    * Get the information for a class
+    * 
+    * @param name the name
+    * @param cl the classloader
+    * @return the info
+    * @throws ClassNotFoundException when the class cannot be found
+    */
+   @Override
+   public Object get(String name, ClassLoader cl) throws ClassNotFoundException
+   {
+      if (name == null)
+         throw new IllegalArgumentException("Null name");
+      if (cl == null)
+         throw new IllegalArgumentException("Null classloader");
+      try
+      {
+         CtClass clazz = poolFactory.getPoolForLoader(cl).get(name);
+
+         return get(clazz);
+      }
+      catch(NotFoundException nfe)
+      {
+         throw new ClassNotFoundException(nfe.getMessage());
+      }
+   }
+   
+   /**
+    * Get the information for a class
+    * 
+    * 
+    * @param clazz the class
+    * @return the info
+    */
+   @Override
+   public Object get(Class clazz)
+   {
+      try
+      {
+         ClassLoader cl = clazz.getClassLoader();
+         if(cl == null)
+            cl = Thread.currentThread().getContextClassLoader();
+         return get(clazz.getName(), cl);
+      }
+      catch (ClassNotFoundException e)
+      {
+         throw new RuntimeException("Class not found: "+e.getMessage());
+      }
+   }
+   
+
+   /**
+    * Get the information for a class
+    * 
+    * @param clazz the class
+    * @return the info
+    */
+   public Object get(CtClass clazz)
+   {
+      if (clazz == null)
+         throw new IllegalArgumentException("Null class");
+      
+      Map classLoaderCache = getClassLoaderCache(clazz.getClassPool().getClassLoader());
+
+      WeakReference weak = (WeakReference) classLoaderCache.get(clazz.getName());
+      if (weak != null)
+      {
+         Object result = weak.get();
+         if (result != null)
+         {
+            if(compare(clazz, (ClassInfo) result))
+               return result;
+            else
+            {
+               classLoaderCache.remove(clazz.getName());
+            }
+         }
+      }
+
+      Object result = instantiate(clazz);
+      
+      weak = new WeakReference(result);
+      classLoaderCache.put(clazz.getName(), weak);
+      
+//      we just ignore generate(..) since it doesnt do anything atm
+//      generate(clazz, result);
+      
+      return result;
+   }
+   
+   /**
     * Get the type info
     *
     * @param ctClass the ctClass
@@ -237,7 +381,8 @@
    {
       try
       {
-         return pool.get(name);
+         //TODO: Need to change this
+         return poolFactory.getPoolForLoader(null).get(name);
       }
       catch (NotFoundException e)
       {
@@ -369,4 +514,14 @@
       return AnnotationValueFactory.createAnnotationValue(this, this, info, ann);
    }
    
+
+   private boolean compare(CtClass clazz, ClassInfo info)
+   {
+      if(clazz.getDeclaredMethods().length == info.getDeclaredMethods().length &&
+            clazz.getDeclaredConstructors().length == info.getDeclaredConstructors().length &&
+            clazz.getDeclaredFields().length == info.getDeclaredFields().length)
+         return true;
+      else
+         return false;
+   }
 }




More information about the jboss-cvs-commits mailing list