Skip to content

Commit a5d78d7

Browse files
committed
Polish MoveFieldAnnotationToType
1 parent 0bd9340 commit a5d78d7

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/main/java/org/openrewrite/staticanalysis/java/MoveFieldAnnotationToType.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
import org.jspecify.annotations.Nullable;
2121
import org.openrewrite.*;
2222
import org.openrewrite.internal.ListUtils;
23-
import org.openrewrite.internal.StringUtils;
2423
import org.openrewrite.java.JavaIsoVisitor;
24+
import org.openrewrite.java.TypeMatcher;
2525
import org.openrewrite.java.search.UsesType;
2626
import org.openrewrite.java.tree.*;
2727

2828
import java.util.ArrayList;
2929
import java.util.concurrent.atomic.AtomicReference;
30-
import java.util.regex.Pattern;
3130

3231
@EqualsAndHashCode(callSuper = false)
3332
@Value
@@ -59,22 +58,23 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5958
String annotationTypeInput = annotationType == null ? "org.openrewrite..*" : annotationType;
6059

6160
return Preconditions.check(new UsesType<>(annotationTypeInput, null), new JavaIsoVisitor<ExecutionContext>() {
62-
final Pattern typePattern = Pattern.compile(StringUtils.aspectjNameToPattern(annotationTypeInput));
61+
final TypeMatcher typePattern = new TypeMatcher(annotationTypeInput);
6362

6463
@Override
6564
public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, ExecutionContext ctx) {
6665
J.AnnotatedType at = super.visitAnnotatedType(annotatedType, ctx);
6766

6867
if (isQualifiedClass(at.getTypeExpression())) {
69-
AtomicReference<J.Annotation> matchingAnnotation = new AtomicReference<>();
68+
AtomicReference<J.@Nullable Annotation> matchingAnnotation = new AtomicReference<>();
7069
at = at.withAnnotations(ListUtils.map(at.getAnnotations(), a -> {
7170
if (matchesType(a)) {
7271
matchingAnnotation.set(a);
7372
return null;
7473
}
7574
return a;
7675
}));
77-
if (at.getTypeExpression() != null && matchingAnnotation.get() != null) {
76+
if (matchingAnnotation.get() != null) {
77+
//noinspection DataFlowIssue
7878
TypeTree te = annotateInnerClass(at.getTypeExpression(), matchingAnnotation.get());
7979
at = at.withTypeExpression(te);
8080
// auto format should handle this, but evidently doesn't
@@ -92,7 +92,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
9292
J.VariableDeclarations mv = super.visitVariableDeclarations(multiVariable, ctx);
9393

9494
if (isQualifiedClass(mv.getTypeExpression())) {
95-
AtomicReference<J.Annotation> matchingAnnotation = new AtomicReference<>();
95+
AtomicReference<J.@Nullable Annotation> matchingAnnotation = new AtomicReference<>();
9696
mv = mv.withLeadingAnnotations(ListUtils.map(mv.getLeadingAnnotations(), a -> {
9797
if (matchesType(a)) {
9898
matchingAnnotation.set(a);
@@ -101,6 +101,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
101101
return a;
102102
}));
103103
if (mv.getTypeExpression() != null && matchingAnnotation.get() != null) {
104+
//noinspection DataFlowIssue
104105
TypeTree te = annotateInnerClass(mv.getTypeExpression(), matchingAnnotation.get());
105106
mv = mv.withTypeExpression(te);
106107
// auto format should handle this, but evidently doesn't
@@ -118,7 +119,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
118119
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);
119120

120121
if (isQualifiedClass(md.getReturnTypeExpression())) {
121-
AtomicReference<J.Annotation> matchingAnnotation = new AtomicReference<>();
122+
AtomicReference<J.@Nullable Annotation> matchingAnnotation = new AtomicReference<>();
122123
md = md.withLeadingAnnotations(ListUtils.map(md.getLeadingAnnotations(), a -> {
123124
if (matchesType(a)) {
124125
matchingAnnotation.set(a);
@@ -127,6 +128,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
127128
return a;
128129
}));
129130
if (md.getReturnTypeExpression() != null && matchingAnnotation.get() != null) {
131+
//noinspection DataFlowIssue
130132
TypeTree te = annotateInnerClass(md.getReturnTypeExpression(), matchingAnnotation.get());
131133
md = md.withReturnTypeExpression(te);
132134
// auto format should handle this, but evidently doesn't
@@ -141,7 +143,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
141143

142144
private boolean matchesType(J.Annotation ann) {
143145
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(ann.getType());
144-
return fq != null && typePattern.matcher(fq.getFullyQualifiedName()).matches();
146+
return fq != null && typePattern.matches(fq);
145147
}
146148

147149
private boolean isQualifiedClass(@Nullable TypeTree tree) {

0 commit comments

Comments
 (0)