-
Notifications
You must be signed in to change notification settings - Fork 93
Add recipe ReplaceClassIsInstanceWithInstanceof for SonarQube RSPEC-6202
#381
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
timtebeek
merged 25 commits into
openrewrite:main
from
yurii-yu:feature/new_recipes_for_rspec_1170_and_6202
Oct 28, 2024
Merged
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3c6f9b0
add: recipe for RSPEC-S1170, adding "static" to "public final" consta…
8771f69
add: recipe and unit test for RSPEC-S6202
dc85402
update: add recipe to list
81f30fd
Merge branch 'main' into feature/new_recipes_for_rspec_1170_and_6202
554ce96
fix: correct the name of the recipe
6cc2947
update: for more cases in unit test
4140086
update: correct copyright messages
7d43903
refactor: format code
0b6bac5
Merge branch 'main' into feature/new_recipes_for_rspec_1170_and_6202
40c508e
Update src/test/java/org/openrewrite/staticanalysis/ReplaceClassIsIns…
yurii-yu 137fc0c
Update src/test/java/org/openrewrite/staticanalysis/ReplaceClassIsIns…
yurii-yu 8f8008d
Update src/test/java/org/openrewrite/staticanalysis/ReplaceClassIsIns…
yurii-yu 3826d81
Update src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsIns…
yurii-yu 5c6fc8b
Update src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsIns…
yurii-yu b4a1505
Update src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsIns…
yurii-yu 9160beb
Update src/main/java/org/openrewrite/staticanalysis/AddStaticModifier…
yurii-yu e8e062c
Update src/main/java/org/openrewrite/staticanalysis/AddStaticModifier…
yurii-yu 21c31cf
Merge branch 'main' into feature/new_recipes_for_rspec_1170_and_6202
timtebeek f1990fa
Test additional cases pointed out in review
timtebeek 5b0b9b2
Format tests
timtebeek 1bc5dce
Slight polish to ReplaceClassIsInstanceWithInstanceof
timtebeek 9fb0732
Apply suggestions from code review
timtebeek 8bdf819
update: Recipe for RSPEC-S1170 is removed because it needs further co…
d405b9b
Merge branch 'main' into feature/new_recipes_for_rspec_1170_and_6202
yurii-yu 8d59c77
Minor polish
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
79 changes: 79 additions & 0 deletions
79
...java/org/openrewrite/staticanalysis/AddStaticModifierToPublicFinalConstantsAndFields.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,79 @@ | ||
| /* | ||
| * Copyright 2024 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.Recipe; | ||
| import org.openrewrite.Tree; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.J.Modifier; | ||
| import org.openrewrite.java.tree.Space; | ||
| import org.openrewrite.marker.Markers; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
|
|
||
| public class AddStaticModifierToPublicFinalConstantsAndFields extends Recipe { | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Add `static` to `public final` variables"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Finds fields declared as `public final` and adds `static` modifier, meanwhile sort all the modifiers."; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getTags() { | ||
| return Collections.singleton("RSPEC-S1170"); | ||
| } | ||
|
|
||
| @Override | ||
| public Duration getEstimatedEffortPerOccurrence() { | ||
| return Duration.ofMinutes(3); | ||
| } | ||
|
|
||
| @Override | ||
| public JavaIsoVisitor<ExecutionContext> getVisitor() { | ||
| return new JavaIsoVisitor<ExecutionContext>() { | ||
|
|
||
| @Override | ||
| public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, | ||
| ExecutionContext executionContext) { | ||
|
|
||
| if (multiVariable.hasModifier(Modifier.Type.Public) && | ||
| multiVariable.hasModifier(Modifier.Type.Final) && | ||
| !multiVariable.hasModifier(Modifier.Type.Static)) { | ||
| J.VariableDeclarations v = super.visitVariableDeclarations(multiVariable, executionContext); | ||
|
|
||
| multiVariable.getModifiers().add(new J.Modifier(Tree.randomId(), | ||
| Space.format(" "), Markers.EMPTY, " ", | ||
| Modifier.Type.Static, emptyList())); | ||
|
|
||
| return v.withModifiers(ModifierOrder.sortModifiers(multiVariable.getModifiers())); | ||
|
|
||
| } | ||
| return super.visitVariableDeclarations(multiVariable, executionContext); | ||
| } | ||
yurii-yu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
| } | ||
99 changes: 99 additions & 0 deletions
99
src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.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 2024 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 java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.NlsRewrite.Description; | ||
yurii-yu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import org.openrewrite.Recipe; | ||
| 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.J.FieldAccess; | ||
| import org.openrewrite.java.tree.J.Identifier; | ||
| import org.openrewrite.java.tree.J.MethodInvocation; | ||
| import org.openrewrite.java.tree.JavaType; | ||
|
|
||
| public class ReplaceClassIsInstanceWithInstanceof extends Recipe { | ||
yurii-yu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Replace `A.class.isInstance(a)` with `a instanceof A`"; | ||
| } | ||
|
|
||
| @Override | ||
| public @Description | ||
yurii-yu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String getDescription() { | ||
| return "There should be no `A.class.isInstance(a)`, it should be replaced by `a instanceof A`."; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getTags() { | ||
| return Collections.singleton("RSPEC-S6202"); | ||
| } | ||
|
|
||
| @Override | ||
| public Duration getEstimatedEffortPerOccurrence() { | ||
| return Duration.ofMinutes(3); | ||
| } | ||
|
|
||
| @Override | ||
| public JavaVisitor<ExecutionContext> getVisitor() { | ||
| //use JavaVisitor instead of JavaIsoVisitor because we changed the type of LST | ||
| return new JavaVisitor<ExecutionContext>() { | ||
|
|
||
| private final MethodMatcher matcher = new MethodMatcher("java.lang.Class isInstance(java.lang.Object)"); | ||
| private final JavaTemplate template = JavaTemplate.builder( | ||
| "#{any(org.openrewrite.java.tree.Expression)} instanceof #{}").build(); | ||
|
|
||
| @Override | ||
| public J visitMethodInvocation(MethodInvocation method, ExecutionContext ctx) { | ||
|
|
||
| //make sure we find the right method and the left part is something like "SomeClass.class" | ||
| if (matcher.matches(method) && isObjectClass(method.getSelect())) { | ||
| //for code like "A.class.isInstance(a)", select is "String.class", name is "isInstance", argument is "a" | ||
| Identifier objectExpression = (Identifier) method.getArguments().get(0); | ||
|
|
||
| FieldAccess fieldAccessPart = (FieldAccess) method.getSelect(); | ||
| String className = ((JavaType.Class) ((Identifier) fieldAccessPart.getTarget()).getType()).getClassName(); | ||
|
|
||
| //upcast to type J, so J.MethodInfocation can be replaced by J.InstanceOf | ||
| return maybeAutoFormat((J)method, | ||
| (J)template.apply(updateCursor(method), method.getCoordinates().replace(), objectExpression, className), | ||
| ctx); | ||
|
|
||
| } | ||
| return (MethodInvocation) super.visitMethodInvocation(method, ctx); | ||
| } | ||
|
|
||
| private boolean isObjectClass(Expression expression) { | ||
| if (expression instanceof J.FieldAccess) { | ||
| J.FieldAccess fieldAccess = (J.FieldAccess) expression; | ||
| if (fieldAccess.getTarget() instanceof Identifier) { | ||
| Identifier identifier = (Identifier) fieldAccess.getTarget(); | ||
| return identifier.getType() instanceof JavaType.Class; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
yurii-yu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
69 changes: 69 additions & 0 deletions
69
.../org/openrewrite/staticanalysis/AddStaticModifierToPublicFinalConstantsAndFieldsTest.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,69 @@ | ||
| /* | ||
| * Copyright 2022 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.DocumentExample; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| class AddStaticModifierToPublicFinalConstantsAndFieldsTest implements RewriteTest { | ||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new AddStaticModifierToPublicFinalConstantsAndFields()); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotModify() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| class A { | ||
| public final static String s1; | ||
| public String s2; | ||
| static String s3; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
| void addStaticModifier() { | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| class A { | ||
| final public String s1; | ||
| public final String s2; | ||
| } | ||
| """, | ||
| """ | ||
| class A { | ||
| public static final String s1; | ||
| public static final String s2; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/test/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceofTest.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,61 @@ | ||
| package org.openrewrite.staticanalysis; | ||
yurii-yu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import static org.openrewrite.java.Assertions.java; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| class ReplaceClassIsInstanceWithInstanceofTest implements RewriteTest { | ||
|
|
||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new ReplaceClassIsInstanceWithInstanceof()); | ||
| //spec.recipe(new AvoidBoxedBooleanExpressions()); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotMatchMethod() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| class A { | ||
| boolean foo() { | ||
| String s = ""; | ||
| return s instanceof String; | ||
| } | ||
yurii-yu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void changeInstanceOf() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| class A { | ||
| void foo() { | ||
| String s = ""; | ||
| boolean result = String.class.isInstance(s); | ||
| result = Integer.class.isInstance(s); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| class A { | ||
| void foo() { | ||
| String s = ""; | ||
| boolean result = s instanceof String; | ||
| result = s instanceof Integer; | ||
| } | ||
yurii-yu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| } | ||
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.