You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2023/01/26 18:39:35 UTC

[openwebbeans] branch main updated: OWB-1417 createInjectionTarget IllegalArgumentException on failure

This is an automated email from the ASF dual-hosted git repository.

struberg pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git


The following commit(s) were added to refs/heads/main by this push:
     new 1768aee36 OWB-1417 createInjectionTarget IllegalArgumentException on failure
1768aee36 is described below

commit 1768aee366c767695c2b7a6a4b920eaab0e429b2
Author: Mark Struberg <st...@apache.org>
AuthorDate: Thu Jan 26 19:37:54 2023 +0100

    OWB-1417 createInjectionTarget IllegalArgumentException on failure
    
    According to the spec createInjectionTarget should throw an IAE if a problem
    exists with an InjectionPoint.
---
 .../apache/webbeans/container/BeanManagerImpl.java |  2 +-
 .../container/InjectionTargetFactoryImpl.java      |  1 +
 .../ValidatingInjectionTargetFactory.java          | 57 ++++++++++++++++++++++
 webbeans-tck/testng-dev.xml                        |  4 +-
 4 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java b/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
index e11e9518c..945df6c7b 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
@@ -871,7 +871,7 @@ public class BeanManagerImpl implements BeanManager, Referenceable
 
     public <X> InjectionTargetFactory<X> getInjectionTargetFactory(AnnotatedType<X> type)
     {
-        return new InjectionTargetFactoryImpl<>(type, webBeansContext);
+        return new ValidatingInjectionTargetFactory<>(type, webBeansContext);
     }
 
     public <T> Bean<T> createBean(BeanAttributes<T> attributes, Class<T> type, InjectionTargetFactory<T> factory)
diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionTargetFactoryImpl.java b/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionTargetFactoryImpl.java
index 2763a3723..c8f46c007 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionTargetFactoryImpl.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionTargetFactoryImpl.java
@@ -64,6 +64,7 @@ public class InjectionTargetFactoryImpl<T> implements InjectionTargetFactory<T>
         AnnotatedType<T> at = getAnnotatedType();
         InjectionTargetImpl<T> injectionTarget
             = new InjectionTargetImpl<>(at, createInjectionPoints(bean), webBeansContext, getPostConstructMethods(), getPreDestroyMethods());
+
         if (ManagedBean.class.isInstance(bean))
         {
             ManagedBean.class.cast(bean).setOriginalInjectionTarget(injectionTarget);
diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/container/ValidatingInjectionTargetFactory.java b/webbeans-impl/src/main/java/org/apache/webbeans/container/ValidatingInjectionTargetFactory.java
new file mode 100644
index 000000000..f87b4c114
--- /dev/null
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/container/ValidatingInjectionTargetFactory.java
@@ -0,0 +1,57 @@
+/*
+ * 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.container;
+
+import java.util.Set;
+
+import jakarta.enterprise.inject.spi.AnnotatedType;
+import jakarta.enterprise.inject.spi.Bean;
+import jakarta.enterprise.inject.spi.InjectionPoint;
+import jakarta.enterprise.inject.spi.InjectionTarget;
+import org.apache.webbeans.config.WebBeansContext;
+
+/**
+ * InjectionTargetFactory which validates the craeted InjectionTarget.
+ * This is only required if the InjectionTarget gets created manually
+ * via the BeanManager.
+ */
+public class ValidatingInjectionTargetFactory<T> extends InjectionTargetFactoryImpl<T>
+{
+    public ValidatingInjectionTargetFactory(AnnotatedType<T> annotatedType, WebBeansContext webBeansContext)
+    {
+        super(annotatedType, webBeansContext);
+    }
+
+    @Override
+    public InjectionTarget<T> createInjectionTarget(Bean<T> bean)
+    {
+        final InjectionTarget<T> injectionTarget = super.createInjectionTarget(bean);
+        final Set<InjectionPoint> injectionPoints = injectionTarget.getInjectionPoints();
+        try
+        {
+            getWebBeansContext().getWebBeansUtil().validate(injectionPoints, null);
+        }
+        catch (Exception e)
+        {
+            // This is really a bit weird.
+            // The spec does mandate an IAE instead of a CDI DefinitionException for whatever reason.
+            throw new IllegalArgumentException("Problem while creating InjectionTarget", e);
+        }
+
+        return injectionTarget;
+    }
+}
diff --git a/webbeans-tck/testng-dev.xml b/webbeans-tck/testng-dev.xml
index 1be83ce4e..260787f45 100644
--- a/webbeans-tck/testng-dev.xml
+++ b/webbeans-tck/testng-dev.xml
@@ -18,9 +18,9 @@
 <suite name="JSR-346-TCK" verbose="2" configfailurepolicy="continue">
   <test name="JSR-346 TCK">
     <classes>
-          <class name="org.jboss.cdi.tck.tests.full.extensions.configurators.bean.alternativePriority.BeanConfiguratorAlternativePriorityTest" >
+          <class name="org.jboss.cdi.tck.tests.full.extensions.beanManager.BeanManagerTest" >
             <methods>
-              <include name=".*"/>
+              <include name="testObtainingInjectionTargetWithDefinitionError"/>
             </methods>
           </class>