[jboss-cvs] jbossretro/src/main/org/jboss/lang ...

Kabir Khan kkhan at jboss.com
Mon Jul 17 12:55:50 EDT 2006


  User: kkhan   
  Date: 06/07/17 12:55:50

  Modified:    src/main/org/jboss/lang   EnumImpl.java
                        AnnotationHelper.java
  Log:
  Use javassist getAvailableAnnotations() instead of getAnnotations() to avoid ClassNotFoundExceptions if the annotation is not on the classpath
  
  Make EnumImpl.valueOf() force initialization of the class if this has not happened already and a test for this border-line case
  
  Revision  Changes    Path
  1.2       +40 -8     jbossretro/src/main/org/jboss/lang/EnumImpl.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: EnumImpl.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbossretro/src/main/org/jboss/lang/EnumImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- EnumImpl.java	29 Mar 2006 22:27:05 -0000	1.1
  +++ EnumImpl.java	17 Jul 2006 16:55:50 -0000	1.2
  @@ -22,6 +22,8 @@
   package org.jboss.lang;
   
   import java.io.Serializable;
  +import java.lang.reflect.Field;
  +import java.lang.reflect.Method;
   import java.util.ArrayList;
   import java.util.WeakHashMap;
   
  @@ -30,7 +32,7 @@
    * 
    * @author <a href="adrian at jboss.com">Adrian Brock</a>
    * @author Scott.Stark at jboss.org
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public abstract class EnumImpl<E extends EnumImpl<E>> implements Serializable, Comparable<E>
   {
  @@ -49,11 +51,8 @@
       */
      public static <T extends EnumImpl<T>> T valueOf(Class<T> enumType, String name)
      {
  -      ArrayList<T> enums;
  -      synchronized(enumConstants)
  -      {
  -         enums = (ArrayList<T>) enumConstants.get(enumType);
  -      }
  +      ArrayList<T> enums = getEnumConstants(enumType);
  +      
         T match = null;
         for(T t : enums)
         {
  @@ -73,7 +72,7 @@
       */
      public static <T extends EnumImpl<T>> T[] values(Class<T> enumType)
      {
  -      ArrayList<T> enums = (ArrayList<T>) enumConstants.get(enumType);
  +      ArrayList<T> enums = getEnumConstants(enumType);
         T[] values = null;
         if( enums != null )
         {
  @@ -84,9 +83,41 @@
   
      static <T extends EnumImpl<T>> ArrayList<T> getEnumConstants(Class<T> enumType)
      {
  -      return (ArrayList<T>) enumConstants.get(enumType);
  +      ArrayList<T> enums =  enumConstants.get(enumType);
  +      
  +      if (enums == null)
  +      {
  +         enums = forceInitialization(enumType);
  +      }
  +      
  +      return enums;
      }
      
  +   final static Class[] NO_PARAMS = new Class[0];
  +   final static Object[] NO_VALUES = new Object[0];
  +   
  +   static synchronized <T extends EnumImpl<T>> ArrayList<T> forceInitialization(final Class<T> enumType)
  +   {
  +      try
  +      {
  +         //Force initialisation of the woven enum
  +         Method valuesMethod = enumType.getMethod("values", NO_PARAMS);
  +         valuesMethod.invoke(null,NO_VALUES);
  +         
  +         ArrayList<T> enums = enumConstants.get(enumType);
  +         if (enums == null)
  +         {
  +            enums = new ArrayList<T>();
  +            enumConstants.put(enumType, enums);
  +         }
  +         return enums;
  +      }
  +      catch (Exception e)
  +      {
  +         // AutoGenerated
  +         throw new RuntimeException(e);
  +      }
  +   }
      /**
       * Create an enum type. This registers the enum with the class enumConstants
       * to support the valueOf, values methods.
  @@ -156,4 +187,5 @@
         else
            return superClass;
      }
  +   
   }
  
  
  
  1.8       +7 -49     jbossretro/src/main/org/jboss/lang/AnnotationHelper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: AnnotationHelper.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbossretro/src/main/org/jboss/lang/AnnotationHelper.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -b -r1.7 -r1.8
  --- AnnotationHelper.java	13 Jul 2006 10:21:07 -0000	1.7
  +++ AnnotationHelper.java	17 Jul 2006 16:55:50 -0000	1.8
  @@ -41,7 +41,7 @@
    * AnnotationHelper.
    * 
    * @author <a href="adrian at jboss.com">Adrian Brock</a>
  - * @version $Revision: 1.7 $
  + * @version $Revision: 1.8 $
    */
   public class AnnotationHelper
   {
  @@ -89,14 +89,7 @@
       */
      public static Object[] getAnnotations(Class clazz)
      {
  -      try
  -      {
  -         return getCtClass(clazz).getAnnotations();
  -      }
  -      catch (ClassNotFoundException e)
  -      {
  -         throw new UnsupportedOperationException("FIXME: should ignore annotations for missing classes");
  -      }
  +      return getCtClass(clazz).getAvailableAnnotations();
      }
   
      /**
  @@ -159,14 +152,7 @@
       */
      public static Object[] getAnnotations(Method method)
      {
  -      try
  -      {
  -         return getCtMethod(method).getAnnotations();
  -      }
  -      catch (ClassNotFoundException e)
  -      {
  -         throw new UnsupportedOperationException("FIXME: should ignore annotations for missing classes");
  -      }
  +      return getCtMethod(method).getAvailableAnnotations();
      }
   
      /**
  @@ -177,14 +163,7 @@
       */
      public static Object[][] getParameterAnnotations(Method method)
      {
  -      try
  -      {
  -         return getCtMethod(method).getParameterAnnotations();
  -      }
  -      catch (ClassNotFoundException e)
  -      {
  -         throw new UnsupportedOperationException("FIXME: should ignore annotations for missing classes");
  -      }
  +      return getCtMethod(method).getAvailableParameterAnnotations();
      }
   
      /**
  @@ -256,14 +235,7 @@
       */
      public static Object[] getAnnotations(Constructor constructor)
      {
  -      try
  -      {
  -         return getCtConstructor(constructor).getAnnotations();
  -      }
  -      catch (ClassNotFoundException e)
  -      {
  -         throw new UnsupportedOperationException("FIXME: should ignore annotations for missing classes");
  -      }
  +      return getCtConstructor(constructor).getAvailableAnnotations();
      }
   
      /**
  @@ -274,14 +246,7 @@
       */
      public static Object[][] getParameterAnnotations(Constructor constructor)
      {
  -      try
  -      {
  -         return getCtConstructor(constructor).getParameterAnnotations();
  -      }
  -      catch (ClassNotFoundException e)
  -      {
  -         throw new UnsupportedOperationException("FIXME: should ignore annotations for missing classes");
  -      }
  +      return getCtConstructor(constructor).getAvailableParameterAnnotations();
      }
   
      /**
  @@ -352,14 +317,7 @@
       */
      public static Object[] getAnnotations(Field field)
      {
  -      try
  -      {
  -         return getCtField(field).getAnnotations();
  -      }
  -      catch (ClassNotFoundException e)
  -      {
  -         throw new UnsupportedOperationException("FIXME: should ignore annotations for missing classes");
  -      }
  +      return getCtField(field).getAvailableAnnotations();
      }
   
      /**
  
  
  



More information about the jboss-cvs-commits mailing list