openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1729867 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/portable/ProducerFieldProducer.java test/java/org/apache/webbeans/test/profields/InterceptedProducerFieldTest.java
Date Thu, 11 Feb 2016 17:27:42 GMT
Author: rmannibucau
Date: Thu Feb 11 17:27:42 2016
New Revision: 1729867

URL: http://svn.apache.org/viewvc?rev=1729867&view=rev
Log:
OWB-1118 unwrapping intercepted beans for producer fields

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/profields/InterceptedProducerFieldTest.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ProducerFieldProducer.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ProducerFieldProducer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ProducerFieldProducer.java?rev=1729867&r1=1729866&r2=1729867&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ProducerFieldProducer.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ProducerFieldProducer.java
Thu Feb 11 17:27:42 2016
@@ -22,6 +22,9 @@ import org.apache.webbeans.component.Abs
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.context.creational.CreationalContextImpl;
 import org.apache.webbeans.exception.WebBeansException;
+import org.apache.webbeans.intercept.DefaultInterceptorHandler;
+import org.apache.webbeans.proxy.InterceptorHandler;
+import org.apache.webbeans.proxy.OwbInterceptorProxy;
 import org.apache.webbeans.util.Asserts;
 import org.apache.webbeans.util.WebBeansUtil;
 
@@ -80,7 +83,16 @@ public class ProducerFieldProducer<T, P>
             else
             { 
                 parentInstance = getParentInstanceFromContext(parentCreational);
-                
+
+                if (OwbInterceptorProxy.class.isInstance(parentInstance))
+                {
+                    final InterceptorHandler handler = webBeansContext.getInterceptorDecoratorProxyFactory()
+                        .getInterceptorHandler(OwbInterceptorProxy.class.cast(parentInstance));
+                    if (DefaultInterceptorHandler.class.isInstance(handler))
+                    {
+                        parentInstance = (P) DefaultInterceptorHandler.class.cast(handler).getTarget();
+                    }
+                }
                 instance = (T) field.get(parentInstance);
             }
         }

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/profields/InterceptedProducerFieldTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/profields/InterceptedProducerFieldTest.java?rev=1729867&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/profields/InterceptedProducerFieldTest.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/profields/InterceptedProducerFieldTest.java
Thu Feb 11 17:27:42 2016
@@ -0,0 +1,92 @@
+/*
+ * 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.profields;
+
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.apache.webbeans.test.profields.beans.classproducer.MyProductBean;
+import org.junit.Test;
+
+import javax.enterprise.inject.Produces;
+import javax.inject.Inject;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InterceptorBinding;
+import javax.interceptor.InvocationContext;
+import java.io.Serializable;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class InterceptedProducerFieldTest extends AbstractUnitTest {
+    @Inject
+    private MyProductBean bean;
+
+    @Test
+    public void testProductProducer() {
+        addInterceptor(BoundInterceptor.class);
+        startContainer(Collections.<Class<?>>singletonList(Producing.class),
Collections.<String>emptyList(), true);
+
+        BoundInterceptor.CALLED.set(false);
+
+        assertEquals(1234, bean.getI());
+        assertFalse(BoundInterceptor.CALLED.get()); // we access the field so no interception
+        getInstance(Producing.class).intercepted();
+        assertTrue(BoundInterceptor.CALLED.get()); // ensure interceptors were correctly
setup
+    }
+
+    @Bound
+    public static class Producing {
+        @Produces
+        MyProductBean product = new MyProductBean() {
+            @Override
+            public int getI() {
+                return 1234;
+            }
+        };
+
+        public void intercepted() {
+        }
+    }
+
+
+    @InterceptorBinding
+    @Target({ElementType.METHOD, ElementType.TYPE})
+    @Retention(RetentionPolicy.RUNTIME)
+    public @interface Bound {
+    }
+
+    @Bound
+    @Interceptor
+    public static class BoundInterceptor implements Serializable {
+        private static final AtomicBoolean CALLED = new AtomicBoolean();
+
+        @AroundInvoke
+        public Object measure(final InvocationContext ctx) throws Exception {
+            CALLED.set(true);
+            return ctx.proceed();
+        }
+    }
+}



Mime
View raw message