Skip to content

Commit

Permalink
WELD-2798 Correct injection into injected @resource fields; add autom…
Browse files Browse the repository at this point in the history
…ated test
  • Loading branch information
manovotn committed Sep 30, 2024
1 parent 895bcd6 commit 342acb0
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public interface EnhancedAnnotatedType<T> extends EnhancedAnnotated<T, Class<T>>
* @return A set of abstracted fields with the given annotation. Returns an
* empty set if there are no matches
*/
Collection<EnhancedAnnotatedField<?, ?>> getEnhancedFields(Class<? extends Annotation> annotationType);
Collection<EnhancedAnnotatedField<?, ? super T>> getEnhancedFields(Class<? extends Annotation> annotationType);

/**
* Gets all fields declared on this class only.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class EnhancedAnnotatedTypeImpl<T> extends AbstractEnhancedAnnotated<T, C
// The set of abstracted fields
private final Set<EnhancedAnnotatedField<?, ? super T>> fields;
// The map from annotation type to abstracted field with annotation
private final Multimap<Class<? extends Annotation>, EnhancedAnnotatedField<?, ?>> annotatedFields;
private final Multimap<Class<? extends Annotation>, EnhancedAnnotatedField<?, ? super T>> annotatedFields;

// The set of abstracted fields
private final Set<EnhancedAnnotatedField<?, ? super T>> declaredFields;
Expand Down Expand Up @@ -189,7 +189,7 @@ protected EnhancedAnnotatedTypeImpl(SlimAnnotatedType<T> annotatedType,
}
this.declaredFields = new HashSet<EnhancedAnnotatedField<?, ? super T>>(declaredFieldsTemp);
} else {
Multimap<Class<? extends Annotation>, EnhancedAnnotatedField<?, ?>> annotatedFields = new ListMultimap<Class<? extends Annotation>, EnhancedAnnotatedField<?, ?>>();
Multimap<Class<? extends Annotation>, EnhancedAnnotatedField<?, ? super T>> annotatedFields = new ListMultimap<>();
fieldsTemp = new HashSet<EnhancedAnnotatedField<?, ? super T>>();
for (AnnotatedField<? super T> annotatedField : annotatedType.getFields()) {
EnhancedAnnotatedField<?, ? super T> weldField = EnhancedAnnotatedFieldImpl.of(annotatedField, this,
Expand Down Expand Up @@ -443,10 +443,10 @@ public EnhancedAnnotatedConstructor<T> getDeclaredEnhancedConstructor(Constructo
* @return A set of matching abstracted fields, null if none are found.
*/
@Override
public Collection<EnhancedAnnotatedField<?, ?>> getEnhancedFields(Class<? extends Annotation> annotationType) {
public Collection<EnhancedAnnotatedField<?, ? super T>> getEnhancedFields(Class<? extends Annotation> annotationType) {
if (annotatedFields == null) {
// Build collection from class hierarchy
ArrayList<EnhancedAnnotatedField<?, ?>> aggregatedFields = new ArrayList<EnhancedAnnotatedField<?, ?>>(
ArrayList<EnhancedAnnotatedField<?, ? super T>> aggregatedFields = new ArrayList<>(
this.declaredAnnotatedFields.get(annotationType));
if ((superclass != null) && (superclass.getJavaClass() != Object.class)) {
aggregatedFields.addAll(superclass.getEnhancedFields(annotationType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,14 @@ public <T, X> MethodInjectionPoint<T, X> createMethodInjectionPoint(MethodInject
/*
* Utility methods for field InjectionPoints
*/
public List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPoints(Bean<?> declaringBean, EnhancedAnnotatedType<?> type,
public <T> List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPoints(Bean<?> declaringBean,
EnhancedAnnotatedType<T> type,
BeanManagerImpl manager) {
List<Set<FieldInjectionPoint<?, ?>>> injectableFieldsList = new ArrayList<Set<FieldInjectionPoint<?, ?>>>();

if (type.slim() instanceof UnbackedAnnotatedType<?>) {
// external AnnotatedTypes require special treatment
Collection<EnhancedAnnotatedField<?, ?>> allFields = type.getEnhancedFields(Inject.class);
Collection<EnhancedAnnotatedField<?, ? super T>> allFields = type.getEnhancedFields(Inject.class);

for (Class<?> clazz = type.getJavaClass(); clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) {
ImmutableSet.Builder<FieldInjectionPoint<?, ?>> fields = ImmutableSet.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ protected <T> Set<ResourceInjection<?>> createResourceInjections(Bean<?> declari
}

Class<? extends Annotation> marker = getMarkerAnnotation(processorContext);

final Collection<EnhancedAnnotatedField<?, ? super T>> fields = type.getDeclaredEnhancedFields(marker);
final Collection<EnhancedAnnotatedField<?, ? super T>> fields = type.getEnhancedFields(marker);
final Collection<EnhancedAnnotatedMethod<?, ? super T>> methods = type.getDeclaredEnhancedMethods(marker);

return createResourceInjections(fields, methods, declaringBean, type.getJavaClass(), manager);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>Resource Injection Tests</display-name>

<env-entry>
<env-entry-name>org.jboss.weld.tests.reproducer.Foo/world</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>world</env-entry-value>
</env-entry>

</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Collection<EnhancedAnnotatedConstructor<T>> getEnhancedConstructors() {
return delegate().getEnhancedConstructors();
}

public Collection<EnhancedAnnotatedField<?, ?>> getEnhancedFields(Class<? extends Annotation> annotationType) {
public Collection<EnhancedAnnotatedField<?, ? super T>> getEnhancedFields(Class<? extends Annotation> annotationType) {
return delegate().getEnhancedFields(annotationType);
}

Expand Down

0 comments on commit 342acb0

Please sign in to comment.