Skip to content

Commit

Permalink
WELD-2773 Prevent producer field inheritance when extension manipulat…
Browse files Browse the repository at this point in the history
…es the annotated type.
  • Loading branch information
manovotn committed Jan 22, 2024
1 parent 62b468f commit f324a81
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ protected EnhancedAnnotatedTypeImpl(SlimAnnotatedType<T> annotatedType,
EnhancedAnnotatedField<?, ? super T> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jboss.weld.tests.producer.field;

import jakarta.enterprise.context.Dependent;

@Dependent
public class FieldProducerBeanSubclass extends FieldProducerBean {
}
Original file line number Diff line number Diff line change
@@ -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<Object> 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<? extends Instance.Handle<FieldProducerBean>> collect = instance.select(FieldProducerBean.class).handlesStream()
.collect(Collectors.toList());
Assert.assertEquals(2, collect.size());
}

public static class Foo {
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends FieldProducerBean> pat) {
extensionTriggered++;
pat.configureAnnotatedType();
}
}

0 comments on commit f324a81

Please sign in to comment.