Skip to content

Commit c69eb61

Browse files
OpenRewrite recipe best practices (#1074)
* OpenRewrite recipe best practices Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices?organizationId=QUxML01vZGVybmUvTW9kZXJuZSArIE9wZW5SZXdyaXRl Co-authored-by: Moderne <team@moderne.io> * Use Java 25 toolchain Allows the unnamed variable `_` produced by the OpenRewrite best-practices recipe to compile without preview flags. * Revert "Use Java 25 toolchain" This reverts commit 16e93cc. * Avoid unnamed lambda parameter in UpgradeFacesOpenSourceLibrariesTest The recipe rewrote `(a, b) -> b` to `(_, b) -> b`, but the build's Java 21 toolchain rejects unnamed variables (a Java 22 feature) without --enable-preview. Restore the named parameter so compileTestJava succeeds on Java 21. --------- Co-authored-by: Moderne <team@moderne.io>
1 parent a1f85d2 commit c69eb61

12 files changed

Lines changed: 97 additions & 122 deletions

src/test/java/org/openrewrite/java/migrate/AddSurefireFailsafeArgLineTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525

2626
class AddSurefireFailsafeArgLineTest implements RewriteTest {
2727

28-
private static final String ARG_LINE = "--add-opens java.base/java.lang=ALL-UNNAMED" +
29-
" --add-opens java.base/java.util=ALL-UNNAMED" +
30-
" --add-opens java.base/java.lang.reflect=ALL-UNNAMED";
28+
private static final String ARG_LINE = """
29+
--add-opens java.base/java.lang=ALL-UNNAMED\
30+
--add-opens java.base/java.util=ALL-UNNAMED\
31+
--add-opens java.base/java.lang.reflect=ALL-UNNAMED""";
3132

3233
@Override
3334
public void defaults(RecipeSpec spec) {

src/test/java/org/openrewrite/java/migrate/UpgradeToJava17Test.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.openrewrite.test.RecipeSpec;
2222
import org.openrewrite.test.RewriteTest;
2323

24-
import java.util.Optional;
2524
import java.util.regex.Pattern;
2625

2726
import static org.assertj.core.api.Assertions.assertThat;
@@ -150,7 +149,7 @@ public String test() {
150149
}
151150
""",
152151
spec -> spec.afterRecipe(cu -> {
153-
Optional<JavaVersion> jv = cu.getMarkers().findFirst(JavaVersion.class);
152+
var jv = cu.getMarkers().findFirst(JavaVersion.class);
154153
assertThat(jv)
155154
.map(JavaVersion::getSourceCompatibility)
156155
.hasValue("17");

src/test/java/org/openrewrite/java/migrate/jakarta/JavaxAnnotationMigrationToJakartaAnnotationTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ class A {
130130
""",
131131
spec -> spec.after(pom -> assertThat(pom)
132132
.containsPattern(
133-
"<groupId>jakarta\\.annotation</groupId>\\s*" +
134-
"<artifactId>jakarta\\.annotation-api</artifactId>\\s*" +
135-
"<version>2\\.0\\.\\d+</version>")
133+
"""
134+
<groupId>jakarta\\.annotation</groupId>\\s*\
135+
<artifactId>jakarta\\.annotation-api</artifactId>\\s*\
136+
<version>2\\.0\\.\\d+</version>""")
136137
.actual())
137138
)
138139
)

src/test/java/org/openrewrite/java/migrate/jakarta/JavaxInjectToJakartaInjectTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ public class TestApplication {
112112
spec -> spec.after(pom -> {
113113
return assertThat( pom )
114114
.containsPattern(
115-
"<groupId>jakarta\\.inject</groupId>\\s*" +
116-
"<artifactId>jakarta\\.inject-api</artifactId>\\s*" +
117-
"<version>2\\.0\\.\\d+</version>" ).actual();
115+
"""
116+
<groupId>jakarta\\.inject</groupId>\\s*\
117+
<artifactId>jakarta\\.inject-api</artifactId>\\s*\
118+
<version>2\\.0\\.\\d+</version>""" ).actual();
118119
})
119120
)
120121
)

src/test/java/org/openrewrite/java/migrate/jakarta/JavaxJspToJakartaJspTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ void switchesJavaxJspApiDependencyToJakartaJspApiDependency() {
6565
return assertThat( pom )
6666
.doesNotContain( "javax.servlet.jsp" )
6767
.containsPattern(
68-
"<groupId>jakarta\\.servlet\\.jsp</groupId>\\s*" +
69-
"<artifactId>jakarta\\.servlet\\.jsp-api</artifactId>\\s*" +
70-
"<version>3\\.0\\.\\d+</version>" ).actual();
68+
"""
69+
<groupId>jakarta\\.servlet\\.jsp</groupId>\\s*\
70+
<artifactId>jakarta\\.servlet\\.jsp-api</artifactId>\\s*\
71+
<version>3\\.0\\.\\d+</version>""" ).actual();
7172
})
7273
)
7374
),
@@ -129,9 +130,10 @@ public class TestApplication {
129130
spec -> spec.after(pom -> {
130131
return assertThat( pom )
131132
.containsPattern(
132-
"<groupId>jakarta\\.servlet\\.jsp</groupId>\\s*" +
133-
"<artifactId>jakarta\\.servlet\\.jsp-api</artifactId>\\s*" +
134-
"<version>3\\.0\\.\\d+</version>" ).actual();
133+
"""
134+
<groupId>jakarta\\.servlet\\.jsp</groupId>\\s*\
135+
<artifactId>jakarta\\.servlet\\.jsp-api</artifactId>\\s*\
136+
<version>3\\.0\\.\\d+</version>""" ).actual();
135137
})
136138
)
137139
)
@@ -163,9 +165,10 @@ void upgradesJakartaJspApiDependencyIfAlreadyExistingAtLowerVersion() {
163165
spec -> spec.after(pom -> {
164166
return assertThat( pom )
165167
.containsPattern(
166-
"<groupId>jakarta\\.servlet\\.jsp</groupId>\\s*" +
167-
"<artifactId>jakarta\\.servlet\\.jsp-api</artifactId>\\s*" +
168-
"<version>3\\.0\\.\\d+</version>" ).actual();
168+
"""
169+
<groupId>jakarta\\.servlet\\.jsp</groupId>\\s*\
170+
<artifactId>jakarta\\.servlet\\.jsp-api</artifactId>\\s*\
171+
<version>3\\.0\\.\\d+</version>""" ).actual();
169172
})
170173
)
171174
)

src/test/java/org/openrewrite/java/migrate/jakarta/JavaxPersistenceToJakartaPersistenceTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ void switchesJavaxPersistenceApiDependencyToJakartaPersistenceApiDependency() {
6363
return assertThat( pom )
6464
.doesNotContain( "javax.persistence" )
6565
.containsPattern(
66-
"<groupId>jakarta\\.persistence</groupId>\\s*" +
67-
"<artifactId>jakarta\\.persistence-api</artifactId>\\s*" +
68-
"<version>3\\.0\\.\\d+</version>" ).actual();
66+
"""
67+
<groupId>jakarta\\.persistence</groupId>\\s*\
68+
<artifactId>jakarta\\.persistence-api</artifactId>\\s*\
69+
<version>3\\.0\\.\\d+</version>""" ).actual();
6970
})
7071
)
7172
),
@@ -127,9 +128,10 @@ public class TestApplication {
127128
spec -> spec.after(pom -> {
128129
return assertThat( pom )
129130
.containsPattern(
130-
"<groupId>jakarta\\.persistence</groupId>\\s*" +
131-
"<artifactId>jakarta\\.persistence-api</artifactId>\\s*" +
132-
"<version>3\\.0\\.\\d+</version>" ).actual();
131+
"""
132+
<groupId>jakarta\\.persistence</groupId>\\s*\
133+
<artifactId>jakarta\\.persistence-api</artifactId>\\s*\
134+
<version>3\\.0\\.\\d+</version>""" ).actual();
133135
})
134136
)
135137
)

src/test/java/org/openrewrite/java/migrate/jakarta/JavaxTransactionMigrationToJakartaTransactionTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ public class TestApplication {
150150
spec -> spec.after(pom -> {
151151
return assertThat( pom )
152152
.containsPattern(
153-
"<groupId>jakarta\\.transaction</groupId>\\s*" +
154-
"<artifactId>jakarta\\.transaction-api</artifactId>\\s*" +
155-
"<version>2\\.0\\.\\d+</version>" ).actual();
153+
"""
154+
<groupId>jakarta\\.transaction</groupId>\\s*\
155+
<artifactId>jakarta\\.transaction-api</artifactId>\\s*\
156+
<version>2\\.0\\.\\d+</version>""" ).actual();
156157
})
157158
)
158159
)

src/test/java/org/openrewrite/java/migrate/jakarta/JavaxValidationMigrationToJakartaValidationTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ public class TestApplication {
201201
spec -> spec.after(pom -> {
202202
return assertThat( pom )
203203
.containsPattern(
204-
"<groupId>jakarta\\.validation</groupId>\\s*" +
205-
"<artifactId>jakarta\\.validation-api</artifactId>\\s*" +
206-
"<version>3\\.0\\.\\d+</version>" ).actual();
204+
"""
205+
<groupId>jakarta\\.validation</groupId>\\s*\
206+
<artifactId>jakarta\\.validation-api</artifactId>\\s*\
207+
<version>3\\.0\\.\\d+</version>""" ).actual();
207208
})
208209
)
209210
)

src/test/java/org/openrewrite/java/migrate/lang/RenameUnderscoreIdentifierTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828

2929
class RenameUnderscoreIdentifierTest implements RewriteTest {
3030

31-
/**
32-
* Setup recipe that renames {@code UNDERSCORE} to {@code _} in the LST,
33-
* simulating Java 8 source code that uses {@code _} as an identifier.
34-
* This is necessary because the Java 9+ parser cannot parse {@code _} as a regular identifier.
35-
*/
31+
/// Setup recipe that renames `UNDERSCORE` to `_` in the LST,
32+
/// simulating Java 8 source code that uses `_` as an identifier.
33+
/// This is necessary because the Java 9+ parser cannot parse `_` as a regular identifier.
3634
private static Recipe renameToUnderscore() {
3735
return new Recipe() {
3836
@Override

src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import org.openrewrite.test.RecipeSpec;
2626
import org.openrewrite.test.RewriteTest;
2727

28+
import java.util.List;
29+
import java.util.Set;
2830
import java.util.function.Consumer;
2931
import java.util.function.UnaryOperator;
3032

31-
import static java.util.Collections.emptySet;
32-
import static java.util.Collections.singletonList;
3333
import static org.assertj.core.api.Assertions.assertThat;
3434
import static org.openrewrite.Tree.randomId;
3535
import static org.openrewrite.java.Assertions.java;
@@ -489,10 +489,10 @@ class Test {
489489
}
490490

491491
private static Consumer<RecipeSpec> tabsAndIndents(UnaryOperator<TabsAndIndentsStyle> with, int tabSize) {
492-
return spec -> spec.parser(JavaParser.fromJavaVersion().styles(singletonList(
492+
return spec -> spec.parser(JavaParser.fromJavaVersion().styles(List.of(
493493
new NamedStyles(
494-
randomId(), "TabsOnlyFile", "TabsOnlyFile", "tabSize is x", emptySet(),
495-
singletonList(with.apply(buildTabsAndIndents(tabSize)))
494+
randomId(), "TabsOnlyFile", "TabsOnlyFile", "tabSize is x", Set.of(),
495+
List.of(with.apply(buildTabsAndIndents(tabSize)))
496496
)
497497
)));
498498
}
@@ -648,11 +648,9 @@ class Test {
648648
);
649649
}
650650

651-
/**
652-
* Single escaping a quote in a string literal provides: " -> \"
653-
* <p>
654-
* On converting this to a text block, we can let go of the escaping for the double quote: \" -> "
655-
*/
651+
/// Single escaping a quote in a string literal provides: " -> \"
652+
///
653+
/// On converting this to a text block, we can let go of the escaping for the double quote: \" -> "
656654
@Test
657655
void singleEscapedQuote() {
658656
rewriteRun(
@@ -688,12 +686,10 @@ class Test {
688686
);
689687
}
690688

691-
/**
692-
* Double escaping a quote in a string literal provides: " -> \" -> \\\"
693-
* <p>
694-
* On converting this to a text block, the escaped backslash should remain, but we can let go of the
695-
* escaping for the double quote: \\\" -> \\"
696-
*/
689+
/// Double escaping a quote in a string literal provides: " -> \" -> \\\"
690+
///
691+
/// On converting this to a text block, the escaped backslash should remain, but we can let go of the
692+
/// escaping for the double quote: \\\" -> \\"
697693
@Test
698694
void doubleEscapedQuote() {
699695
rewriteRun(
@@ -729,11 +725,9 @@ class Test {
729725
);
730726
}
731727

732-
/**
733-
* Triple quotes in a string literal are escaped as: """ -> \"\"\"
734-
* <p>
735-
* On converting this to a text block, only one of the quotes needs to be escaped: \"\"\" -> ""\"
736-
*/
728+
/// Triple quotes in a string literal are escaped as: """ -> \"\"\"
729+
///
730+
/// On converting this to a text block, only one of the quotes needs to be escaped: \"\"\" -> ""\"
737731
@Test
738732
void tripleQuotes() {
739733
rewriteRun(
@@ -795,21 +789,19 @@ class Test {
795789
);
796790
}
797791

798-
/**
799-
* Quote in a string literal is escaped as: " -> \"
800-
* <p>
801-
* On converting this to a text block, it needs to be escaped if it is the last character:
802-
* <pre>
803-
* """
804-
* "test\"" -> test\""""
805-
* </pre>
806-
* However, this is not required in case we use the newline escape (\) to put the ending delimiter on the next line:
807-
* <pre>
808-
* """
809-
* "test\"" -> test"\
810-
* """
811-
* </pre>
812-
*/
792+
/// Quote in a string literal is escaped as: " -> \"
793+
///
794+
/// On converting this to a text block, it needs to be escaped if it is the last character:
795+
/// ```
796+
/// """
797+
/// "test\"" -> test\""""
798+
/// ```
799+
/// However, this is not required in case we use the newline escape (\) to put the ending delimiter on the next line:
800+
/// ```
801+
/// """
802+
/// "test\"" -> test"\
803+
/// """
804+
/// ```
813805
@Test
814806
void endingQuote() {
815807
rewriteRun(

0 commit comments

Comments
 (0)