openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r1589607 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/container/ main/java/org/apache/webbeans/event/ main/java/org/apache/webbeans/portable/ main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/tes...
Date Thu, 24 Apr 2014 06:47:19 GMT
Author: arne
Date: Thu Apr 24 06:47:19 2014
New Revision: 1589607

URL: http://svn.apache.org/r1589607
Log:
OWB-946: Moved event-related checks to EventMetadataImpl

Removed:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/config/ClassTest.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventMetadataImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventUtil.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/EventProducer.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
Thu Apr 24 06:47:19 2014
@@ -417,7 +417,7 @@ public class BeanManagerImpl extends Abs
         {
             type = new OwbParametrizedTypeImpl(event.getClass().getDeclaringClass(), event.getClass(),
event.getClass().getTypeParameters());
         }
-        fireEvent(event, new EventMetadataImpl(type, null, bindings), false);
+        fireEvent(event, new EventMetadataImpl(type, null, bindings, webBeansContext), false);
     }
 
     /**
@@ -427,7 +427,7 @@ public class BeanManagerImpl extends Abs
      */
     public void fireLifecycleEvent(Object event, Annotation... bindings)
     {
-        fireEvent(event, new EventMetadataImpl(event.getClass(), null, bindings), true);
+        fireEvent(event, new EventMetadataImpl(event.getClass(), null, bindings, webBeansContext),
true);
     }
 
     public void fireEvent(Object event, EventMetadata metadata, boolean isLifecycleEvent)
@@ -964,16 +964,11 @@ public class BeanManagerImpl extends Abs
     @Override
     public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(T
event, Annotation... qualifiers)
     {
-        return resolveObserverMethods(event, new EventMetadataImpl(event.getClass(), null,
qualifiers));
+        return resolveObserverMethods(event, new EventMetadataImpl(event.getClass(), null,
qualifiers, webBeansContext));
     }
 
     public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(T
event, EventMetadata metadata) 
     {
-        if(ClassUtil.isDefinitionContainsTypeVariables(ClassUtil.getClass(metadata.getType())))
-        {
-            throw new IllegalArgumentException("Event type can not contain type variables.
Event class is : " + event.getClass());
-        }
-        
         return notificationManager.resolveObservers(event, metadata);
     }
 

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
Thu Apr 24 06:47:19 2014
@@ -23,17 +23,13 @@ import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.Set;
 
 import javax.enterprise.event.Event;
-import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.util.TypeLiteral;
 
 import org.apache.webbeans.config.WebBeansContext;
-import org.apache.webbeans.util.ClassUtil;
+import org.apache.webbeans.util.Asserts;
 import org.apache.webbeans.util.OwbCustomObjectInputStream;
 import org.apache.webbeans.util.WebBeansUtil;
 
@@ -47,14 +43,7 @@ public class EventImpl<T> implements Eve
 {
     private static final long serialVersionUID = -9035218380365451350L;
     
-    /**Event binding types*/
-    private Annotation[] injectedBindings;
-
-    /**Event types*/
-    private Type eventType;
-    
-    /**injection point of the event*/
-    private InjectionPoint injectionPoint;
+    private EventMetadataImpl metadata;
 
     private transient WebBeansContext webBeansContext;
 
@@ -65,38 +54,33 @@ public class EventImpl<T> implements Eve
      * @param eventType event type
      * @param webBeansContext
      */
-    public EventImpl(Annotation[] injectedBindings, Type eventType, InjectionPoint injectionPoint,
WebBeansContext webBeansContext)
+    public EventImpl(EventMetadata metadata, WebBeansContext webBeansContext)
     {
+        Asserts.assertNotNull(metadata, "event metadata may not be null");
+        this.metadata = wrapMetadata(metadata);
         this.webBeansContext = webBeansContext;
-        this.injectedBindings = injectedBindings;
-        this.eventType = eventType;
-        this.injectionPoint = injectionPoint;
     }
 
-    /**
-     * Fires event with given event object.
-     */
-    @Override
-    public void fire(T event)
+    private EventMetadataImpl wrapMetadata(EventMetadata metadata)
     {
-        webBeansContext.getBeanManagerImpl().fireEvent(event, new EventMetadataImpl(eventType,
injectionPoint, injectedBindings), false);
+        if (metadata instanceof EventMetadataImpl)
+        {
+            return (EventMetadataImpl)metadata;
+        }
+        else
+        {
+            Set<Annotation> qualifiers = metadata.getQualifiers();
+            return new EventMetadataImpl(metadata.getType(), metadata.getInjectionPoint(),
qualifiers.toArray(new Annotation[qualifiers.size()]), webBeansContext);
+        }
     }
 
     /**
-     * Returns total binding annotations.
-     * 
-     * @param annotations new annotations
-     * @return total binding annotations
+     * Fires event with given event object.
      */
-    private Annotation[] getEventBindings(Annotation... annotations)
+    @Override
+    public void fire(T event)
     {
-        webBeansContext.getAnnotationManager().checkQualifierConditions(annotations);
-
-        Set<Annotation> eventBindings = new HashSet<Annotation>();
-        Collections.addAll(eventBindings, injectedBindings);
-        Collections.addAll(eventBindings, annotations);
-
-        return eventBindings.toArray(new Annotation[eventBindings.size()]);
+        webBeansContext.getBeanManagerImpl().fireEvent(event, metadata, false);
     }
 
     /**
@@ -105,9 +89,8 @@ public class EventImpl<T> implements Eve
     @Override
     public Event<T> select(Annotation... bindings)
     {
-        Event<T> sub = new EventImpl<T>(getEventBindings(bindings), eventType,
injectionPoint, webBeansContext);
         
-        return sub;
+        return new EventImpl<T>(metadata.select(bindings), webBeansContext);
     }
     
     /**
@@ -116,24 +99,7 @@ public class EventImpl<T> implements Eve
     @Override
     public <U extends T> Event<U> select(Class<U> subtype, Annotation...
bindings)
     {
-        if(ClassUtil.isDefinitionContainsTypeVariables(subtype))
-        {
-            throw new IllegalArgumentException("Class : " + subtype + " cannot contain type
variable");
-        }
-        Type sub = subtype;
-        return select(sub, bindings);
-    }
-
-    private <U extends T> Event<U> select(Type sub, Annotation... bindings)
-    {
-        if(sub == null)
-        {
-            sub = eventType;
-        }
-        
-        Event<U> subEvent = new EventImpl<U>(getEventBindings(bindings),sub,
injectionPoint, webBeansContext);
-        
-        return subEvent;
+        return new EventImpl<U>(metadata.select(subtype, bindings), webBeansContext);
     }
     
     /**
@@ -142,15 +108,13 @@ public class EventImpl<T> implements Eve
     @Override
     public <U extends T> Event<U> select(TypeLiteral<U> subtype, Annotation...
bindings)
     {
-        //TODO check for type variables
-        return select(subtype.getType(), bindings);
+        return new EventImpl<U>(metadata.select(subtype, bindings), webBeansContext);
     }
     
     private void writeObject(java.io.ObjectOutputStream op) throws IOException
     {
         ObjectOutputStream oos = new ObjectOutputStream(op);
-        oos.writeObject(eventType);
-        oos.writeObject(injectedBindings);
+        oos.writeObject(metadata);
         
         oos.flush();
     }
@@ -158,8 +122,7 @@ public class EventImpl<T> implements Eve
     private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
     {
         final ObjectInputStream inputStream = new OwbCustomObjectInputStream(in, WebBeansUtil.getCurrentClassLoader());
-        eventType = (Type)inputStream.readObject();
-        injectedBindings = (Annotation[])inputStream.readObject();
+        metadata = (EventMetadataImpl)inputStream.readObject();
 
         webBeansContext = WebBeansContext.currentInstance();
     }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventMetadataImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventMetadataImpl.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventMetadataImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventMetadataImpl.java
Thu Apr 24 06:47:19 2014
@@ -18,6 +18,7 @@
  */
 package org.apache.webbeans.event;
 
+import java.io.Serializable;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.Arrays;
@@ -26,21 +27,32 @@ import java.util.HashSet;
 import java.util.Set;
 
 import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.util.TypeLiteral;
 
 import org.apache.webbeans.annotation.AnyLiteral;
+import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.util.AnnotationUtil;
+import org.apache.webbeans.util.ArrayUtil;
+import org.apache.webbeans.util.GenericsUtil;
 
-public class EventMetadataImpl implements EventMetadata
+public class EventMetadataImpl implements EventMetadata, Serializable
 {
 
     private final Type type;
     private final InjectionPoint injectionPoint;
     private final Set<Annotation> qualifiers;
+    private final WebBeansContext context;
     
-    public EventMetadataImpl(Type type, InjectionPoint injectionPoint, Annotation... qualifiers)
+    public EventMetadataImpl(Type type, InjectionPoint injectionPoint, Annotation[] qualifiers,
WebBeansContext context)
     {
+        if (GenericsUtil.containsTypeVariable(type))
+        {
+            throw new IllegalArgumentException("event type may not contain type variable:
" + type);
+        }
+        context.getAnnotationManager().checkQualifierConditions(qualifiers);
         this.type = type;
         this.injectionPoint = injectionPoint;
+        this.context = context;
         Set<Annotation> completeQualifiers;
         if (qualifiers.length == 0)
         {
@@ -78,4 +90,25 @@ public class EventMetadataImpl implement
     {
         return qualifiers;
     }
+    
+    public EventMetadata select(Annotation... bindings)
+    {
+        return select(type, bindings);
+    }
+    
+    public EventMetadata select(TypeLiteral<?> subtype, Annotation... bindings)
+    {
+        return select(subtype.getType(), bindings);
+    }
+    
+    public EventMetadata select(Type subtype, Annotation... bindings)
+    {
+        Set<Annotation> newQualifiers = ArrayUtil.asSet(bindings);
+        newQualifiers.addAll(qualifiers);
+        if (newQualifiers.size() != qualifiers.size() + bindings.length)
+        {
+            throw new IllegalArgumentException("duplicate qualifier");
+        }
+        return new EventMetadataImpl(subtype, injectionPoint, newQualifiers.toArray(new Annotation[newQualifiers.size()]),
context);
+    }
 }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventUtil.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventUtil.java
Thu Apr 24 06:47:19 2014
@@ -34,7 +34,6 @@ import javax.enterprise.inject.spi.Injec
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import org.apache.webbeans.util.AnnotationUtil;
-import org.apache.webbeans.util.Asserts;
 import org.apache.webbeans.util.ClassUtil;
 
 public final class EventUtil
@@ -44,16 +43,6 @@ public final class EventUtil
 
     }
 
-    public static void checkEventType(Class<?> eventType)
-    {
-        Asserts.assertNotNull(eventType, "eventType parameter can not be null");
-
-        if (ClassUtil.isDefinitionContainsTypeVariables(eventType))
-        {
-            throw new IllegalArgumentException("Event type : " + eventType.getName() + "
can not be generic");
-        }
-    }
-
     //expensive check needed by the TCK (EventBindingTypesTest#testFireEventWithNonRuntimeBindingTypeFails)
- see OWB-798
     public static void checkQualifierImplementations(Set<Annotation> qualifiers)
     {

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
Thu Apr 24 06:47:19 2014
@@ -82,8 +82,6 @@ public final class NotificationManager
 
     public <T> void addObserver(ObserverMethod<T> observer, TypeLiteral<T>
typeLiteral)
     {
-        EventUtil.checkEventType(typeLiteral.getRawType());
-
         addObserver(observer, typeLiteral.getType());
     }
 
@@ -152,7 +150,11 @@ public final class NotificationManager
         
         Set<ObserverMethod<? super T>> matching = new HashSet<ObserverMethod<?
super T>>();
 
-        Set<Type> eventTypes = GenericsUtil.getTypeClosure(declaredEventType, eventClass);

+        Set<Type> eventTypes = GenericsUtil.getTypeClosure(declaredEventType, eventClass);
+        if (GenericsUtil.containTypeVariable(eventTypes))
+        {
+            throw new IllegalArgumentException("event type may not contain unbound type variable:
" + eventTypes);
+        }
 
         Set<Type> observedTypes = observers.keySet();
 

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/EventProducer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/EventProducer.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/EventProducer.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/EventProducer.java
Thu Apr 24 06:47:19 2014
@@ -30,6 +30,7 @@ import javax.enterprise.inject.spi.Inter
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.context.creational.CreationalContextImpl;
 import org.apache.webbeans.event.EventImpl;
+import org.apache.webbeans.event.EventMetadataImpl;
 import org.apache.webbeans.exception.WebBeansException;
 
 public class EventProducer<T> extends AbstractProducer<Event<T>>
@@ -75,7 +76,7 @@ public class EventProducer<T> extends Ab
             
             try
             {
-                instance = new EventImpl<T>(qualifiers, eventType, injectionPoint,
webBeansContext);
+                instance = new EventImpl<T>(new EventMetadataImpl(eventType, injectionPoint,
qualifiers, webBeansContext), webBeansContext);
             }
             catch (Exception e)
             {

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
Thu Apr 24 06:47:19 2014
@@ -708,20 +708,6 @@ public final class ClassUtil
     }
 
     /**
-     * Learn whether the specified class is defined with type parameters.
-     * @param type to check
-     * @return true if there are type parameters
-     * @since 1.1.1
-     */
-    public static boolean isDefinitionContainsTypeVariables(Type type)
-    {
-        Class<?> clazz = ClassUtil.getClass(type);
-        Asserts.nullCheckForClass(clazz);
-        
-        return clazz.getTypeParameters().length > 0;
-    }
-
-    /**
      * Returns declared type arguments if {@code type} is a
      * {@link ParameterizedType} instance, else an empty array.
      * Get the actual type arguments of a type.

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
Thu Apr 24 06:47:19 2014
@@ -30,6 +30,7 @@ import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
 import java.lang.reflect.WildcardType;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -341,6 +342,58 @@ public final class GenericsUtil
     }
     
     /**
+     * @return <tt>true</tt>, if the specified type declaration contains an unresolved
type variable.
+     */
+    public static boolean containsTypeVariable(Type type)
+    {
+        if (type instanceof Class)
+        {
+            return false;
+        }
+        else if (type instanceof TypeVariable)
+        {
+            return true;
+        }
+        else if (type instanceof ParameterizedType)
+        {
+            ParameterizedType parameterizedType = (ParameterizedType)type;
+            return containTypeVariable(parameterizedType.getActualTypeArguments());
+        }
+        else if (type instanceof WildcardType)
+        {
+            WildcardType wildcardType = (WildcardType)type;
+            return containTypeVariable(wildcardType.getUpperBounds()) || containTypeVariable(wildcardType.getLowerBounds());
+        }
+        else if (type instanceof GenericArrayType)
+        {
+            GenericArrayType arrayType = (GenericArrayType)type;
+            return containsTypeVariable(arrayType.getGenericComponentType());
+        }
+        else
+        {
+            throw new IllegalArgumentException("Unsupported type " + type.getClass().getName());
+        }
+
+    }
+    
+    public static boolean containTypeVariable(Collection<? extends Type> types)
+    {
+        return containTypeVariable(types.toArray(new Type[types.size()]));
+    }
+    
+    public static boolean containTypeVariable(Type[] types)
+    {
+        for (Type type: types)
+        {
+            if (containsTypeVariable(type))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    /**
      * Resolves the actual type of the specified field for the type hierarchy specified by
the given subclass
      */
     public static Type resolveType(Class<?> subclass, Field field)

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=1589607&r1=1589606&r2=1589607&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
Thu Apr 24 06:47:19 2014
@@ -165,7 +165,7 @@ public final class WebBeansUtil
     {
         Asserts.assertNotNull(clazz);
 
-        if (ClassUtil.isDefinitionContainsTypeVariables(clazz))
+        if (clazz.getTypeParameters().length > 0)
         {
             if(!scope.equals(Dependent.class))
             {



Mime
View raw message