-
Notifications
You must be signed in to change notification settings - Fork 101
Java 25: Migrate new StringReader(String)
to Reader.of(CharSequence)
#845
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 all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8d42eec
Setup
JohannisK baa1537
polish
JohannisK ba8ba6e
Use `ctx` consistently
timtebeek 634cd66
Use `ctx` in `visitAssignment`
timtebeek e5b47da
polish
JohannisK 7bda627
polish
JohannisK 35ba5fa
Use java 25 for test
JohannisK b7ea7f9
Use a parameterized test instead of repetition
timtebeek 1d056de
Remove unused import
timtebeek 253356d
Updated java 25 for bot
JohannisK aa6bfa2
Use method matcher in preconditions; use static class
timtebeek a5a79cb
Update src/test/java/org/openrewrite/java/migrate/util/MigrateStringR…
JohannisK 529d437
Update src/test/java/org/openrewrite/java/migrate/util/MigrateStringR…
JohannisK f80499a
Update src/test/java/org/openrewrite/java/migrate/util/MigrateStringR…
JohannisK ba7f2c5
Fixed issue after bot commit
JohannisK 5deee4a
Apply formatter
timtebeek fc26686
Tolerate missing type information until we run on Java 25+
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
123 changes: 123 additions & 0 deletions
123
src/main/java/org/openrewrite/java/migrate/util/MigrateStringReaderToReaderOf.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,123 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
* <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.migrate.util; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.internal.ListUtils; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesJavaVersion; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
|
||
@EqualsAndHashCode(callSuper = false) | ||
@Value | ||
public class MigrateStringReaderToReaderOf extends Recipe { | ||
private static final MethodMatcher STRING_READER_CONSTRUCTOR = new MethodMatcher("java.io.StringReader <constructor>(java.lang.String)"); | ||
private static final MethodMatcher TO_STRING_METHOD = new MethodMatcher("java.lang.Object toString()", true); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Use `Reader.of(CharSequence)` for non-synchronized readers"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Migrate `new StringReader(String)` to `Reader.of(CharSequence)` in Java 25+. " + | ||
"This only applies when assigning to `Reader` variables or returning from methods that return `Reader`. " + | ||
"The new method creates non-synchronized readers which are more efficient when thread-safety is not required."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
Preconditions.and(new UsesJavaVersion<>(25), new UsesMethod<>(STRING_READER_CONSTRUCTOR)), | ||
new JavaVisitor<ExecutionContext>() { | ||
@Override | ||
public J visitVariableDeclarations(J.VariableDeclarations mV, ExecutionContext ctx) { | ||
if (TypeUtils.isOfClassType(mV.getTypeAsFullyQualified(), "java.io.Reader")) { | ||
return mV.withVariables(ListUtils.map(mV.getVariables(), v -> { | ||
maybeRemoveImport("java.io.StringReader"); | ||
maybeAddImport("java.io.Reader"); | ||
return (J.VariableDeclarations.NamedVariable) new TransformVisitor().visitNonNull(v, ctx, getCursor().getParentOrThrow()); | ||
})); | ||
} | ||
return super.visitVariableDeclarations(mV, ctx); | ||
} | ||
|
||
@Override | ||
public J visitAssignment(J.Assignment a, ExecutionContext ctx) { | ||
if (a.getVariable() instanceof J.Identifier) { | ||
J.Identifier variable = (J.Identifier) a.getVariable(); | ||
if (TypeUtils.isOfClassType(variable.getType(), "java.io.Reader")) { | ||
maybeRemoveImport("java.io.StringReader"); | ||
maybeAddImport("java.io.Reader"); | ||
return new TransformVisitor().visitNonNull(a, ctx, getCursor().getParentOrThrow()); | ||
} | ||
} | ||
return super.visitAssignment(a, ctx); | ||
} | ||
|
||
@Override | ||
public J visitReturn(J.Return r, ExecutionContext ctx) { | ||
J.MethodDeclaration method = getCursor().firstEnclosing(J.MethodDeclaration.class); | ||
if (method != null && method.getReturnTypeExpression() != null) { | ||
JavaType returnType = method.getReturnTypeExpression().getType(); | ||
if (TypeUtils.isOfClassType(returnType, "java.io.Reader")) { | ||
maybeRemoveImport("java.io.StringReader"); | ||
maybeAddImport("java.io.Reader"); | ||
return new TransformVisitor().visitNonNull(r, ctx, getCursor().getParentOrThrow()); | ||
} | ||
} | ||
return super.visitReturn(r, ctx); | ||
} | ||
} | ||
); | ||
} | ||
|
||
private static class TransformVisitor extends JavaVisitor<ExecutionContext> { | ||
@Override | ||
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
if (STRING_READER_CONSTRUCTOR.matches(newClass)) { | ||
return JavaTemplate.builder("Reader.of(#{any(java.lang.CharSequence)})") | ||
.imports("java.io.Reader") | ||
.build() | ||
.apply(getCursor(), newClass.getCoordinates().replace(), optimizeCharSequenceToString(newClass.getArguments().get(0))); | ||
} | ||
return super.visitNewClass(newClass, ctx); | ||
} | ||
|
||
private Expression optimizeCharSequenceToString(Expression expr) { | ||
if (expr instanceof J.MethodInvocation) { | ||
J.MethodInvocation mi = (J.MethodInvocation) expr; | ||
if (TO_STRING_METHOD.matches(mi) && | ||
mi.getSelect() != null && TypeUtils.isAssignableTo("java.lang.CharSequence", mi.getSelect().getType())) { | ||
return mi.getSelect(); | ||
} | ||
} | ||
return expr; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.