Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beans enabled via build profile/properties should not be alternatives #11936

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}