openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1504030 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/proxy/ test/java/org/apache/webbeans/newtests/interceptors/constructor/
Date Wed, 17 Jul 2013 07:27:44 GMT
Author: rmannibucau
Date: Wed Jul 17 07:27:44 2013
New Revision: 1504030

URL: http://svn.apache.org/r1504030
Log:
OWB-881 interceptors doesn't use the same constructor as intercepted instance, just a simple
no arg constructor is fine

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/constructor/
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/constructor/InterceptionOfBeanWithConstructorInjectionTest.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java?rev=1504030&r1=1504029&r2=1504030&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java
Wed Jul 17 07:27:44 2013
@@ -32,7 +32,6 @@ import org.apache.xbean.asm4.Type;
 
 import javax.enterprise.inject.spi.Bean;
 import java.io.ObjectStreamException;
-import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
@@ -261,32 +260,25 @@ public class InterceptorDecoratorProxyFa
     protected void createConstructor(ClassWriter cw, String proxyClassFileName, Class<?>
classToProxy, String classFileName)
             throws ProxyGenerationException
     {
-        try
-        {
-            Constructor superDefaultCt = classToProxy.getDeclaredConstructor(null);
+        // was: final String descriptor = Type.getConstructorDescriptor(classToProxy.getDeclaredConstructor());
+        // but we need to get a default constructor even if the bean uses constructor injection
+        final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V",
null, null);
+        mv.visitCode();
+        mv.visitVarInsn(Opcodes.ALOAD, 0);
+        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classFileName, "<init>", "()V");
 
-            final String descriptor = Type.getConstructorDescriptor(superDefaultCt);
-            final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", descriptor,
null, null);
-            mv.visitCode();
-            mv.visitVarInsn(Opcodes.ALOAD, 0);
-            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classFileName, "<init>", descriptor);
+        mv.visitVarInsn(Opcodes.ALOAD, 0);
+        mv.visitInsn(Opcodes.ACONST_NULL);
+        mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_PROXIED_INSTANCE, Type.getDescriptor(classToProxy));
 
-            mv.visitVarInsn(Opcodes.ALOAD, 0);
-            mv.visitInsn(Opcodes.ACONST_NULL);
-            mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_PROXIED_INSTANCE,
Type.getDescriptor(classToProxy));
+        mv.visitVarInsn(Opcodes.ALOAD, 0);
+        mv.visitInsn(Opcodes.ACONST_NULL);
+        mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER,
Type.getDescriptor(InterceptorHandler.class));
 
-            mv.visitVarInsn(Opcodes.ALOAD, 0);
-            mv.visitInsn(Opcodes.ACONST_NULL);
-            mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER,
Type.getDescriptor(InterceptorHandler.class));
+        mv.visitInsn(Opcodes.RETURN);
+        mv.visitMaxs(-1, -1);
+        mv.visitEnd();
 
-            mv.visitInsn(Opcodes.RETURN);
-            mv.visitMaxs(-1, -1);
-            mv.visitEnd();
-        }
-        catch (NoSuchMethodException e)
-        {
-            throw new ProxyGenerationException(e);
-        }
     }
 
     /**

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/constructor/InterceptionOfBeanWithConstructorInjectionTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/constructor/InterceptionOfBeanWithConstructorInjectionTest.java?rev=1504030&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/constructor/InterceptionOfBeanWithConstructorInjectionTest.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/constructor/InterceptionOfBeanWithConstructorInjectionTest.java
Wed Jul 17 07:27:44 2013
@@ -0,0 +1,103 @@
+/*
+ * 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.newtests.interceptors.constructor;
+
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.junit.Test;
+
+import javax.inject.Inject;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InterceptorBinding;
+import javax.interceptor.InvocationContext;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class InterceptionOfBeanWithConstructorInjectionTest extends AbstractUnitTest
+{
+    @Inject
+    private BuildMeWithMyConstructor bean;
+
+    @Test
+    public void checkBeanWasBuiltWithItsConstructorAndIntercepted()
+    {
+        addInterceptor(ConstructorIsNotAnIssueForMe.class);
+
+        final Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
+        beanClasses.add(BuildMeWithMyConstructor.class);
+        beanClasses.add(Injected.class);
+
+        startContainer(beanClasses, Collections.<String>emptyList(), true);
+        assertNotNull(bean);
+        assertNotNull(bean.getInjected());
+        assertEquals(1, ConstructorIsNotAnIssueForMe.count);
+        shutDownContainer();
+
+    }
+
+    @ConstructorInterceptorBindingType
+    public static class BuildMeWithMyConstructor
+    {
+        private final Injected injected;
+
+        @Inject
+        public BuildMeWithMyConstructor(final Injected injected)
+        {
+            this.injected = injected;
+        }
+
+        public Injected getInjected()
+        {
+            return injected;
+        }
+    }
+
+    public static class Injected
+    {
+    }
+
+    @ConstructorInterceptorBindingType @Interceptor
+    public static class ConstructorIsNotAnIssueForMe
+    {
+        public static int count = 0;
+
+        @AroundInvoke
+        public Object around(final InvocationContext ic) throws Throwable
+        {
+            count++;
+            return ic.proceed();
+        }
+    }
+
+    @InterceptorBinding
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target( { ElementType.TYPE, ElementType.METHOD })
+    public @interface ConstructorInterceptorBindingType
+    {
+    }
+
+}



Mime
View raw message