-
Notifications
You must be signed in to change notification settings - Fork 93
Alek/remove to string calls from array instances #126
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 21 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3f4e109
new recipe that changes .equals() to .contentEquals() when comparing …
AlekSimpson b4877a3
polished somethings up and added license headers
AlekSimpson ae41eb5
Update src/test/java/org/openrewrite/staticanalysis/EqualsToContentEq…
AlekSimpson 90338a6
Update src/main/java/org/openrewrite/staticanalysis/EqualsToContentEq…
AlekSimpson b17a46b
Update src/main/java/org/openrewrite/staticanalysis/EqualsToContentEq…
AlekSimpson 05753ca
Update src/main/java/org/openrewrite/staticanalysis/EqualsToContentEq…
AlekSimpson 74ed6f4
Update src/main/java/org/openrewrite/staticanalysis/EqualsToContentEq…
AlekSimpson 24c02a3
Update src/main/java/org/openrewrite/staticanalysis/EqualsToContentEq…
AlekSimpson ac3ccee
Update src/test/java/org/openrewrite/staticanalysis/EqualsToContentEq…
AlekSimpson 15089c1
added missing import
AlekSimpson 86e3d79
updated license header year
AlekSimpson 9423d0e
added test to check that the recipe runs correctly on selects that ar…
AlekSimpson a69675b
added first test, need to test recipe
AlekSimpson dbc8624
the recipe for the toString() replacement is mostly finished, except …
AlekSimpson 83890a9
Merge branch 'main' into alek/RemoveToStringCallsFromArrayInstances
AlekSimpson a574b69
fixed edge case, all tests pass now
AlekSimpson 3c4ed65
added preconidition check
AlekSimpson bd586b7
added support for more methods that implicitly call toString() and al…
AlekSimpson 18e5c8c
added RSPEC tags and updated import statements
AlekSimpson 40167dd
The cursor message must be added before the `super` call
knutwannheden 5ca3b34
fixed cursor messaging, updated tests for more edge case methods, rec…
AlekSimpson ffadf48
more changes from PR feedback
AlekSimpson f488a15
added support for static methods
AlekSimpson 522afbe
Update src/main/java/org/openrewrite/staticanalysis/RemoveToStringCal…
AlekSimpson 81f193a
Update src/test/java/org/openrewrite/staticanalysis/RemoveToStringCal…
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
132 changes: 132 additions & 0 deletions
132
src/main/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstances.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,132 @@ | ||
| /* | ||
| * Copyright 2023 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.staticanalysis; | ||
|
|
||
| import org.openrewrite.*; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.tree.Expression; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class RemoveToStringCallsFromArrayInstances extends Recipe { | ||
| private static final MethodMatcher OBJECT_TOSTRING_MATCHER = new MethodMatcher("java.lang.Object toString()"); | ||
| private static final MethodMatcher OBJECTS_TOSTRING_MATCHER = new MethodMatcher("java.lang.Objects toString()"); | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private static final MethodMatcher PRINT_MATCHER = new MethodMatcher("java.io.PrintStream print*(String)"); | ||
| private static final List<String> PATTERNS = Arrays.asList( | ||
| "java.io.PrintStream print*(Object)", | ||
| "java.lang.String format*(..)", | ||
| "java.lang.String valueOf(java.lang.Object)", | ||
| "java.lang.StringBuilder insert(int, Object)", | ||
| "java.lang.StringBuilder append(Object)", | ||
| "java.io.PrintStream format(String, Object[])" | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| private static final List<MethodMatcher> METHOD_MATCHERS = PATTERNS.stream().map(MethodMatcher::new).collect(Collectors.toList()); | ||
|
|
||
| @Override | ||
| public Set<String> getTags() { | ||
| return Collections.singleton("RSPEC-2116"); | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Remove `toString()` calls on arrays"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "The result from `toString()` calls on arrays is largely useless. The output does not actually reflect" + | ||
| " the contents of the array. `Arrays.toString(array)` give the contents of the array."; | ||
| } | ||
|
|
||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new RemoveToStringFromArraysVisitor(); | ||
| } | ||
|
|
||
| private static class RemoveToStringFromArraysVisitor extends JavaVisitor<ExecutionContext> { | ||
| @Override | ||
| public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { | ||
| if (OBJECT_TOSTRING_MATCHER.matches(mi) || OBJECTS_TOSTRING_MATCHER.matches(mi)) { | ||
| Expression select = mi.getSelect(); | ||
|
|
||
| if (select != null) { | ||
| if (!(select.getType() instanceof JavaType.Array)) { | ||
| return mi; | ||
| } | ||
|
|
||
| maybeAddImport("java.util.Arrays"); | ||
| return JavaTemplate.builder("Arrays.toString(#{anyArray(java.lang.Object)})") | ||
| .imports("java.util.Arrays") | ||
| .build() | ||
| .apply(getCursor(), mi.getCoordinates().replace(), select); | ||
| } | ||
| } else if (METHOD_MATCHERS.stream().anyMatch(matcher -> matcher.matches(mi))) { | ||
| // deals with edge cases where .toString() is called implicitly | ||
| List<Expression> arguments = mi.getArguments(); | ||
| for (Expression arg : arguments) { | ||
| if (arg.getType() instanceof JavaType.Array) { | ||
| Cursor c = getCursor(); | ||
| c.putMessage("METHOD_KEY", mi); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return super.visitMethodInvocation(mi, ctx); | ||
| } | ||
|
|
||
| @Override | ||
| public Expression visitExpression(Expression exp, ExecutionContext ctx) { | ||
| Expression e = (Expression) super.visitExpression(exp, ctx); | ||
| if (e.getType() instanceof JavaType.Array) { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Cursor c = getCursor().dropParentWhile(is -> is instanceof J.Parentheses || !(is instanceof Tree)); | ||
| if (c.getMessage("METHOD_KEY") != null || c.getMessage("BINARY_FOUND") != null) { | ||
| maybeAddImport("java.util.Arrays"); | ||
| return JavaTemplate.builder("Arrays.toString(#{anyArray(java.lang.Object)})") | ||
| .imports("java.util.Arrays") | ||
| .build() | ||
| .apply(getCursor(), e.getCoordinates().replace(), e); | ||
| } | ||
| } | ||
|
|
||
| return e; | ||
| } | ||
|
|
||
| @Override | ||
| public J.Binary visitBinary(J.Binary binary, ExecutionContext ctx) { | ||
| Expression left = binary.getLeft(); | ||
| Expression right = binary.getRight(); | ||
|
|
||
| Cursor c = getCursor().dropParentWhile(is -> is instanceof J.Parentheses || !(is instanceof Tree)); | ||
| if (c.getValue() instanceof J.MethodInvocation) { | ||
| if (PRINT_MATCHER.matches((J.MethodInvocation) c.getValue())) { | ||
| if (left.getType() instanceof JavaType.Array || right.getType() instanceof JavaType.Array) { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| getCursor().putMessage("BINARY_FOUND", binary); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return (J.Binary) super.visitBinary(binary, ctx); | ||
| } | ||
| } | ||
| } | ||
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.