Skip to content

Commit

Permalink
Replace usages of now deprecated APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Jun 4, 2024
1 parent 300bb41 commit 0025806
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static org.jboss.jandex.AnnotationInstance.create;
import static org.jboss.jandex.AnnotationTarget.Kind.CLASS;
import static org.jboss.jandex.AnnotationTarget.Kind.FIELD;
import static org.jboss.jandex.AnnotationTarget.Kind.METHOD;
import static org.jboss.jandex.AnnotationTarget.Kind.METHOD_PARAMETER;
import static org.jboss.jandex.AnnotationValue.createStringValue;

import java.util.ArrayList;
Expand Down Expand Up @@ -42,6 +42,7 @@
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.MethodParameterInfo;
import org.jboss.jandex.Type;
import org.jboss.jandex.Type.Kind;

Expand Down Expand Up @@ -174,12 +175,12 @@ void configPropertyInjectionPoints(
} else {
// org.acme.Foo.config
if (injectionPoint.isField()) {
FieldInfo field = injectionPoint.getTarget().asField();
FieldInfo field = injectionPoint.getAnnotationTarget().asField();
propertyName = getPropertyName(field.name(), field.declaringClass());
} else if (injectionPoint.isParam()) {
MethodInfo method = injectionPoint.getTarget().asMethod();
propertyName = getPropertyName(method.parameterName(injectionPoint.getPosition()),
method.declaringClass());
MethodParameterInfo methodParameterInfo = injectionPoint.getAnnotationTarget().asMethodParameter();
propertyName = getPropertyName(methodParameterInfo.name(),
methodParameterInfo.method().declaringClass());
} else {
throw new IllegalStateException("Unsupported injection point target: " + injectionPoint);
}
Expand All @@ -205,8 +206,8 @@ void configPropertyInjectionPoints(
propertyDefaultValue = defaultValue.asString();
}

if (injectionPoint.getTarget().kind().equals(METHOD)
&& observerMethods.contains(injectionPoint.getTarget().asMethod())) {
if (injectionPoint.getAnnotationTarget().kind().equals(METHOD_PARAMETER)
&& observerMethods.contains(injectionPoint.getAnnotationTarget().asMethodParameter().method())) {
configProperties
.produce(ConfigPropertyBuildItem.staticInit(propertyName, injectedType, propertyDefaultValue));
}
Expand Down Expand Up @@ -405,19 +406,17 @@ void validateConfigMappingsInjectionPoints(
Type type = Type.create(injectionPoint.getRequiredType().name(), Type.Kind.CLASS);
ConfigClassBuildItem configClass = configMappingTypes.get(type);
if (configClass != null) {
AnnotationTarget target = injectionPoint.getTarget();
AnnotationTarget target = injectionPoint.getAnnotationTarget();
AnnotationInstance mapping = null;
if (target.kind().equals(FIELD)) {
mapping = target.asField().annotation(CONFIG_MAPPING_NAME);
} else if (target.kind().equals(METHOD)) {
List<Type> parameters = target.asMethod().parameterTypes();
for (int i = 0; i < parameters.size(); i++) {
Type parameter = parameters.get(i);
if (parameter.name().equals(type.name())) {
Set<AnnotationInstance> parameterAnnotations = getParameterAnnotations(
validationPhase.getBeanProcessor().getBeanDeployment(), target.asMethod(), i);
mapping = Annotations.find(parameterAnnotations, CONFIG_MAPPING_NAME);
}
} else if (target.kind().equals(METHOD_PARAMETER)) {
MethodParameterInfo methodParameterInfo = target.asMethodParameter();
if (methodParameterInfo.type().name().equals(type.name())) {
Set<AnnotationInstance> parameterAnnotations = getParameterAnnotations(
validationPhase.getBeanProcessor().getBeanDeployment(),
target.asMethodParameter().method(), methodParameterInfo.position());
mapping = Annotations.find(parameterAnnotations, CONFIG_MAPPING_NAME);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,17 @@ public void transform(TransformationContext ctx) {
AnnotationInstance clientAnnotation = Annotations.find(ctx.getQualifiers(), GrpcDotNames.GRPC_CLIENT);
if (clientAnnotation != null && clientAnnotation.value() == null) {
String clientName = null;
AnnotationTarget annotationTarget = ctx.getTarget();
if (ctx.getTarget().kind() == Kind.FIELD) {
AnnotationTarget annotationTarget = ctx.getAnnotationTarget();
if (ctx.getAnnotationTarget().kind() == Kind.FIELD) {
clientName = clientAnnotation.target().asField().name();
} else if (ctx.getTarget().kind() == Kind.METHOD
&& clientAnnotation.target().kind().equals(Kind.METHOD_PARAMETER)) {
} else if (annotationTarget.kind() == Kind.METHOD_PARAMETER) {
MethodParameterInfo param = clientAnnotation.target().asMethodParameter();
annotationTarget = param;
// We don't need to check if parameter names are recorded - that's validated elsewhere
clientName = param.method().parameterName(param.position());
}
if (clientName != null) {
ctx.transform().remove(GrpcDotNames::isGrpcClient)
.add(AnnotationInstance.builder(GrpcDotNames.GRPC_CLIENT)
.value(clientName)
.buildWithTarget(annotationTarget))
.add(AnnotationInstance.builder(GrpcDotNames.GRPC_CLIENT).value(clientName).build())
.done();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3341,10 +3341,10 @@ static void collectNamespaceExpressions(Expression expression, Set<Expression> n

public static String getName(InjectionPointInfo injectionPoint) {
if (injectionPoint.isField()) {
return injectionPoint.getTarget().asField().name();
return injectionPoint.getAnnotationTarget().asField().name();
} else if (injectionPoint.isParam()) {
String name = injectionPoint.getTarget().asMethod().parameterName(injectionPoint.getPosition());
return name == null ? injectionPoint.getTarget().asMethod().name() : name;
String name = injectionPoint.getAnnotationTarget().asMethodParameter().name();
return name == null ? injectionPoint.getAnnotationTarget().asMethodParameter().method().name() : name;
}
throw new IllegalArgumentException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public boolean appliesTo(Type requiredType) {
}

public void transform(TransformationContext context) {
if (context.getTarget().kind() == AnnotationTarget.Kind.FIELD) {
var declaringClassName = context.getTarget().asField().declaringClass().name();
if (context.getAnnotationTarget().kind() == AnnotationTarget.Kind.FIELD) {
var declaringClassName = context.getAnnotationTarget().asField().declaringClass().name();
if (JPA_IDENTITY_PROVIDER_NAME.equals(declaringClassName)
|| JPA_TRUSTED_IDENTITY_PROVIDER_NAME.equals(declaringClassName)) {
context.transform()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -135,67 +134,40 @@ public void transform(TransformationContext transformationContext) {
&& !ann.name().equals(io.quarkus.arc.processor.DotNames.DEFAULT))) {
return;
}
AnnotationTarget target = transformationContext.getTarget();
AnnotationTarget target = transformationContext.getAnnotationTarget();
AnnotationInstance meConfigInstance = Annotations.find(
transformationContext.getAllTargetAnnotations(),
DotNames.MANAGED_EXECUTOR_CONFIG);
AnnotationInstance tcConfigInstance = Annotations.find(
transformationContext.getAllTargetAnnotations(),
DotNames.THREAD_CONTEXT_CONFIG);
String mpConfigIpName = null;
if (target.kind().equals(AnnotationTarget.Kind.FIELD)) {
AnnotationInstance meConfigInstance = Annotations.find(transformationContext.getAllAnnotations(),
DotNames.MANAGED_EXECUTOR_CONFIG);
AnnotationInstance tcConfigInstance = Annotations.find(transformationContext.getAllAnnotations(),
DotNames.THREAD_CONTEXT_CONFIG);

if (meConfigInstance != null || tcConfigInstance != null) {
// create a unique name based on the injection point
String mpConfigIpName = target.asField().declaringClass().name().toString()
mpConfigIpName = target.asField().declaringClass().name().toString()
+ NAME_DELIMITER
+ target.asField().name();

// add @NamedInstance with the generated name
transformationContext.transform()
.add(DotNames.NAMED_INSTANCE, AnnotationValue.createStringValue("value", mpConfigIpName))
.done();
}
} else if (target.kind().equals(AnnotationTarget.Kind.METHOD)) {
// If it's method, we can have multiple parameters that we might need to configure and
// each injection point needs its own unique @NamedInstance.
// Finally, we register these annotation instance with the transformer. Note that when creating
// each annotation instance, we have to use AnnotationTarget of the _method parameter_
Collection<AnnotationInstance> annotationsToAdd = new ArrayList<>();
createRequiredAnnotationInstances(Annotations.getAnnotations(AnnotationTarget.Kind.METHOD_PARAMETER,
DotNames.MANAGED_EXECUTOR_CONFIG, transformationContext.getAllAnnotations()),
transformationContext.getQualifiers(), annotationsToAdd);
createRequiredAnnotationInstances(Annotations.getAnnotations(AnnotationTarget.Kind.METHOD_PARAMETER,
DotNames.THREAD_CONTEXT_CONFIG, transformationContext.getAllAnnotations()),
transformationContext.getQualifiers(), annotationsToAdd);
transformationContext.transform().addAll(annotationsToAdd).done();
} else if (target.kind().equals(AnnotationTarget.Kind.METHOD_PARAMETER)) {
if (meConfigInstance != null || tcConfigInstance != null) {
// create a unique name based on the injection point
mpConfigIpName = target.asMethodParameter().method().declaringClass().name().toString()
+ NAME_DELIMITER
+ target.asMethodParameter().method().name()
+ NAME_DELIMITER
+ (target.asMethodParameter().position() + 1);
}
}

}
});
}

private void createRequiredAnnotationInstances(Collection<AnnotationInstance> configAnnotationInstances,
Collection<AnnotationInstance> knownQualifiers,
Collection<AnnotationInstance> instancesToAdd) {
for (AnnotationInstance annotationInstance : configAnnotationInstances) {
if (annotationInstance.target().kind().equals(AnnotationTarget.Kind.METHOD_PARAMETER)) {
MethodParameterInfo methodParameterInfo = annotationInstance.target().asMethodParameter();
// skip if the method param injection point has custom qualifiers on it (including @NamedInstance)
if (methodParameterInfo.annotations().stream()
.anyMatch(ann -> knownQualifiers.contains(ann)
&& !ann.name().equals(io.quarkus.arc.processor.DotNames.ANY)
&& !ann.name().equals(io.quarkus.arc.processor.DotNames.DEFAULT))) {
continue;
if (mpConfigIpName != null) {
// add @NamedInstance with the generated name
transformationContext.transform()
.add(DotNames.NAMED_INSTANCE, AnnotationValue.createStringValue("value", mpConfigIpName))
.done();
}
String mpConfigIpName = methodParameterInfo.method().declaringClass().name().toString()
+ NAME_DELIMITER
+ methodParameterInfo.method().name()
+ NAME_DELIMITER
+ (methodParameterInfo.position() + 1);
// create a new AnnotationInstance with annotation target set to the respective _method parameter_
instancesToAdd.add(AnnotationInstance.builder(DotNames.NAMED_INSTANCE)
.value(mpConfigIpName)
.buildWithTarget(methodParameterInfo));
}
}
});
}

@BuildStep
Expand All @@ -207,7 +179,7 @@ void createSynthBeansForConfiguredInjectionPoints(BuildProducer<SyntheticBeanBui
Map<String, ThreadConfig> threadContextMap = new HashMap<>();
Set<String> unconfiguredContextIPs = new HashSet<>();
for (InjectionPointInfo ipInfo : bdFinishedBuildItem.getInjectionPoints()) {
if (AnnotationTarget.Kind.FIELD.equals(ipInfo.getTarget().kind())) {
if (AnnotationTarget.Kind.FIELD.equals(ipInfo.getAnnotationTarget().kind())) {
AnnotationInstance namedAnnotation = ipInfo.getRequiredQualifier(DotNames.NAMED_INSTANCE);
// only look for IP with @NamedInstance on it because the IP transformation made sure it's there
if (namedAnnotation == null) {
Expand All @@ -220,9 +192,9 @@ void createSynthBeansForConfiguredInjectionPoints(BuildProducer<SyntheticBeanBui
&& !ann.name().equals(io.quarkus.arc.processor.DotNames.DEFAULT))) {
continue;
}
AnnotationInstance meConfigInstance = Annotations.find(ipInfo.getTarget().asField().annotations(),
AnnotationInstance meConfigInstance = Annotations.find(ipInfo.getAnnotationTarget().asField().annotations(),
DotNames.MANAGED_EXECUTOR_CONFIG);
AnnotationInstance tcConfigInstance = Annotations.find(ipInfo.getTarget().asField().annotations(),
AnnotationInstance tcConfigInstance = Annotations.find(ipInfo.getAnnotationTarget().asField().annotations(),
DotNames.THREAD_CONTEXT_CONFIG);

// get the name from @NamedInstance qualifier
Expand Down Expand Up @@ -255,7 +227,7 @@ void createSynthBeansForConfiguredInjectionPoints(BuildProducer<SyntheticBeanBui
tcConfigInstance.value("unchanged")));
}
}
} else if (AnnotationTarget.Kind.METHOD.equals(ipInfo.getTarget().kind())) {
} else if (AnnotationTarget.Kind.METHOD_PARAMETER.equals(ipInfo.getAnnotationTarget().kind())) {
// for a method, we need to process each parameter as a separate injection point
for (AnnotationInstance annotationInstance : ipInfo.getRequiredQualifiers()) {
// just METHOD_PARAMETER and filter to only @NamedInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@ static class SomeBean {
final ManagedExecutor ctorExecutor1;
final ManagedExecutor ctorExecutor2;
final ThreadContext ctorThreadContext;
final ManagedExecutor ctorExecutor3;

// c-tor injection, three out of four params should be configured
// c-tor injection, three out of five params should be configured
public SomeBean(@ManagedExecutorConfig(maxAsync = 8, maxQueued = 8) ManagedExecutor ctorExecutor1,
@ManagedExecutorConfig(maxAsync = 3, maxQueued = 3) ManagedExecutor ctorExecutor2, Foo foo,
@ThreadContextConfig(cleared = "CDI") ThreadContext ctorTc) {
@ThreadContextConfig(cleared = "CDI") ThreadContext ctorTc,
@MyQualifier ManagedExecutor ctorExecutor3) { // this one has custom qualifier, we shouldn't modify it
this.ctorExecutor1 = ctorExecutor1;
this.ctorExecutor2 = ctorExecutor2;
this.ctorThreadContext = ctorTc;
this.ctorExecutor3 = ctorExecutor3;
}

@Inject
Expand Down Expand Up @@ -264,6 +267,10 @@ public void assertConfiguredInjectionPointsInConstructor() {
Set<String> cleared = providersToStringSet(plan.clearedProviders);
assertTrue(propagated.isEmpty());
assertTrue(cleared.contains(ThreadContext.CDI));

exec = unwrapExecutor(ctorExecutor3);
assertEquals(2, exec.getMaxAsync());
assertEquals(-1, exec.getMaxQueued()); // default value
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ static boolean isSynthetic(MethodInfo method) {
static Optional<AnnotationInstance> getAnnotation(TransformedAnnotationsBuildItem transformedAnnotations,
InjectionPointInfo injectionPoint,
DotName annotationName) {
Collection<AnnotationInstance> annotations = transformedAnnotations.getAnnotations(injectionPoint.getTarget());
// Query annotations for either field, or whole method
AnnotationTarget annotationTarget = injectionPoint.isField() ? injectionPoint.getAnnotationTarget()
: injectionPoint.getAnnotationTarget().asMethodParameter().method();
Collection<AnnotationInstance> annotations = transformedAnnotations.getAnnotations(annotationTarget);
for (AnnotationInstance annotation : annotations) {
if (annotationName.equals(annotation.name())) {
// For method parameter we must check the position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface AnnotationsTransformation<T extends AnnotationsTransformation<T
*/
T add(Class<? extends Annotation> annotationType, AnnotationValue... values);

// TODO this isn't really right for IP transformations anymore?
/**
* NOTE: The annotation target is derived from the transformation context.. If you need to add an annotation instance
* to a method parameter use methods consuming {@link AnnotationInstance} directly and supply the correct
Expand Down
Loading

0 comments on commit 0025806

Please sign in to comment.