openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1629359 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/annotation/ main/java/org/apache/webbeans/intercept/ main/java/org/apache/webbeans/portable/events/discovery/ main/java/org/apache/webbeans/util/ test/java/o...
Date Sat, 04 Oct 2014 08:19:11 GMT
Author: rmannibucau
Date: Sat Oct  4 08:19:10 2014
New Revision: 1629359

URL: http://svn.apache.org/r1629359
Log:
OWB-1017 supporting @NonBinding through extension and AnnotatedType

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/extension/BeforeBeanDiscoveryImplTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java?rev=1629359&r1=1629358&r2=1629359&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java
Sat Oct  4 08:19:10 2014
@@ -223,8 +223,12 @@ public final class AnnotationManager
         Asserts.assertNotNull(anns, "anns parameter can not be null");
         List<Annotation> interAnns = new ArrayList<Annotation>();
 
-        for (Annotation ann : anns)
+        for (final Annotation ann : anns)
         {
+            if (ann.annotationType().getName().startsWith("java.lang."))
+            {
+                continue;
+            }
             if (isInterceptorBindingAnnotation(ann.annotationType()))
             {
                 interAnns.add(ann);

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java?rev=1629359&r1=1629358&r2=1629359&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
Sat Oct  4 08:19:10 2014
@@ -20,6 +20,7 @@ package org.apache.webbeans.intercept;
 
 import java.lang.annotation.Annotation;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -78,6 +79,8 @@ public class InterceptorsManager
      */
     private Map<Class<? extends Annotation>, Set<Annotation>> additionalInterceptorBindingTypes
             = new HashMap<Class<? extends Annotation>, Set<Annotation>>();
+    private final Collection<AnnotatedType<?>> additionalInterceptorBindingTypesAnnotatedTypes
+            = new ArrayList<AnnotatedType<?>>();
 
     private final PriorityClasses priorityInterceptors = new PriorityClasses();
 
@@ -230,16 +233,43 @@ public class InterceptorsManager
             // if an interceptor has multiple bindings then all of them must be in the
             // requestedInterceptorBindings for a positive match
 
-            if (!inBindingArray(interceptorBinding, requestedInterceptorBindings))
+            // first check AT since it can override some methods (@NonBinding)
+            boolean found = false;
+            for (final AnnotatedType<?> at : additionalInterceptorBindingTypesAnnotatedTypes)
             {
-                return false;
+                if (interceptorBinding.annotationType().equals(at.getJavaClass()))
+                {
+                    found = true;
+                    if (!inBindingArray(at, interceptorBinding, requestedInterceptorBindings))
+                    {
+                        return false;
+                    }
+                }
+            }
+            if (!found)
+            {
+                if (!inBindingArray(interceptorBinding, requestedInterceptorBindings))
+                {
+                    return false;
+                }
             }
-
         }
 
         return true;
     }
 
+    private boolean inBindingArray(final AnnotatedType<?> at, final Annotation interceptorBinding,
final Annotation[] requestedInterceptorBindings)
+    {
+        for (final Annotation requestedBinding : requestedInterceptorBindings)
+        {
+            if (AnnotationUtil.isCdiAnnotationEqual(at, requestedBinding, interceptorBinding))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private boolean inBindingArray(Annotation interceptorBinding, Annotation[] requestedInterceptorBindings)
     {
         for (Annotation requestedBinding : requestedInterceptorBindings)
@@ -289,6 +319,11 @@ public class InterceptorsManager
         return additionalInterceptorClasses.contains(clazz);
     }
 
+    public void addInterceptorBindingType(final AnnotatedType<? extends Annotation>
annotatedType)
+    {
+        additionalInterceptorBindingTypesAnnotatedTypes.add(annotatedType);
+    }
+
     public void addInterceptorBindingType(Class<? extends Annotation> bindingType,
Annotation... inheritsArray)
     {
         Set<Annotation> inherits = additionalInterceptorBindingTypes.get(bindingType);
@@ -297,16 +332,24 @@ public class InterceptorsManager
             inherits = new HashSet<Annotation>();
             additionalInterceptorBindingTypes.put(bindingType, inherits);
         }
-        for(Annotation ann : inheritsArray)
-        {
-            inherits.add(ann);
-        }
-
+        Collections.addAll(inherits, inheritsArray);
     }
 
     public boolean hasInterceptorBindingType(Class<? extends Annotation> bindingType)
     {
-        return additionalInterceptorBindingTypes.keySet().contains(bindingType);
+        final boolean contains = additionalInterceptorBindingTypes.keySet().contains(bindingType);
+        if (contains)
+        {
+            return true;
+        }
+        for (final AnnotatedType<?> at : additionalInterceptorBindingTypesAnnotatedTypes)
+        {
+            if (bindingType.equals(at.getJavaClass()))
+            {
+                return true;
+            }
+        }
+        return false;
     }
 
 

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java?rev=1629359&r1=1629358&r2=1629359&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java
Sat Oct  4 08:19:10 2014
@@ -110,7 +110,7 @@ public class BeforeBeanDiscoveryImpl imp
     public void addInterceptorBinding(AnnotatedType<? extends Annotation> annotatedType)
     {
         // TODO extract inherited types
-        webBeansContext.getInterceptorsManager().addInterceptorBindingType(annotatedType.getJavaClass());
+        webBeansContext.getInterceptorsManager().addInterceptorBindingType(annotatedType);
     }
 
     /**

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java?rev=1629359&r1=1629358&r2=1629359&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
Sat Oct  4 08:19:10 2014
@@ -30,6 +30,7 @@ import java.util.Set;
 
 import javax.enterprise.inject.spi.AnnotatedMethod;
 import javax.enterprise.inject.spi.AnnotatedParameter;
+import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.util.Nonbinding;
 
 import org.apache.webbeans.annotation.AnyLiteral;
@@ -202,6 +203,20 @@ public final class AnnotationUtil
         throw new IllegalArgumentException("annotation @" + annotation.getName() + " not
found on any parameter");
     }
 
+    private static boolean areParamEquals(final Annotation annotation1, final Annotation
annotation2, final List<Method> bindingCdiAnnotationMethods)
+    {
+        for (final Method method : bindingCdiAnnotationMethods)
+        {
+            final Object value1 = callMethod(annotation1, method);
+            final Object value2 = callMethod(annotation2, method);
+            if (!checkEquality(value1, value2))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
     /**
      * Checks if the given cdi annotations are equal. cdi annotations may either be qualifiers
or interceptor bindings.
      *
@@ -212,6 +227,27 @@ public final class AnnotationUtil
      * @param annotation2
      * @return 
      */
+    public static boolean isCdiAnnotationEqual(final AnnotatedType<?> at, final Annotation
annotation1, final Annotation annotation2)
+    {
+        Asserts.assertNotNull(annotation1, "annotation1 argument can not be null");
+        Asserts.assertNotNull(annotation2, "annotation2 argument can not be null");
+
+        Class<?> qualifier1AnnotationType = at.getJavaClass();
+
+        // check if the annotationTypes are equal
+        if (qualifier1AnnotationType == null
+                || !qualifier1AnnotationType.equals(annotation2.annotationType()))
+        {
+            return false;
+        }
+
+        // check the values of all qualifier-methods
+        // except those annotated with @Nonbinding
+        List<Method> bindingCdiAnnotationMethods = getBindingCdiAnnotationMethods(at);
+
+        return areParamEquals(annotation1, annotation2, bindingCdiAnnotationMethods);
+    }
+
     public static boolean isCdiAnnotationEqual(Annotation annotation1, Annotation annotation2)
     {
         Asserts.assertNotNull(annotation1, "annotation1 argument can not be null");
@@ -232,18 +268,7 @@ public final class AnnotationUtil
         List<Method> bindingCdiAnnotationMethods
                 = getBindingCdiAnnotationMethods(qualifier1AnnotationType);
 
-        for (Method method : bindingCdiAnnotationMethods)
-        {
-            Object value1 = callMethod(annotation1, method);
-            Object value2 = callMethod(annotation2, method);
-
-            if (!checkEquality(value1, value2))
-            {
-                return false;
-            }
-        }
-
-        return true;
+        return areParamEquals(annotation1, annotation2, bindingCdiAnnotationMethods);
     }
 
     /**
@@ -448,6 +473,20 @@ public final class AnnotationUtil
         return Collections.emptyList();
     }
 
+    private static List<Method> getBindingCdiAnnotationMethods(final AnnotatedType<?>
at)
+    {
+        final List<Method> bindingMethods = new ArrayList<Method>();
+        for (final AnnotatedMethod<?> method : at.getMethods())
+        {
+            if (method.isAnnotationPresent(Nonbinding.class))
+            {
+                continue;
+            }
+            bindingMethods.add(method.getJavaMember());
+        }
+        return bindingMethods;
+    }
+
     /**
      * Check whether or not class contains the given annotation.
      * 

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/extension/BeforeBeanDiscoveryImplTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/extension/BeforeBeanDiscoveryImplTest.java?rev=1629359&r1=1629358&r2=1629359&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/extension/BeforeBeanDiscoveryImplTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/extension/BeforeBeanDiscoveryImplTest.java
Sat Oct  4 08:19:10 2014
@@ -18,11 +18,9 @@
  */
 package org.apache.webbeans.test.interceptors.extension;
 
+import org.apache.webbeans.annotation.EmptyAnnotationLiteral;
 import org.apache.webbeans.container.AnnotatedTypeWrapper;
 import org.apache.webbeans.test.AbstractUnitTest;
-import org.apache.webbeans.test.interceptors.annotation.DependentInterceptorBindingType;
-import org.apache.webbeans.util.AnnotationUtil;
-import org.junit.Ignore;
 import org.junit.Test;
 
 import javax.enterprise.event.Observes;
@@ -53,20 +51,39 @@ import static org.junit.Assert.assertEqu
 public class BeforeBeanDiscoveryImplTest extends AbstractUnitTest
 {
     @Test
-    @Ignore("not yet implemented")
     public void nonBindingByExtension()
     {
         addExtension(new TheExtension());
         addInterceptor(TheInterceptor.class);
-        startContainer(TheInterceptor.class, Intercepted.class);
-        assertEquals("interceptor", getInstance(Intercepted.class).value());
+        startContainer(Intercepted.class);
+
+        final Intercepted instance = getInstance(Intercepted.class);
+        assertEquals("interceptor", instance.value());
+        assertEquals("bean", instance.noInterceptor());
     }
 
     public static class TheExtension implements Extension
     {
         void obs(@Observes final BeforeBeanDiscovery bbd, final BeanManager bm)
         {
-            final AnnotatedType<TheBindingType> annotatedType = bm.createAnnotatedType(TheBindingType.class);
+            final AnnotatedType<TheBindingType> annotatedType1 = bm.createAnnotatedType(TheBindingType.class);
+            final Set<Annotation> annotations = annotatedType1.getAnnotations();
+            annotations.add(new EmptyAnnotationLiteral<InterceptorBinding>() {});
+
+            final AnnotatedType<TheBindingType> annotatedType = new AnnotatedTypeWrapper<TheBindingType>(this,
annotatedType1) {
+                @Override
+                public Set<Annotation> getAnnotations()
+                {
+                    return annotations;
+                }
+
+                @Override
+                public boolean isAnnotationPresent(final Class<? extends Annotation>
aClass)
+                {
+                    return super.isAnnotationPresent(aClass) || InterceptorBinding.class
== aClass;
+                }
+            };
+
             final AnnotatedTypeWrapper<TheBindingType> wrapper = new AnnotatedTypeWrapper<TheBindingType>(this,
annotatedType) {
                 @Override
                 public Set<AnnotatedMethod<? super TheBindingType>> getMethods()
{
@@ -114,14 +131,7 @@ public class BeforeBeanDiscoveryImplTest
                                     final T annotation = m.getAnnotation(annotationType);
                                     if (annotation == null && Nonbinding.class ==
annotationType)
                                     {
-                                        return (T) new Nonbinding()
-                                        {
-                                            @Override
-                                            public Class<? extends Annotation> annotationType()
-                                            {
-                                                return Nonbinding.class;
-                                            }
-                                        };
+                                        return (T) new EmptyAnnotationLiteral<Nonbinding>()
{};
                                     }
                                     return annotation;
                                 }
@@ -149,17 +159,19 @@ public class BeforeBeanDiscoveryImplTest
         }
     }
 
-    @InterceptorBinding
+    // @InterceptorBinding: done by the extension
     @Retention(RetentionPolicy.RUNTIME)
     @Target( { ElementType.TYPE, ElementType.METHOD })
     public static @interface TheBindingType
     {
         String shouldBeBound();
+
+        boolean binding();
     }
 
     @Interceptor
-    @TheBindingType(shouldBeBound = "useless normally")
-    public class TheInterceptor
+    @TheBindingType(shouldBeBound = "useless normally", binding = true)
+    public static class TheInterceptor
     {
         @AroundInvoke
         public Object aroundInvoke(final InvocationContext ctx) throws Exception
@@ -170,10 +182,16 @@ public class BeforeBeanDiscoveryImplTest
 
     public static class Intercepted
     {
-        @TheBindingType(shouldBeBound = "another value")
+        @TheBindingType(shouldBeBound = "another value", binding = true)
         public String value()
         {
-            throw new UnsupportedOperationException("shouldn't be called");
+            throw new UnsupportedOperationException("the test failed :(");
+        }
+
+        @TheBindingType(shouldBeBound = "useless normally", binding = false)
+        public String noInterceptor()
+        {
+            return "bean";
         }
     }
 }



Mime
View raw message