Skip to content

Commit

Permalink
Correct J.FieldAccess#isFullyQualifiedClassReference() (#4774)
Browse files Browse the repository at this point in the history
* Correct `J.FieldAccess.isFullyQualifiedClassReference()`

The commit e536ed2 introduced a regression here.

Fixes: #4773

* Fix

* Fix again

* More improvements and tests

* More improvements

* Polish

* Polish

* Polish

* Polish

* Exclude `JavaType.Unknown` from being matched
  • Loading branch information
knutwannheden authored Dec 12, 2024
1 parent a473f26 commit 15f2502
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Test {
class Test {
List p;
List p2;
java.util.List p2;
java.util.List p3;
}
"""
Expand Down Expand Up @@ -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"))
);
Expand Down Expand Up @@ -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();
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class A {

@DocumentExample
@Test
void foo() {
void skipMissingTypeAttribution() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()),
java(
Expand Down
10 changes: 7 additions & 3 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -1979,10 +1979,14 @@ public List<J> 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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 15f2502

Please sign in to comment.