Skip to content

Commit

Permalink
Apply formatter & order imports
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Aug 6, 2024
1 parent 3d1de0a commit deb96c3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@
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;
import org.openrewrite.TreeVisitor;
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";
Expand All @@ -39,27 +49,20 @@ 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()";
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<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(methodPatternGetPeer), new JavaVisitor<ExecutionContext>() {
Expand All @@ -79,14 +82,13 @@ public <T extends J> J.ControlParentheses<T> 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
Expand All @@ -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;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/META-INF/rewrite/java-version-11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,3 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: java.nio.file.Path get(..)
newMethodName: of

Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
}
}
}
"""
)
Expand Down

0 comments on commit deb96c3

Please sign in to comment.