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..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 @@ -208,7 +208,7 @@ class Test { class Test { List p; - List p2; + java.util.List p2; java.util.List p3; } """ @@ -2072,10 +2072,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 +2106,116 @@ 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)) + .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; + } + } + """ + ) + ); + } + + @Test + @Issue("https://github.com/openrewrite/rewrite/issues/4764") + 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-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 7afe8698797..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 @@ -1979,10 +1979,14 @@ public List getSideEffects() { } public boolean isFullyQualifiedClassReference(String className) { - if (!className.contains(".")) { + 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(".")) { return false; } - return isFullyQualifiedClassReference(this, className, className.length()); + return isFullyQualifiedClassReference(this, TypeUtils.toFullyQualifiedName(className), className.length()); } private boolean isFullyQualifiedClassReference(J.FieldAccess fieldAccess, String className, int prevDotIndex) { @@ -1991,7 +1995,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, Math.max(simpleName.length(), prevDotIndex - dotIndex - 1))) { return false; } if (fieldAccess.getTarget() instanceof J.FieldAccess) {