-
Notifications
You must be signed in to change notification settings - Fork 107
Recipe ReplaceAWTGetPeerMethod in Java 11 Migration #524
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 24 commits into
openrewrite:main
from
ranuradh:recipe_detectAWTGetPeerMethod
Aug 9, 2024
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
61abfb8
getPeer() recipe
AnuRam123 f60b422
updated with fixes
AnuRam123 3c0e764
small updates
AnuRam123 6a4a5fa
Merge branch 'main' into recipe_detectAWTGetPeerMethod
ranuradh 1bb77d2
Apply suggestions from code review
timtebeek 3d1de0a
Apply suggestions from code review
timtebeek deb96c3
Apply formatter & order imports
timtebeek 4e63d90
Align before and after code blocks again
timtebeek 1d16fc4
updated recipe name, updated tests and formatted code
AnuRam123 1e26b14
removing extra file commit and test update
AnuRam123 1883f5d
adding comments, Test file cleanup
AnuRam123 c30aefb
Demonstrate the need for `super.visitControlParentheses(cp, ctx)`
timtebeek d1b97da
Only override `visitBinary` and `visitInstanceOf`
timtebeek 6764297
Update src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMe…
timtebeek b28d9bb
update to code and adding missing arguments to .yml
AnuRam123 94bc136
Merge branch 'main' into recipe_detectAWTGetPeerMethod
ranuradh 5679678
right format for java-version.yml
AnuRam123 46b0c86
Merge branch 'main' into recipe_detectAWTGetPeerMethod
timtebeek 13be5af
Remove import after instanceof replacement
timtebeek 99801ef
Also handle flipped case
timtebeek 694e592
Replace `checkClassNameIsEqualToFQCN` with TypeUtils
timtebeek e8247db
Remove duplication in arguments
timtebeek d2cce4f
Document options and shorten description
timtebeek e00a481
fixed small typo in description
AnuRam123 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
119 changes: 119 additions & 0 deletions
119
src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.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,119 @@ | ||
| /* | ||
| * 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.java.migrate; | ||
|
|
||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Value; | ||
| import org.openrewrite.*; | ||
| import org.openrewrite.internal.lang.Nullable; | ||
| import org.openrewrite.java.ChangeMethodName; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.search.UsesMethod; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
| import org.openrewrite.java.tree.TypedTree; | ||
|
|
||
| @Value | ||
| @EqualsAndHashCode(callSuper = false) | ||
| class ReplaceAWTGetPeerMethod extends Recipe { | ||
|
|
||
| @Option(displayName = "Method pattern to replace", | ||
| description = "The method pattern to match and replace.", | ||
| example = "java.awt.* getPeer()") | ||
| String getPeerMethodPattern; | ||
|
|
||
| @Option(displayName = "The LightweightPeer interface FQCN", | ||
| description = "The fully qualified class name of the LightweightPeer interface to replace in `instanceof`.", | ||
| example = "java.awt.peer.LightweightPeer") | ||
| String lightweightPeerFQCN; | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Replace AWT `getPeer()` method"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "This recipe replaces the use of `getPeer()` method in `java.awt.*` classes. " + | ||
| "`component.getPeer() != null` is replaced with `component.isDisplayable()` and " + | ||
| "`component.getPeer() instanceof LightweightPeer` is replaced with `component.isLightweight()`."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| MethodMatcher methodMatcherGetPeer = new MethodMatcher(getPeerMethodPattern); | ||
| return Preconditions.check(new UsesMethod<>(methodMatcherGetPeer), new JavaVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J visitBinary(J.Binary binary, ExecutionContext ctx) { | ||
| J.Binary bi = (J.Binary) super.visitBinary(binary, ctx); | ||
|
|
||
| J.MethodInvocation mi = findMatchingMethodInvocation(bi); | ||
| if (mi != null) { | ||
| mi = (J.MethodInvocation) new ChangeMethodName( | ||
| getPeerMethodPattern, "isDisplayable", true, null) | ||
| .getVisitor().visit(mi, ctx); | ||
| mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType( | ||
| getPeerMethodPattern.split(" ")[0] + " isDisplayable()", "boolean") | ||
| .getVisitor().visit(mi, ctx); | ||
| assert mi != null; | ||
| return mi.withPrefix(bi.getPrefix()); | ||
| } | ||
|
|
||
| return bi; | ||
| } | ||
|
|
||
| private @Nullable J.MethodInvocation findMatchingMethodInvocation(J.Binary binaryCondition) { | ||
| J.MethodInvocation mi = null; | ||
| if (binaryCondition.getOperator() == J.Binary.Type.NotEqual) { | ||
| if (binaryCondition.getLeft() instanceof J.MethodInvocation && | ||
| binaryCondition.getRight() instanceof J.Literal) { | ||
| mi = (J.MethodInvocation) binaryCondition.getLeft(); | ||
| } else if (binaryCondition.getLeft() instanceof J.Literal && | ||
| binaryCondition.getRight() instanceof J.MethodInvocation) { | ||
| mi = (J.MethodInvocation) binaryCondition.getRight(); | ||
| } | ||
| } | ||
| if (methodMatcherGetPeer.matches(mi)) { | ||
| return mi; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public J visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx) { | ||
| J.InstanceOf instanceOfVar = (J.InstanceOf) super.visitInstanceOf(instOf, ctx); | ||
|
|
||
| if (instanceOfVar.getExpression() instanceof J.MethodInvocation) { | ||
| J.MethodInvocation mi = ((J.MethodInvocation) instanceOfVar.getExpression()); | ||
| if (methodMatcherGetPeer.matches(mi) && TypeUtils.isAssignableTo(lightweightPeerFQCN, ((TypedTree) instanceOfVar.getClazz()).getType())) { | ||
| mi = (J.MethodInvocation) new ChangeMethodName(getPeerMethodPattern, "isLightweight", true, null) | ||
| .getVisitor().visit(mi, ctx); | ||
| mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType( | ||
| getPeerMethodPattern.split(" ")[0] + " isLightweight()", "boolean") | ||
| .getVisitor().visit(mi, ctx); | ||
| assert mi != null; | ||
| maybeRemoveImport(lightweightPeerFQCN); | ||
| return mi.withPrefix(instanceOfVar.getPrefix()); | ||
| } | ||
| } | ||
|
|
||
| return instanceOfVar; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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
141 changes: 141 additions & 0 deletions
141
src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.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,141 @@ | ||
| /* | ||
| * 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.java.migrate; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.openrewrite.DocumentExample; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| @SuppressWarnings("StatementWithEmptyBody") | ||
| class ReplaceAWTGetPeerMethodTest implements RewriteTest { | ||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new ReplaceAWTGetPeerMethod("com.test.Component1 getPeer()", "com.test.TestDummy")) | ||
| .parser(JavaParser.fromJavaVersion() | ||
| //language=java | ||
| .dependsOn( | ||
| """ | ||
| package com.test; | ||
| public class Component1 { | ||
| public String getPeer() { | ||
| return "x"; | ||
| } | ||
| public boolean isDisplayable() { | ||
| return true; | ||
| } | ||
| public boolean isLightweight() { | ||
| return true; | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| package com.test; | ||
| public class TestDummy { | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DocumentExample | ||
| void instanceAndGetPeerMethodControlParentheses() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import com.test.Component1; | ||
| import com.test.TestDummy; | ||
| class Test { | ||
| void foo() { | ||
| Test t1 = new Test(); | ||
| Component1 c = new Component1(); | ||
| if (c.getPeer() instanceof com.test.TestDummy) { | ||
| } | ||
| if (c.getPeer() instanceof TestDummy) { | ||
| } | ||
| Component1 y = new Component1(); | ||
| if (y.getPeer() != null) { | ||
| } | ||
| if (null != y.getPeer()) { | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import com.test.Component1; | ||
| class Test { | ||
| void foo() { | ||
| Test t1 = new Test(); | ||
| Component1 c = new Component1(); | ||
| if (c.isLightweight()) { | ||
| } | ||
| if (c.isLightweight()) { | ||
| } | ||
| Component1 y = new Component1(); | ||
| if (y.isDisplayable()) { | ||
| } | ||
| if (y.isDisplayable()) { | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void instanceAndGetPeerMethodNoParenthesis() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import com.test.Component1; | ||
| import com.test.TestDummy; | ||
| class Test { | ||
| void foo() { | ||
| Component1 y = new Component1(); | ||
| boolean instance = y.getPeer() instanceof TestDummy; | ||
| if (instance){ | ||
| } | ||
| boolean instance1 = y.getPeer() != null; | ||
| if (instance1){ | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import com.test.Component1; | ||
| class Test { | ||
| void foo() { | ||
| Component1 y = new Component1(); | ||
| boolean instance = y.isLightweight(); | ||
| if (instance){ | ||
| } | ||
| boolean instance1 = y.isDisplayable(); | ||
| if (instance1){ | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } |
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.