From 61abfb80ee2d5b2eb4273b3ba618ed19dfd07602 Mon Sep 17 00:00:00 2001 From: anuram Date: Thu, 1 Aug 2024 11:27:18 -0400 Subject: [PATCH 01/21] getPeer() recipe --- .../java/migrate/DetectAWTGetPeerMethod.java | 104 ++++++++++++++++++ .../migrate/DetectAWTGetPeerMethodTest.java | 78 +++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java create mode 100644 src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java diff --git a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java new file mode 100644 index 0000000000..d99f59e54a --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java @@ -0,0 +1,104 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JRightPadded; +import org.openrewrite.java.tree.Space; +import org.openrewrite.marker.Markers; + +import static org.openrewrite.Tree.randomId; + + +public class DetectAWTGetPeerMethod extends Recipe { + @Override + public String getDisplayName() { + return "Replace `getPeer()` method"; + } + + @Override + public String getDescription() { + return " All methods that refer to types defined in the java.awt.peer package are removed in Java 11. " + + "This recipe replaces the use of getPeer() method on the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." + + "The occurrence of `if (component.getPeer() != null) { .. }` is replaced with `if (component.isDisplayable())` " + + "and the occurence of `if (component.getPeer() instanceof LightweightPeer)` " + + "is replaced with `if (component.isLightweight())`."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesMethod<>("com.test.Component1 getPeer()"), new JavaVisitor() { + public J.If visitIf(J.If iff, ExecutionContext ctx) { + J.If ifJ = (J.If) super.visitIf(iff, ctx); + Expression ifCExp = ifJ.getIfCondition().getTree(); + //J.ControlParentheses ifCExp1 = ifJ.getIfCondition(); + if (!(ifCExp instanceof J.Binary)) { + return ifJ; + } + J.Binary binaryCondition = (J.Binary) ifCExp; + //check if(x.getPeer() != null) + if (binaryCondition.getLeft() instanceof J.MethodInvocation && binaryCondition.getRight() instanceof J.Literal && binaryCondition.getOperator().name().equals("NotEqual")) { + J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); + J.Literal lt = (J.Literal) binaryCondition.getRight(); + if (mi.getName().getSimpleName().equals("getPeer") && lt.getValueSource().equals("null")) { + String objectName = ((J.Identifier) mi.getSelect()).getSimpleName(); + JavaTemplate newIfConditionTemplate = JavaTemplate.builder(objectName + ".isDisplayable()") + .imports("java.awt.Component").build(); + ifJ = newIfConditionTemplate.apply(getCursor(), ifCExp.getCoordinates().replace()); +// Expression newIfExpression = newIfConditionTemplate.apply(new Cursor(getCursor(),ifCExp),ifCExp.getCoordinates().replace());; +// J.ControlParentheses jNewCP = new J.ControlParentheses<>(randomId(),Space.EMPTY,Markers.EMPTY,JRightPadded.build(newIfExpression)).withPrefix(Space.SINGLE_SPACE); +// ifJ = ifJ.withIfCondition(jNewCP); + + return ifJ; + } + } + return ifJ; + } + + public J.ControlParentheses visitControlParentheses(J.ControlParentheses cp, ExecutionContext ctx) { + Expression ifCExp = (Expression) cp.getTree(); + if (!(ifCExp instanceof J.Binary)) { + return cp; + } + J.Binary binaryCondition = (J.Binary) ifCExp; + //check if(x.getPeer() != null) + if (binaryCondition.getLeft() instanceof J.MethodInvocation && binaryCondition.getRight() instanceof J.Literal && binaryCondition.getOperator().name().equals("NotEqual")) { + J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); + J.Literal lt = (J.Literal) binaryCondition.getRight(); + if (mi.getName().getSimpleName().equals("getPeer") && lt.getValueSource().equals("null")) { + String objectName = ((J.Identifier) mi.getSelect()).getSimpleName(); + JavaTemplate newIfConditionTemplate = JavaTemplate.builder(objectName + ".isDisplayable()") + .imports("java.awt.Component").build(); + //cp = newIfConditionTemplate.apply(getCursor(),cp.getCoordinates().replace()); + Expression newIfExpression = newIfConditionTemplate.apply(getCursor(), cp.getCoordinates().replace()); + J.ControlParentheses jNewCP = new J.ControlParentheses<>(randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(newIfExpression)); + return (J.ControlParentheses) jNewCP; + } + } + return cp; + } + }); + } +} diff --git a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java new file mode 100644 index 0000000000..6d77baf020 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.openrewrite.java.JavaParser; +import org.openrewrite.test.RewriteTest; +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RecipeSpec; +import static org.openrewrite.java.Assertions.java; + +class DetectAWTGetPeerMethodTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new DetectAWTGetPeerMethod()); + } + //language=java + String componentClass = """ + package com.test; + public class Component1 { + + public String getPeer(){ + return "x"; + } + public boolean getPeer1(){ + return true; + } + public boolean isDisplayable(){ + return true; + } + } + """; + @DocumentExample + @Test + void testMethod(){ + rewriteRun( + //language=java + java(componentClass), + java( + """ + package com.test; + + public class Test { + + public void doX() { + Component1 y = new Component1(); + if (y.getPeer() != null){} + } + } + """, + """ + package com.test; + + public class Test { + + public void doX() { + Component1 y = new Component1(); + if( y.isDisplayable()){} + } + } + """ + ) + ); + } +} From f60b42225740e33506055e4a6890d42774c5c088 Mon Sep 17 00:00:00 2001 From: anuram Date: Mon, 5 Aug 2024 15:38:25 -0400 Subject: [PATCH 02/21] updated with fixes --- .../java/migrate/DetectAWTGetPeerMethod.java | 119 ++++++++++-------- .../META-INF/rewrite/java-version-11.yml | 3 +- .../migrate/DetectAWTGetPeerMethodTest.java | 61 +++++---- 3 files changed, 107 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java index d99f59e54a..ed1c061c7c 100644 --- a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java @@ -16,23 +16,23 @@ package org.openrewrite.java.migrate; +import com.fasterxml.jackson.annotation.JsonCreator; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; -import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.ChangeMethodName; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.Expression; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JRightPadded; -import org.openrewrite.java.tree.Space; -import org.openrewrite.marker.Markers; +import org.openrewrite.java.tree.*; -import static org.openrewrite.Tree.randomId; public class DetectAWTGetPeerMethod extends Recipe { + private final String methodPatternGetPeer ; + private final String methodUpdateIsDisplayable; + private final String className; + private final String methodUpdateIsLightweight; @Override public String getDisplayName() { return "Replace `getPeer()` method"; @@ -41,64 +41,77 @@ public String getDisplayName() { @Override public String getDescription() { return " All methods that refer to types defined in the java.awt.peer package are removed in Java 11. " - + "This recipe replaces the use of getPeer() method on the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." + + "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." + "The occurrence of `if (component.getPeer() != null) { .. }` is replaced with `if (component.isDisplayable())` " - + "and the occurence of `if (component.getPeer() instanceof LightweightPeer)` " + + "and the occurrence of `if (component.getPeer() instanceof LightweightPeer)` " + "is replaced with `if (component.isLightweight())`."; } - + @JsonCreator + public DetectAWTGetPeerMethod() { + this.methodPatternGetPeer = "java.awt.* getPeer()"; + this.methodUpdateIsDisplayable = "java.awt.* isDisplayable()"; + this.className = "java.awt.peer.LightweightPeer"; + this.methodUpdateIsLightweight = "java.awt.* isLightweight()"; + } + /** + * Overload constructor to allow for custom method patterns used in tests only. + */ + DetectAWTGetPeerMethod(String methodPatternGetPeer, String methodUpdatedIsDisplayable, String className, String methodUpdateIsLightweight ) { + this.methodPatternGetPeer = methodPatternGetPeer; + this.methodUpdateIsDisplayable = methodUpdatedIsDisplayable; + this.className = className; + this.methodUpdateIsLightweight = methodUpdateIsLightweight; + } @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesMethod<>("com.test.Component1 getPeer()"), new JavaVisitor() { - public J.If visitIf(J.If iff, ExecutionContext ctx) { - J.If ifJ = (J.If) super.visitIf(iff, ctx); - Expression ifCExp = ifJ.getIfCondition().getTree(); - //J.ControlParentheses ifCExp1 = ifJ.getIfCondition(); - if (!(ifCExp instanceof J.Binary)) { - return ifJ; - } - J.Binary binaryCondition = (J.Binary) ifCExp; - //check if(x.getPeer() != null) - if (binaryCondition.getLeft() instanceof J.MethodInvocation && binaryCondition.getRight() instanceof J.Literal && binaryCondition.getOperator().name().equals("NotEqual")) { - J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); - J.Literal lt = (J.Literal) binaryCondition.getRight(); - if (mi.getName().getSimpleName().equals("getPeer") && lt.getValueSource().equals("null")) { - String objectName = ((J.Identifier) mi.getSelect()).getSimpleName(); - JavaTemplate newIfConditionTemplate = JavaTemplate.builder(objectName + ".isDisplayable()") - .imports("java.awt.Component").build(); - ifJ = newIfConditionTemplate.apply(getCursor(), ifCExp.getCoordinates().replace()); -// Expression newIfExpression = newIfConditionTemplate.apply(new Cursor(getCursor(),ifCExp),ifCExp.getCoordinates().replace());; -// J.ControlParentheses jNewCP = new J.ControlParentheses<>(randomId(),Space.EMPTY,Markers.EMPTY,JRightPadded.build(newIfExpression)).withPrefix(Space.SINGLE_SPACE); -// ifJ = ifJ.withIfCondition(jNewCP); - - return ifJ; - } - } - return ifJ; - } - + return Preconditions.check(new UsesMethod<>(methodPatternGetPeer), new JavaVisitor() { public J.ControlParentheses visitControlParentheses(J.ControlParentheses cp, ExecutionContext ctx) { Expression ifCExp = (Expression) cp.getTree(); - if (!(ifCExp instanceof J.Binary)) { + //Check if there is a Binary or an instanceOf inside the paranthesis + if (!(ifCExp instanceof J.Binary) && !(ifCExp instanceof J.InstanceOf)) { return cp; } - J.Binary binaryCondition = (J.Binary) ifCExp; - //check if(x.getPeer() != null) - if (binaryCondition.getLeft() instanceof J.MethodInvocation && binaryCondition.getRight() instanceof J.Literal && binaryCondition.getOperator().name().equals("NotEqual")) { - J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); - J.Literal lt = (J.Literal) binaryCondition.getRight(); - if (mi.getName().getSimpleName().equals("getPeer") && lt.getValueSource().equals("null")) { - String objectName = ((J.Identifier) mi.getSelect()).getSimpleName(); - JavaTemplate newIfConditionTemplate = JavaTemplate.builder(objectName + ".isDisplayable()") - .imports("java.awt.Component").build(); - //cp = newIfConditionTemplate.apply(getCursor(),cp.getCoordinates().replace()); - Expression newIfExpression = newIfConditionTemplate.apply(getCursor(), cp.getCoordinates().replace()); - J.ControlParentheses jNewCP = new J.ControlParentheses<>(randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(newIfExpression)); - return (J.ControlParentheses) jNewCP; + if (ifCExp instanceof J.Binary) { + J.Binary binaryCondition = (J.Binary) ifCExp; + //check if(x.getPeer() != null) + if (binaryCondition.getLeft() instanceof J.MethodInvocation && binaryCondition.getRight() instanceof J.Literal && binaryCondition.getOperator().name().equals("NotEqual")) { + J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); + J.Literal lt = (J.Literal) binaryCondition.getRight(); + if (mi.getName().getSimpleName().equals("getPeer") && lt.getValueSource().equals("null")) { + mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isDisplayable", true, null + ).getVisitor().visit(mi, ctx); + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable,"boolean").getVisitor().visit(mi, ctx); + return cp.withTree((T) mi); + } + } + } + else if (ifCExp instanceof J.InstanceOf){ + J.InstanceOf instanceOfVar = (J.InstanceOf) ifCExp; + if((instanceOfVar.getExpression() instanceof J.MethodInvocation)) { + J.MethodInvocation mi = ((J.MethodInvocation) instanceOfVar.getExpression()); + if (mi.getName().getSimpleName().equals("getPeer") && checkClassNameIsEqualFQCN(instanceOfVar)) { + mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null + ).getVisitor().visit(mi, ctx); + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean").getVisitor().visit(mi, ctx); + return cp.withTree((T) mi); + } } } return cp; } - }); + }); + } + private boolean checkClassNameIsEqualFQCN(J.InstanceOf instOf){ + if(instOf.getClazz() instanceof J.Identifier){ + J.Identifier id = (J.Identifier) instOf.getClazz(); + return ((JavaType.Class) id.getType()).getFullyQualifiedName().toString().equals(className); + } + else if (instOf.getClazz() instanceof J.FieldAccess){ + J.FieldAccess fid = (J.FieldAccess) instOf.getClazz(); + return ((JavaType.Class) fid.getType()).getFullyQualifiedName().toString().equals(className); + } + else{ + return false; + } } } diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index 02e9cd8afa..d8b9df6a20 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -69,7 +69,7 @@ recipeList: - org.openrewrite.java.migrate.RemovedPolicy - org.openrewrite.java.migrate.ReferenceCloneMethod - org.openrewrite.java.migrate.ThreadStopDestroy - + - org.openrewrite.java.migrate.DetectAWTGetPeerMethod --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.UpgradeBuildToJava11 @@ -265,3 +265,4 @@ recipeList: methodPattern: java.lang.Thread destroy() - org.openrewrite.java.migrate.RemoveMethodInvocation: methodPattern: java.lang.Thread stop(java.lang.Throwable) +--- \ No newline at end of file diff --git a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java index 6d77baf020..21ee83186c 100644 --- a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java @@ -25,54 +25,71 @@ class DetectAWTGetPeerMethodTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new DetectAWTGetPeerMethod()); + spec.recipe(new DetectAWTGetPeerMethod("com.test.Component1 getPeer()","com.test.Component1 isDisplayable()", "com.test.TestDummy","com.test.Component1 isLightweight()")); } //language=java String componentClass = """ package com.test; public class Component1 { - public String getPeer(){ - return "x"; - } - public boolean getPeer1(){ - return true; - } - public boolean isDisplayable(){ - return true; - } + public String getPeer(){ + return "x"; + } + public boolean getPeer1(){ + return true; + } + public boolean isDisplayable(){ + return true; + } + public boolean isLightweight(){ + return true; + } + } + """; + String instantOfDummyClass = """ + package com.test; + public class TestDummy { } """; @DocumentExample @Test - void testMethod(){ + void testInstanceAndGetPeerMethod(){ rewriteRun( //language=java java(componentClass), + java(instantOfDummyClass), java( """ package com.test; - public class Test { + public class Test extends TestDummy{ - public void doX() { - Component1 y = new Component1(); - if (y.getPeer() != null){} + public static void main(String args[]) { + 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){} } } """, """ package com.test; - public class Test { + public class Test extends TestDummy{ - public void doX() { - Component1 y = new Component1(); - if( y.isDisplayable()){} + public static void main(String args[]) { + Test t1 = new Test(); + Component1 c = new Component1(); + if(c.isLightweight()){}; + if(c.isLightweight()){}; + Component1 y = new Component1(); + if (y.isDisplayable()){} } } """ - ) - ); - } + ) + ); + } } From 3c0e764bacdd549fe9e476f3a5c10f9abfd9904d Mon Sep 17 00:00:00 2001 From: anuram Date: Mon, 5 Aug 2024 16:36:16 -0400 Subject: [PATCH 03/21] small updates --- .../java/migrate/DetectAWTGetPeerMethod.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java index ed1c061c7c..8b637aff6e 100644 --- a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java @@ -28,7 +28,7 @@ -public class DetectAWTGetPeerMethod extends Recipe { +class DetectAWTGetPeerMethod extends Recipe { private final String methodPatternGetPeer ; private final String methodUpdateIsDisplayable; private final String className; @@ -42,9 +42,9 @@ public String getDisplayName() { public String getDescription() { return " All methods that refer to types defined in the java.awt.peer package are removed in Java 11. " + "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." - + "The occurrence of `if (component.getPeer() != null) { .. }` is replaced with `if (component.isDisplayable())` " - + "and the occurrence of `if (component.getPeer() instanceof LightweightPeer)` " - + "is replaced with `if (component.isLightweight())`."; + + "The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` " + + "and the occurrence of `(component.getPeer() instanceof LightweightPeer)` " + + "is replaced with `(component.isLightweight())`."; } @JsonCreator public DetectAWTGetPeerMethod() { @@ -89,7 +89,7 @@ else if (ifCExp instanceof J.InstanceOf){ J.InstanceOf instanceOfVar = (J.InstanceOf) ifCExp; if((instanceOfVar.getExpression() instanceof J.MethodInvocation)) { J.MethodInvocation mi = ((J.MethodInvocation) instanceOfVar.getExpression()); - if (mi.getName().getSimpleName().equals("getPeer") && checkClassNameIsEqualFQCN(instanceOfVar)) { + if (mi.getName().getSimpleName().equals("getPeer") && checkClassNameIsEqualToFQCN(instanceOfVar)) { mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null ).getVisitor().visit(mi, ctx); mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean").getVisitor().visit(mi, ctx); @@ -101,7 +101,7 @@ else if (ifCExp instanceof J.InstanceOf){ } }); } - private boolean checkClassNameIsEqualFQCN(J.InstanceOf instOf){ + private boolean checkClassNameIsEqualToFQCN(J.InstanceOf instOf){ if(instOf.getClazz() instanceof J.Identifier){ J.Identifier id = (J.Identifier) instOf.getClazz(); return ((JavaType.Class) id.getType()).getFullyQualifiedName().toString().equals(className); From 1bb77d2b3e6ab2f922865273d376029bb9417d1e Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 6 Aug 2024 00:29:21 +0200 Subject: [PATCH 04/21] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java | 1 + .../openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java index 8b637aff6e..fb6b49e4c2 100644 --- a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java @@ -65,6 +65,7 @@ public DetectAWTGetPeerMethod() { @Override public TreeVisitor getVisitor() { return Preconditions.check(new UsesMethod<>(methodPatternGetPeer), new JavaVisitor() { + @Override public J.ControlParentheses visitControlParentheses(J.ControlParentheses cp, ExecutionContext ctx) { Expression ifCExp = (Expression) cp.getTree(); //Check if there is a Binary or an instanceOf inside the paranthesis diff --git a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java index 21ee83186c..d73db106dd 100644 --- a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java @@ -17,6 +17,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.test.RewriteTest; + import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.test.RecipeSpec; @@ -53,7 +54,7 @@ public class TestDummy { """; @DocumentExample @Test - void testInstanceAndGetPeerMethod(){ + void instanceAndGetPeerMethod(){ rewriteRun( //language=java java(componentClass), From 3d1de0aab7b6996da583901431557d532d9dfaa4 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 6 Aug 2024 00:33:48 +0200 Subject: [PATCH 05/21] Apply suggestions from code review --- .../org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java index fb6b49e4c2..74abbaac62 100644 --- a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java @@ -26,8 +26,6 @@ import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.*; - - class DetectAWTGetPeerMethod extends Recipe { private final String methodPatternGetPeer ; private final String methodUpdateIsDisplayable; From deb96c35f40ea13a8de35370b12b4e9c91d41bf0 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 6 Aug 2024 10:22:06 +0200 Subject: [PATCH 06/21] Apply formatter & order imports --- .../java/migrate/BeansXmlNamespace.java | 1 + .../java/migrate/DetectAWTGetPeerMethod.java | 59 +++++----- .../META-INF/rewrite/java-version-11.yml | 1 - .../migrate/DetectAWTGetPeerMethodTest.java | 104 ++++++++++-------- 4 files changed, 87 insertions(+), 78 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/BeansXmlNamespace.java b/src/main/java/org/openrewrite/java/migrate/BeansXmlNamespace.java index ad2956be73..4d2821a988 100644 --- a/src/main/java/org/openrewrite/java/migrate/BeansXmlNamespace.java +++ b/src/main/java/org/openrewrite/java/migrate/BeansXmlNamespace.java @@ -23,6 +23,7 @@ import org.openrewrite.xml.XmlVisitor; import org.openrewrite.xml.tree.Xml; +import java.util.List; import java.util.Map; import static java.util.stream.Collectors.toMap; diff --git a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java index 74abbaac62..0ae4aa75a9 100644 --- a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java @@ -17,6 +17,10 @@ package org.openrewrite.java.migrate; import com.fasterxml.jackson.annotation.JsonCreator; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Value; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; @@ -24,13 +28,19 @@ import org.openrewrite.java.ChangeMethodName; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.*; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +@Value +@EqualsAndHashCode(callSuper = false) +@AllArgsConstructor(access = AccessLevel.PACKAGE) // For tests class DetectAWTGetPeerMethod extends Recipe { - private final String methodPatternGetPeer ; - private final String methodUpdateIsDisplayable; - private final String className; - private final String methodUpdateIsLightweight; + String methodPatternGetPeer; + String methodUpdateIsDisplayable; + String className; + String methodUpdateIsLightweight; + @Override public String getDisplayName() { return "Replace `getPeer()` method"; @@ -39,11 +49,12 @@ public String getDisplayName() { @Override public String getDescription() { return " All methods that refer to types defined in the java.awt.peer package are removed in Java 11. " - + "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." - + "The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` " - + "and the occurrence of `(component.getPeer() instanceof LightweightPeer)` " - + "is replaced with `(component.isLightweight())`."; + + "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." + + "The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` " + + "and the occurrence of `(component.getPeer() instanceof LightweightPeer)` " + + "is replaced with `(component.isLightweight())`."; } + @JsonCreator public DetectAWTGetPeerMethod() { this.methodPatternGetPeer = "java.awt.* getPeer()"; @@ -51,15 +62,7 @@ public DetectAWTGetPeerMethod() { this.className = "java.awt.peer.LightweightPeer"; this.methodUpdateIsLightweight = "java.awt.* isLightweight()"; } - /** - * Overload constructor to allow for custom method patterns used in tests only. - */ - DetectAWTGetPeerMethod(String methodPatternGetPeer, String methodUpdatedIsDisplayable, String className, String methodUpdateIsLightweight ) { - this.methodPatternGetPeer = methodPatternGetPeer; - this.methodUpdateIsDisplayable = methodUpdatedIsDisplayable; - this.className = className; - this.methodUpdateIsLightweight = methodUpdateIsLightweight; - } + @Override public TreeVisitor getVisitor() { return Preconditions.check(new UsesMethod<>(methodPatternGetPeer), new JavaVisitor() { @@ -79,14 +82,13 @@ public J.ControlParentheses visitControlParentheses(J.ControlPa if (mi.getName().getSimpleName().equals("getPeer") && lt.getValueSource().equals("null")) { mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isDisplayable", true, null ).getVisitor().visit(mi, ctx); - mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable,"boolean").getVisitor().visit(mi, ctx); + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean").getVisitor().visit(mi, ctx); return cp.withTree((T) mi); } } - } - else if (ifCExp instanceof J.InstanceOf){ + } else if (ifCExp instanceof J.InstanceOf) { J.InstanceOf instanceOfVar = (J.InstanceOf) ifCExp; - if((instanceOfVar.getExpression() instanceof J.MethodInvocation)) { + if ((instanceOfVar.getExpression() instanceof J.MethodInvocation)) { J.MethodInvocation mi = ((J.MethodInvocation) instanceOfVar.getExpression()); if (mi.getName().getSimpleName().equals("getPeer") && checkClassNameIsEqualToFQCN(instanceOfVar)) { mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null @@ -98,18 +100,17 @@ else if (ifCExp instanceof J.InstanceOf){ } return cp; } - }); + }); } - private boolean checkClassNameIsEqualToFQCN(J.InstanceOf instOf){ - if(instOf.getClazz() instanceof J.Identifier){ + + private boolean checkClassNameIsEqualToFQCN(J.InstanceOf instOf) { + if (instOf.getClazz() instanceof J.Identifier) { J.Identifier id = (J.Identifier) instOf.getClazz(); return ((JavaType.Class) id.getType()).getFullyQualifiedName().toString().equals(className); - } - else if (instOf.getClazz() instanceof J.FieldAccess){ + } else if (instOf.getClazz() instanceof J.FieldAccess) { J.FieldAccess fid = (J.FieldAccess) instOf.getClazz(); return ((JavaType.Class) fid.getType()).getFullyQualifiedName().toString().equals(className); - } - else{ + } else { return false; } } diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index 3f8ef93bc0..21d9b5b665 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -282,4 +282,3 @@ recipeList: - org.openrewrite.java.ChangeMethodName: methodPattern: java.nio.file.Path get(..) newMethodName: of - diff --git a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java index d73db106dd..c4232bcfea 100644 --- a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java @@ -15,79 +15,87 @@ */ package org.openrewrite.java.migrate; -import org.openrewrite.java.JavaParser; -import org.openrewrite.test.RewriteTest; - 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; class DetectAWTGetPeerMethodTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new DetectAWTGetPeerMethod("com.test.Component1 getPeer()","com.test.Component1 isDisplayable()", "com.test.TestDummy","com.test.Component1 isLightweight()")); + spec.recipe(new DetectAWTGetPeerMethod( + "com.test.Component1 getPeer()", + "com.test.Component1 isDisplayable()", + "com.test.TestDummy", + "com.test.Component1 isLightweight()")) + .parser(JavaParser.fromJavaVersion() + //language=java + .dependsOn( + """ + package com.test; + public class Component1 { + public String getPeer() { + return "x"; + } + public boolean getPeer1() { + return true; + } + public boolean isDisplayable() { + return true; + } + public boolean isLightweight() { + return true; + } + } + """, + """ + package com.test; + public class TestDummy { + } + """ + )); } - //language=java - String componentClass = """ - package com.test; - public class Component1 { - public String getPeer(){ - return "x"; - } - public boolean getPeer1(){ - return true; - } - public boolean isDisplayable(){ - return true; - } - public boolean isLightweight(){ - return true; - } - } - """; - String instantOfDummyClass = """ - package com.test; - public class TestDummy { - } - """; + @DocumentExample @Test - void instanceAndGetPeerMethod(){ + void instanceAndGetPeerMethod() { rewriteRun( //language=java - java(componentClass), - java(instantOfDummyClass), java( """ package com.test; - - public class Test extends TestDummy{ - - public static void main(String args[]) { + class Test extends TestDummy { + public 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 (c.getPeer() instanceof com.test.TestDummy) { + } + if (c.getPeer() instanceof TestDummy) { + } + Component1 y = new Component1(); + if (y.getPeer() != null) { + } + } } """, """ package com.test; - - public class Test extends TestDummy{ - - public static void main(String args[]) { + public class Test extends TestDummy { + public static void main(String args[]) { Test t1 = new Test(); Component1 c = new Component1(); - if(c.isLightweight()){}; - if(c.isLightweight()){}; - Component1 y = new Component1(); - if (y.isDisplayable()){} - } + if (c.isLightweight()) { + } + if (c.isLightweight()) { + } + Component1 y = new Component1(); + if (y.isDisplayable()) { + } + } } """ ) From 4e63d90029556d959b4c61bdea0e655a8fe1dc45 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 6 Aug 2024 10:32:47 +0200 Subject: [PATCH 07/21] Align before and after code blocks again --- .../java/migrate/DetectAWTGetPeerMethodTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java index c4232bcfea..9fca644625 100644 --- a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java @@ -69,7 +69,7 @@ void instanceAndGetPeerMethod() { """ package com.test; class Test extends TestDummy { - public void foo() { + void foo() { Test t1 = new Test(); Component1 c = new Component1(); if (c.getPeer() instanceof com.test.TestDummy) { @@ -84,8 +84,8 @@ public void foo() { """, """ package com.test; - public class Test extends TestDummy { - public static void main(String args[]) { + class Test extends TestDummy { + void foo() { Test t1 = new Test(); Component1 c = new Component1(); if (c.isLightweight()) { From 1d16fc489ab8bf82fb49d7b2cb1931c036e732e8 Mon Sep 17 00:00:00 2001 From: anuram Date: Tue, 6 Aug 2024 15:08:57 -0400 Subject: [PATCH 08/21] updated recipe name, updated tests and formatted code --- ...thod.java => ReplaceAWTGetPeerMethod.java} | 81 +++++++++++++------ .../META-INF/rewrite/java-version-11.yml | 2 +- ....java => ReplaceAWTGetPeerMethodTest.java} | 46 ++++++++++- 3 files changed, 100 insertions(+), 29 deletions(-) rename src/main/java/org/openrewrite/java/migrate/{DetectAWTGetPeerMethod.java => ReplaceAWTGetPeerMethod.java} (58%) rename src/test/java/org/openrewrite/java/migrate/{DetectAWTGetPeerMethodTest.java => ReplaceAWTGetPeerMethodTest.java} (71%) diff --git a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java similarity index 58% rename from src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java rename to src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index 0ae4aa75a9..5544c563c4 100644 --- a/src/main/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -27,6 +27,7 @@ import org.openrewrite.TreeVisitor; 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.Expression; import org.openrewrite.java.tree.J; @@ -35,12 +36,20 @@ @Value @EqualsAndHashCode(callSuper = false) @AllArgsConstructor(access = AccessLevel.PACKAGE) // For tests -class DetectAWTGetPeerMethod extends Recipe { +class ReplaceAWTGetPeerMethod extends Recipe { String methodPatternGetPeer; String methodUpdateIsDisplayable; String className; String methodUpdateIsLightweight; + @JsonCreator + public ReplaceAWTGetPeerMethod() { + this.methodPatternGetPeer = "java.awt.* getPeer()"; + this.methodUpdateIsDisplayable = "java.awt.* isDisplayable()"; + this.className = "java.awt.peer.LightweightPeer"; + this.methodUpdateIsLightweight = "java.awt.* isLightweight()"; + } + @Override public String getDisplayName() { return "Replace `getPeer()` method"; @@ -49,18 +58,10 @@ public String getDisplayName() { @Override public String getDescription() { return " All methods that refer to types defined in the java.awt.peer package are removed in Java 11. " - + "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." - + "The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` " - + "and the occurrence of `(component.getPeer() instanceof LightweightPeer)` " - + "is replaced with `(component.isLightweight())`."; - } - - @JsonCreator - public DetectAWTGetPeerMethod() { - this.methodPatternGetPeer = "java.awt.* getPeer()"; - this.methodUpdateIsDisplayable = "java.awt.* isDisplayable()"; - this.className = "java.awt.peer.LightweightPeer"; - this.methodUpdateIsLightweight = "java.awt.* isLightweight()"; + + "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." + + "The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` " + + "and the occurrence of `(component.getPeer() instanceof LightweightPeer)` " + + "is replaced with `(component.isLightweight())`."; } @Override @@ -68,29 +69,34 @@ public TreeVisitor getVisitor() { return Preconditions.check(new UsesMethod<>(methodPatternGetPeer), new JavaVisitor() { @Override public J.ControlParentheses visitControlParentheses(J.ControlParentheses cp, ExecutionContext ctx) { + MethodMatcher methodMatcherGetPeer = new MethodMatcher(methodPatternGetPeer); Expression ifCExp = (Expression) cp.getTree(); - //Check if there is a Binary or an instanceOf inside the paranthesis + //Check if there is a Binary or an instanceOf inside the Parentheses if (!(ifCExp instanceof J.Binary) && !(ifCExp instanceof J.InstanceOf)) { return cp; } if (ifCExp instanceof J.Binary) { J.Binary binaryCondition = (J.Binary) ifCExp; //check if(x.getPeer() != null) - if (binaryCondition.getLeft() instanceof J.MethodInvocation && binaryCondition.getRight() instanceof J.Literal && binaryCondition.getOperator().name().equals("NotEqual")) { + //Not sure if we need to check if (null != getPeer()) + if (checkMethodInvocationNotEqualLiteralInBinary(binaryCondition)) { J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); J.Literal lt = (J.Literal) binaryCondition.getRight(); - if (mi.getName().getSimpleName().equals("getPeer") && lt.getValueSource().equals("null")) { - mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isDisplayable", true, null - ).getVisitor().visit(mi, ctx); - mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean").getVisitor().visit(mi, ctx); - return cp.withTree((T) mi); + if (methodMatcherGetPeer.matches(mi.getMethodType())) { + assert lt.getValueSource() != null; + if (lt.getValueSource().equals("null")) { + mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isDisplayable", true, null + ).getVisitor().visit(mi, ctx); + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean").getVisitor().visit(mi, ctx); + return cp.withTree((T) mi); + } } } - } else if (ifCExp instanceof J.InstanceOf) { + } else { J.InstanceOf instanceOfVar = (J.InstanceOf) ifCExp; - if ((instanceOfVar.getExpression() instanceof J.MethodInvocation)) { + if (instanceOfVar.getExpression() instanceof J.MethodInvocation) { J.MethodInvocation mi = ((J.MethodInvocation) instanceOfVar.getExpression()); - if (mi.getName().getSimpleName().equals("getPeer") && checkClassNameIsEqualToFQCN(instanceOfVar)) { + if (methodMatcherGetPeer.matches(mi.getMethodType()) && checkClassNameIsEqualToFQCN(instanceOfVar)) { mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null ).getVisitor().visit(mi, ctx); mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean").getVisitor().visit(mi, ctx); @@ -100,18 +106,43 @@ public J.ControlParentheses visitControlParentheses(J.ControlPa } return cp; } +// @Override +// public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, ExecutionContext ctx){ +// return md; +// } +// @Override +// public J.Binary visitBinary(J.Binary binary, ExecutionContext ctx){ +// return binary; +// } +// @Override +// public J.InstanceOf visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx){ +// return instOf; +// } }); } private boolean checkClassNameIsEqualToFQCN(J.InstanceOf instOf) { if (instOf.getClazz() instanceof J.Identifier) { J.Identifier id = (J.Identifier) instOf.getClazz(); - return ((JavaType.Class) id.getType()).getFullyQualifiedName().toString().equals(className); + assert id.getType() != null; + return ((JavaType.Class) id.getType()).getFullyQualifiedName().equals(className); } else if (instOf.getClazz() instanceof J.FieldAccess) { J.FieldAccess fid = (J.FieldAccess) instOf.getClazz(); - return ((JavaType.Class) fid.getType()).getFullyQualifiedName().toString().equals(className); + assert fid.getType() != null; + return ((JavaType.Class) fid.getType()).getFullyQualifiedName().equals(className); } else { return false; } } + + private boolean checkMethodInvocationNotEqualLiteralInBinary(J.Binary binaryCondition) { + if (binaryCondition.getLeft() instanceof J.MethodInvocation) { + return (binaryCondition.getRight() instanceof J.Literal && binaryCondition.getOperator() == J.Binary.Type.NotEqual); + } + if (binaryCondition.getRight() instanceof J.MethodInvocation) { + return (binaryCondition.getLeft() instanceof J.Literal && binaryCondition.getOperator() == J.Binary.Type.NotEqual); + } + return false; + } + } diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index 21d9b5b665..9132781947 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -70,7 +70,7 @@ recipeList: - org.openrewrite.java.migrate.RemovedPolicy - org.openrewrite.java.migrate.ReferenceCloneMethod - org.openrewrite.java.migrate.ThreadStopDestroy - - org.openrewrite.java.migrate.DetectAWTGetPeerMethod + - org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.UpgradeBuildToJava11 diff --git a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java similarity index 71% rename from src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java rename to src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index 9fca644625..0617723e4f 100644 --- a/src/test/java/org/openrewrite/java/migrate/DetectAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -62,7 +62,7 @@ public class TestDummy { @DocumentExample @Test - void instanceAndGetPeerMethod() { + void instanceAndGetPeerMethodControlParentheses() { rewriteRun( //language=java java( @@ -71,14 +71,14 @@ void instanceAndGetPeerMethod() { class Test extends TestDummy { void foo() { Test t1 = new Test(); - Component1 c = new Component1(); + Component1 c = new Component1(); if (c.getPeer() instanceof com.test.TestDummy) { } if (c.getPeer() instanceof TestDummy) { } Component1 y = new Component1(); if (y.getPeer() != null) { - } + } } } """, @@ -94,6 +94,46 @@ void foo() { } Component1 y = new Component1(); if (y.isDisplayable()) { + } + } + } + """ + ) + ); + } + + @DocumentExample + @Test + void instanceAndGetPeerMethodNoParenthesis() { + rewriteRun( + //language=java + java( + """ + package com.test; + + class Test { + void foo() { + Component1 y = new Component1(); + boolean instance = (y.getPeer() instanceof TestDummy); + if (instance){ + } + boolean instance1 = (y.getPeer() != null); + if (instance1){ + } + } + } + """, + """ + package com.test; + + class Test { + void foo() { + Component1 y = new Component1(); + boolean instance = y.isLightweight(); + if (instance){ + } + boolean instance1 = isDisplayable(); + if (instance1){ } } } From 1e26b144311f44d727b6ac5c29741a0a971efac2 Mon Sep 17 00:00:00 2001 From: anuram Date: Tue, 6 Aug 2024 15:20:42 -0400 Subject: [PATCH 09/21] removing extra file commit and test update --- .../org/openrewrite/java/migrate/BeansXmlNamespace.java | 1 - .../java/migrate/ReplaceAWTGetPeerMethodTest.java | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/BeansXmlNamespace.java b/src/main/java/org/openrewrite/java/migrate/BeansXmlNamespace.java index 4d2821a988..ad2956be73 100644 --- a/src/main/java/org/openrewrite/java/migrate/BeansXmlNamespace.java +++ b/src/main/java/org/openrewrite/java/migrate/BeansXmlNamespace.java @@ -23,7 +23,6 @@ import org.openrewrite.xml.XmlVisitor; import org.openrewrite.xml.tree.Xml; -import java.util.List; import java.util.Map; import static java.util.stream.Collectors.toMap; diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index 0617723e4f..8dadbaa24d 100644 --- a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -23,10 +23,10 @@ import static org.openrewrite.java.Assertions.java; -class DetectAWTGetPeerMethodTest implements RewriteTest { +class ReplaceAWTGetPeerMethodTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new DetectAWTGetPeerMethod( + spec.recipe(new ReplaceAWTGetPeerMethod( "com.test.Component1 getPeer()", "com.test.Component1 isDisplayable()", "com.test.TestDummy", @@ -114,10 +114,10 @@ void instanceAndGetPeerMethodNoParenthesis() { class Test { void foo() { Component1 y = new Component1(); - boolean instance = (y.getPeer() instanceof TestDummy); + boolean instance = y.getPeer() instanceof TestDummy; if (instance){ } - boolean instance1 = (y.getPeer() != null); + boolean instance1 = y.getPeer() != null; if (instance1){ } } From 1883f5d3aae3af2163e4d754b89a2650d39e38a7 Mon Sep 17 00:00:00 2001 From: anuram Date: Tue, 6 Aug 2024 15:23:52 -0400 Subject: [PATCH 10/21] adding comments, Test file cleanup --- .../org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java | 3 ++- .../openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index 5544c563c4..a216b9ed16 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -78,7 +78,7 @@ public J.ControlParentheses visitControlParentheses(J.ControlPa if (ifCExp instanceof J.Binary) { J.Binary binaryCondition = (J.Binary) ifCExp; //check if(x.getPeer() != null) - //Not sure if we need to check if (null != getPeer()) + //Not checking (null != getPeer()) if (checkMethodInvocationNotEqualLiteralInBinary(binaryCondition)) { J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); J.Literal lt = (J.Literal) binaryCondition.getRight(); @@ -106,6 +106,7 @@ public J.ControlParentheses visitControlParentheses(J.ControlPa } return cp; } + //Placeholders // @Override // public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, ExecutionContext ctx){ // return md; diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index 8dadbaa24d..f72a6f0eca 100644 --- a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -132,7 +132,7 @@ void foo() { boolean instance = y.isLightweight(); if (instance){ } - boolean instance1 = isDisplayable(); + boolean instance1 = y.isDisplayable(); if (instance1){ } } From c30aefb7d46e7a533e979efa2326971d43df4dca Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 8 Aug 2024 00:19:01 +0200 Subject: [PATCH 11/21] Demonstrate the need for `super.visitControlParentheses(cp, ctx)` --- .../java/migrate/ReplaceAWTGetPeerMethod.java | 20 ++++++++++--------- .../migrate/ReplaceAWTGetPeerMethodTest.java | 17 +++++++--------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index a216b9ed16..2798918389 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -104,21 +104,23 @@ public J.ControlParentheses visitControlParentheses(J.ControlPa } } } - return cp; + return (J.ControlParentheses) super.visitControlParentheses(cp, ctx); } //Placeholders // @Override // public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, ExecutionContext ctx){ // return md; // } -// @Override -// public J.Binary visitBinary(J.Binary binary, ExecutionContext ctx){ -// return binary; -// } -// @Override -// public J.InstanceOf visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx){ -// return instOf; -// } + @Override + public J.Binary visitBinary(J.Binary binary, ExecutionContext ctx){ + //TODO handle binary outside of control parentheses + return (J.Binary) super.visitBinary(binary, ctx); + } + @Override + public J.InstanceOf visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx){ + //TODO handle instanceOf outside of control parentheses + return (J.InstanceOf) super.visitInstanceOf(instOf, ctx); + } }); } diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index f72a6f0eca..295d9feefe 100644 --- a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -59,8 +59,6 @@ public class TestDummy { )); } - - @DocumentExample @Test void instanceAndGetPeerMethodControlParentheses() { rewriteRun( @@ -71,14 +69,14 @@ void instanceAndGetPeerMethodControlParentheses() { class Test extends TestDummy { void foo() { Test t1 = new Test(); - Component1 c = new Component1(); + Component1 c = new Component1(); if (c.getPeer() instanceof com.test.TestDummy) { } if (c.getPeer() instanceof TestDummy) { } Component1 y = new Component1(); if (y.getPeer() != null) { - } + } } } """, @@ -94,7 +92,7 @@ void foo() { } Component1 y = new Component1(); if (y.isDisplayable()) { - } + } } } """ @@ -102,7 +100,6 @@ void foo() { ); } - @DocumentExample @Test void instanceAndGetPeerMethodNoParenthesis() { rewriteRun( @@ -112,8 +109,8 @@ void instanceAndGetPeerMethodNoParenthesis() { package com.test; class Test { - void foo() { - Component1 y = new Component1(); + void foo() { + Component1 y = new Component1(); boolean instance = y.getPeer() instanceof TestDummy; if (instance){ } @@ -127,8 +124,8 @@ void foo() { package com.test; class Test { - void foo() { - Component1 y = new Component1(); + void foo() { + Component1 y = new Component1(); boolean instance = y.isLightweight(); if (instance){ } From d1b97dab22ca4f758e6a172c3c641c1415eda94b Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 8 Aug 2024 00:25:25 +0200 Subject: [PATCH 12/21] Only override `visitBinary` and `visitInstanceOf` --- .../java/migrate/ReplaceAWTGetPeerMethod.java | 79 ++++++++----------- 1 file changed, 32 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index 2798918389..a63d1f7736 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -66,60 +66,45 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesMethod<>(methodPatternGetPeer), new JavaVisitor() { + MethodMatcher methodMatcherGetPeer = new MethodMatcher(methodPatternGetPeer); + return Preconditions.check(new UsesMethod<>(methodMatcherGetPeer), new JavaVisitor() { @Override - public J.ControlParentheses visitControlParentheses(J.ControlParentheses cp, ExecutionContext ctx) { - MethodMatcher methodMatcherGetPeer = new MethodMatcher(methodPatternGetPeer); - Expression ifCExp = (Expression) cp.getTree(); - //Check if there is a Binary or an instanceOf inside the Parentheses - if (!(ifCExp instanceof J.Binary) && !(ifCExp instanceof J.InstanceOf)) { - return cp; - } - if (ifCExp instanceof J.Binary) { - J.Binary binaryCondition = (J.Binary) ifCExp; - //check if(x.getPeer() != null) - //Not checking (null != getPeer()) - if (checkMethodInvocationNotEqualLiteralInBinary(binaryCondition)) { - J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); - J.Literal lt = (J.Literal) binaryCondition.getRight(); - if (methodMatcherGetPeer.matches(mi.getMethodType())) { - assert lt.getValueSource() != null; - if (lt.getValueSource().equals("null")) { - mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isDisplayable", true, null - ).getVisitor().visit(mi, ctx); - mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean").getVisitor().visit(mi, ctx); - return cp.withTree((T) mi); - } - } - } - } else { - J.InstanceOf instanceOfVar = (J.InstanceOf) ifCExp; - if (instanceOfVar.getExpression() instanceof J.MethodInvocation) { - J.MethodInvocation mi = ((J.MethodInvocation) instanceOfVar.getExpression()); - if (methodMatcherGetPeer.matches(mi.getMethodType()) && checkClassNameIsEqualToFQCN(instanceOfVar)) { - mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null + public J visitBinary(J.Binary binary, ExecutionContext ctx){ + J.Binary binaryCondition = (J.Binary) super.visitBinary(binary, ctx); + + //check if(x.getPeer() != null) + //Not checking (null != getPeer()) + if (checkMethodInvocationNotEqualLiteralInBinary(binaryCondition)) { + J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); + J.Literal lt = (J.Literal) binaryCondition.getRight(); + if (methodMatcherGetPeer.matches(mi.getMethodType())) { + assert lt.getValueSource() != null; + if (lt.getValueSource().equals("null")) { + mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isDisplayable", true, null ).getVisitor().visit(mi, ctx); - mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean").getVisitor().visit(mi, ctx); - return cp.withTree((T) mi); + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean").getVisitor().visit(mi, ctx); + return mi.withPrefix(binaryCondition.getPrefix()); } } } - return (J.ControlParentheses) super.visitControlParentheses(cp, ctx); - } - //Placeholders -// @Override -// public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, ExecutionContext ctx){ -// return md; -// } - @Override - public J.Binary visitBinary(J.Binary binary, ExecutionContext ctx){ - //TODO handle binary outside of control parentheses - return (J.Binary) super.visitBinary(binary, ctx); + + return binaryCondition; } @Override - public J.InstanceOf visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx){ - //TODO handle instanceOf outside of control parentheses - return (J.InstanceOf) super.visitInstanceOf(instOf, ctx); + 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.getMethodType()) && checkClassNameIsEqualToFQCN(instanceOfVar)) { + mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null + ).getVisitor().visit(mi, ctx); + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean").getVisitor().visit(mi, ctx); + return mi.withPrefix(instanceOfVar.getPrefix()); + } + } + + return instanceOfVar; } }); } From 676429783cf9fd0f3fa565740eab4132895240ee Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 8 Aug 2024 00:47:22 +0200 Subject: [PATCH 13/21] Update src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java --- .../openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index 295d9feefe..23a635b88c 100644 --- a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -60,6 +60,7 @@ public class TestDummy { } @Test + @DocumentExample void instanceAndGetPeerMethodControlParentheses() { rewriteRun( //language=java From b28d9bb1c156eeec5925e45a015f89474a86d75f Mon Sep 17 00:00:00 2001 From: anuram Date: Thu, 8 Aug 2024 13:14:37 -0400 Subject: [PATCH 14/21] update to code and adding missing arguments to .yml --- .../java/migrate/ReplaceAWTGetPeerMethod.java | 3 ++- .../META-INF/rewrite/java-version-11.yml | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index a63d1f7736..9150f462b4 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -29,7 +29,6 @@ import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; @@ -83,6 +82,7 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx){ mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isDisplayable", true, null ).getVisitor().visit(mi, ctx); mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean").getVisitor().visit(mi, ctx); + assert mi != null; return mi.withPrefix(binaryCondition.getPrefix()); } } @@ -100,6 +100,7 @@ public J visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx){ mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null ).getVisitor().visit(mi, ctx); mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean").getVisitor().visit(mi, ctx); + assert mi != null; return mi.withPrefix(instanceOfVar.getPrefix()); } } diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index 9132781947..d39a25d344 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -282,3 +282,20 @@ recipeList: - org.openrewrite.java.ChangeMethodName: methodPattern: java.nio.file.Path get(..) newMethodName: of +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod +displayName: Replace `getPeer()` method +description: + All methods that refer to types defined in the java.awt.peer package are removed in Java 11. + This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse. + The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` + and the occurrence of `(component.getPeer() instanceof LightweightPeer)` is replaced with `(component.isLightweight())`. +tags: + - java11 +recipeList: + - org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod: + methodPatternGetPeer: java.awt.* getPeer() + methodUpdateIsDisplayable: java.awt.* isDisplayable() + className: java.awt.peer.LightweightPeer + methodUpdateIsLightweight: java.awt.* isLightweight() \ No newline at end of file From 567967892c84367500d01d90a92d4830fbaf1104 Mon Sep 17 00:00:00 2001 From: anuram Date: Thu, 8 Aug 2024 13:50:16 -0400 Subject: [PATCH 15/21] right format for java-version.yml --- .../META-INF/rewrite/java-version-11.yml | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index d39a25d344..f78fd7e6d0 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -70,7 +70,11 @@ recipeList: - org.openrewrite.java.migrate.RemovedPolicy - org.openrewrite.java.migrate.ReferenceCloneMethod - org.openrewrite.java.migrate.ThreadStopDestroy - - org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod + - org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod: + methodPatternGetPeer: java.awt.* getPeer() + methodUpdateIsDisplayable: java.awt.* isDisplayable() + className: java.awt.peer.LightweightPeer + methodUpdateIsLightweight: java.awt.* isLightweight() --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.UpgradeBuildToJava11 @@ -282,20 +286,4 @@ recipeList: - org.openrewrite.java.ChangeMethodName: methodPattern: java.nio.file.Path get(..) newMethodName: of ---- -type: specs.openrewrite.org/v1beta/recipe -name: org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod -displayName: Replace `getPeer()` method -description: - All methods that refer to types defined in the java.awt.peer package are removed in Java 11. - This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse. - The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` - and the occurrence of `(component.getPeer() instanceof LightweightPeer)` is replaced with `(component.isLightweight())`. -tags: - - java11 -recipeList: - - org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod: - methodPatternGetPeer: java.awt.* getPeer() - methodUpdateIsDisplayable: java.awt.* isDisplayable() - className: java.awt.peer.LightweightPeer - methodUpdateIsLightweight: java.awt.* isLightweight() \ No newline at end of file +--- \ No newline at end of file From 13be5af1e3a11dfac53f91d514a2e6a6538dfb17 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 9 Aug 2024 09:49:22 +0200 Subject: [PATCH 16/21] Remove import after instanceof replacement --- .../java/migrate/ReplaceAWTGetPeerMethod.java | 1 + .../META-INF/rewrite/java-version-11.yml | 1 - .../migrate/ReplaceAWTGetPeerMethodTest.java | 22 +++++++++---------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index 9150f462b4..5fed1ef46e 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -101,6 +101,7 @@ public J visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx){ ).getVisitor().visit(mi, ctx); mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean").getVisitor().visit(mi, ctx); assert mi != null; + maybeRemoveImport(className); return mi.withPrefix(instanceOfVar.getPrefix()); } } diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index f78fd7e6d0..3b7d720586 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -286,4 +286,3 @@ recipeList: - org.openrewrite.java.ChangeMethodName: methodPattern: java.nio.file.Path get(..) newMethodName: of ---- \ No newline at end of file diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index 23a635b88c..94e39aa6dc 100644 --- a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -40,9 +40,6 @@ public class Component1 { public String getPeer() { return "x"; } - public boolean getPeer1() { - return true; - } public boolean isDisplayable() { return true; } @@ -56,7 +53,8 @@ public boolean isLightweight() { public class TestDummy { } """ - )); + ) + ); } @Test @@ -66,8 +64,9 @@ void instanceAndGetPeerMethodControlParentheses() { //language=java java( """ - package com.test; - class Test extends TestDummy { + import com.test.Component1; + import com.test.TestDummy; + class Test { void foo() { Test t1 = new Test(); Component1 c = new Component1(); @@ -82,8 +81,8 @@ void foo() { } """, """ - package com.test; - class Test extends TestDummy { + import com.test.Component1; + class Test { void foo() { Test t1 = new Test(); Component1 c = new Component1(); @@ -107,8 +106,8 @@ void instanceAndGetPeerMethodNoParenthesis() { //language=java java( """ - package com.test; - + import com.test.Component1; + import com.test.TestDummy; class Test { void foo() { Component1 y = new Component1(); @@ -122,8 +121,7 @@ void foo() { } """, """ - package com.test; - + import com.test.Component1; class Test { void foo() { Component1 y = new Component1(); From 99801efa3727ae0d3b51cba2c948fe44cf089d9c Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 9 Aug 2024 10:04:24 +0200 Subject: [PATCH 17/21] Also handle flipped case --- .../java/migrate/ReplaceAWTGetPeerMethod.java | 109 +++++++++--------- .../migrate/ReplaceAWTGetPeerMethodTest.java | 4 + 2 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index 5fed1ef46e..94747695ee 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -25,6 +25,7 @@ import org.openrewrite.Preconditions; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.ChangeMethodName; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; @@ -51,16 +52,16 @@ public ReplaceAWTGetPeerMethod() { @Override public String getDisplayName() { - return "Replace `getPeer()` method"; + return "Replace AWT `getPeer()` method"; } @Override public String getDescription() { - return " All methods that refer to types defined in the java.awt.peer package are removed in Java 11. " - + "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasse." - + "The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` " - + "and the occurrence of `(component.getPeer() instanceof LightweightPeer)` " - + "is replaced with `(component.isLightweight())`."; + return "All methods that refer to types defined in the java.awt.peer package are removed in Java 11. " + + "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasses. " + + "The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` " + + "and the occurrence of `(component.getPeer() instanceof LightweightPeer)` " + + "is replaced with `(component.isLightweight())`."; } @Override @@ -68,38 +69,51 @@ public TreeVisitor getVisitor() { MethodMatcher methodMatcherGetPeer = new MethodMatcher(methodPatternGetPeer); return Preconditions.check(new UsesMethod<>(methodMatcherGetPeer), new JavaVisitor() { @Override - public J visitBinary(J.Binary binary, ExecutionContext ctx){ - J.Binary binaryCondition = (J.Binary) super.visitBinary(binary, ctx); + public J visitBinary(J.Binary binary, ExecutionContext ctx) { + J.Binary bi = (J.Binary) super.visitBinary(binary, ctx); - //check if(x.getPeer() != null) - //Not checking (null != getPeer()) - if (checkMethodInvocationNotEqualLiteralInBinary(binaryCondition)) { - J.MethodInvocation mi = (J.MethodInvocation) binaryCondition.getLeft(); - J.Literal lt = (J.Literal) binaryCondition.getRight(); - if (methodMatcherGetPeer.matches(mi.getMethodType())) { - assert lt.getValueSource() != null; - if (lt.getValueSource().equals("null")) { - mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isDisplayable", true, null - ).getVisitor().visit(mi, ctx); - mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean").getVisitor().visit(mi, ctx); - assert mi != null; - return mi.withPrefix(binaryCondition.getPrefix()); - } - } + J.MethodInvocation mi = findMatchingMethodInvocation(bi); + if (mi != null) { + mi = (J.MethodInvocation) new ChangeMethodName( + methodPatternGetPeer, "isDisplayable", true, null) + .getVisitor().visit(mi, ctx); + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean") + .getVisitor().visit(mi, ctx); + assert mi != null; + return mi.withPrefix(bi.getPrefix()); } - return binaryCondition; + 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){ + 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.getMethodType()) && checkClassNameIsEqualToFQCN(instanceOfVar)) { - mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null - ).getVisitor().visit(mi, ctx); - mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean").getVisitor().visit(mi, ctx); + mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null) + .getVisitor().visit(mi, ctx); + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean") + .getVisitor().visit(mi, ctx); assert mi != null; maybeRemoveImport(className); return mi.withPrefix(instanceOfVar.getPrefix()); @@ -108,31 +122,20 @@ public J visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx){ return instanceOfVar; } - }); - } - - private boolean checkClassNameIsEqualToFQCN(J.InstanceOf instOf) { - if (instOf.getClazz() instanceof J.Identifier) { - J.Identifier id = (J.Identifier) instOf.getClazz(); - assert id.getType() != null; - return ((JavaType.Class) id.getType()).getFullyQualifiedName().equals(className); - } else if (instOf.getClazz() instanceof J.FieldAccess) { - J.FieldAccess fid = (J.FieldAccess) instOf.getClazz(); - assert fid.getType() != null; - return ((JavaType.Class) fid.getType()).getFullyQualifiedName().equals(className); - } else { - return false; - } - } - private boolean checkMethodInvocationNotEqualLiteralInBinary(J.Binary binaryCondition) { - if (binaryCondition.getLeft() instanceof J.MethodInvocation) { - return (binaryCondition.getRight() instanceof J.Literal && binaryCondition.getOperator() == J.Binary.Type.NotEqual); - } - if (binaryCondition.getRight() instanceof J.MethodInvocation) { - return (binaryCondition.getLeft() instanceof J.Literal && binaryCondition.getOperator() == J.Binary.Type.NotEqual); - } - return false; + private boolean checkClassNameIsEqualToFQCN(J.InstanceOf instOf) { + if (instOf.getClazz() instanceof J.Identifier) { + J.Identifier id = (J.Identifier) instOf.getClazz(); + assert id.getType() != null; + return ((JavaType.Class) id.getType()).getFullyQualifiedName().equals(className); + } else if (instOf.getClazz() instanceof J.FieldAccess) { + J.FieldAccess fid = (J.FieldAccess) instOf.getClazz(); + assert fid.getType() != null; + return ((JavaType.Class) fid.getType()).getFullyQualifiedName().equals(className); + } else { + return false; + } + } + }); } - } diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index 94e39aa6dc..c76d5ae438 100644 --- a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -77,6 +77,8 @@ void foo() { Component1 y = new Component1(); if (y.getPeer() != null) { } + if (null != y.getPeer()) { + } } } """, @@ -93,6 +95,8 @@ void foo() { Component1 y = new Component1(); if (y.isDisplayable()) { } + if (y.isDisplayable()) { + } } } """ From 694e59299e7efddc7398266a0a1d52b058247a5f Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 9 Aug 2024 10:18:33 +0200 Subject: [PATCH 18/21] Replace `checkClassNameIsEqualToFQCN` with TypeUtils --- .../java/migrate/ReplaceAWTGetPeerMethod.java | 19 +++---------------- .../migrate/ReplaceAWTGetPeerMethodTest.java | 1 + 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index 94747695ee..ecd00b1967 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -31,7 +31,8 @@ import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.TypeUtils; +import org.openrewrite.java.tree.TypedTree; @Value @EqualsAndHashCode(callSuper = false) @@ -109,7 +110,7 @@ public J visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx) { if (instanceOfVar.getExpression() instanceof J.MethodInvocation) { J.MethodInvocation mi = ((J.MethodInvocation) instanceOfVar.getExpression()); - if (methodMatcherGetPeer.matches(mi.getMethodType()) && checkClassNameIsEqualToFQCN(instanceOfVar)) { + if (methodMatcherGetPeer.matches(mi) && TypeUtils.isAssignableTo(className, ((TypedTree) instanceOfVar.getClazz()).getType())) { mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null) .getVisitor().visit(mi, ctx); mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsLightweight, "boolean") @@ -122,20 +123,6 @@ public J visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx) { return instanceOfVar; } - - private boolean checkClassNameIsEqualToFQCN(J.InstanceOf instOf) { - if (instOf.getClazz() instanceof J.Identifier) { - J.Identifier id = (J.Identifier) instOf.getClazz(); - assert id.getType() != null; - return ((JavaType.Class) id.getType()).getFullyQualifiedName().equals(className); - } else if (instOf.getClazz() instanceof J.FieldAccess) { - J.FieldAccess fid = (J.FieldAccess) instOf.getClazz(); - assert fid.getType() != null; - return ((JavaType.Class) fid.getType()).getFullyQualifiedName().equals(className); - } else { - return false; - } - } }); } } diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index c76d5ae438..122a1bdc52 100644 --- a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -23,6 +23,7 @@ import static org.openrewrite.java.Assertions.java; +@SuppressWarnings("StatementWithEmptyBody") class ReplaceAWTGetPeerMethodTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { From e8247db948f44a99512fcbcb0c4dc37b617bd3a6 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 9 Aug 2024 10:50:07 +0200 Subject: [PATCH 19/21] Remove duplication in arguments --- .../java/migrate/ReplaceAWTGetPeerMethod.java | 34 +++++++------------ .../META-INF/rewrite/java-version-11.yml | 6 ++-- .../migrate/ReplaceAWTGetPeerMethodTest.java | 6 +--- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index ecd00b1967..1bb57b1b2d 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -16,9 +16,6 @@ package org.openrewrite.java.migrate; -import com.fasterxml.jackson.annotation.JsonCreator; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Value; import org.openrewrite.ExecutionContext; @@ -36,20 +33,11 @@ @Value @EqualsAndHashCode(callSuper = false) -@AllArgsConstructor(access = AccessLevel.PACKAGE) // For tests class ReplaceAWTGetPeerMethod extends Recipe { - String methodPatternGetPeer; - String methodUpdateIsDisplayable; - String className; - String methodUpdateIsLightweight; - @JsonCreator - public ReplaceAWTGetPeerMethod() { - this.methodPatternGetPeer = "java.awt.* getPeer()"; - this.methodUpdateIsDisplayable = "java.awt.* isDisplayable()"; - this.className = "java.awt.peer.LightweightPeer"; - this.methodUpdateIsLightweight = "java.awt.* isLightweight()"; - } + // Fields configurable for tests + String getPeerMethodPattern; + String lightweightPeerFQCN; @Override public String getDisplayName() { @@ -67,7 +55,7 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - MethodMatcher methodMatcherGetPeer = new MethodMatcher(methodPatternGetPeer); + MethodMatcher methodMatcherGetPeer = new MethodMatcher(getPeerMethodPattern); return Preconditions.check(new UsesMethod<>(methodMatcherGetPeer), new JavaVisitor() { @Override public J visitBinary(J.Binary binary, ExecutionContext ctx) { @@ -76,9 +64,10 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) { J.MethodInvocation mi = findMatchingMethodInvocation(bi); if (mi != null) { mi = (J.MethodInvocation) new ChangeMethodName( - methodPatternGetPeer, "isDisplayable", true, null) + getPeerMethodPattern, "isDisplayable", true, null) .getVisitor().visit(mi, ctx); - mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType(methodUpdateIsDisplayable, "boolean") + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType( + getPeerMethodPattern.split(" ")[0] + " isDisplayable()", "boolean") .getVisitor().visit(mi, ctx); assert mi != null; return mi.withPrefix(bi.getPrefix()); @@ -110,13 +99,14 @@ public J visitInstanceOf(J.InstanceOf instOf, ExecutionContext ctx) { if (instanceOfVar.getExpression() instanceof J.MethodInvocation) { J.MethodInvocation mi = ((J.MethodInvocation) instanceOfVar.getExpression()); - if (methodMatcherGetPeer.matches(mi) && TypeUtils.isAssignableTo(className, ((TypedTree) instanceOfVar.getClazz()).getType())) { - mi = (J.MethodInvocation) new ChangeMethodName(methodPatternGetPeer, "isLightweight", true, null) + 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(methodUpdateIsLightweight, "boolean") + mi = (J.MethodInvocation) new ChangeMethodInvocationReturnType( + getPeerMethodPattern.split(" ")[0] + " isLightweight()", "boolean") .getVisitor().visit(mi, ctx); assert mi != null; - maybeRemoveImport(className); + maybeRemoveImport(lightweightPeerFQCN); return mi.withPrefix(instanceOfVar.getPrefix()); } } diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index 3b7d720586..7f61cda364 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -71,10 +71,8 @@ recipeList: - org.openrewrite.java.migrate.ReferenceCloneMethod - org.openrewrite.java.migrate.ThreadStopDestroy - org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod: - methodPatternGetPeer: java.awt.* getPeer() - methodUpdateIsDisplayable: java.awt.* isDisplayable() - className: java.awt.peer.LightweightPeer - methodUpdateIsLightweight: java.awt.* isLightweight() + getPeerMethodPattern: java.awt.* getPeer() + lightweightPeerFQCN: java.awt.peer.LightweightPeer --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.UpgradeBuildToJava11 diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java index 122a1bdc52..a861795077 100644 --- a/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethodTest.java @@ -27,11 +27,7 @@ class ReplaceAWTGetPeerMethodTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new ReplaceAWTGetPeerMethod( - "com.test.Component1 getPeer()", - "com.test.Component1 isDisplayable()", - "com.test.TestDummy", - "com.test.Component1 isLightweight()")) + spec.recipe(new ReplaceAWTGetPeerMethod("com.test.Component1 getPeer()", "com.test.TestDummy")) .parser(JavaParser.fromJavaVersion() //language=java .dependsOn( From d2cce4f785d66d7490842ee262f081a169a9ceca Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 9 Aug 2024 10:55:34 +0200 Subject: [PATCH 20/21] Document options and shorten description --- .../java/migrate/ReplaceAWTGetPeerMethod.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index 1bb57b1b2d..441d9e37ce 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -18,10 +18,7 @@ import lombok.EqualsAndHashCode; import lombok.Value; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; +import org.openrewrite.*; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.ChangeMethodName; import org.openrewrite.java.JavaVisitor; @@ -35,8 +32,14 @@ @EqualsAndHashCode(callSuper = false) class ReplaceAWTGetPeerMethod extends Recipe { - // Fields configurable for tests + @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 @@ -46,11 +49,9 @@ public String getDisplayName() { @Override public String getDescription() { - return "All methods that refer to types defined in the java.awt.peer package are removed in Java 11. " + - "This recipe replaces the use of getPeer() method in the java.awt.Component, java.awt.Font, and java.awt.MenuComponent classes and direct known subclasses. " + - "The occurrence of `(component.getPeer() != null) { .. }` is replaced with `(component.isDisplayable())` " + - "and the occurrence of `(component.getPeer() instanceof LightweightPeer)` " + - "is replaced with `(component.isLightweight())`."; + return "This recipe replaces the use of `getPeer()` method in `java.awt.*` classes. " + + "`component.getPeer() != nul` is replaced with `component.isDisplayable()` and " + + "`component.getPeer() instanceof LightweightPeer` is replaced with `component.isLightweight()`."; } @Override From e00a481513bac8db655feeff64caecd8a19864bb Mon Sep 17 00:00:00 2001 From: anuram Date: Fri, 9 Aug 2024 09:36:36 -0400 Subject: [PATCH 21/21] fixed small typo in description --- .../org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java index 441d9e37ce..6b4e490452 100644 --- a/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceAWTGetPeerMethod.java @@ -50,7 +50,7 @@ public String getDisplayName() { @Override public String getDescription() { return "This recipe replaces the use of `getPeer()` method in `java.awt.*` classes. " + - "`component.getPeer() != nul` is replaced with `component.isDisplayable()` and " + + "`component.getPeer() != null` is replaced with `component.isDisplayable()` and " + "`component.getPeer() instanceof LightweightPeer` is replaced with `component.isLightweight()`."; }