Skip to content

Commit

Permalink
Merge pull request quarkusio#43069 from manovotn/issue42834
Browse files Browse the repository at this point in the history
Arc - support @ALL List<X> synthetic injection points
  • Loading branch information
mkouba authored Sep 9, 2024
2 parents ff9b902 + 9e43202 commit 7a16604
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private static void generateListBytecode(GeneratorContext ctx) {
InjectionPointInfo injectionPoint = ctx.injectionPoint;
if (injectionPoint.isField()) {
ctx.reflectionRegistration.registerField(injectionPoint.getAnnotationTarget().asField());
} else {
} else if (injectionPoint.isParam()) {
ctx.reflectionRegistration.registerMethod(injectionPoint.getAnnotationTarget().asMethodParameter().method());
}

Expand Down Expand Up @@ -475,6 +475,13 @@ private static void validateList(InjectionTargetInfo injectionTarget, InjectionP
// Note that at this point we can be sure that the required type is List<>
Type typeParam = injectionPoint.getType().asParameterizedType().arguments().get(0);
if (typeParam.kind() == Type.Kind.WILDCARD_TYPE) {
if (injectionPoint.isSynthetic()) {
errors.accept(
new DefinitionException(
"Wildcard is not a legal type argument for a synthetic @All List<?> injection point used in: "
+ injectionTarget.toString()));
return;
}
ClassInfo declaringClass;
if (injectionPoint.isField()) {
declaringClass = injectionPoint.getAnnotationTarget().asField().declaringClass();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.quarkus.arc.test.buildextension.beans;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

import jakarta.enterprise.util.TypeLiteral;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.ParameterizedType;
import org.jboss.jandex.WildcardType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.All;
import io.quarkus.arc.BeanCreator;
import io.quarkus.arc.SyntheticCreationalContext;
import io.quarkus.arc.processor.BeanRegistrar;
import io.quarkus.arc.test.ArcTestContainer;

public class SyntheticInjectionPointListAllInvalidTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder()
.beanRegistrars(new TestRegistrar())
.shouldFail()
.build();

@Test
public void testListAllInjection() {
assertNotNull(container.getFailure());
assertTrue(container.getFailure().getMessage().contains(
"Wildcard is not a legal type argument for a synthetic @All List<?> injection point"));
}

static class SyntheticBean {

public SyntheticBean(List<?> list) {
}
}

static class TestRegistrar implements BeanRegistrar {

@Override
public void register(RegistrationContext context) {
context.configure(SyntheticBean.class)
.addType(ClassType.create(DotName.createSimple(SyntheticBean.class)))
.creator(SynthBeanCreator.class)
// add injection point for @All List<?> - wildcard is NOT a legal bean type
.addInjectionPoint(ParameterizedType.create(List.class, WildcardType.UNBOUNDED),
AnnotationInstance.builder(All.class).build())
.unremovable()
.done();
}

}

public static class SynthBeanCreator implements BeanCreator<SyntheticBean> {

@Override
public SyntheticBean create(SyntheticCreationalContext<SyntheticBean> context) {
return new SyntheticBean(context.getInjectedReference(new TypeLiteral<List<?>>() {
}, All.Literal.INSTANCE));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.quarkus.arc.test.buildextension.beans;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.List;

import jakarta.enterprise.util.TypeLiteral;
import jakarta.inject.Singleton;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.ParameterizedType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.All;
import io.quarkus.arc.Arc;
import io.quarkus.arc.BeanCreator;
import io.quarkus.arc.SyntheticCreationalContext;
import io.quarkus.arc.processor.BeanRegistrar;
import io.quarkus.arc.test.ArcTestContainer;

public class SyntheticInjectionPointListAllTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder()
.beanClasses(SomeBean.class)
.beanRegistrars(new TestRegistrar()).build();

@Test
public void testListAllInjection() {
SyntheticBean synthBean = Arc.container().instance(SyntheticBean.class).get();
assertNotNull(synthBean);
List<SomeBean> list = synthBean.getList();
assertNotNull(list);
assertEquals(1, list.size());
}

@Singleton
static class SomeBean {

public String ping() {
return SomeBean.class.getSimpleName();
}

}

static class SyntheticBean {

private List<SomeBean> list;

public SyntheticBean(List<SomeBean> list) {
this.list = list;
}

public List<SomeBean> getList() {
return list;
}
}

static class TestRegistrar implements BeanRegistrar {

@Override
public void register(RegistrationContext context) {
context.configure(SyntheticBean.class)
.addType(ClassType.create(DotName.createSimple(SyntheticBean.class)))
.creator(SynthBeanCreator.class)
// add injection point for @All List<SomeBean>
.addInjectionPoint(ParameterizedType.create(List.class, ClassType.create(SomeBean.class)),
AnnotationInstance.builder(All.class).build())
.unremovable()
.done();
}

}

public static class SynthBeanCreator implements BeanCreator<SyntheticBean> {

@Override
public SyntheticBean create(SyntheticCreationalContext<SyntheticBean> context) {
return new SyntheticBean(context.getInjectedReference(new TypeLiteral<List<SomeBean>>() {
}, All.Literal.INSTANCE));
}

}
}

0 comments on commit 7a16604

Please sign in to comment.