Skip to content

Commit 1d9b54a

Browse files
authored
Handle nested types in AnnotateNullableMethods (#579)
* Handle nested types in AnnotateNullableMethods - As discovered on #578 * No need to update cursor after changing course on implementation
1 parent fee6b95 commit 1d9b54a

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openrewrite.java.tree.Expression;
2525
import org.openrewrite.java.tree.J;
2626
import org.openrewrite.java.tree.JavaType;
27+
import org.openrewrite.staticanalysis.java.MoveFieldAnnotationToType;
2728

2829
import java.util.Arrays;
2930
import java.util.Comparator;
@@ -92,7 +93,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
9293
.build()
9394
.apply(getCursor(), md.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
9495
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(annotatedMethod));
95-
return (J.MethodDeclaration) new NullableOnMethodReturnType().getVisitor().visitNonNull(annotatedMethod, ctx, getCursor().getParentTreeCursor());
96+
doAfterVisit(new MoveFieldAnnotationToType(fullyQualifiedName).getVisitor());
97+
return (J.MethodDeclaration) new NullableOnMethodReturnType().getVisitor()
98+
.visitNonNull(annotatedMethod, ctx, getCursor().getParentTreeCursor());
9699
}
97100
return md;
98101
}

src/test/java/org/openrewrite/staticanalysis/AnnotateNullableMethodsTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.test.RecipeSpec;
2121
import org.openrewrite.test.RewriteTest;
22+
import org.openrewrite.test.SourceSpec;
2223

2324
import static org.assertj.core.api.Assertions.assertThat;
2425
import static org.openrewrite.java.Assertions.java;
@@ -334,4 +335,42 @@ public String getString() {
334335
)
335336
);
336337
}
338+
339+
@Test
340+
void nestedType() {
341+
rewriteRun(
342+
//language=java
343+
java(
344+
"""
345+
package a;
346+
public class B {
347+
public static class C {}
348+
}
349+
""",
350+
SourceSpec::skip
351+
),
352+
//language=java
353+
java(
354+
"""
355+
import a.B;
356+
public class Foo {
357+
public B.C bar() {
358+
return null;
359+
}
360+
}
361+
""",
362+
"""
363+
import a.B;
364+
import org.jspecify.annotations.Nullable;
365+
366+
public class Foo {
367+
368+
public B.@Nullable C bar() {
369+
return null;
370+
}
371+
}
372+
"""
373+
)
374+
);
375+
}
337376
}

src/test/java/org/openrewrite/staticanalysis/NullableOnMethodReturnTypeTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.test.RecipeSpec;
2121
import org.openrewrite.test.RewriteTest;
22+
import org.openrewrite.test.SourceSpec;
2223

2324
import static org.openrewrite.java.Assertions.java;
2425

@@ -55,6 +56,47 @@ class Test {
5556
);
5657
}
5758

59+
@Test
60+
void nestedType() {
61+
rewriteRun(
62+
//language=java
63+
java(
64+
"""
65+
package a;
66+
public class B {
67+
public static class C {}
68+
}
69+
""",
70+
SourceSpec::skip
71+
),
72+
//language=java
73+
java(
74+
"""
75+
import a.B;
76+
import org.openrewrite.internal.lang.Nullable;
77+
78+
public class Foo {
79+
@Nullable
80+
public B.C bar() {
81+
return null;
82+
}
83+
}
84+
""",
85+
"""
86+
import a.B;
87+
import org.openrewrite.internal.lang.Nullable;
88+
89+
public class Foo {
90+
91+
public @Nullable B.C bar() {
92+
return null;
93+
}
94+
}
95+
"""
96+
)
97+
);
98+
}
99+
58100
@Test
59101
void dontTouchArguments() {
60102
rewriteRun(

src/test/java/org/openrewrite/staticanalysis/java/MoveFieldAnnotationToTypeTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.test.RecipeSpec;
2121
import org.openrewrite.test.RewriteTest;
22+
import org.openrewrite.test.SourceSpec;
2223

2324
import static org.openrewrite.java.Assertions.java;
2425

@@ -222,4 +223,45 @@ public void someFunction(org.openrewrite.internal.@Nullable MetricsHelper metric
222223
)
223224
);
224225
}
226+
227+
@Test
228+
void nestedType() {
229+
rewriteRun(
230+
//language=java
231+
java(
232+
"""
233+
package a;
234+
public class B {
235+
public static class C {}
236+
}
237+
""",
238+
SourceSpec::skip
239+
),
240+
//language=java
241+
java(
242+
"""
243+
import a.B;
244+
import org.openrewrite.internal.lang.Nullable;
245+
246+
public class Foo {
247+
@Nullable
248+
public B.C bar() {
249+
return null;
250+
}
251+
}
252+
""",
253+
"""
254+
import a.B;
255+
import org.openrewrite.internal.lang.Nullable;
256+
257+
public class Foo {
258+
259+
public B.@Nullable C bar() {
260+
return null;
261+
}
262+
}
263+
"""
264+
)
265+
);
266+
}
225267
}

0 commit comments

Comments
 (0)