You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ta...@apache.org on 2013/04/05 14:37:18 UTC

svn commit: r1464954 - /openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/EmptyAnnotationLiteral.java

Author: tandraschko
Date: Fri Apr  5 12:37:18 2013
New Revision: 1464954

URL: http://svn.apache.org/r1464954
Log:
#OWB-813

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/EmptyAnnotationLiteral.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/EmptyAnnotationLiteral.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/EmptyAnnotationLiteral.java?rev=1464954&r1=1464953&r2=1464954&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/EmptyAnnotationLiteral.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/EmptyAnnotationLiteral.java Fri Apr  5 12:37:18 2013
@@ -27,6 +27,23 @@ import java.lang.annotation.Annotation;
  */
 public abstract class EmptyAnnotationLiteral<T extends Annotation> extends AnnotationLiteral<T>
 {
+    private Class<T> annotationType;
+
+    protected EmptyAnnotationLiteral()
+    {
+        this.annotationType = getAnnotationType(getClass());
+    }
+
+    /**
+     * Implemented for compatibility reasons with other cdi-api jar's.
+     * See OWB-802.
+     */
+    @Override
+    public Class<? extends Annotation> annotationType()
+    {
+        return annotationType;
+    }
+
     /**
      * Implemented for performance reasons.
      * This is needed because an Annotation always returns 0 as hashCode
@@ -54,4 +71,45 @@ public abstract class EmptyAnnotationLit
         return Annotation.class.isInstance(other) &&
                 Annotation.class.cast(other).annotationType().equals(annotationType());
     }
+
+    private Class<T> getAnnotationType(Class<?> definedClazz)
+    {
+        Type superClazz = definedClazz.getGenericSuperclass();
+
+        Class<T> clazz = null;
+
+        if (superClazz.equals(Object.class))
+        {
+            throw new RuntimeException("Super class must be parametrized type!");
+        }
+        else if (superClazz instanceof ParameterizedType)
+        {
+            ParameterizedType paramType = (ParameterizedType) superClazz;
+            Type[] actualArgs = paramType.getActualTypeArguments();
+
+            if (actualArgs.length == 1)
+            {
+                //Actual annotation type
+                Type type = actualArgs[0];
+
+                if (type instanceof Class)
+                {
+                    clazz = (Class<T>) type;
+                    return clazz;
+                }
+                else
+                {
+                    throw new RuntimeException("Not class type!");
+                }
+            }
+            else
+            {
+                throw new RuntimeException("More than one parametric type!");
+            }
+        }
+        else
+        {
+            return getAnnotationType((Class<?>) superClazz);
+        }
+    }
 }