-
Notifications
You must be signed in to change notification settings - Fork 92
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
Changes from all commits
3f4e109
b4877a3
ae41eb5
90338a6
b17a46b
05753ca
74ed6f4
24c02a3
ac3ccee
15089c1
86e3d79
9423d0e
a69675b
dbc8624
83890a9
a574b69
3c4ed65
bd586b7
18e5c8c
40167dd
5ca3b34
ffadf48
f488a15
522afbe
81f193a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /* | ||
| * 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.*; | ||
|
|
||
| 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 VALUEOF_MATCHER = new MethodMatcher("java.lang.String valueOf(java.lang.Object)"); | ||
| private static final MethodMatcher OBJECTS_TOSTRING_MATCHER = new MethodMatcher("java.util.Objects toString(Object)"); | ||
| private static final MethodMatcher TOSTRING_MATCHER = new MethodMatcher("java.lang.Object toString()"); | ||
|
|
||
| private static final List<String> PATTERNS = Arrays.asList( | ||
| "java.io.PrintStream print*(Object)", | ||
| "java.lang.String format*(..)", | ||
| "java.lang.StringBuilder insert(int, Object)", | ||
| "java.lang.StringBuilder append(Object)", | ||
| "java.io.PrintStream format(String, Object[])", | ||
| "java.io.PrintWriter print*(..)", | ||
| "java.io.PrintWriter format(..)" | ||
| ); | ||
| 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 (TOSTRING_MATCHER.matches(mi)) { | ||
| Expression select = mi.getSelect(); | ||
| if (select == null) { | ||
| return mi; | ||
| } | ||
|
|
||
| return buildReplacement(select, mi); | ||
| } 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) { | ||
| getCursor().putMessage("METHOD_KEY", mi); | ||
| break; | ||
| } | ||
| } | ||
| }else if (OBJECTS_TOSTRING_MATCHER.matches(mi) || VALUEOF_MATCHER.matches(mi)) { | ||
| // method is static | ||
| Expression select = mi.getArguments().get(0); | ||
| maybeRemoveImport("java.util.Objects"); | ||
|
|
||
| return buildReplacement(select, mi); | ||
| } | ||
|
|
||
| return super.visitMethodInvocation(mi, ctx); | ||
| } | ||
|
|
||
| public J buildReplacement(Expression select, J.MethodInvocation mi) { | ||
| 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); | ||
| } | ||
|
|
||
| @Override | ||
| public Expression visitExpression(Expression exp, ExecutionContext ctx) { | ||
| Expression e = (Expression) super.visitExpression(exp, ctx); | ||
| if (e instanceof TypedTree && e.getType() instanceof JavaType.Array) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is more a question for Knut of Kun, but I had expected a call out to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could replace |
||
| 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(); | ||
|
|
||
| if (binary.getOperator() == J.Binary.Type.Addition && (left.getType() instanceof JavaType.Array || right.getType() instanceof JavaType.Array)) { | ||
| getCursor().putMessage("BINARY_FOUND", binary); | ||
| } | ||
|
|
||
| return (J.Binary) super.visitBinary(binary, ctx); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.