You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ta...@apache.org on 2015/02/13 21:49:43 UTC

svn commit: r1659678 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/proxy/ test/java/org/apache/webbeans/test/interceptors/factory/ test/java/org/apache/webbeans/test/interceptors/factory/beans/

Author: tandraschko
Date: Fri Feb 13 20:49:42 2015
New Revision: 1659678

URL: http://svn.apache.org/r1659678
Log:
OWB-1036 NormalScoped ASM proxies broken in some cases for partial beans

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanInterface.java
      - copied, changed from r1659662, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanClass.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface2.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface3.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/NormalScopeProxyFactoryTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java?rev=1659678&r1=1659677&r2=1659678&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java Fri Feb 13 20:49:42 2015
@@ -25,6 +25,8 @@ import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.logging.Logger;
 
 import org.apache.webbeans.config.WebBeansContext;
@@ -190,20 +192,58 @@ public abstract class AbstractProxyFacto
      * @param <T>
      * @return the proxy class
      */
-     protected <T> Class<T> createProxyClass(ClassLoader classLoader, String proxyClassName, Class<T> classToProxy,
+    protected <T> Class<T> createProxyClass(ClassLoader classLoader, String proxyClassName, Class<T> classToProxy,
                                                       Method[] interceptedMethods, Method[] nonInterceptedMethods,
                                                       Constructor<T> constructor)
             throws ProxyGenerationException
     {
         String proxyClassFileName = proxyClassName.replace('.', '/');
 
-        final byte[] proxyBytes = generateProxy(
-                classLoader, classToProxy, proxyClassName, proxyClassFileName,
-                interceptedMethods, nonInterceptedMethods, constructor);
+        final byte[] proxyBytes = generateProxy(classLoader,
+                classToProxy,
+                proxyClassName,
+                proxyClassFileName,
+                sortOutDuplicateMethods(interceptedMethods),
+                sortOutDuplicateMethods(nonInterceptedMethods),
+                constructor);
 
         return defineAndLoadClass(classLoader, proxyClassName, proxyBytes);
     }
 
+    private Method[] sortOutDuplicateMethods(Method[] methods)
+    {
+        if (methods == null || methods.length == 0)
+        {
+            return null;
+        }
+        
+        ArrayList<Method> duplicates = new ArrayList<Method>();
+        
+        for (Method outer : methods)
+        {
+            for (Method inner : methods)
+            {
+                if (inner != outer
+                        && hasSameSignature(outer, inner)
+                        && !(duplicates.contains(outer) || duplicates.contains(inner)))
+                {
+                    duplicates.add(inner);
+                }
+            }
+        }
+
+        ArrayList<Method> outsorted = new ArrayList<Method>(Arrays.asList(methods));
+        outsorted.removeAll(duplicates);
+        return outsorted.toArray(new Method[outsorted.size()]);
+    }
+     
+    private boolean hasSameSignature(Method a, Method b)
+    {
+        return a.getName().equals(b.getName())
+                && a.getReturnType().equals(b.getReturnType())
+                && Arrays.equals(a.getParameterTypes(), b.getParameterTypes());
+    }
+     
     private byte[] generateProxy(ClassLoader classLoader, Class<?> classToProxy, String proxyClassName, String proxyClassFileName,
                                  Method[] interceptedMethods, Method[] nonInterceptedMethods, Constructor<?> constructor)
             throws ProxyGenerationException

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/NormalScopeProxyFactoryTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/NormalScopeProxyFactoryTest.java?rev=1659678&r1=1659677&r2=1659678&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/NormalScopeProxyFactoryTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/NormalScopeProxyFactoryTest.java Fri Feb 13 20:49:42 2015
@@ -39,6 +39,7 @@ import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.Set;
 import org.apache.webbeans.test.interceptors.factory.beans.PartialBeanClass;
+import org.apache.webbeans.test.interceptors.factory.beans.PartialBeanInterface;
 
 import static org.junit.Assert.assertNotNull;
 
@@ -209,7 +210,7 @@ public class NormalScopeProxyFactoryTest
     }
 
     @Test
-    public void textPartialBeanProxyCreation() throws Exception
+    public void testPartialBeanProxyCreation() throws Exception
     {
         NormalScopeProxyFactory pf = new NormalScopeProxyFactory(new WebBeansContext());
 
@@ -242,6 +243,32 @@ public class NormalScopeProxyFactoryTest
         proxy.willFail2();
         proxy.willFail3();
     }
+    
+    @Test
+    public void testPartialBeanProxyCreation2() throws Exception
+    {
+        NormalScopeProxyFactory pf = new NormalScopeProxyFactory(new WebBeansContext());
+
+        // we take a fresh URLClassLoader to not blur the test classpath with synthetic classes.
+        ClassLoader classLoader = new URLClassLoader(new URL[0]);
+
+        Class<PartialBeanInterface> proxyClass = pf.createProxyClass(classLoader, PartialBeanInterface.class);
+        Assert.assertNotNull(proxyClass);
+
+        PartialBeanInterface internalInstance = new PartialBeanInterface()
+        {
+            @Override
+            public Object test(Object e) {
+                return e;
+            }
+        };
+
+        TestContextualInstanceProvider provider = new TestContextualInstanceProvider(internalInstance);
+
+        PartialBeanInterface proxy = pf.createProxyInstance(proxyClass, provider);
+
+        proxy.test(new Object());
+    }
 
     /**
      * Test if protected and package scope methods are proxied as well.

Copied: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanInterface.java (from r1659662, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanClass.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanInterface.java?p2=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanInterface.java&p1=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanClass.java&r1=1659662&r2=1659678&rev=1659678&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanClass.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanInterface.java Fri Feb 13 20:49:42 2015
@@ -18,13 +18,7 @@
  */
 package org.apache.webbeans.test.interceptors.factory.beans;
 
-import javax.enterprise.context.ApplicationScoped;
-
-@ApplicationScoped
-public abstract class PartialBeanClass extends PartialBeanClassSuperClass implements PartialBeanClassSuperInterface
+public interface PartialBeanInterface extends PartialBeanSuperInterface2<Object>, PartialBeanSuperInterface3<Object>
 {
-    public String willFail3()
-    {
-        return "";
-    }
+
 }

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface2.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface2.java?rev=1659678&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface2.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface2.java Fri Feb 13 20:49:42 2015
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.webbeans.test.interceptors.factory.beans;
+
+public interface PartialBeanSuperInterface2<E>
+{
+    E test(E e);
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface3.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface3.java?rev=1659678&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface3.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/beans/PartialBeanSuperInterface3.java Fri Feb 13 20:49:42 2015
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.webbeans.test.interceptors.factory.beans;
+
+public interface PartialBeanSuperInterface3<E>
+{
+    E test(E e);
+}