Skip to content

Commit

Permalink
Merge pull request quarkusio#11936 from mkouba/issue-11892
Browse files Browse the repository at this point in the history
Beans enabled via build profile/properties should not be alternatives
  • Loading branch information
mkouba authored Sep 9, 2020
2 parents 70f40d9 + ba1315f commit 0e09f14
Showing 1 changed file with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jboss.logging.Logger;

import io.quarkus.arc.processor.AnnotationsTransformer;
import io.quarkus.arc.processor.AnnotationsTransformer.TransformationContext;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.arc.processor.Transformation;
import io.quarkus.arc.profile.IfBuildProfile;
Expand Down Expand Up @@ -146,18 +147,18 @@ public void transform(TransformationContext ctx) {
if (ctx.isClass()) {
DotName classDotName = target.asClass().name();
if (classTargets.containsKey(classDotName)) {
transformBean(target, ctx.transform(), classTargets.get(classDotName));
transformBean(target, ctx, classTargets.get(classDotName));
}
} else if (ctx.isMethod()) {
MethodInfo method = target.asMethod();
if (methodTargets.containsKey(method)) {
transformBean(target, ctx.transform(), methodTargets.get(method));
transformBean(target, ctx, methodTargets.get(method));
}
} else if (ctx.isField()) {
FieldInfo field = target.asField();
String uniqueFieldName = toUniqueString(field);
if (fieldTargets.containsKey(uniqueFieldName)) {
transformBean(target, ctx.transform(), fieldTargets.get(uniqueFieldName));
transformBean(target, ctx, fieldTargets.get(uniqueFieldName));
}
}
}
Expand All @@ -168,32 +169,18 @@ private String toUniqueString(FieldInfo field) {
return field.declaringClass().name().toString() + "." + field.name();
}

private void transformBean(AnnotationTarget target, Transformation transform, boolean enable) {
if (enable) {
enableBean(transform);
} else {
disableBean(target, transform);
private void transformBean(AnnotationTarget target, TransformationContext ctx, boolean enabled) {
if (!enabled) {
Transformation transform = ctx.transform();
if (target.kind() == Kind.CLASS) {
// Veto the class
transform.add(DotNames.VETOED);
} else {
// Add @Alternative to the producer
transform.add(DotNames.ALTERNATIVE);
}
transform.done();
}
transform.done();
}

private void enableBean(Transformation transform) {
transform.add(DotNames.ALTERNATIVE_PRIORITY,
createAlternativePriority());
}

private AnnotationValue createAlternativePriority() {
// use Integer.MAX_VALUE - 1 to avoid having conflicts with enabling beans via config
return AnnotationValue.createIntegerValue("value", Integer.MAX_VALUE - 1);
}

private void disableBean(AnnotationTarget target, Transformation transform) {
if (target.kind() == Kind.CLASS) {
// Veto the class
transform.add(DotNames.VETOED);
} else {
// Add @Alternative to the producer
transform.add(DotNames.ALTERNATIVE);
}
}
}

0 comments on commit 0e09f14

Please sign in to comment.