Skip to content
Merged
Show file tree
Hide file tree
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 @@ -25,6 +25,7 @@
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -150,18 +151,35 @@ private boolean isQualifiedClass(@Nullable TypeTree tree) {
}

private TypeTree annotateInnerClass(TypeTree qualifiedClassRef, J.Annotation annotation) {
J.Annotation usedAnnotation = annotation;
if (annotation.getAnnotationType() instanceof J.FieldAccess) {
J.Identifier identifier = new J.Identifier(
Tree.randomId(),
annotation.getAnnotationType().getPrefix(),
annotation.getAnnotationType().getMarkers(),
new ArrayList<>(),
annotation.getSimpleName(),
annotation.getType(),
null
);
if (identifier.getType() != null) {
maybeAddImport(((JavaType.Class) identifier.getType()).getFullyQualifiedName());
}
usedAnnotation = usedAnnotation.withAnnotationType(
identifier);
}
if (qualifiedClassRef instanceof J.FieldAccess) {
J.FieldAccess q = (J.FieldAccess) qualifiedClassRef;
q = q.withName(q.getName().withAnnotations(
ListUtils.concat(annotation.withPrefix(Space.EMPTY), q.getName().getAnnotations())));
ListUtils.concat(usedAnnotation.withPrefix(Space.EMPTY), q.getName().getAnnotations())));
if (q.getName().getPrefix().getWhitespace().isEmpty()) {
q = q.withName(q.getName().withPrefix(q.getName().getPrefix().withWhitespace(" ")));
}
return q;
} else if (qualifiedClassRef instanceof J.ParameterizedType &&
((J.ParameterizedType) qualifiedClassRef).getClazz() instanceof TypeTree) {
J.ParameterizedType pt = (J.ParameterizedType) qualifiedClassRef;
return pt.withClazz(annotateInnerClass((TypeTree) pt.getClazz(), annotation));
return pt.withClazz(annotateInnerClass((TypeTree) pt.getClazz(), usedAnnotation));
} else if (qualifiedClassRef instanceof J.ArrayType) {
J.ArrayType at = (J.ArrayType) qualifiedClassRef;
at = at.withAnnotations(ListUtils.concat(annotation.withPrefix(Space.SINGLE_SPACE), at.getAnnotations()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,30 @@ interface Test {
)
);
}

@Test
void fullyDefinedAnnotationInMethodDeclaration() {
rewriteRun(
java(
"""
package org.openrewrite;

public class Test {
public void someFunction(@org.openrewrite.internal.lang.Nullable org.openrewrite.internal.MetricsHelper metrics) {
}
}
""",
"""
package org.openrewrite;

import org.openrewrite.internal.lang.Nullable;

public class Test {
public void someFunction(org.openrewrite.internal.@Nullable MetricsHelper metrics) {
}
}
"""
)
);
}
}