[weld-commits] Weld SVN: r4767 - api/trunk/cdi/src/main/java/javax/enterprise/util.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Sun Nov 8 03:29:57 EST 2009


Author: gavin.king at jboss.com
Date: 2009-11-08 03:29:57 -0500 (Sun, 08 Nov 2009)
New Revision: 4767

Modified:
   api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java
   api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java
Log:
make them serializable, and improve

Modified: api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java	2009-11-08 01:46:00 UTC (rev 4766)
+++ api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java	2009-11-08 08:29:57 UTC (rev 4767)
@@ -17,6 +17,7 @@
 
 package javax.enterprise.util;
 
+import java.io.Serializable;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -50,31 +51,23 @@
  * @see javax.enterprise.event.Event#select(Annotation...)
  * 
  */
-public abstract class AnnotationLiteral<T extends Annotation> implements
-      Annotation
+public abstract class AnnotationLiteral<T extends Annotation> 
+      implements Annotation, Serializable
 {
    
-   private Class<T> annotationType;
-   private Method[] members;
+   private transient Class<T> annotationType;
+   private transient Method[] members;
 
-   protected AnnotationLiteral()
+   protected AnnotationLiteral() {}
+   
+   private Method[] getMembers() 
    {
-      Class<?> annotationLiteralSubclass = getAnnotationLiteralSubclass(this.getClass());
-      if (annotationLiteralSubclass == null)
-      {
-         throw new RuntimeException(getClass() + "is not a subclass of AnnotationLiteral ");
+      if (members==null) {
+         members = annotationType().getDeclaredMethods();
       }
-      
-      annotationType = getTypeParameter(annotationLiteralSubclass);
-      
-      if (annotationType == null)
-      {
-         throw new RuntimeException(getClass() + " is missing type parameter in AnnotationLiteral");
-      }
-      
-      this.members = annotationType.getDeclaredMethods();
+      return members;
    }
-   
+	   
    private static Class<?> getAnnotationLiteralSubclass(Class<?> clazz)
    {
       Class<?> superclass = clazz.getSuperclass();
@@ -110,25 +103,87 @@
 
    public Class<? extends Annotation> annotationType()
    {
+      if (annotationType==null) 
+      {
+         Class<?> annotationLiteralSubclass = getAnnotationLiteralSubclass(this.getClass());
+         if (annotationLiteralSubclass == null)
+         {
+            throw new RuntimeException(getClass() + "is not a subclass of AnnotationLiteral");
+         }
+         annotationType = getTypeParameter(annotationLiteralSubclass);
+         if (annotationType == null)
+         {
+            throw new RuntimeException(getClass() + " is missing type parameter in AnnotationLiteral");
+         }
+      }
       return annotationType;
    }
 
    @Override
    public String toString()
    {
-     String string = "@" + annotationType().getName() + "(";
-     for (int i = 0; i < members.length; i++)
-     {
-        string += members[i].getName() + "=";
-        string += invoke(members[i], this);
-        if (i < members.length - 1)
-        {
-           string += ",";
-        }
-     }
-     return string + ")";
+      StringBuilder string = new StringBuilder();
+      string.append('@').append(annotationType().getName()).append('(');
+      for (int i = 0; i < getMembers().length; i++)
+      {
+         string.append(getMembers()[i].getName()).append('=');
+         Object value = invoke(getMembers()[i], this);
+         if (value instanceof boolean[]) 
+         {
+            appendInBraces(string, Arrays.toString((boolean[])value));
+         }
+         else if (value instanceof byte[]) 
+         {
+            appendInBraces(string, Arrays.toString((byte[])value));
+         }
+         else if (value instanceof short[]) 
+         {
+            appendInBraces(string, Arrays.toString((short[])value));
+         }
+         else if (value instanceof int[]) 
+         {
+            appendInBraces(string, Arrays.toString((int[])value));
+         }
+         else if (value instanceof long[]) 
+         {
+            appendInBraces(string, Arrays.toString((long[])value));
+         }
+         else if (value instanceof float[]) 
+         {
+            appendInBraces(string, Arrays.toString((float[])value));
+         }
+         else if (value instanceof double[]) 
+         {
+            appendInBraces(string, Arrays.toString((double[])value));
+         }
+         else if (value instanceof char[]) 
+         {
+            appendInBraces(string, Arrays.toString((char[])value));
+         }
+         else if (value instanceof Object[]) 
+         {
+            appendInBraces(string, Arrays.toString((Object[])value));
+         }
+         /*else if (value instanceof Class<?>) 
+         {
+            string.append(((Class<?>)value).getName()).append(".class");
+         }*/
+         else 
+         {
+            string.append(value);
+         }
+         if (i < getMembers().length - 1)
+         {
+            string.append(',');
+         }
+      }
+      return string.append(')').toString();
    }
    
+   private void appendInBraces(StringBuilder buf, String s) {
+      buf.append('{').append(s.substring(1,s.length()-1)).append('}');
+   }
+   
    @Override
    public boolean equals(Object other)
    {
@@ -137,12 +192,13 @@
          Annotation that = (Annotation) other;
          if (this.annotationType().equals(that.annotationType()))
          {
-            for (Method member : members)
+            for (Method member : getMembers())
             {
                Object thisValue = invoke(member, this);
                Object thatValue = invoke(member, that);
                if (thisValue.getClass().isArray() && thatValue.getClass().isArray())
                {
+                  //TODO: broken for primitive arrays!
                   if (!Arrays.equals(Object[].class.cast(thisValue), Object[].class.cast(thatValue)))
                   {
                      return false;
@@ -163,10 +219,11 @@
    public int hashCode()
    {
       int hashCode = 0;
-      for (Method member : members)
+      for (Method member : getMembers())
       {
          int memberNameHashCode = 127 * member.getName().hashCode();
          Object value = invoke(member, this);
+         //TODO: broken for primitive arrays!
          int memberValueHashCode = value.getClass().isArray() ? Arrays.hashCode(Object[].class.cast(value)) : value.hashCode();
          hashCode += memberNameHashCode ^ memberValueHashCode;
       }       
@@ -193,4 +250,5 @@
          throw new RuntimeException("Error checking value of member method " + method.getName() + " on " + method.getDeclaringClass(), e);
       }
    }
+
 }

Modified: api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java	2009-11-08 01:46:00 UTC (rev 4766)
+++ api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java	2009-11-08 08:29:57 UTC (rev 4767)
@@ -17,6 +17,7 @@
 
 package javax.enterprise.util;
 
+import java.io.Serializable;
 import java.lang.reflect.GenericArrayType;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
@@ -41,35 +42,36 @@
  * @see javax.enterprise.event.Event#select(TypeLiteral, Annotation...)
  * 
  */
-public abstract class TypeLiteral<T> 
+public abstract class TypeLiteral<T> implements Serializable
 {
 
-   private Type actualType;
+   private transient Type actualType;
    
-   protected TypeLiteral() 
-   {
-      Class<?> typeLiteralSubclass = getTypeLiteralSubclass(this.getClass());
-      if (typeLiteralSubclass == null) 
-      {
-         throw new RuntimeException(getClass() + " is not a subclass of TypeLiteral");
-      }
-      actualType = getTypeParameter(typeLiteralSubclass);
-      if (actualType == null)
-      {
-         throw new RuntimeException(getClass() + " is missing type parameter in TypeLiteral");
-      }
-   }
+   protected TypeLiteral() {}
 
    /**
-    * @return the actual type represented by type literal
+    * @return the actual type represented by this object
     */
    public final Type getType() 
    {
+      if (actualType==null) 
+      {
+         Class<?> typeLiteralSubclass = getTypeLiteralSubclass(this.getClass());
+         if (typeLiteralSubclass == null) 
+         {
+            throw new RuntimeException(getClass() + " is not a subclass of TypeLiteral");
+         }
+         actualType = getTypeParameter(typeLiteralSubclass);
+         if (actualType == null)
+         {
+            throw new RuntimeException(getClass() + " is missing type parameter in TypeLiteral");
+         }
+      }
       return actualType;
    }
 
    /**
-    * @return the raw type represented by the type literal
+    * @return the raw type represented by this object
     */
    @SuppressWarnings("unchecked")
    public final Class<T> getRawType() {
@@ -123,5 +125,25 @@
       return null;
    }
    
-   // TODO: equals(), hashCode()
+   @Override
+   public boolean equals(Object obj) {
+      if (obj instanceof TypeLiteral<?>)
+      {
+         TypeLiteral<?> that = (TypeLiteral<?>) obj;
+         return this.getType().equals(that.getType());
+      }
+      return false;
+   }
+   
+   @Override
+   public int hashCode() {
+      return getType().hashCode();
+   }
+
+   @Override
+   public String toString()
+   {
+      return getType().toString();
+   }
+   
 }



More information about the weld-commits mailing list