Skip to content

Commit

Permalink
Change TransformedAnnotationsBuildItem to account for AnnotationTarge…
Browse files Browse the repository at this point in the history
…t being a method parameter
  • Loading branch information
manovotn committed Jun 5, 2024
1 parent 8b8770f commit c364212
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.arc.deployment;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Function;

Expand All @@ -27,19 +28,27 @@ public final class TransformedAnnotationsBuildItem extends SimpleBuildItem
}

public Collection<AnnotationInstance> getAnnotations(AnnotationTarget target) {
return beanDeployment.getAnnotations(target);
return queryAndConditionallyFilter(target);
}

public AnnotationInstance getAnnotation(AnnotationTarget target, DotName annotationName) {
return beanDeployment.getAnnotation(target, annotationName);
if (target.kind().equals(AnnotationTarget.Kind.METHOD_PARAMETER)) {
return queryForMethodParam(target, annotationName);
} else {
return beanDeployment.getAnnotation(target, annotationName);
}
}

public AnnotationInstance getAnnotation(AnnotationTarget target, Class<? extends Annotation> annotationClass) {
return getAnnotation(target, DotName.createSimple(annotationClass));
}

public boolean hasAnnotation(AnnotationTarget target, DotName annotationName) {
return beanDeployment.hasAnnotation(target, annotationName);
if (target.kind().equals(AnnotationTarget.Kind.METHOD_PARAMETER)) {
return queryForMethodParam(target, annotationName) == null ? false : true;
} else {
return beanDeployment.hasAnnotation(target, annotationName);
}
}

public boolean hasAnnotation(AnnotationTarget target, Class<? extends Annotation> annotationClass) {
Expand All @@ -48,7 +57,39 @@ public boolean hasAnnotation(AnnotationTarget target, Class<? extends Annotation

@Override
public Collection<AnnotationInstance> apply(AnnotationTarget target) {
return beanDeployment.getAnnotations(target);
return queryAndConditionallyFilter(target);
}

private Collection<AnnotationInstance> queryAndConditionallyFilter(AnnotationTarget target) {
if (target.kind().equals(AnnotationTarget.Kind.METHOD_PARAMETER)) {
// We cannot query Jandex for method param. annotation target, so we operate on the whole method
// and filter results accordingly
Collection<AnnotationInstance> result = new ArrayList<>();
for (AnnotationInstance instance : beanDeployment.getAnnotations(target.asMethodParameter().method())) {
if (instance.target().kind().equals(AnnotationTarget.Kind.METHOD_PARAMETER)
&& instance.target().asMethodParameter().position() == target.asMethodParameter().position()) {
result.add(instance);
}
}
return result;
} else {
return beanDeployment.getAnnotations(target);
}
}

private AnnotationInstance queryForMethodParam(AnnotationTarget target, DotName annotationName) {
if (!target.kind().equals(AnnotationTarget.Kind.METHOD_PARAMETER)) {
throw new IllegalArgumentException(
"TransformedAnnotationsBuildItem#queryForMethodParam needs to operate on METHOD_PARAMETER AnnotationTarget");
}
// We cannot query Jandex for method param. annotation target, so we operate on the whole method
// and filter results accordingly
AnnotationInstance instance = beanDeployment.getAnnotation(target.asMethodParameter().method(), annotationName);
if (instance.target().kind().equals(AnnotationTarget.Kind.METHOD_PARAMETER)
&& instance.target().asMethodParameter().position() == target.asMethodParameter().position()) {
return instance;
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
Expand Down Expand Up @@ -264,21 +263,13 @@ static boolean isSynthetic(MethodInfo method) {
static Optional<AnnotationInstance> getAnnotation(TransformedAnnotationsBuildItem transformedAnnotations,
InjectionPointInfo injectionPoint,
DotName annotationName) {
// 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 field IP -> set of field annotations
// For method param IP -> set of param annotations
Collection<AnnotationInstance> annotations = transformedAnnotations
.getAnnotations(injectionPoint.getAnnotationTarget());
for (AnnotationInstance annotation : annotations) {
if (annotationName.equals(annotation.name())) {
// For method parameter we must check the position
if (annotation.target().kind() == AnnotationTarget.Kind.METHOD_PARAMETER
&& injectionPoint.isParam()
&& annotation.target().asMethodParameter().position() == injectionPoint.getPosition()) {
return Optional.of(annotation);
} else if (annotation.target().kind() != AnnotationTarget.Kind.METHOD_PARAMETER) {
// For other kind, no need to check anything else
return Optional.of(annotation);
}
return Optional.of(annotation);
}
}
return Optional.empty();
Expand Down

0 comments on commit c364212

Please sign in to comment.