openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1554526 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/inject/impl/ main/java/org/apache/webbeans/inject/instance/ main/java/org/apache/webbeans/portable/ test/java/org/apache/webbeans/newtests/injection/injectio...
Date Tue, 31 Dec 2013 14:40:46 GMT
Author: struberg
Date: Tue Dec 31 14:40:45 2013
New Revision: 1554526

URL: http://svn.apache.org/r1554526
Log:
OWB-922 create synthethic InjectionPoint 

initial version + a few tests. Still needs Instance#select improvements

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointOwnerInstance.java
Modified:
    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/inject/instance/InstanceImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/AbstractInjectionPointOwner.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointBeansOwner.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/tests/InjectionPointInjectionTest.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/instance/InstanceWithTypedTest.java

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=1554526&r1=1554525&r2=1554526&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
Tue Dec 31 14:40:45 2013
@@ -169,6 +169,14 @@ public class InjectionPointFactory
         return new InjectionPointImpl(owner, Arrays.asList(bindings), parameter);
     }
 
+    /**
+     * This method gets used for InjectionPoints needed during programmatic lookup.
+     */
+    public static InjectionPoint getVirtualInjectionPoint(Bean<?> bean)
+    {
+        return new InjectionPointImpl(bean.getBeanClass(), bean.getQualifiers());
+    }
+
     private void validateInitializerConstructor(AnnotatedConstructor<?> constructor)
     {
         for (AnnotatedParameter<?> parameter: constructor.getParameters())

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=1554526&r1=1554525&r2=1554526&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
Tue Dec 31 14:40:45 2013
@@ -81,13 +81,20 @@ class InjectionPointImpl implements Inje
     {
         this(ownerBean, parameter.getBaseType(), qualifiers, parameter, parameter.getDeclaringCallable().getJavaMember(),
parameter.isAnnotationPresent(Delegate.class), false);
     }
+
+    /**
+     * This constructor is used to create a 'virtual' InjectionPoint.
+     * This is needed if an InjectionPoint was needed during a programmatic lookup.
+     */
+    InjectionPointImpl(Type type, Collection<Annotation> qualifiers)
+    {
+        this(null, type, qualifiers, null, null, false, false);
+    }
     
     private InjectionPointImpl(Bean<?> ownerBean, Type type, Collection<Annotation>
qualifiers, Annotated annotated, Member member, boolean delegate, boolean isTransient)
     {
         Asserts.assertNotNull(type, "required type may not be null");
         Asserts.assertNotNull(qualifiers, "qualifiers may not be null");
-        Asserts.assertNotNull(annotated, "annotated may not be null");
-        Asserts.assertNotNull(member, "member may not be null");
         this.ownerBean = ownerBean;
         injectionType = type;
         qualifierAnnotations = Collections.unmodifiableSet(new HashSet<Annotation>(qualifiers));

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java?rev=1554526&r1=1554525&r2=1554526&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
Tue Dec 31 14:40:45 2013
@@ -205,18 +205,14 @@ public class InstanceImpl<T> implements 
     private Annotation[] getAdditionalQualifiers(Annotation[] qualifiers)
     {
         webBeansContext.getAnnotationManager().checkQualifierConditions(qualifiers);
+
         Set<Annotation> newQualifiers = new HashSet<Annotation>(qualifierAnnotations);
 
         if (qualifiers != null && qualifiers.length > 0)
         {
-            for (Annotation annot : qualifiers)
+            for (int i = 0; i < qualifiers.length; i++)
             {
-                if (newQualifiers.contains(annot))
-                {
-                    throw new IllegalArgumentException("Duplicate Qualifier Exception, "
+ toString());
-                }
-
-                newQualifiers.add(annot);
+                newQualifiers.add(qualifiers[i]);
             }
         }
 

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=1554526&r1=1554525&r2=1554526&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
Tue Dec 31 14:40:45 2013
@@ -22,6 +22,7 @@ import javax.enterprise.context.spi.Crea
 import javax.enterprise.inject.spi.InjectionPoint;
 
 import org.apache.webbeans.context.creational.CreationalContextImpl;
+import org.apache.webbeans.inject.impl.InjectionPointFactory;
 import org.apache.webbeans.util.ClassUtil;
 
 public class InjectionPointProducer extends AbstractProducer<InjectionPoint>
@@ -46,7 +47,16 @@ public class InjectionPointProducer exte
         }
         try
         {
-            return creationalContextImpl.getInjectionPoint();
+            InjectionPoint injectionPoint = creationalContextImpl.getInjectionPoint();
+
+            if (injectionPoint == null)
+            {
+                // create a 'virtual' Injection Point.
+                // This is required since CDI-1.1 and is being used in case of programmatic
lookups
+                injectionPoint = InjectionPointFactory.getVirtualInjectionPoint(((CreationalContextImpl<InjectionPoint>)
creationalContext).getBean());
+            }
+
+            return injectionPoint;
         }
         finally
         {

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/AbstractInjectionPointOwner.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/AbstractInjectionPointOwner.java?rev=1554526&r1=1554525&r2=1554526&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/AbstractInjectionPointOwner.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/AbstractInjectionPointOwner.java
Tue Dec 31 14:40:45 2013
@@ -17,11 +17,17 @@ package org.apache.webbeans.newtests.inj
 
 import javax.enterprise.inject.spi.InjectionPoint;
 
-public abstract class AbstractInjectionPointOwner {
-
+public abstract class AbstractInjectionPointOwner
+{
     protected InjectionPoint injectionPoint;
     
-    public String getName() {
+    public String getName()
+    {
         return injectionPoint.getMember().getName();
     }
+
+    public InjectionPoint getInjectionPoint()
+    {
+        return injectionPoint;
+    }
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointBeansOwner.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointBeansOwner.java?rev=1554526&r1=1554525&r2=1554526&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointBeansOwner.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointBeansOwner.java
Tue Dec 31 14:40:45 2013
@@ -23,22 +23,31 @@ public class InjectionPointBeansOwner {
 
     @Inject
     private ConstructorInjectionPointOwner constructorInjection;
+
     @Inject
     private FieldInjectionPointOwner fieldInjection;
+
     @Inject
     private MethodInjectionPointOwner methodInjection;
+
     @Inject
     private ProducerMethodInjectionPointOwner producerMethodInjection;
+
     @Inject
     private Instance<ConstructorInjectionPointOwner> constructorInjectionInstance;
+
     @Inject
     private Instance<FieldInjectionPointOwner> fieldInjectionInstance;
+
     @Inject
     private Instance<MethodInjectionPointOwner> methodInjectionInstance;
+
     @Inject
     private Instance<ProducerMethodInjectionPointOwner> producerMethodInjectionInstance;
+
     @Inject
     private Event<StringBuilder> observerInjection;
+
     @Inject
     private Event<StringBuffer> parameterizedObserverInjection;
     

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointOwnerInstance.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointOwnerInstance.java?rev=1554526&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointOwnerInstance.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/beans/InjectionPointOwnerInstance.java
Tue Dec 31 14:40:45 2013
@@ -0,0 +1,34 @@
+/*
+ * 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.injection.injectionpoint.beans;
+
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
+public class InjectionPointOwnerInstance
+{
+    @Inject
+    private Instance<MethodInjectionPointOwner> ipOwnerInstance;
+
+
+    public Instance<MethodInjectionPointOwner> getIpOwnerInstance()
+    {
+        return ipOwnerInstance;
+    }
+}

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/tests/InjectionPointInjectionTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/tests/InjectionPointInjectionTest.java?rev=1554526&r1=1554525&r2=1554526&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/tests/InjectionPointInjectionTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/injection/injectionpoint/tests/InjectionPointInjectionTest.java
Tue Dec 31 14:40:45 2013
@@ -15,21 +15,29 @@
  */
 package org.apache.webbeans.newtests.injection.injectionpoint.tests;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertNotNull;
 
 import java.util.ArrayList;
 import java.util.Collection;
 
 import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.AnnotatedField;
 import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.InjectionPoint;
 
+import org.apache.webbeans.annotation.DefaultLiteral;
 import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.apache.webbeans.newtests.injection.injectionpoint.beans.AbstractInjectionPointOwner;
 import org.apache.webbeans.newtests.injection.injectionpoint.beans.ConstructorInjectionPointOwner;
 import org.apache.webbeans.newtests.injection.injectionpoint.beans.FieldInjectionPointOwner;
 import org.apache.webbeans.newtests.injection.injectionpoint.beans.InjectionPointBeansOwner;
 import org.apache.webbeans.newtests.injection.injectionpoint.beans.InjectionPointObserver;
+import org.apache.webbeans.newtests.injection.injectionpoint.beans.InjectionPointOwnerInstance;
 import org.apache.webbeans.newtests.injection.injectionpoint.beans.InjectionPointOwnerProducer;
 import org.apache.webbeans.newtests.injection.injectionpoint.beans.MethodInjectionPointOwner;
 import org.apache.webbeans.newtests.injection.injectionpoint.beans.ProducerMethodInjectionPointOwner;
@@ -82,4 +90,38 @@ public class InjectionPointInjectionTest
         assertThat(pipo, is(notNullValue()));
         assertThat(pipo.getName(), is("pimp"));
     }
+
+    @Test
+    public void testDynamicResolvingInjectionPoint()
+    {
+        startContainer(InjectionPointOwnerProducer.class, ProducerMethodInjectionPointOwner.class,
AbstractInjectionPointOwner.class);
+
+        ProducerMethodInjectionPointOwner producedInstance = getInstance(ProducerMethodInjectionPointOwner.class);
+        assertThat(producedInstance, notNullValue());
+        InjectionPoint ip = producedInstance.getInjectionPoint();
+        assertThat(ip, notNullValue());
+        assertThat(ip.getAnnotated(), nullValue());
+    }
+
+
+    @Test
+    public void testDynamicResolvingInstanceInjectionPoint()
+    {
+        startContainer(InjectionPointOwnerInstance.class, InjectionPointOwnerProducer.class,
+                       ProducerMethodInjectionPointOwner.class, AbstractInjectionPointOwner.class,
+                       MethodInjectionPointOwner.class);
+
+        InjectionPointOwnerInstance producedInstanceOwner = getInstance(InjectionPointOwnerInstance.class);
+        assertThat(producedInstanceOwner, notNullValue());
+
+        MethodInjectionPointOwner ipOwner = producedInstanceOwner.getIpOwnerInstance().select(new
DefaultLiteral()).get();
+        assertThat(ipOwner, notNullValue());
+
+        InjectionPoint ip = ipOwner.getInjectionPoint();
+        assertNotNull(ip);
+        assertNotNull(ip.getAnnotated());
+        assertThat(ip.getAnnotated(), instanceOf(AnnotatedField.class));
+    }
+
+
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/instance/InstanceWithTypedTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/instance/InstanceWithTypedTest.java?rev=1554526&r1=1554525&r2=1554526&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/instance/InstanceWithTypedTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/instance/InstanceWithTypedTest.java
Tue Dec 31 14:40:45 2013
@@ -18,6 +18,7 @@
  */
 package org.apache.webbeans.newtests.instance;
 
+import org.apache.webbeans.annotation.DefaultLiteral;
 import org.apache.webbeans.newtests.AbstractUnitTest;
 import org.junit.Test;
 
@@ -45,21 +46,52 @@ public class InstanceWithTypedTest exten
         InstanceHolder instance = getInstance(InstanceHolder.class);
 
         assertNotNull(instance);
-        assertNotNull(instance.bean());
+        assertNotNull(instance.getBean());
 
         shutDownContainer();
     }
 
+    @Test
+    public void testDynamicInstanceResolving() {
+        startContainer(RealRunnable.class, TypedBean.class, InstanceHolder.class);
+
+        InstanceHolder ih = getInstance(InstanceHolder.class);
+        assertNotNull(ih);
+        assertNotNull(ih.getBean());
+
+        // now try to trigger the resolving programmatically
+        Instance<Runnable> instance = ih.getInstance();
+        Runnable r1 = instance.select(DefaultLiteral.INSTANCE).get();
+        assertNotNull(r1);
+
+        Instance<Runnable> instance2 = ih.getDefaultRunnableInstance();
+        Runnable r2 = instance2.select(DefaultLiteral.INSTANCE).get();
+        assertNotNull(r2);
+    }
+
     public static class InstanceHolder
     {
         @Inject
         @Any
         private Instance<Runnable> bean;
 
-        public Runnable bean()
+        @Inject
+        private Instance<Runnable> defaultRunnableInstance;
+
+        public Runnable getBean()
         {
             return bean.get();
         }
+
+        public Instance<Runnable> getInstance()
+        {
+            return bean;
+        }
+
+        public Instance<Runnable> getDefaultRunnableInstance()
+        {
+            return defaultRunnableInstance;
+        }
     }
 
     public static class RealRunnable implements Runnable



Mime
View raw message