You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2014/11/21 11:34:16 UTC

svn commit: r1640893 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/component/third/ main/java/org/apache/webbeans/inject/impl/ main/java/org/apache/webbeans/portable/ test/java/org/apache/webbeans/test/portable/

Author: rmannibucau
Date: Fri Nov 21 10:34:16 2014
New Revision: 1640893

URL: http://svn.apache.org/r1640893
Log:
allowing to get InjectionPoint from 3rd party bean#create

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/GetInjectionPointFromCustomBeanCreateTest.java
      - copied, changed from r1638706, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/CustomBeanDestroyCalledTest.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/third/ThirdpartyBeanImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/third/ThirdpartyBeanImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/third/ThirdpartyBeanImpl.java?rev=1640893&r1=1640892&r2=1640893&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/third/ThirdpartyBeanImpl.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/third/ThirdpartyBeanImpl.java Fri Nov 21 10:34:16 2014
@@ -46,7 +46,6 @@ public class ThirdpartyBeanImpl<T> exten
               bean.isNullable());
         
         this.bean = bean;
-        
     }
 
     @Override
@@ -92,7 +91,17 @@ public class ThirdpartyBeanImpl<T> exten
         {
             contextImpl = CreationalContextImpl.class.cast(context);
         }
-        final T t = bean.create(context);
+
+        final T t;
+        final Bean<T> oldBean = contextImpl.putBean(this);
+        try
+        {
+            t = bean.create(contextImpl);
+        }
+        finally
+        {
+            contextImpl.putBean(oldBean);
+        }
         if (getScope().equals(Dependent.class))
         {
             contextImpl.addDependent(this, t);

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java?rev=1640893&r1=1640892&r2=1640893&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java Fri Nov 21 10:34:16 2014
@@ -186,7 +186,7 @@ public class InjectionPointFactory
      */
     public static InjectionPoint getVirtualInjectionPoint(Bean<?> bean)
     {
-        return new InjectionPointImpl(bean.getBeanClass(), bean.getQualifiers());
+        return new InjectionPointImpl(bean);
     }
 
     private void validateInitializerConstructor(AnnotatedConstructor<?> constructor)

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java?rev=1640893&r1=1640892&r2=1640893&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java Fri Nov 21 10:34:16 2014
@@ -84,11 +84,20 @@ class InjectionPointImpl implements Inje
 
     /**
      * This constructor is used to create a 'virtual' InjectionPoint.
-     * This is needed if an InjectionPoint was needed during a programmatic lookup.
+     * This is needed if an InjectionPoint was needed during a programmatic lookup or 3rd party beans.
      */
-    InjectionPointImpl(Type type, Collection<Annotation> qualifiers)
+    InjectionPointImpl(Bean<?> bean)
     {
-        this(null, type, qualifiers, null, null, false, false);
+        Asserts.assertNotNull(bean, "bean may not be null");
+        this.ownerBean = bean;
+        this.injectionType = bean.getBeanClass();
+        this.qualifierAnnotations = bean.getQualifiers() == null ?
+                Collections.<Annotation>emptySet() :
+                Collections.unmodifiableSet(new HashSet<Annotation>(bean.getQualifiers()));
+        this.annotated = null;
+        this.injectionMember = null;
+        this.delegate = false;
+        this.transientt = false;
     }
     
     private InjectionPointImpl(Bean<?> ownerBean, Type type, Collection<Annotation> qualifiers, Annotated annotated, Member member, boolean delegate, boolean isTransient)

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java?rev=1640893&r1=1640892&r2=1640893&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java Fri Nov 21 10:34:16 2014
@@ -35,6 +35,7 @@ import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.Interceptor;
 
+import org.apache.webbeans.component.third.ThirdpartyBeanImpl;
 import org.apache.webbeans.context.creational.CreationalContextImpl;
 import org.apache.webbeans.util.ClassUtil;
 import org.apache.webbeans.util.OwbCustomObjectInputStream;
@@ -47,27 +48,36 @@ public class InjectionPointProducer exte
      * {@inheritDoc}
      */
     @Override
-    protected InjectionPoint produce(Map<Interceptor<?>, ?> interceptors, CreationalContextImpl<InjectionPoint> creationalContext)
+    protected InjectionPoint produce(Map<Interceptor<?>, ?> interceptors, CreationalContextImpl<InjectionPoint> creationalContextImpl)
     {
-        if (!(creationalContext instanceof CreationalContextImpl))
+        if (creationalContextImpl == null)
         {
             return null;
         }
+
         // the first injection point on the stack is of type InjectionPoint, so we need the second one
-        CreationalContextImpl<InjectionPoint> creationalContextImpl = (CreationalContextImpl<InjectionPoint>)creationalContext;
         InjectionPoint first = creationalContextImpl.removeInjectionPoint();
+        final InjectionPoint injectionPoint;
         if (!InjectionPoint.class.isAssignableFrom(ClassUtil.getClass(first.getType())))
         {
-            throw new IllegalStateException("Inconsistent injection point stack");
-        }
-        try
-        {
-            final InjectionPoint injectionPoint = creationalContextImpl.getInjectionPoint();
-            if (injectionPoint == null)
+            if (!ThirdpartyBeanImpl.class.isInstance(creationalContextImpl.getBean()))
             {
-                return null;
+                throw new IllegalStateException("Inconsistent injection point stack");
             }
+            injectionPoint = first;
+        }
+        else
+        {
+            injectionPoint = creationalContextImpl.getInjectionPoint();
+        }
 
+        if (injectionPoint == null)
+        {
+            return null;
+        }
+
+        try
+        {
             final Type type = injectionPoint.getType();
             if (ParameterizedType.class.isInstance(type))
             {

Copied: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/GetInjectionPointFromCustomBeanCreateTest.java (from r1638706, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/CustomBeanDestroyCalledTest.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/GetInjectionPointFromCustomBeanCreateTest.java?p2=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/GetInjectionPointFromCustomBeanCreateTest.java&p1=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/CustomBeanDestroyCalledTest.java&r1=1638706&r2=1640893&rev=1640893&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/CustomBeanDestroyCalledTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/GetInjectionPointFromCustomBeanCreateTest.java Fri Nov 21 10:34:16 2014
@@ -19,15 +19,10 @@
 package org.apache.webbeans.test.portable;
 
 import org.apache.webbeans.annotation.DefaultLiteral;
-import org.apache.webbeans.spi.ContextsService;
 import org.apache.webbeans.test.AbstractUnitTest;
-import org.junit.Assert;
 import org.junit.Test;
 
-import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
 import javax.enterprise.context.Dependent;
-import javax.enterprise.context.RequestScoped;
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
@@ -43,34 +38,30 @@ import java.lang.reflect.Type;
 import java.util.Collections;
 import java.util.Set;
 
-public class CustomBeanDestroyCalledTest extends AbstractUnitTest
+import static java.util.Collections.singleton;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class GetInjectionPointFromCustomBeanCreateTest extends AbstractUnitTest
 {
 
     @Test
-    public void dependentScopedBeanTest()
+    public void doTest()
     {
-        addExtension(new ExternalBeanExtension());
+        addExtension(new AddBeanExtension());
         startContainer(BeanHolder.class);
 
-        final ContextsService contextsService = getWebBeansContext().getContextsService();
-
-        contextsService.startContext(RequestScoped.class, null);
-
         final BeanHolder beanHolder = getInstance(BeanHolder.class);
-        Assert.assertNotNull(beanHolder.getManuelBean());
-        Assert.assertTrue(beanHolder.getManuelBean().isConstructCalled());
-
-        ManuelBean refToPreviousManuelBean = beanHolder.getManuelBean();
-
-        contextsService.endContext(RequestScoped.class, null);
-
-        Assert.assertTrue(refToPreviousManuelBean.isLifecycleDestroyCalled());
-        Assert.assertTrue(refToPreviousManuelBean.isDestroyCalled());
+        final InjectionPoint ip = beanHolder.getManuelBean().ip;
+        assertNotNull(ip);
+        assertEquals(singleton(new DefaultLiteral()), ip.getQualifiers());
+        assertEquals(ManuelBean.class, ip.getType());
+        assertEquals(BeanHolder.class, ip.getBean().getBeanClass());
     }
 
-    public static class ExternalBeanExtension implements Extension
+    public static class AddBeanExtension implements Extension
     {
-        protected void addBeans(@Observes AfterBeanDiscovery abd, BeanManager beanManager)
+        protected void addBeans(@Observes final AfterBeanDiscovery abd, final BeanManager beanManager)
         {
             final AnnotatedType<ManuelBean> annotatedType = beanManager.createAnnotatedType(ManuelBean.class);
 
@@ -132,18 +123,17 @@ public class CustomBeanDestroyCalledTest
                 }
 
                 @Override
-                public ManuelBean create(CreationalContext<ManuelBean> manuelBeanCreationalContext)
+                public ManuelBean create(final CreationalContext<ManuelBean> manuelBeanCreationalContext)
                 {
                     final ManuelBean result = new ManuelBean();
-                    injectionTarget.postConstruct(result);
+                    final Bean<InjectionPoint> bean = (Bean<InjectionPoint>) beanManager.resolve(beanManager.getBeans(InjectionPoint.class));
+                    result.ip = (InjectionPoint) beanManager.getReference(bean, InjectionPoint.class, manuelBeanCreationalContext);
                     return result;
                 }
 
                 @Override
-                public void destroy(ManuelBean manuelBean, CreationalContext<ManuelBean> manuelBeanCreationalContext)
+                public void destroy(final ManuelBean manuelBean, final CreationalContext<ManuelBean> manuelBeanCreationalContext)
                 {
-                    manuelBean.setLifecycleDestroyCalled(true);
-                    injectionTarget.preDestroy(manuelBean);
                     manuelBeanCreationalContext.release();
                 }
             };
@@ -151,7 +141,7 @@ public class CustomBeanDestroyCalledTest
         }
 
     }
-    @RequestScoped
+
     public static class BeanHolder
     {
         @Inject
@@ -165,40 +155,6 @@ public class CustomBeanDestroyCalledTest
 
     public static class ManuelBean
     {
-        private boolean constructCalled;
-        private boolean destroyCalled;
-        private boolean lifecycleDestroyCalled;
-
-        @PostConstruct
-        protected void onConstruct()
-        {
-            this.constructCalled = true;
-        }
-
-        @PreDestroy
-        protected void onDestroy()
-        {
-            this.destroyCalled = true;
-        }
-
-        public boolean isConstructCalled()
-        {
-            return constructCalled;
-        }
-
-        public boolean isDestroyCalled()
-        {
-            return destroyCalled;
-        }
-
-        public boolean isLifecycleDestroyCalled()
-        {
-            return lifecycleDestroyCalled;
-        }
-
-        public void setLifecycleDestroyCalled(boolean lifecycleDestroyCalled)
-        {
-            this.lifecycleDestroyCalled = lifecycleDestroyCalled;
-        }
+        private InjectionPoint ip;
     }
 }