Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct J.FieldAccess#isFullyQualifiedClassReference() #4774

Merged
merged 10 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading