Skip to content

Commit

Permalink
ArC bean resolution - handle qualifier default values correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Sep 13, 2021
1 parent 994e164 commit 8a85f14
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ static boolean hasQualifier(BeanDeployment beanDeployment, AnnotationInstance re
ClassInfo requiredClazz = beanDeployment.getQualifier(requiredQualifier.name());
List<AnnotationValue> values = new ArrayList<>();
Set<String> nonBindingFields = beanDeployment.getQualifierNonbindingMembers(requiredQualifier.name());
for (AnnotationValue val : requiredQualifier.values()) {
for (AnnotationValue val : requiredQualifier.valuesWithDefaults(beanDeployment.getBeanArchiveIndex())) {
if (!requiredClazz.method(val.name()).hasAnnotation(DotNames.NONBINDING)
&& !nonBindingFields.contains(val.name())) {
values.add(val);
Expand All @@ -695,7 +695,7 @@ static boolean hasQualifier(BeanDeployment beanDeployment, AnnotationInstance re
// Must have the same annotation member value for each member which is not annotated @Nonbinding
boolean matches = true;
for (AnnotationValue value : values) {
if (!value.equals(qualifier.value(value.name()))) {
if (!value.equals(qualifier.valueWithDefault(beanDeployment.getBeanArchiveIndex(), value.name()))) {
matches = false;
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.arc.test.qualifiers.defaultvalues;

public interface Animal {

int noOfLeg();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.arc.test.qualifiers.defaultvalues;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.TYPE })
public @interface AnimalQualifier {

String value() default "";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.arc.test.qualifiers.defaultvalues;

import javax.enterprise.context.Dependent;

@Dependent
@AnimalQualifier("cat")
public class Cat implements Animal {

@Override
public int noOfLeg() {
return 4;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.arc.test.qualifiers.defaultvalues;

import javax.enterprise.context.Dependent;

@Dependent
@AnimalQualifier
public class Owl implements Animal {

@Override
public int noOfLeg() {
return 2;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.arc.test.qualifiers.defaultvalues;

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

import io.quarkus.arc.Arc;
import io.quarkus.arc.test.ArcTestContainer;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class QualifierDefaultValuesTest {

@RegisterExtension
public ArcTestContainer container = new ArcTestContainer(Consumer.class, Animal.class, AnimalQualifier.class, Cat.class,
Owl.class);

@Test
public void testDefaultValues() {
Consumer consumer = Arc.container().instance(Consumer.class).get();
assertEquals(2, consumer.animal.noOfLeg());
}

@Dependent
public static class Consumer {

@Inject
@AnimalQualifier
Animal animal;

}

}

0 comments on commit 8a85f14

Please sign in to comment.