generated from moderneinc/rewrite-recipe-starter
-
Notifications
You must be signed in to change notification settings - Fork 8
Format aligned Mappers #65
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1e0a6ae
add test tha show how to move from `ObjectMapper` with language speci…
MBoegers 8028c1a
Implement Json, XML and YAML migrations for UseFormatAlignedObjectMap…
MBoegers d52822d
apply code suggestions
MBoegers 16f0551
Remove imports before adding new imports, to avoid conflicts
timtebeek 49718e6
apply comments from PR on do no harm and inclusion in broader landscape
MBoegers ad06b89
reorder tests to have documented example on top
MBoegers 24cb0f9
Inline method
timtebeek 93027c6
Use Preconditions to speed up UseFormatAlignedObjectMappers
timtebeek 7c36e7f
Use stubs to support more formats without any parser classpath entries
timtebeek 4400dc3
Only test `xml` data format, as handling for yaml is the same
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/org/openrewrite/java/jackson/UseFormatAlignedObjectMappers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.java.jackson; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.tree.Expression; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| public class UseFormatAlignedObjectMappers extends Recipe { | ||
|
|
||
| private static final MethodMatcher OBJECT_MAPPER_FACTORY = new MethodMatcher("com.fasterxml.jackson.databind.ObjectMapper <constructor>(com.fasterxml.jackson.core.JsonFactory)"); | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Use format alignment `ObjectMappers`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Replace wrapping `ObjectMapper` calls with their format aligned implementation."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
| J.NewClass nc = super.visitNewClass(newClass, ctx); | ||
|
|
||
| if (!OBJECT_MAPPER_FACTORY.matches(nc)) { | ||
| return nc; | ||
| } | ||
|
|
||
| JavaTemplate explicitMapperTemplate = createExplicitMapperTemplate(nc.getArguments().get(0), ctx); | ||
| if (explicitMapperTemplate == null) { // unsupported migration | ||
| return nc; | ||
| } | ||
|
|
||
| return explicitMapperTemplate.apply(getCursor(), nc.getCoordinates().replace()); | ||
| } | ||
|
|
||
| private @Nullable JavaTemplate createExplicitMapperTemplate(Expression factory, ExecutionContext ctx) { | ||
| JavaType type = factory.getType(); | ||
|
|
||
| if (TypeUtils.isAssignableTo("com.fasterxml.jackson.dataformat.yaml.YAMLFactory", type)) { | ||
| maybeRemoveImport("com.fasterxml.jackson.dataformat.yaml.YAMLFactory"); | ||
| maybeAddImport("com.fasterxml.jackson.dataformat.yaml.YAMLMapper"); | ||
| return JavaTemplate.builder("new YAMLMapper()") | ||
| .imports("com.fasterxml.jackson.dataformat.yaml.YAMLMapper") | ||
| .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jackson-core-2", "jackson-databind-2", "jackson-dataformat-yaml-2")) | ||
| .build(); | ||
| } | ||
|
|
||
| if (TypeUtils.isAssignableTo("com.fasterxml.jackson.dataformat.xml.XmlFactory", type)) { | ||
| maybeRemoveImport("com.fasterxml.jackson.dataformat.xml.XmlFactory"); | ||
| maybeAddImport("com.fasterxml.jackson.dataformat.xml.XmlMapper"); | ||
| return JavaTemplate.builder("new XmlMapper()") | ||
| .imports("com.fasterxml.jackson.dataformat.xml.XmlMapper") | ||
| .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jackson-core-2", "jackson-databind-2", "jackson-dataformat-xml-2")) | ||
| .build(); | ||
| } | ||
|
|
||
| if (TypeUtils.isAssignableTo("com.fasterxml.jackson.core.JsonFactory", type)) { | ||
| // we default back to JSON as it's the Jackson default | ||
| maybeRemoveImport("com.fasterxml.jackson.core.JsonFactory"); | ||
| maybeAddImport("com.fasterxml.jackson.databind.json.JsonMapper"); | ||
| return JavaTemplate.builder("new JsonMapper()") | ||
| .imports("com.fasterxml.jackson.databind.json.JsonMapper") | ||
| .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jackson-core-2", "jackson-databind-2")) | ||
| .build(); | ||
| } | ||
|
|
||
| return null; // unsupported factory type | ||
| } | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/test/java/org/openrewrite/java/jackson/UseFormatAlignedObjectMappersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.java.jackson; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.openrewrite.DocumentExample; | ||
| import org.openrewrite.InMemoryExecutionContext; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| class UseFormatAlignedObjectMappersTest implements RewriteTest { | ||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new UseFormatAlignedObjectMappers()) | ||
| .parser(JavaParser.fromJavaVersion() | ||
| .logCompilationWarningsAndErrors(true) | ||
| .classpathFromResources(new InMemoryExecutionContext(), | ||
| "jackson-core-2", "jackson-databind-2", "jackson-dataformat-yaml-2", "jackson-dataformat-xml-2")); | ||
| } | ||
|
|
||
| @Test | ||
MBoegers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void keepObjectMapper() { | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| class A { | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
MBoegers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @DocumentExample | ||
| @Test | ||
| void jsonMapper() { | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.core.JsonFactory; | ||
|
|
||
| class A { | ||
| ObjectMapper mapper = new ObjectMapper(new JsonFactory()); | ||
| } | ||
| """, | ||
| """ | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
|
||
| class A { | ||
| ObjectMapper mapper = new JsonMapper(); | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void xmlMapper() { | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.dataformat.xml.XmlFactory; | ||
|
|
||
| class A { | ||
| ObjectMapper mapper = new ObjectMapper(new XmlFactory()); | ||
| } | ||
| """, | ||
| """ | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.dataformat.xml.XmlMapper; | ||
|
|
||
| class A { | ||
| ObjectMapper mapper = new XmlMapper(); | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void ymlMapper() { | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
|
|
||
| class A { | ||
| ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); | ||
| } | ||
| """, | ||
| """ | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; | ||
|
|
||
| class A { | ||
| ObjectMapper mapper = new YAMLMapper(); | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.