From 96c02c945467b415564a77d68f9416c2f226bc05 Mon Sep 17 00:00:00 2001 From: Michael Keppler Date: Tue, 28 Nov 2023 08:19:23 +0100 Subject: [PATCH 1/3] Handle annotated types in UseCollectionInterfaces Fixes #223. --- .../UseCollectionInterfaces.java | 57 ++++++++-------- .../UseCollectionInterfacesTest.java | 68 +++++++++++++++++++ 2 files changed, 98 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java b/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java index a87227eb17..d68c0d199e 100644 --- a/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java +++ b/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java @@ -131,21 +131,13 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex newType, null ); + } else if (m.getReturnTypeExpression() instanceof J.AnnotatedType) { + J.AnnotatedType annotatedType = (J.AnnotatedType) m.getReturnTypeExpression(); + J.ParameterizedType parameterizedType = (J.ParameterizedType) annotatedType.getTypeExpression(); + typeExpression = annotatedType.withTypeExpression(removeFromParameterizedType(newType, parameterizedType)); } else { J.ParameterizedType parameterizedType = (J.ParameterizedType) m.getReturnTypeExpression(); - J.Identifier returnType = new J.Identifier( - randomId(), - Space.EMPTY, - Markers.EMPTY, - emptyList(), - newType.getClassName(), - newType, - null); - JavaType.Parameterized javaType = (JavaType.Parameterized) parameterizedType.getType(); - typeExpression = parameterizedType.withClazz(returnType) - .withType(javaType != null ? javaType.withType(newType) : - new JavaType.Parameterized(null, newType, null) - ); + typeExpression = removeFromParameterizedType(newType, parameterizedType); } m = m.withReturnTypeExpression(typeExpression); } @@ -183,22 +175,13 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m newType, null ); + } else if (mv.getTypeExpression() instanceof J.AnnotatedType) { + J.AnnotatedType annotatedType = (J.AnnotatedType) mv.getTypeExpression(); + J.ParameterizedType parameterizedType = (J.ParameterizedType) annotatedType.getTypeExpression(); + typeExpression = annotatedType.withTypeExpression(removeFromParameterizedType(newType, parameterizedType)); } else { J.ParameterizedType parameterizedType = (J.ParameterizedType) mv.getTypeExpression(); - J.Identifier returnType = new J.Identifier( - randomId(), - Space.EMPTY, - Markers.EMPTY, - emptyList(), - newType.getClassName(), - newType, - null - ); - JavaType.Parameterized javaType = (JavaType.Parameterized) parameterizedType.getType(); - typeExpression = parameterizedType.withClazz(returnType) - .withType(javaType != null ? javaType.withType(newType) : - new JavaType.Parameterized(null, newType, null) - ); + typeExpression = removeFromParameterizedType(newType, parameterizedType); } mv = mv.withTypeExpression(typeExpression); @@ -213,6 +196,26 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m } return mv; } + + private TypeTree removeFromParameterizedType(JavaType.FullyQualified newType, + J.ParameterizedType parameterizedType) { + TypeTree typeExpression; + J.Identifier returnType = new J.Identifier( + randomId(), + Space.EMPTY, + Markers.EMPTY, + emptyList(), + newType.getClassName(), + newType, + null + ); + JavaType.Parameterized javaType = (JavaType.Parameterized) parameterizedType.getType(); + typeExpression = parameterizedType.withClazz(returnType) + .withType(javaType != null ? javaType.withType(newType) : + new JavaType.Parameterized(null, newType, null) + ); + return typeExpression; + } }; } } diff --git a/src/test/java/org/openrewrite/staticanalysis/UseCollectionInterfacesTest.java b/src/test/java/org/openrewrite/staticanalysis/UseCollectionInterfacesTest.java index ade6075de1..330d9362f5 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UseCollectionInterfacesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UseCollectionInterfacesTest.java @@ -19,6 +19,7 @@ import org.junitpioneer.jupiter.ExpectedToFail; import org.openrewrite.DocumentExample; import org.openrewrite.Issue; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.marker.JavaVersion; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; @@ -27,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.java.Assertions.javaVersion; @SuppressWarnings("rawtypes") class UseCollectionInterfacesTest implements RewriteTest { @@ -130,6 +132,41 @@ public Set method() { ); } + @Test + @Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/223") + void annotatedReturnType() { + rewriteRun( + spec -> spec + .allSources(s -> s.markers(javaVersion(9))) + .parser(JavaParser.fromJavaVersion().classpath("annotations-24.1.0")), + //language=java + java( + """ + import java.util.HashSet; + import org.jetbrains.annotations.Nullable; + + class Test { + public @Nullable HashSet<@Nullable Integer> method() { + return new HashSet<>(); + } + } + """, + """ + import java.util.HashSet; + import java.util.Set; + + import org.jetbrains.annotations.Nullable; + + class Test { + public @Nullable Set<@Nullable Integer> method() { + return new HashSet<>(); + } + } + """ + ) + ); + } + @Test void preserveParameters() { rewriteRun( @@ -223,6 +260,37 @@ class Test { ); } + @Test + @Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/223") + void annotatedFieldType() { + rewriteRun( + spec -> spec + .allSources(s -> s.markers(javaVersion(9))) + .parser(JavaParser.fromJavaVersion().classpath("annotations-24.1.0")), + //language=java + java( + """ + import java.util.HashSet; + import org.jetbrains.annotations.Nullable; + + class Test { + public @Nullable HashSet<@Nullable Integer> values = new HashSet<>(); + } + """, + """ + import java.util.HashSet; + import java.util.Set; + + import org.jetbrains.annotations.Nullable; + + class Test { + public @Nullable Set<@Nullable Integer> values = new HashSet<>(); + } + """ + ) + ); + } + @Test void arrayDeque() { rewriteRun( From 17b4f91d8791834174947ea92746f65e3c5625f4 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 28 Nov 2023 10:32:48 +0100 Subject: [PATCH 2/3] Inline `typeExpression` to return --- .../staticanalysis/UseCollectionInterfaces.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java b/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java index d68c0d199e..d36c7652b0 100644 --- a/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java +++ b/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java @@ -198,8 +198,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m } private TypeTree removeFromParameterizedType(JavaType.FullyQualified newType, - J.ParameterizedType parameterizedType) { - TypeTree typeExpression; + J.ParameterizedType parameterizedType) { J.Identifier returnType = new J.Identifier( randomId(), Space.EMPTY, @@ -210,11 +209,9 @@ private TypeTree removeFromParameterizedType(JavaType.FullyQualified newType, null ); JavaType.Parameterized javaType = (JavaType.Parameterized) parameterizedType.getType(); - typeExpression = parameterizedType.withClazz(returnType) + return parameterizedType.withClazz(returnType) .withType(javaType != null ? javaType.withType(newType) : - new JavaType.Parameterized(null, newType, null) - ); - return typeExpression; + new JavaType.Parameterized(null, newType, null)); } }; } From 7dff96c1b3f1218bb2f00ac223a9ccc847e8610c Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 28 Nov 2023 10:32:56 +0100 Subject: [PATCH 3/3] Remove unused field --- .../java/org/openrewrite/staticanalysis/EmptyBlockVisitor.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/EmptyBlockVisitor.java b/src/main/java/org/openrewrite/staticanalysis/EmptyBlockVisitor.java index c9218dc7d0..27bfe66d47 100644 --- a/src/main/java/org/openrewrite/staticanalysis/EmptyBlockVisitor.java +++ b/src/main/java/org/openrewrite/staticanalysis/EmptyBlockVisitor.java @@ -42,9 +42,6 @@ @EqualsAndHashCode(callSuper = true) public class EmptyBlockVisitor

extends JavaIsoVisitor

{ EmptyBlockStyle emptyBlockStyle; - JavaTemplate throwException = JavaTemplate.builder("throw new #{}(#{any(String)});") - .imports("java.io.UncheckedIOException") - .build(); JavaTemplate continueStatement = JavaTemplate.builder("continue;").build(); @Override