Skip to content

refactor: OpenRewrite Recipe best practices #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/resources/META-INF/rewrite/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6358,6 +6358,23 @@ examples:
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: org.openrewrite.java.migrate.lombok.AdoptLombokGetterMethodNames
examples:
- description: ''
sources:
- before: |
class A {
int foo = 9;
int giveFoo() { return foo; }
}
after: |
class A {
int foo = 9;
int getFoo() { return foo; }
}
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: org.openrewrite.java.migrate.lombok.LombokBestPractices
examples:
- description: ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,49 @@ public void defaults(RecipeSpec spec) {
.allSources(src -> src.markers(javaVersion(21)));
}

@DocumentExample
@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/459")
void skipInterfaces() {
void happyPath() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
interface I2 extends I1 {}
class C2 implements I1 {}
""",
"""
interface I1 {}
class C2 implements I1 {
public void m1() {
System.out.println("m1");
}}
"""
)
);
}

@Test
void skipAbstractClasses() {
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/459")
void skipInterfaces() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
abstract class AC2 implements I1 {}
interface I2 extends I1 {}
"""
)
);
}

@DocumentExample
@Test
void happyPath() {
void skipAbstractClasses() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
class C2 implements I1 {}
""",
"""
interface I1 {}
class C2 implements I1 {
public void m1() {
System.out.println("m1");
}}
abstract class AC2 implements I1 {}
"""
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,40 +80,6 @@ public static void setComponentMixingCutoutShape(Component c, Shape sh){
);
}

@Test
void replaceComSunAWTUtilitiesClassesIsTranslucencySupported() {
rewriteRun(
//language=java
java(
"""
import com.test.AWTUtilitiesTest;

class Test {
void foo() {
boolean f = AWTUtilitiesTest.isTranslucencySupported1(AWTUtilitiesTest.Translucency.TRANSLUCENT);
boolean j = AWTUtilitiesTest.isTranslucencySupported1(AWTUtilitiesTest.Translucency.PERPIXEL_TRANSPARENT);
boolean k = AWTUtilitiesTest.isTranslucencySupported1(AWTUtilitiesTest.Translucency.PERPIXEL_TRANSLUCENT);
}
}
""",
"""
import java.awt.GraphicsDevice;
import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment;
import java.awt.Window;

class Test {
void foo() {
boolean f = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT);
boolean j = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT);
boolean k = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT);
}
}
"""
)
);
}

@Test
@DocumentExample
void replaceComSunAWTUtilitiesClassesRemaining() {
Expand Down Expand Up @@ -168,4 +134,38 @@ void foo() {
)
);
}

@Test
void replaceComSunAWTUtilitiesClassesIsTranslucencySupported() {
rewriteRun(
//language=java
java(
"""
import com.test.AWTUtilitiesTest;

class Test {
void foo() {
boolean f = AWTUtilitiesTest.isTranslucencySupported1(AWTUtilitiesTest.Translucency.TRANSLUCENT);
boolean j = AWTUtilitiesTest.isTranslucencySupported1(AWTUtilitiesTest.Translucency.PERPIXEL_TRANSPARENT);
boolean k = AWTUtilitiesTest.isTranslucencySupported1(AWTUtilitiesTest.Translucency.PERPIXEL_TRANSLUCENT);
}
}
""",
"""
import java.awt.GraphicsDevice;
import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment;
import java.awt.Window;

class Test {
void foo() {
boolean f = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT);
boolean j = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT);
boolean k = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ public void defaults(RecipeSpec spec) {
.parser(JavaParser.fromJavaVersion().classpath("guava"));
}

@Test
@DocumentExample
void replaceArguments() {
//language=java
rewriteRun(
version(
java(
"""
import java.util.Set;
import com.google.common.collect.ImmutableSet;

class Test {
Set<String> m = ImmutableSet.of("A", "B", "C", "D");
}
""",
"""
import java.util.Set;

class Test {
Set<String> m = Set.of("A", "B", "C", "D");
}
"""
),
9
)
);
}

@Test
void doNotChangeReturnsImmutableSet() {
//language=java
Expand Down Expand Up @@ -157,34 +185,6 @@ void method() {
);
}

@Test
@DocumentExample
void replaceArguments() {
//language=java
rewriteRun(
version(
java(
"""
import java.util.Set;
import com.google.common.collect.ImmutableSet;

class Test {
Set<String> m = ImmutableSet.of("A", "B", "C", "D");
}
""",
"""
import java.util.Set;

class Test {
Set<String> m = Set.of("A", "B", "C", "D");
}
"""
),
9
)
);
}

@Test
void fieldAssignmentToSet() {
//language=java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,6 @@ public void defaults(RecipeSpec spec) {
);
}

@Test
void doNotAddImportWhenNoChangesWereMade() {
rewriteRun(
java("public class B {}")
);
}

@DocumentExample
@Test
void changeImport() {
Expand All @@ -97,6 +90,13 @@ public class B {
);
}

@Test
void doNotAddImportWhenNoChangesWereMade() {
rewriteRun(
java("public class B {}")
);
}

@Test
void fullyQualifiedName() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ public void defaults(RecipeSpec spec) {
);
}

@Test
void doNotChangeImportWhenPackageFromJavaSE() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javax_transaction_xa)),
//language=java
java(
"""
import javax.transaction.xa.*;
public class A {
XAResource xa;
}
"""
)
);
}

@DocumentExample
@Test
void changeImportWhenPackageFromJakartaTransaction() {
Expand All @@ -100,4 +84,20 @@ public void foo() {}
)
);
}

@Test
void doNotChangeImportWhenPackageFromJavaSE() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javax_transaction_xa)),
//language=java
java(
"""
import javax.transaction.xa.*;
public class A {
XAResource xa;
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,27 @@ void migrateSun() {
);
}

@DocumentExample
@Test
void migrateJCP() {
void migrateWithSQLDataSource() {
rewriteRun(
//language=xml
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_0.xsd"
version="2.0">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<resource-ref>
<res-ref-name>myDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>CONTAINER</res-auth>
</resource-ref>
</web-fragment>
""",
"""
Expand All @@ -93,34 +99,33 @@ void migrateJCP() {
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<resource-ref>
<res-ref-name>myDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>CONTAINER</res-auth>
</resource-ref>
</web-fragment>
""",
sourceSpecs -> sourceSpecs.path("web.xml")
)
);
}

@DocumentExample
@Test
void migrateWithSQLDataSource() {
void migrateJCP() {
rewriteRun(
//language=xml
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_0.xsd"
version="2.0">
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<resource-ref>
<res-ref-name>myDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>CONTAINER</res-auth>
</resource-ref>
</web-fragment>
""",
"""
Expand All @@ -133,11 +138,6 @@ void migrateWithSQLDataSource() {
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<resource-ref>
<res-ref-name>myDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>CONTAINER</res-auth>
</resource-ref>
</web-fragment>
""",
sourceSpecs -> sourceSpecs.path("web.xml")
Expand Down
Loading
Loading