openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1435547 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/component/creation/ main/java/org/apache/webbeans/intercept/ main/java/org/apache/webbeans/proxy/ test/java/org/apache/webbeans/newtests/decorators/common/ t...
Date Sat, 19 Jan 2013 13:50:29 GMT
Author: struberg
Date: Sat Jan 19 13:50:28 2013
New Revision: 1435547

URL: http://svn.apache.org/viewvc?rev=1435547&view=rev
Log:
OWB-344 fix abstract Decorator handling

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/SubclassProxyFactory.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Breeded.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Cow.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.java
    openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.xml

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java?rev=1435547&r1=1435546&r2=1435547&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
Sat Jan 19 13:50:28 2013
@@ -31,7 +31,6 @@ import javax.inject.Inject;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Constructor;
-import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
@@ -364,10 +363,7 @@ public class DecoratorBeanBuilder<T> ext
             ClassLoader classLoader = this.getClass().getClassLoader();
             Class<T> classToProxy = annotatedType.getJavaClass();
 
-            List<Method> methods = ClassUtil.getNonPrivateMethods(classToProxy);
-
-            proxySubClass = webBeansContext.getSubclassProxyFactory().createDecoratorSubclass(null);
-
+            proxySubClass = webBeansContext.getSubclassProxyFactory().createImplementedSubclass(classLoader,
classToProxy);
 
             //X TODO what about @Inject constructors?
             Constructor<T> ct = webBeansContext.getWebBeansUtil().getNoArgConstructor(proxySubClass);

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java?rev=1435547&r1=1435546&r2=1435547&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
Sat Jan 19 13:50:28 2013
@@ -326,6 +326,7 @@ public class InterceptorResolutionServic
                 ParameterizedType parameterizedType = (ParameterizedType)decoratedType;
                 decoratedType = parameterizedType.getRawType();
             }
+
             if (decoratedType instanceof Class)
             {
                 Class decoratedClass = (Class) decoratedType;
@@ -340,27 +341,40 @@ public class InterceptorResolutionServic
                         continue;
                     }
 
-                    if (decoratorMethod.getName().equals(annotatedMethodName))
+                    if (methodEquals(decoratorMethod, annotatedMethod.getJavaMember()))
                     {
-                        Class<?>[] decoratorMethodParams = decoratorMethod.getParameterTypes();
-                        Class<?>[] annotatedMethodParams = annotatedMethod.getJavaMember().getParameterTypes();
-                        if (decoratorMethodParams.length == annotatedMethodParams.length)
+                        // yikes our method is decorated by this very decorator type.
+
+                        if (Modifier.isAbstract((decorator.getBeanClass().getModifiers())))
                         {
-                            boolean paramsMatch = true;
-                            for (int i = 0; i < decoratorMethodParams.length; i++)
+                            // For abstract classes we will only decorate this method if
it's really implemented on the decorator itself
+                            Class decoratorClass = decorator.getBeanClass();
+                            while (decoratorClass != Object.class)
                             {
-                                if (!decoratorMethodParams[i].equals(annotatedMethodParams[i]))
+                                try
                                 {
-                                    paramsMatch = false;
-                                    break;
+                                    Method m = decoratorClass.getDeclaredMethod(decoratorMethod.getName(),
decoratorMethod.getParameterTypes());
+                                    if (Modifier.isAbstract(m.getModifiers()))
+                                    {
+                                        return null;
+                                    }
+                                    else
+                                    {
+                                        return decoratorMethod;
+                                    }
+                                }
+                                catch (NoSuchMethodException e)
+                                {
+                                    // all ok, just continue
                                 }
-                            }
 
-                            if (paramsMatch)
-                            {
-                                // yikes our method is decorated by this very decorator type.
-                                return decoratorMethod;
+                                decoratorClass = decoratorClass.getSuperclass();
                             }
+
+                            return null;
+                        }
+                        {
+                            return decoratorMethod;
                         }
                     }
                 }
@@ -370,6 +384,34 @@ public class InterceptorResolutionServic
         return null;
     }
 
+    private boolean methodEquals(Method method1, Method method2)
+    {
+        if (method1.getName().equals(method2.getName()))
+        {
+            Class<?>[] method1Params = method1.getParameterTypes();
+            Class<?>[] method2Params = method2.getParameterTypes();
+            if (method1Params.length == method2Params.length)
+            {
+                boolean paramsMatch = true;
+                for (int i = 0; i < method1Params.length; i++)
+                {
+                    if (!method1Params[i].equals(method2Params[i]))
+                    {
+                        paramsMatch = false;
+                        break;
+                    }
+                }
+
+                if (paramsMatch)
+                {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
     private void calculateCdiMethodInterceptors(BusinessMethodInterceptorInfo methodInterceptorInfo,
                                                 Set<Interceptor<?>> allUsedCdiInterceptors,
                                                 AnnotatedMethod annotatedMethod,

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/SubclassProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/SubclassProxyFactory.java?rev=1435547&r1=1435546&r2=1435547&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/SubclassProxyFactory.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/SubclassProxyFactory.java
Sat Jan 19 13:50:28 2013
@@ -18,13 +18,11 @@
  */
 package org.apache.webbeans.proxy;
 
-import javax.enterprise.inject.spi.Bean;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.List;
 
-import org.apache.webbeans.component.OwbBean;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import org.apache.webbeans.util.ClassUtil;
@@ -52,22 +50,8 @@ public class SubclassProxyFactory extend
     }
 
 
-    public <T> Class<T> createDecoratorSubclass(Bean<T> bean)
+    public <T> Class<T> createImplementedSubclass(ClassLoader classLoader, Class<T>
classToProxy)
     {
-        ClassLoader classLoader = bean.getClass().getClassLoader();
-
-        Class<T> classToProxy;
-        if (bean instanceof OwbBean)
-        {
-            classToProxy = ((OwbBean<T>) bean).getReturnType();
-
-        }
-        else
-        {
-            // TODO: that might be wrong sometimes
-            classToProxy = (Class<T>) bean.getBeanClass();
-        }
-
         if (!Modifier.isAbstract(classToProxy.getModifiers()))
         {
             throw new WebBeansConfigurationException("Only abstract classes should get subclassed,
not " + classToProxy);

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Breeded.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Breeded.java?rev=1435547&r1=1435546&r2=1435547&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Breeded.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Breeded.java
Sat Jan 19 13:50:28 2013
@@ -24,4 +24,6 @@ package org.apache.webbeans.newtests.dec
 public interface Breeded
 {
     int getAge();
+
+    String getFarmer();
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Cow.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Cow.java?rev=1435547&r1=1435546&r2=1435547&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Cow.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/common/Cow.java
Sat Jan 19 13:50:28 2013
@@ -37,4 +37,10 @@ public class Cow extends Animal implemen
     {
         this.color = color;
     }
+
+    @Override
+    public String getFarmer()
+    {
+        return "Deary Farmer";
+    }
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.java?rev=1435547&r1=1435546&r2=1435547&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.java
Sat Jan 19 13:50:28 2013
@@ -29,10 +29,12 @@ import java.util.Map;
 
 import org.apache.webbeans.intercept.InterceptorResolutionService;
 import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.apache.webbeans.newtests.decorators.common.Cow;
 import org.apache.webbeans.newtests.interceptors.factory.beans.ClassInterceptedClass;
 import org.apache.webbeans.newtests.interceptors.factory.beans.ClassMultiInterceptedClass;
 import org.apache.webbeans.newtests.interceptors.factory.beans.DecoratedClass;
 import org.apache.webbeans.newtests.interceptors.factory.beans.MethodInterceptedClass;
+import org.apache.webbeans.newtests.interceptors.factory.beans.MyAbstractTestDecorator;
 import org.apache.webbeans.newtests.interceptors.factory.beans.StereotypeInterceptedClass;
 import org.apache.webbeans.test.annotation.binding.Binding1;
 import org.apache.webbeans.test.component.decorator.clean.ServiceDecorator;
@@ -252,6 +254,36 @@ public class InterceptorResolutionServic
         shutDownContainer();
     }
 
+
+    @Test
+    public void testAbstractDecoratorResolution() throws Exception
+    {
+        Collection<String> beanXmls = new ArrayList<String>();
+        beanXmls.add(getXmlPath(this.getClass().getPackage().getName(), this.getClass().getSimpleName()));
+
+        Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
+        beanClasses.add(Cow.class);
+        beanClasses.add(MyAbstractTestDecorator.class);
+
+        startContainer(beanClasses, beanXmls);
+
+        InterceptorResolutionService ir = new InterceptorResolutionService(getWebBeansContext());
+        AnnotatedType<Cow> annotatedType = getBeanManager().createAnnotatedType(Cow.class);
+        Bean<Cow> bean = (Bean<Cow>) getBeanManager().resolve(
+                getBeanManager().getBeans(Cow.class));
+        Assert.assertNotNull(bean);
+
+        BeanInterceptorInfo interceptorInfo = ir.calculateInterceptorInfo(bean.getTypes(),
bean.getQualifiers(), annotatedType);
+        Assert.assertNotNull(interceptorInfo);
+
+        Assert.assertNotNull(interceptorInfo.getBusinessMethodsInfo());
+        Assert.assertEquals(1, interceptorInfo.getBusinessMethodsInfo().size());
+
+        Assert.assertNotNull(interceptorInfo.getDecorators());
+        Assert.assertEquals(1, interceptorInfo.getDecorators().size());
+
+        shutDownContainer();
+    }
     @Test
     public void testEjbStyleInterceptorResolution() throws Exception
     {

Modified: openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.xml?rev=1435547&r1=1435546&r2=1435547&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.xml
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionServiceTest.xml
Sat Jan 19 13:50:28 2013
@@ -26,5 +26,6 @@ under the License.
     </interceptors>
     <decorators>
         <class>org.apache.webbeans.test.component.decorator.clean.ServiceDecorator</class>
+        <class>org.apache.webbeans.newtests.interceptors.factory.beans.MyAbstractTestDecorator</class>
     </decorators>
 </beans>



Mime
View raw message