[webbeans-commits] Webbeans SVN: r2204 - ri/trunk/impl/src/main/java/org/jboss/webbeans/event and 7 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Wed Mar 25 16:01:41 EDT 2009


Author: pete.muir at jboss.org
Date: 2009-03-25 16:01:41 -0400 (Wed, 25 Mar 2009)
New Revision: 2204

Added:
   tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/
   tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/BostonTerrier.java
   tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/ObserverMethodAndTypeVariableEventTypeTest.java
   tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/Foo.java
   tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/FooObserver.java
Modified:
   ri/trunk/impl/src/main/java/org/jboss/webbeans/ManagerImpl.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/event/EventManager.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/event/EventObserver.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedItem.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/util/Reflections.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/util/reflection/ParameterizedTypeImpl.java
   tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3/BostonTerrier.java
   tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/ChecksTypeParametersWhenResolvingTest.java
Log:
WBRI-136, fix hashcode on parameterizedtypeimpl, fix it so that only event types with type variables and wildcard types are definition exceptions (and fix tests accordingly)

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/ManagerImpl.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/ManagerImpl.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -255,6 +255,7 @@
       {
          throw new DuplicateBindingTypeException("Duplicate binding types: " + bindings);
       }
+      Type t = new Reflections.HierarchyDiscovery(clazz).getResolvedType();
       for (Type type : Reflections.getActualTypeArguments(clazz))
       {
          if (type instanceof WildcardType)
@@ -502,7 +503,7 @@
    
    public <T> Manager addObserver(ObserverImpl<T> observer)
    {
-      addObserver(observer, observer.getEventType(), observer.getBindingsAsArray());
+      this.eventManager.addObserver(observer, observer.getEventType(), observer.getBindingsAsArray());
       return this;
    }
    
@@ -522,7 +523,7 @@
     */
    public <T> Manager addObserver(Observer<T> observer, TypeLiteral<T> eventType, Annotation... bindings)
    {
-      eventManager.addObserver(observer, eventType.getRawType(), bindings);
+      eventManager.addObserver(observer, eventType.getType(), bindings);
       return this;
    }
    

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/event/EventManager.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/event/EventManager.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/event/EventManager.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -96,7 +96,7 @@
        * @param eventType The event type
        * @param observer The observer to add
        */
-      public void put(Class<?> eventType, EventObserver<?> observer)
+      public void put(Type eventType, EventObserver<?> observer)
       {
          List<EventObserver<?>> observers = super.get(eventType);
          if (observers == null)
@@ -142,7 +142,7 @@
     * @param eventType The event type the observer is interested in
     * @param bindings The bindings the observer wants to filter on
     */
-   public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
+   public <T> void addObserver(Observer<T> observer, Type eventType, Annotation... bindings)
    {
       EventObserver<T> eventObserver = new EventObserver<T>(observer, eventType, bindings);
       registeredObservers.put(eventType, eventObserver);
@@ -161,7 +161,7 @@
    public <T> Set<Observer<T>> getObservers(T event, Annotation... bindings)
    {
       Set<Observer<T>> interestedObservers = new HashSet<Observer<T>>();
-      Set<Type> types = new HierarchyDiscovery<Type>(event.getClass()).getFlattenedTypes();
+      Set<Type> types = new HierarchyDiscovery(event.getClass()).getFlattenedTypes();
       for (Type type : types)
       {
          for (EventObserver<?> observer : registeredObservers.get(type))

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/event/EventObserver.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/event/EventObserver.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/event/EventObserver.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -18,6 +18,7 @@
 package org.jboss.webbeans.event;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -45,7 +46,7 @@
 public class EventObserver<T>
 {
 
-   private final Class<T> eventType;
+   private final Type eventType;
    private final List<Annotation> eventBindings;
    private final Observer<T> observer;
 
@@ -56,7 +57,7 @@
     * @param eventType The class of event being observed
     * @param eventBindings The array of annotation event bindings, if any
     */
-   public EventObserver(final Observer<T> observer, final Class<T> eventType, final Annotation... eventBindings)
+   public EventObserver(final Observer<T> observer, final Type eventType, final Annotation... eventBindings)
    {
       this.observer = observer;
       this.eventType = eventType;
@@ -93,7 +94,7 @@
    /**
     * @return the eventType
     */
-   public final Class<T> getEventType()
+   public final Type getEventType()
    {
       return eventType;
    }
@@ -191,7 +192,7 @@
    {
       StringBuilder buffer = new StringBuilder();
       buffer.append("Event Observer:\n");
-      buffer.append("  Event Type: " + eventType.getName() + "\n");
+      buffer.append("  Event Type: " + eventType + "\n");
       buffer.append(Strings.collectionToString("  Event Bindings: ", eventBindings));
       buffer.append("  Observer: " + observer);
       return buffer.toString();

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/event/ObserverImpl.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/event/ObserverImpl.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -18,6 +18,9 @@
 package org.jboss.webbeans.event;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+import java.lang.reflect.WildcardType;
 import java.util.List;
 
 import javax.context.Dependent;
@@ -59,7 +62,7 @@
    private final boolean conditional;
    private final boolean asynchronous;
    protected ManagerImpl manager;
-   private final Class<T> eventType;
+   private final Type eventType;
    private final Annotation[] bindings;
 
    /**
@@ -75,12 +78,8 @@
       this.manager = manager;
       this.observerBean = observerBean;
       this.observerMethod = MethodInjectionPoint.of(observerBean, observer);
-      
+      this.eventType = observerMethod.getAnnotatedParameters(Observes.class).get(0).getType();
 
-      @SuppressWarnings("unchecked")
-      Class<T> c = (Class<T>) observerMethod.getAnnotatedParameters(Observes.class).get(0).getRawType();
-      this.eventType = c;
-
       this.bindings = observerMethod.getAnnotatedParameters(Observes.class).get(0).getBindingsAsArray();
       this.conditional = !observerMethod.getAnnotatedParameters(IfExists.class).isEmpty();
       this.asynchronous = !observerMethod.getAnnotatedParameters(Asynchronously.class).isEmpty();
@@ -116,7 +115,17 @@
          AnnotatedParameter<?> eventParam = eventObjects.iterator().next();
          if (eventParam.isParameterizedType())
          {
-            throw new DefinitionException(this + " cannot observe parameterized event types");
+            for (Type type : eventParam.getActualTypeArguments())
+            {
+               if (type instanceof TypeVariable)
+               {
+                  throw new DefinitionException("Cannot use a type variable " + type + " in an parameterized type " + toString());
+               }
+               else if (type instanceof WildcardType)
+               {
+                  throw new DefinitionException("Cannot use a wildcard variable " + type + " in an parameterized type " + toString());
+               }
+            }
          }
       }
       // Check for parameters annotated with @Disposes
@@ -218,7 +227,7 @@
       return builder.toString();
    }
 
-   public Class<T> getEventType()
+   public Type getEventType()
    {
       return eventType;
    }

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedItem.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedItem.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedItem.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -86,7 +86,7 @@
          this.actualTypeArguments = new Type[0];
       }
       this._parameterizedType = Reflections.isParameterizedType(rawType);
-      this.flattenedTypes = new Reflections.HierarchyDiscovery<Type>(type).getFlattenedTypes();
+      this.flattenedTypes = new Reflections.HierarchyDiscovery(type).getFlattenedTypes();
       this.proxyable = Proxies.isTypesProxyable(flattenedTypes);
    }
    

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/util/Reflections.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/util/Reflections.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/util/Reflections.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -48,35 +48,39 @@
 public class Reflections
 {
    
-   public static class HierarchyDiscovery<T extends Type>
+   public static class HierarchyDiscovery
    {
       
-      private final T type;
+      private final Type type;
      
-      private Set<T> types;
+      private Set<Type> types;
       
-      public HierarchyDiscovery(T type)
+      public HierarchyDiscovery(Type type)
       {
          this.type = type;
       }
       
-      protected void add(T type)
+      protected void add(Type type)
       {
          types.add(type);
       }
       
-      public Set<T> getFlattenedTypes()
+      public Set<Type> getFlattenedTypes()
       {
          if (types == null)
          {
-            this.types = new HashSet<T>();
+            this.types = new HashSet<Type>();
             discoverTypes(type);
          }
          return types;
       }
       
+      public Type getResolvedType()
+      {
+         return resolveType(type, type);
+      }
       
-      private void discoverTypes(T type)
+      private void discoverTypes(Type type)
       {
          if (type != null)
          {
@@ -99,10 +103,10 @@
       @SuppressWarnings("unchecked")
       private void discoverFromClass(Class<?> clazz)
       {
-         discoverTypes((T) resolveType(type, clazz.getGenericSuperclass()));
+         discoverTypes(resolveType(type, clazz.getGenericSuperclass()));
          for (Type c : clazz.getGenericInterfaces())
          {
-            discoverTypes((T) resolveType(type, c));
+            discoverTypes(resolveType(type, c));
          }
       }
       

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/util/reflection/ParameterizedTypeImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/util/reflection/ParameterizedTypeImpl.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/util/reflection/ParameterizedTypeImpl.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -35,42 +35,28 @@
    @Override
    public int hashCode()
    {
-      final int prime = 31;
-      int result = 1;
-      result = prime * result + Arrays.hashCode(actualTypeArguments);
-      result = prime * result + ((ownerType == null) ? 0 : ownerType.hashCode());
-      result = prime * result + ((rawType == null) ? 0 : rawType.hashCode());
-      return result;
+      return Arrays.hashCode(actualTypeArguments) ^ (ownerType == null ? 0 : ownerType.hashCode()) ^ (rawType == null ? 0 : rawType.hashCode());
    }
 
    @Override
    public boolean equals(Object obj)
    {
       if (this == obj)
-         return true;
-      if (obj == null)
-         return false;
-      if (!(obj instanceof ParameterizedType))
-         return false;
-      
-      final ParameterizedType other = (ParameterizedType) obj;
-      if (!Arrays.equals(actualTypeArguments, other.getActualTypeArguments()))
-         return false;
-      if (ownerType == null)
       {
-         if (other.getOwnerType() != null)
-            return false;
+         return true;
       }
-      else if (!ownerType.equals(other.getOwnerType()))
-         return false;
-      if (rawType == null)
+      else if (obj instanceof ParameterizedType)
       {
-         if (other.getRawType() != null)
-            return false;
+         ParameterizedType that = (ParameterizedType) obj;
+         Type thatOwnerType = that.getOwnerType();
+         Type thatRawType = that.getRawType();
+         return (ownerType == null ? thatOwnerType == null : ownerType.equals(thatOwnerType)) && (rawType == null ? thatRawType == null : rawType.equals(thatRawType)) && Arrays.equals(actualTypeArguments, that.getActualTypeArguments());
       }
-      else if (!rawType.equals(other.getRawType()))
+      else
+      {
          return false;
-      return true;
+      }
+      
    }
 
    public String toString()

Modified: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3/BostonTerrier.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3/BostonTerrier.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3/BostonTerrier.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -6,7 +6,7 @@
 
 class BostonTerrier
 {
-   public void observesBadEvent(@Observes List<String> someArray)
+   public void observesBadEvent(@Observes List<?> someArray)
    {
    }
 }

Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/BostonTerrier.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/BostonTerrier.java	                        (rev 0)
+++ tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/BostonTerrier.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -0,0 +1,12 @@
+package org.jboss.jsr299.tck.tests.event.broken.observer3a;
+
+import java.util.List;
+
+import javax.event.Observes;
+
+class BostonTerrier
+{
+   public <T> void observesBadEvent(@Observes List<T> someArray)
+   {
+   }
+}


Property changes on: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/BostonTerrier.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/ObserverMethodAndTypeVariableEventTypeTest.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/ObserverMethodAndTypeVariableEventTypeTest.java	                        (rev 0)
+++ tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/ObserverMethodAndTypeVariableEventTypeTest.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -0,0 +1,47 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.jsr299.tck.tests.event.broken.observer3a;
+
+import javax.inject.DefinitionException;
+
+import org.hibernate.tck.annotations.SpecAssertion;
+import org.jboss.jsr299.tck.AbstractJSR299Test;
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.testharness.impl.packaging.ExpectedDeploymentException;
+import org.testng.annotations.Test;
+
+/**
+ * Tests an observer method defined to observe an event type which
+ * is generic and wildcarded.
+ * 
+ * @author David Allen
+ *
+ */
+ at Artifact
+ at ExpectedDeploymentException(DefinitionException.class)
+public class ObserverMethodAndTypeVariableEventTypeTest extends AbstractJSR299Test
+{
+   @Test(groups = { "events" })
+   @SpecAssertion(section = "7.5.1", id = "c")
+   public void testObserverMethodCannotObserveParameterizedEvents()
+   {
+      assert false;
+   }
+
+
+}


Property changes on: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/broken/observer3a/ObserverMethodAndTypeVariableEventTypeTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/ChecksTypeParametersWhenResolvingTest.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/ChecksTypeParametersWhenResolvingTest.java	2009-03-25 16:05:11 UTC (rev 2203)
+++ tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/ChecksTypeParametersWhenResolvingTest.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -1,7 +1,6 @@
 package org.jboss.jsr299.tck.tests.event.resolve.type;
 
 import java.util.ArrayList;
-import java.util.Set;
 
 import javax.event.Observer;
 import javax.inject.TypeLiteral;
@@ -14,6 +13,17 @@
 @Artifact
 public class ChecksTypeParametersWhenResolvingTest extends AbstractJSR299Test
 {
+   
+   public static class AList extends ArrayList<String>
+   {
+      
+   }
+   
+   public static class AnotherList extends ArrayList<Integer>
+   {
+      
+   }
+   
    public static class AListObserver implements Observer<ArrayList<String>>
    {
       public boolean wasNotified = false;
@@ -34,7 +44,7 @@
       }
    }
 
-   @Test(groups = { "broken", "events" })
+   @Test(groups = { "events" })
    @SpecAssertion(section = "7.7", id = "f")
    public void testResolvingChecksTypeParameters()
    {
@@ -46,8 +56,16 @@
       getCurrentManager().addObserver(anotherObserver, new TypeLiteral<ArrayList<Integer>>()
       {
       });
-      Set<Observer<ArrayList<String>>> resolvedObservers = getCurrentManager().resolveObservers(new ArrayList<String>());
-      //TODO The RI is returning both of the observers above
-      assert resolvedObservers.size() == 1;
+      assert getCurrentManager().resolveObservers(new AList()).size() == 1;
+      assert getCurrentManager().resolveObservers(new AList()).iterator().next().getClass().equals(AListObserver.class);
    }
+   
+   @Test(groups = { "events" })
+   @SpecAssertion(section = "7.7", id = "f")
+   public void testResolvingChecksTypeParametersOnObservesMethod()
+   {
+      Foo<String> fooEvent = new Foo<String>() {};
+      getCurrentManager().fireEvent(fooEvent);
+      assert fooEvent.isObserved();
+   }
 }

Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/Foo.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/Foo.java	                        (rev 0)
+++ tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/Foo.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -0,0 +1,18 @@
+package org.jboss.jsr299.tck.tests.event.resolve.type;
+
+public class Foo<T>
+{
+   
+   private boolean observed;
+   
+   public boolean isObserved()
+   {
+      return observed;
+   }
+   
+   public void setObserved(boolean observed)
+   {
+      this.observed = observed;
+   }
+   
+}


Property changes on: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/Foo.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/FooObserver.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/FooObserver.java	                        (rev 0)
+++ tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/FooObserver.java	2009-03-25 20:01:41 UTC (rev 2204)
@@ -0,0 +1,13 @@
+package org.jboss.jsr299.tck.tests.event.resolve.type;
+
+import javax.event.Observes;
+
+public class FooObserver
+{
+   
+   public void observe(@Observes Foo<String> foo)
+   {
+      foo.setObserved(true);
+   }
+   
+}


Property changes on: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/resolve/type/FooObserver.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain




More information about the weld-commits mailing list