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

ChangeType leads to botched imports for nested fields #4292

Merged
merged 7 commits into from
Jul 2, 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 @@ -172,6 +172,7 @@ void changeInnerClassToOuterClass() {
class Test {
Entry p;
Map.Entry p2;
java.util.Map.Entry p3;
}
""",
"""
Expand All @@ -180,6 +181,7 @@ class Test {
class Test {
List p;
List p2;
java.util.List p3;
}
"""
)
Expand Down Expand Up @@ -1850,11 +1852,16 @@ public Test test() {

@Issue("https://github.com/openrewrite/rewrite-jackson/pull/6")
@Test
void changeTypeOfStaticImportOfNestedEnumValue() {
void changeTypeOfStaticImportOfNestedEnumValueUsed() {
rewriteRun(
recipeSpec -> recipeSpec.recipe(new ChangeType(
"org.codehaus.jackson.map.SerializationConfig$Feature",
"com.fasterxml.jackson.databind.SerializationFeature", true)),
recipeSpec -> recipeSpec.recipes(
new ChangeType(
"org.codehaus.jackson.map.ObjectMapper",
"com.fasterxml.jackson.databind.ObjectMapper", true),
new ChangeType(
"org.codehaus.jackson.map.SerializationConfig$Feature",
"com.fasterxml.jackson.databind.SerializationFeature", true)
),
java(
"""
package org.codehaus.jackson.map;
Expand All @@ -1868,37 +1875,37 @@ public static enum Feature {
),
java(
"""
package com.fasterxml.jackson.databind;
public enum SerializationFeature {
WRAP_ROOT_VALUE
package org.codehaus.jackson.map;
public class ObjectMapper {
public ObjectMapper configure(SerializationConfig.Feature f, boolean state) {
return this;
}
}
""",
SourceSpec::skip
),
java(
"""
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;

import static org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE;

class A {
void test() {
configure(WRAP_ROOT_VALUE, true);
}
void configure(Feature f, boolean b) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(WRAP_ROOT_VALUE, true);
}
}
""",
"""
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import static com.fasterxml.jackson.databind.SerializationFeature.WRAP_ROOT_VALUE;

class A {
void test() {
configure(WRAP_ROOT_VALUE, true);
}
void configure(SerializationFeature f, boolean b) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(WRAP_ROOT_VALUE, true);
}
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
e = i.withType(targetType);
}
return e;
} else if (maybeClass.toString().equals(oldType.getFullyQualifiedName().replace('$', '.'))) {
maybeRemoveImport(oldType.getOwningClass());
return updateOuterClassTypes(TypeTree.build(((JavaType.FullyQualified) targetType).getFullyQualifiedName())
.withPrefix(fieldAccess.getPrefix()));
}
}
return super.visitFieldAccess(fieldAccess, ctx);
Expand Down
Loading