From 04823297a137666d1f319c48cf147cb07e64dd01 Mon Sep 17 00:00:00 2001 From: Matej Novotny Date: Wed, 17 Jan 2024 14:34:55 +0100 Subject: [PATCH] WELD-2773 Prevent producer field inheritance when extension manipulates the annotated type. --- .../jlr/EnhancedAnnotatedTypeImpl.java | 4 +- .../producer/field/FieldProducerBean.java | 13 +++++ .../field/FieldProducerBeanSubclass.java | 7 +++ .../field/FieldProducerExtensionTest.java | 57 +++++++++++++++++++ .../tests/producer/field/MyExtension.java | 15 +++++ 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerBean.java create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerBeanSubclass.java create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerExtensionTest.java create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/MyExtension.java 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 1a4f2c3847..bda1bc1bba 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 @@ -195,12 +195,12 @@ protected EnhancedAnnotatedTypeImpl(SlimAnnotatedType annotatedType, EnhancedAnnotatedField weldField = EnhancedAnnotatedFieldImpl.of(annotatedField, this, classTransformer); fieldsTemp.add(weldField); - if (annotatedField.getDeclaringType().getJavaClass().equals(javaClass)) { + if (annotatedField.getJavaMember().getDeclaringClass().equals(javaClass)) { declaredFieldsTemp.add(weldField); } for (Annotation annotation : weldField.getAnnotations()) { annotatedFields.put(annotation.annotationType(), weldField); - if (annotatedField.getDeclaringType().getJavaClass().equals(javaClass)) { + if (annotatedField.getJavaMember().getDeclaringClass().equals(javaClass)) { declaredAnnotatedFields.put(annotation.annotationType(), weldField); } } diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerBean.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerBean.java new file mode 100644 index 0000000000..a9e309ad2c --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerBean.java @@ -0,0 +1,13 @@ +package org.jboss.weld.tests.producer.field; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.Dependent; +import jakarta.enterprise.inject.Produces; + +@Dependent +public class FieldProducerBean { + + @Produces + @ApplicationScoped + FieldProducerExtensionTest.Foo p = new FieldProducerExtensionTest.Foo(); +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerBeanSubclass.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerBeanSubclass.java new file mode 100644 index 0000000000..692cc1a85e --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerBeanSubclass.java @@ -0,0 +1,7 @@ +package org.jboss.weld.tests.producer.field; + +import jakarta.enterprise.context.Dependent; + +@Dependent +public class FieldProducerBeanSubclass extends FieldProducerBean { +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerExtensionTest.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerExtensionTest.java new file mode 100644 index 0000000000..174a062c79 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/FieldProducerExtensionTest.java @@ -0,0 +1,57 @@ +package org.jboss.weld.tests.producer.field; + +import java.util.List; +import java.util.stream.Collectors; + +import jakarta.enterprise.inject.Instance; +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.BeanArchive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.weld.test.util.Utils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Field producer should not be inherited into bean subclass. In this scenario, a CDI extension modifies the type + * which leads to a registration of a new annotated type that we need to correctly parse. + * + * See https://issues.redhat.com/browse/WELD-2773 + */ +@RunWith(Arquillian.class) +public class FieldProducerExtensionTest { + + @Deployment + public static Archive deploy() { + return ShrinkWrap.create(BeanArchive.class, Utils.getDeploymentNameAsHash(FieldProducerExtensionTest.class)) + .addPackage(FieldProducerExtensionTest.class.getPackage()) + .addAsServiceProvider(Extension.class, MyExtension.class); + } + + @Inject + Instance instance; + + @Inject + Foo foo; + + @Test + public void test() { + // assert extension works + Assert.assertEquals(2, MyExtension.extensionTriggered); + // assert producer works + Assert.assertNotNull(foo); + + // assert both beans are there + List> collect = instance.select(FieldProducerBean.class).handlesStream() + .collect(Collectors.toList()); + Assert.assertEquals(2, collect.size()); + } + + public static class Foo { + } +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/MyExtension.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/MyExtension.java new file mode 100644 index 0000000000..4ebe01ee1b --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/producer/field/MyExtension.java @@ -0,0 +1,15 @@ +package org.jboss.weld.tests.producer.field; + +import jakarta.enterprise.event.Observes; +import jakarta.enterprise.inject.spi.Extension; +import jakarta.enterprise.inject.spi.ProcessAnnotatedType; + +public class MyExtension implements Extension { + + public static int extensionTriggered = 0; + + public void observe(@Observes ProcessAnnotatedType pat) { + extensionTriggered++; + pat.configureAnnotatedType(); + } +}