diff --git a/impl/src/main/java/org/jboss/weld/annotated/enhanced/EnhancedAnnotatedType.java b/impl/src/main/java/org/jboss/weld/annotated/enhanced/EnhancedAnnotatedType.java index b264b6582b6..e6f601b096b 100644 --- a/impl/src/main/java/org/jboss/weld/annotated/enhanced/EnhancedAnnotatedType.java +++ b/impl/src/main/java/org/jboss/weld/annotated/enhanced/EnhancedAnnotatedType.java @@ -69,7 +69,7 @@ public interface EnhancedAnnotatedType extends EnhancedAnnotated> * @return A set of abstracted fields with the given annotation. Returns an * empty set if there are no matches */ - Collection> getEnhancedFields(Class annotationType); + Collection> getEnhancedFields(Class annotationType); /** * Gets all fields declared on this class only. diff --git a/impl/src/main/java/org/jboss/weld/annotated/enhanced/jlr/EnhancedAnnotatedTypeImpl.java b/impl/src/main/java/org/jboss/weld/annotated/enhanced/jlr/EnhancedAnnotatedTypeImpl.java index bda1bc1bba8..3bb9403ef87 100644 --- a/impl/src/main/java/org/jboss/weld/annotated/enhanced/jlr/EnhancedAnnotatedTypeImpl.java +++ b/impl/src/main/java/org/jboss/weld/annotated/enhanced/jlr/EnhancedAnnotatedTypeImpl.java @@ -95,7 +95,7 @@ public class EnhancedAnnotatedTypeImpl extends AbstractEnhancedAnnotated> fields; // The map from annotation type to abstracted field with annotation - private final Multimap, EnhancedAnnotatedField> annotatedFields; + private final Multimap, EnhancedAnnotatedField> annotatedFields; // The set of abstracted fields private final Set> declaredFields; @@ -189,7 +189,7 @@ protected EnhancedAnnotatedTypeImpl(SlimAnnotatedType annotatedType, } this.declaredFields = new HashSet>(declaredFieldsTemp); } else { - Multimap, EnhancedAnnotatedField> annotatedFields = new ListMultimap, EnhancedAnnotatedField>(); + Multimap, EnhancedAnnotatedField> annotatedFields = new ListMultimap<>(); fieldsTemp = new HashSet>(); for (AnnotatedField annotatedField : annotatedType.getFields()) { EnhancedAnnotatedField weldField = EnhancedAnnotatedFieldImpl.of(annotatedField, this, @@ -443,10 +443,10 @@ public EnhancedAnnotatedConstructor getDeclaredEnhancedConstructor(Constructo * @return A set of matching abstracted fields, null if none are found. */ @Override - public Collection> getEnhancedFields(Class annotationType) { + public Collection> getEnhancedFields(Class annotationType) { if (annotatedFields == null) { // Build collection from class hierarchy - ArrayList> aggregatedFields = new ArrayList>( + ArrayList> aggregatedFields = new ArrayList<>( this.declaredAnnotatedFields.get(annotationType)); if ((superclass != null) && (superclass.getJavaClass() != Object.class)) { aggregatedFields.addAll(superclass.getEnhancedFields(annotationType)); diff --git a/impl/src/main/java/org/jboss/weld/injection/InjectionPointFactory.java b/impl/src/main/java/org/jboss/weld/injection/InjectionPointFactory.java index 8c2a9320080..bea9241394e 100644 --- a/impl/src/main/java/org/jboss/weld/injection/InjectionPointFactory.java +++ b/impl/src/main/java/org/jboss/weld/injection/InjectionPointFactory.java @@ -189,13 +189,14 @@ public MethodInjectionPoint createMethodInjectionPoint(MethodInject /* * Utility methods for field InjectionPoints */ - public List>> getFieldInjectionPoints(Bean declaringBean, EnhancedAnnotatedType type, + public List>> getFieldInjectionPoints(Bean declaringBean, + EnhancedAnnotatedType type, BeanManagerImpl manager) { List>> injectableFieldsList = new ArrayList>>(); if (type.slim() instanceof UnbackedAnnotatedType) { // external AnnotatedTypes require special treatment - Collection> allFields = type.getEnhancedFields(Inject.class); + Collection> allFields = type.getEnhancedFields(Inject.class); for (Class clazz = type.getJavaClass(); clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) { ImmutableSet.Builder> fields = ImmutableSet.builder(); diff --git a/impl/src/main/java/org/jboss/weld/injection/ResourceInjectionProcessor.java b/impl/src/main/java/org/jboss/weld/injection/ResourceInjectionProcessor.java index d282a411937..1a742b2aa69 100644 --- a/impl/src/main/java/org/jboss/weld/injection/ResourceInjectionProcessor.java +++ b/impl/src/main/java/org/jboss/weld/injection/ResourceInjectionProcessor.java @@ -90,8 +90,7 @@ protected Set> createResourceInjections(Bean declari } Class marker = getMarkerAnnotation(processorContext); - - final Collection> fields = type.getDeclaredEnhancedFields(marker); + final Collection> fields = type.getEnhancedFields(marker); final Collection> methods = type.getDeclaredEnhancedMethods(marker); return createResourceInjections(fields, methods, declaringBean, type.getJavaClass(), manager); diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/Foo.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/Foo.java new file mode 100644 index 00000000000..7b320b7f46d --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/Foo.java @@ -0,0 +1,14 @@ +package org.jboss.weld.tests.injectionPoint.resource.extension; + +import jakarta.annotation.Resource; +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class Foo { + @Resource(name = "org.jboss.weld.tests.reproducer.Foo/world") + private String value; + + public String value() { + return "hello " + value; + } +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/FooSpecialized.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/FooSpecialized.java new file mode 100644 index 00000000000..3fd0ecab6e2 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/FooSpecialized.java @@ -0,0 +1,9 @@ +package org.jboss.weld.tests.injectionPoint.resource.extension; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Specializes; + +@ApplicationScoped +@Specializes +public class FooSpecialized extends Foo { +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/FooSpecializedNoBeanDef.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/FooSpecializedNoBeanDef.java new file mode 100644 index 00000000000..8d74c464552 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/FooSpecializedNoBeanDef.java @@ -0,0 +1,8 @@ +package org.jboss.weld.tests.injectionPoint.resource.extension; + +import jakarta.enterprise.inject.Specializes; + +// no bean defining annotation - this bean is not picked up via discovery but is instead registered via extension +@Specializes +public class FooSpecializedNoBeanDef extends Foo { +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/MyExtension.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/MyExtension.java new file mode 100644 index 00000000000..1a4ccc3f880 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/MyExtension.java @@ -0,0 +1,14 @@ +package org.jboss.weld.tests.injectionPoint.resource.extension; + +import jakarta.enterprise.event.Observes; +import jakarta.enterprise.inject.spi.AfterTypeDiscovery; +import jakarta.enterprise.inject.spi.BeanManager; +import jakarta.enterprise.inject.spi.Extension; + +public class MyExtension implements Extension { + + void afterTypeDiscovery(@Observes AfterTypeDiscovery event, BeanManager bm) { + event.addAnnotatedType(bm.createAnnotatedType(FooSpecializedNoBeanDef.class), + FooSpecializedNoBeanDef.class.getName() + "_synth"); + } +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/SpecializationDiscoveryTest.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/SpecializationDiscoveryTest.java new file mode 100644 index 00000000000..0cf18887816 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/SpecializationDiscoveryTest.java @@ -0,0 +1,46 @@ +package org.jboss.weld.tests.injectionPoint.resource.extension; + +import static org.junit.Assert.assertEquals; + +import jakarta.inject.Inject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.weld.test.util.Utils; +import org.jboss.weld.tests.category.Integration; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +/** + * Test inheritance of {@code @Resource} field when adding a specialized bean through discovery versus through extension + * See https://issues.redhat.com/browse/WELD-2798 + */ +@Category(Integration.class) +@RunWith(Arquillian.class) +public class SpecializationDiscoveryTest { + + @Deployment + public static Archive deploy() { + return ShrinkWrap + .create(WebArchive.class, + Utils.getDeploymentNameAsHash(SpecializationDiscoveryTest.class, Utils.ARCHIVE_TYPE.WAR)) + .addClasses(Foo.class, FooSpecialized.class, SpecializationDiscoveryTest.class) + .addAsWebInfResource(SpecializationDiscoveryTest.class.getPackage(), "web.xml", "web.xml"); + } + + @Inject + Foo foo; + + @Test + public void test() { + // foo is an instance of the specialized bean + Assert.assertTrue(foo instanceof FooSpecialized); + // resource has been injected + assertEquals("hello world", foo.value()); + } +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/SpecializationExtensionTest.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/SpecializationExtensionTest.java new file mode 100644 index 00000000000..9aa5b891390 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/injectionPoint/resource/extension/SpecializationExtensionTest.java @@ -0,0 +1,53 @@ +package org.jboss.weld.tests.injectionPoint.resource.extension; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import jakarta.enterprise.inject.spi.Extension; +import jakarta.inject.Inject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.weld.test.util.Utils; +import org.jboss.weld.tests.category.Integration; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +/** + * Test inheritance of {@code @Resource} field when adding a specialized bean through discovery versus through extension + * See https://issues.redhat.com/browse/WELD-2798 + */ +@Category(Integration.class) +@RunWith(Arquillian.class) +public class SpecializationExtensionTest { + + @Deployment + public static Archive deploy() { + return ShrinkWrap + .create(WebArchive.class, + Utils.getDeploymentNameAsHash(SpecializationExtensionTest.class, Utils.ARCHIVE_TYPE.WAR)) + .addClasses(Foo.class, FooSpecializedNoBeanDef.class, MyExtension.class, + SpecializationExtensionTest.class) + .addAsServiceProvider(Extension.class, MyExtension.class) + .addAsWebInfResource(SpecializationExtensionTest.class.getPackage(), "web.xml", "web.xml") + // archive has an extension, it also needs beans.xml to be considered bean archive + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); + + } + + @Inject + Foo foo; + + @Test + public void test() { + // foo is an instance of the specialized bean + assertTrue(foo instanceof FooSpecializedNoBeanDef); + // resource has been injected + assertEquals("hello world", foo.value()); + } +} diff --git a/tests-arquillian/src/test/resources/org/jboss/weld/tests/injectionPoint/resource/extension/web.xml b/tests-arquillian/src/test/resources/org/jboss/weld/tests/injectionPoint/resource/extension/web.xml new file mode 100644 index 00000000000..b90c178acfa --- /dev/null +++ b/tests-arquillian/src/test/resources/org/jboss/weld/tests/injectionPoint/resource/extension/web.xml @@ -0,0 +1,15 @@ + + + + Resource Injection Tests + + + org.jboss.weld.tests.reproducer.Foo/world + java.lang.String + world + + + \ No newline at end of file diff --git a/tests-common/src/main/java/org/jboss/weld/test/util/annotated/ForwardingWeldClass.java b/tests-common/src/main/java/org/jboss/weld/test/util/annotated/ForwardingWeldClass.java index ca2ca398a4c..e149bdb7402 100644 --- a/tests-common/src/main/java/org/jboss/weld/test/util/annotated/ForwardingWeldClass.java +++ b/tests-common/src/main/java/org/jboss/weld/test/util/annotated/ForwardingWeldClass.java @@ -65,7 +65,7 @@ public Collection> getEnhancedConstructors() { return delegate().getEnhancedConstructors(); } - public Collection> getEnhancedFields(Class annotationType) { + public Collection> getEnhancedFields(Class annotationType) { return delegate().getEnhancedFields(annotationType); }