openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1623069 - /openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
Date Sun, 07 Sep 2014 16:40:20 GMT
Author: rmannibucau
Date: Sun Sep  7 16:40:19 2014
New Revision: 1623069

URL: http://svn.apache.org/r1623069
Log:
trying to validate decorator generics - this is a first draft and will surely need to adjustments

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java?rev=1623069&r1=1623068&r2=1623069&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
Sun Sep  7 16:40:19 2014
@@ -77,6 +77,7 @@ import org.apache.webbeans.spi.plugins.O
 import org.apache.webbeans.util.AnnotationUtil;
 import org.apache.webbeans.util.ClassUtil;
 import org.apache.webbeans.util.ExceptionUtil;
+import org.apache.webbeans.util.GenericsUtil;
 import org.apache.webbeans.util.InjectionExceptionUtil;
 import org.apache.webbeans.util.SpecializationUtil;
 import org.apache.webbeans.util.WebBeansConstants;
@@ -98,6 +99,8 @@ import javax.enterprise.inject.spi.Obser
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.net.URL;
 import java.security.PrivilegedActionException;
 import java.util.ArrayList;
@@ -112,6 +115,8 @@ import java.util.Stack;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import static java.util.Arrays.asList;
+
 /**
  * Deploys the all beans that are defined in the {@link org.apache.webbeans.spi.ScannerService}
at
  * the scanner phase.
@@ -245,7 +250,9 @@ public class BeansDeployer
                 
                 // Validate injection Points
                 validateInjectionPoints();
-                
+
+                validateDecoratorGenericTypes();
+
                 // fire event
                 fireAfterDeploymentValidationEvent();
 
@@ -288,6 +295,65 @@ public class BeansDeployer
         }
     }
 
+    // avoid delegate implementing Foo<A> and decorator implementing Foo<B> with
no link between A and B
+    private void validateDecoratorGenericTypes()
+    {
+        for (final Decorator<?> decorator : webBeansContext.getDecoratorsManager().getDecorators())
+        {
+            final Type type = decorator.getDelegateType();
+
+            // capture ParameterizedType from decorator type
+            final Collection<Type> types = new HashSet<Type>();
+            if (Class.class.isInstance(type))
+            {
+                Class<?> c = Class.class.cast(type);
+                while (c != Object.class && c != null)
+                {
+                    types.add(c);
+                    for (final Type t : asList(c.getGenericInterfaces()))
+                    {
+                        if (ParameterizedType.class.isInstance(t))
+                        {
+                            types.add(t);
+                        }
+                    }
+                    final Type genericSuperclass = c.getGenericSuperclass();
+                    if (ParameterizedType.class.isInstance(genericSuperclass))
+                    {
+                        types.add(genericSuperclass);
+                    }
+                    c = c.getSuperclass();
+                }
+            } // else?
+
+            // check arguments matches with decorator API
+            for (final Type api : decorator.getTypes())
+            {
+                if (!ParameterizedType.class.isInstance(api)) // no need to check here
+                {
+                    continue;
+                }
+
+                final ParameterizedType pt1 = ParameterizedType.class.cast(api);
+                for (final Type t : types)
+                {
+                    if (ParameterizedType.class.isInstance(t))
+                    {
+                        final ParameterizedType pt2 = ParameterizedType.class.cast(t);
+
+                        if (pt1.getRawType() == pt2.getRawType())
+                        {
+                            if (!GenericsUtil.isAssignableFrom(true, pt1, pt2))
+                            {
+                                throw new DefinitionException("Generic error matching " +
api + " and " + t);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     /**
      * Remove all beans which are not enabled anymore.
      * This might e.g. happen because they are 'overridden'



Mime
View raw message