-
Notifications
You must be signed in to change notification settings - Fork 93
new recipe that changes .equals() to .contentEquals() when comparing … #124
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 2 commits
Commits
Show all changes
14 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 18287b2
Polish to minimize loops & logic
timtebeek 8b93944
Remove unused import
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
94 changes: 94 additions & 0 deletions
94
src/main/java/org/openrewrite/staticanalysis/EqualsToContentEquals.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,94 @@ | ||
| /* | ||
| * Copyright 2021 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.ExecutionContext; | ||
| import org.openrewrite.Preconditions; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.search.UsesType; | ||
| import org.openrewrite.java.tree.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class EqualsToContentEquals extends Recipe { | ||
| private static final MethodMatcher equals_matcher = new MethodMatcher("java.lang.String equals(..)"); | ||
| private static final List<String> TYPE_NAMES = Arrays.asList( | ||
| "java.lang.StringBuffer", | ||
| "java.lang.StringBuilder", | ||
| "java.lang.CharSequence" | ||
| ); | ||
| @SuppressWarnings("unchecked") | ||
| private static final TreeVisitor<?, ExecutionContext> PRECONDITION = | ||
| Preconditions.or(TYPE_NAMES.stream().map(s -> new UsesType<>(s, false)).toArray(UsesType[]::new)); | ||
| private static final List<MethodMatcher> toString_matchers = TYPE_NAMES.stream() | ||
| .map(obj -> new MethodMatcher(obj + " toString()")).collect(Collectors.toList()); | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Use contentEquals to compare StringBuilder to a String"; | ||
| } | ||
| @Override | ||
| public String getDescription() { | ||
| return "Use contentEquals to compare StringBuilder to a String."; | ||
| } | ||
|
|
||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check(PRECONDITION, new EqualsToContentEqualsVisitor()); | ||
| } | ||
|
|
||
| private static class EqualsToContentEqualsVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { | ||
| J.MethodInvocation m = super.visitMethodInvocation(mi, ctx); | ||
| J.Identifier methodName = m.getName(); | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // create method matcher on equals(String) | ||
| if (equals_matcher.matches(m)) { | ||
| Expression argument = m.getArguments().get(0); | ||
|
|
||
| // checks whether the argument is a toString() method call on a StringBuffer or CharSequence | ||
| if (toString_matchers.stream().anyMatch(matcher -> matcher.matches(argument))) { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| J.MethodInvocation inv = (J.MethodInvocation) argument; | ||
| Expression newArg = inv.getSelect(); | ||
| if (inv.getSelect() == null) { return m; } | ||
|
|
||
| Stream<JavaType> TYPES = Stream.of( | ||
| JavaType.buildType("java.lang.StringBuilder"), | ||
| JavaType.buildType("java.lang.StringBuffer"), | ||
| JavaType.buildType("java.lang.CharSequence") | ||
| ); | ||
|
||
|
|
||
| if (TYPES.anyMatch(type -> TypeUtils.isOfType(newArg.getType(), type))) { | ||
| // strip out the toString() on the argument | ||
| List<Expression> args = new ArrayList<>(1); | ||
| args.add(newArg); | ||
| m = m.withArguments(args); | ||
| // rename the method to contentEquals | ||
| methodName = m.getName().withSimpleName("contentEquals"); | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| return m.withName(methodName); | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
120 changes: 120 additions & 0 deletions
120
src/test/java/org/openrewrite/staticanalysis/EqualsToContentEqualsTest.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,120 @@ | ||
| /* | ||
| * Copyright 2021 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.junit.jupiter.api.Test; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| public class EqualsToContentEqualsTest implements RewriteTest { | ||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec | ||
| .parser(JavaParser.fromJavaVersion()) | ||
| .recipe(new EqualsToContentEquals()); | ||
| } | ||
|
|
||
| @Test | ||
| public void replaceStringBuilder() { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| class SomeClass { | ||
| boolean foo(StringBuilder sb) { | ||
| String str = "example string"; | ||
| return str.equals(sb.toString()); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| class SomeClass { | ||
| boolean foo(StringBuilder sb) { | ||
| String str = "example string"; | ||
| return str.contentEquals(sb); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void onlyRunsOnCorrectInvocations() { | ||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| class SomeClass { | ||
| boolean foo(int number, String str) { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return str.equals(number.toString()); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void runsOnStringBuffer() { | ||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| class SomeClass { | ||
| boolean foo(StringBuffer sb, String str) { | ||
| return str.equals(sb.toString()); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| class SomeClass { | ||
| boolean foo(StringBuffer sb, String str) { | ||
| return str.contentEquals(sb); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void runsOnCharSequence() { | ||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| class SomeClass { | ||
| boolean foo(CharSequence cs, String str) { | ||
| return str.equals(cs.toString()); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| class SomeClass { | ||
| boolean foo(CharSequence cs, String str) { | ||
| return str.contentEquals(cs); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to adjust ./gradle/licenceHeader.txt and this here to set the right year.