From e28e4bdbe454c5a5c458eddf81a399c8b1893981 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 11 Dec 2024 21:05:50 +0100 Subject: [PATCH 01/10] Correct `J.FieldAccess.isFullyQualifiedClassReference()` The commit e536ed273fda87ce6857b06f200488130b3ef5f3 introduced a regression here. Fixes: #4773 --- .../org/openrewrite/java/ChangeTypeTest.java | 53 +++++++++++++++++++ .../java/org/openrewrite/java/tree/J.java | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java index ad20be88dc7..f16a15ad36f 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java @@ -25,6 +25,7 @@ import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import org.openrewrite.test.SourceSpec; +import org.openrewrite.test.TypeValidation; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -2072,10 +2073,12 @@ void changeTypeInPropertiesFile() { properties( """ a.property=java.lang.String + c.property=java.lang.StringBuilder b.property=String """, """ a.property=java.lang.Integer + c.property=java.lang.StringBuilder b.property=String """, spec -> spec.path("application.properties")) ); @@ -2104,4 +2107,54 @@ void changeTypeInYaml() { ) ); } + + @Test + void testRecipe() { + // language=java + rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.all()) + .recipe(new ChangeType("org.codehaus.jackson.annotate.JsonIgnoreProperties", "com.fasterxml.jackson.annotation.JsonIgnoreProperties", false)) + .parser(JavaParser.fromJavaVersion() + .dependsOn( + """ + package org.codehaus.jackson.annotate; + public @interface JsonIgnoreProperties { + boolean ignoreUnknown() default false; + } + """, + """ + package org.codehaus.jackson.annotate; + public @interface JsonIgnore { + } + """ + ) + ), + java( + """ + import org.codehaus.jackson.annotate.JsonIgnore; + import org.codehaus.jackson.annotate.JsonIgnoreProperties; + + @JsonIgnoreProperties(ignoreUnknown = true) + public class myClass { + @JsonIgnore + public boolean isDirty() { + return false; + } + } + """, + """ + import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + import org.codehaus.jackson.annotate.JsonIgnore; + + @JsonIgnoreProperties(ignoreUnknown = true) + public class myClass { + @JsonIgnore + public boolean isDirty() { + return false; + } + } + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index 7afe8698797..ab294bca8aa 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -1991,7 +1991,7 @@ private boolean isFullyQualifiedClassReference(J.FieldAccess fieldAccess, String return false; } String simpleName = fieldAccess.getName().getSimpleName(); - if (!simpleName.regionMatches(0, className, dotIndex + 1, simpleName.length())) { + if (!simpleName.regionMatches(0, className, dotIndex + 1, className.length())) { return false; } if (fieldAccess.getTarget() instanceof J.FieldAccess) { From 2ce6ec08d767387b7a21f130df320119c6ea5e69 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 11 Dec 2024 21:31:39 +0100 Subject: [PATCH 02/10] Fix --- .../src/test/java/org/openrewrite/java/ChangeTypeTest.java | 2 +- rewrite-java/src/main/java/org/openrewrite/java/tree/J.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java index f16a15ad36f..2d511a795fb 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java @@ -2109,7 +2109,7 @@ void changeTypeInYaml() { } @Test - void testRecipe() { + void noRenameOfTypeWithMatchingPrefix() { // language=java rewriteRun( spec -> spec.typeValidationOptions(TypeValidation.all()) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index ab294bca8aa..c588564cc52 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -1991,7 +1991,7 @@ private boolean isFullyQualifiedClassReference(J.FieldAccess fieldAccess, String return false; } String simpleName = fieldAccess.getName().getSimpleName(); - if (!simpleName.regionMatches(0, className, dotIndex + 1, className.length())) { + if (!simpleName.regionMatches(0, className, dotIndex + 1, Math.max(className.length() - dotIndex - 1, simpleName.length()))) { return false; } if (fieldAccess.getTarget() instanceof J.FieldAccess) { From 42760f98b5149eb87b30288cdb776f1029a577e5 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 11 Dec 2024 22:00:33 +0100 Subject: [PATCH 03/10] Fix again --- rewrite-java/src/main/java/org/openrewrite/java/tree/J.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index c588564cc52..9e998100802 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -1991,7 +1991,7 @@ private boolean isFullyQualifiedClassReference(J.FieldAccess fieldAccess, String return false; } String simpleName = fieldAccess.getName().getSimpleName(); - if (!simpleName.regionMatches(0, className, dotIndex + 1, Math.max(className.length() - dotIndex - 1, simpleName.length()))) { + if (!simpleName.regionMatches(0, className, dotIndex + 1, prevDotIndex - dotIndex - 1)) { return false; } if (fieldAccess.getTarget() instanceof J.FieldAccess) { From ed270c8e885f8668c3435750d261a4177265ea36 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 12 Dec 2024 08:12:16 +0100 Subject: [PATCH 04/10] More improvements and tests --- .../org/openrewrite/java/ChangeTypeTest.java | 64 ++++++++++++++++++- .../java/org/openrewrite/java/tree/J.java | 8 ++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java index 2d511a795fb..63362413c50 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java @@ -209,7 +209,7 @@ class Test { class Test { List p; - List p2; + java.util.List p2; java.util.List p3; } """ @@ -2157,4 +2157,66 @@ public boolean isDirty() { ) ); } + + @Test + void changeTypeOfInnerClass() { + rewriteRun( + spec -> spec.recipe(new ChangeType("foo.A$Builder", "bar.A$Builder", true)) + .parser(JavaParser.fromJavaVersion().dependsOn( + """ + package foo; + + public class A { + public A.Builder builder() { + return new A.Builder(); + } + + public static class Builder { + public A build() { + return new A(); + } + } + } + """, + """ + package bar; + + public class A { + public A.Builder builder() { + return new A.Builder(); + } + + public static class Builder { + public A build() { + return new A(); + } + } + } + """ + ) + ), + java( + """ + import foo.A; + import foo.A.Builder; + + class Test { + A test() { + A.Builder b = A.builder(); + return b.build(); + } + } + """, """ + import foo.A; + + class Test { + A test() { + bar.A.Builder b = A.builder(); + return b.build(); + } + } + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index 9e998100802..e5b9cc9ecc4 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -1986,12 +1986,18 @@ public boolean isFullyQualifiedClassReference(String className) { } private boolean isFullyQualifiedClassReference(J.FieldAccess fieldAccess, String className, int prevDotIndex) { + if (fieldAccess.getName().getFieldType() == null && + fieldAccess.getName().getType() instanceof JavaType.FullyQualified && + ((JavaType.FullyQualified) fieldAccess.getName().getType()).getFullyQualifiedName().equals(className)) { + return true; + } + int dotIndex = className.lastIndexOf('.', prevDotIndex - 1); if (dotIndex < 0) { return false; } String simpleName = fieldAccess.getName().getSimpleName(); - if (!simpleName.regionMatches(0, className, dotIndex + 1, prevDotIndex - dotIndex - 1)) { + if (!simpleName.regionMatches(0, className, dotIndex + 1, Math.max(simpleName.length(), prevDotIndex - dotIndex - 1))) { return false; } if (fieldAccess.getTarget() instanceof J.FieldAccess) { From ddd129748ef45e6e4c240e5f661231cecb4b4219 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 12 Dec 2024 08:31:55 +0100 Subject: [PATCH 05/10] More improvements --- .../src/main/java/org/openrewrite/java/tree/J.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index e5b9cc9ecc4..bd43762cd80 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -1979,19 +1979,16 @@ public List getSideEffects() { } public boolean isFullyQualifiedClassReference(String className) { - if (!className.contains(".")) { + if (getName().getFieldType() == null && getName().getType() instanceof JavaType.FullyQualified && + ((JavaType.FullyQualified) getName().getType()).getFullyQualifiedName().equals(className)) { + return true; + } else if (!className.contains(".")) { return false; } - return isFullyQualifiedClassReference(this, className, className.length()); + return isFullyQualifiedClassReference(this, className.replace('$', '.'), className.length()); } private boolean isFullyQualifiedClassReference(J.FieldAccess fieldAccess, String className, int prevDotIndex) { - if (fieldAccess.getName().getFieldType() == null && - fieldAccess.getName().getType() instanceof JavaType.FullyQualified && - ((JavaType.FullyQualified) fieldAccess.getName().getType()).getFullyQualifiedName().equals(className)) { - return true; - } - int dotIndex = className.lastIndexOf('.', prevDotIndex - 1); if (dotIndex < 0) { return false; From 2b300ce6d3757d1fbf93a878c464c2f27ebb98d3 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 12 Dec 2024 08:34:03 +0100 Subject: [PATCH 06/10] Polish --- .../src/test/java/org/openrewrite/java/ChangeTypeTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java index 63362413c50..a89b3e7045e 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java @@ -25,7 +25,6 @@ import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import org.openrewrite.test.SourceSpec; -import org.openrewrite.test.TypeValidation; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -2112,8 +2111,7 @@ void changeTypeInYaml() { void noRenameOfTypeWithMatchingPrefix() { // language=java rewriteRun( - spec -> spec.typeValidationOptions(TypeValidation.all()) - .recipe(new ChangeType("org.codehaus.jackson.annotate.JsonIgnoreProperties", "com.fasterxml.jackson.annotation.JsonIgnoreProperties", false)) + spec -> spec.recipe(new ChangeType("org.codehaus.jackson.annotate.JsonIgnoreProperties", "com.fasterxml.jackson.annotation.JsonIgnoreProperties", false)) .parser(JavaParser.fromJavaVersion() .dependsOn( """ From 6ecc7fc9e465a9e98b92b1a287376c3bd3bd57cc Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 12 Dec 2024 08:35:12 +0100 Subject: [PATCH 07/10] Polish --- .../src/test/java/org/openrewrite/java/ChangeTypeTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java index a89b3e7045e..ad9c618a38b 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java @@ -2109,7 +2109,6 @@ void changeTypeInYaml() { @Test void noRenameOfTypeWithMatchingPrefix() { - // language=java rewriteRun( spec -> spec.recipe(new ChangeType("org.codehaus.jackson.annotate.JsonIgnoreProperties", "com.fasterxml.jackson.annotation.JsonIgnoreProperties", false)) .parser(JavaParser.fromJavaVersion() From 1c1d5ec4735fe56ab8194b8fb6057ea5614b232a Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 12 Dec 2024 08:36:19 +0100 Subject: [PATCH 08/10] Polish --- .../src/test/java/org/openrewrite/java/ChangeTypeTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java index ad9c618a38b..c453a2246fb 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java @@ -2108,6 +2108,7 @@ void changeTypeInYaml() { } @Test + @Issue("https://github.com/openrewrite/rewrite/issues/4773") void noRenameOfTypeWithMatchingPrefix() { rewriteRun( spec -> spec.recipe(new ChangeType("org.codehaus.jackson.annotate.JsonIgnoreProperties", "com.fasterxml.jackson.annotation.JsonIgnoreProperties", false)) @@ -2156,6 +2157,7 @@ public boolean isDirty() { } @Test + @Issue("https://github.com/openrewrite/rewrite/issues/4764") void changeTypeOfInnerClass() { rewriteRun( spec -> spec.recipe(new ChangeType("foo.A$Builder", "bar.A$Builder", true)) From ebbcaa3bd1280b73a3d1ee45aa9d5f9e3e5e82f8 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 12 Dec 2024 08:43:37 +0100 Subject: [PATCH 09/10] Polish --- rewrite-java/src/main/java/org/openrewrite/java/tree/J.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index bd43762cd80..e6a2ca6d379 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -1980,12 +1980,12 @@ public List getSideEffects() { public boolean isFullyQualifiedClassReference(String className) { if (getName().getFieldType() == null && getName().getType() instanceof JavaType.FullyQualified && - ((JavaType.FullyQualified) getName().getType()).getFullyQualifiedName().equals(className)) { + TypeUtils.fullyQualifiedNamesAreEqual(((JavaType.FullyQualified) getName().getType()).getFullyQualifiedName(), className)) { return true; } else if (!className.contains(".")) { return false; } - return isFullyQualifiedClassReference(this, className.replace('$', '.'), className.length()); + return isFullyQualifiedClassReference(this, TypeUtils.toFullyQualifiedName(className), className.length()); } private boolean isFullyQualifiedClassReference(J.FieldAccess fieldAccess, String className, int prevDotIndex) { From 9da643a890bed15ae3b0b418f6e6733ebd9ca816 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 12 Dec 2024 09:31:37 +0100 Subject: [PATCH 10/10] Exclude `JavaType.Unknown` from being matched --- .../java/cleanup/SimplifyBooleanExpressionVisitorTest.java | 2 +- rewrite-java/src/main/java/org/openrewrite/java/tree/J.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/SimplifyBooleanExpressionVisitorTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/SimplifyBooleanExpressionVisitorTest.java index 6b8d5b62381..4264fcf0986 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/SimplifyBooleanExpressionVisitorTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/SimplifyBooleanExpressionVisitorTest.java @@ -64,7 +64,7 @@ public class A { @DocumentExample @Test - void foo() { + void skipMissingTypeAttribution() { rewriteRun( spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()), java( diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index e6a2ca6d379..1ad1cee2193 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -1980,6 +1980,7 @@ public List getSideEffects() { public boolean isFullyQualifiedClassReference(String className) { if (getName().getFieldType() == null && getName().getType() instanceof JavaType.FullyQualified && + !(getName().getType() instanceof JavaType.Unknown) && TypeUtils.fullyQualifiedNamesAreEqual(((JavaType.FullyQualified) getName().getType()).getFullyQualifiedName(), className)) { return true; } else if (!className.contains(".")) {