Skip to content

Commit e911312

Browse files
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>
1 parent 83f0156 commit e911312

2 files changed

Lines changed: 34 additions & 38 deletions

File tree

src/main/java/org/openrewrite/java/migrate/search/ModuleHasKotlinSource.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,11 @@ public class ModuleHasKotlinSource extends ScanningRecipe<Set<JavaProject>> {
4141
@Nullable
4242
Boolean invertMarking;
4343

44-
@Override
45-
public String getDisplayName() {
46-
return "Module has Kotlin source files";
47-
}
44+
String displayName = "Module has Kotlin source files";
4845

49-
@Override
50-
public String getDescription() {
51-
return "Marks all files in modules that contain at least one Kotlin source file (`.kt`). " +
52-
"Intended as a precondition to scope recipes to projects that actually compile Kotlin, " +
53-
"as opposed to projects that merely pick up `kotlin-stdlib` transitively.";
54-
}
46+
String description = "Marks all files in modules that contain at least one Kotlin source file (`.kt`). " +
47+
"Intended as a precondition to scope recipes to projects that actually compile Kotlin, " +
48+
"as opposed to projects that merely pick up `kotlin-stdlib` transitively.";
5549

5650
@Override
5751
public Set<JavaProject> getInitialValue(ExecutionContext ctx) {

src/test/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonMapTest.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.migrate.util;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
1920
import org.openrewrite.Issue;
2021
import org.openrewrite.test.RecipeSpec;
2122
import org.openrewrite.test.RewriteTest;
@@ -31,9 +32,9 @@ public void defaults(RecipeSpec spec) {
3132
spec.recipe(new MigrateCollectionsSingletonMap());
3233
}
3334

34-
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
35+
@DocumentExample
3536
@Test
36-
void singletonMap() {
37+
void singletonMapAsArgument() {
3738
//language=java
3839
rewriteRun(
3940
version(
@@ -42,14 +43,20 @@ void singletonMap() {
4243
import java.util.*;
4344
4445
class Test {
45-
Map<String,String> set = Collections.singletonMap("hello", "world");
46+
void take(Map<String, Object> m) {}
47+
void call(String key, Object value) {
48+
take(Collections.singletonMap(key, value));
49+
}
4650
}
4751
""",
4852
"""
4953
import java.util.Map;
5054
5155
class Test {
52-
Map<String,String> set = Map.of("hello", "world");
56+
void take(Map<String, Object> m) {}
57+
void call(String key, Object value) {
58+
take(Map.of(key, value));
59+
}
5360
}
5461
"""
5562
),
@@ -60,25 +67,23 @@ class Test {
6067

6168
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
6269
@Test
63-
void singletonMapCustomType() {
70+
void singletonMap() {
6471
//language=java
6572
rewriteRun(
6673
version(
6774
java(
6875
"""
6976
import java.util.*;
70-
import java.time.LocalDate;
7177
7278
class Test {
73-
Map<String,LocalDate> map = Collections.singletonMap("date", LocalDate.now());
79+
Map<String,String> set = Collections.singletonMap("hello", "world");
7480
}
7581
""",
7682
"""
7783
import java.util.Map;
78-
import java.time.LocalDate;
7984
8085
class Test {
81-
Map<String,LocalDate> map = Map.of("date", LocalDate.now());
86+
Map<String,String> set = Map.of("hello", "world");
8287
}
8388
"""
8489
),
@@ -87,19 +92,27 @@ class Test {
8792
);
8893
}
8994

90-
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/571")
95+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
9196
@Test
92-
void shouldNotConvertLiteralNull() {
97+
void singletonMapCustomType() {
9398
//language=java
9499
rewriteRun(
95100
version(
96101
java(
97102
"""
98103
import java.util.*;
104+
import java.time.LocalDate;
99105
100106
class Test {
101-
Map<String, String> mapWithNullKey = Collections.singletonMap(null, "foo");
102-
Map<String, String> mapWithNullValue = Collections.singletonMap("bar", null);
107+
Map<String,LocalDate> map = Collections.singletonMap("date", LocalDate.now());
108+
}
109+
""",
110+
"""
111+
import java.util.Map;
112+
import java.time.LocalDate;
113+
114+
class Test {
115+
Map<String,LocalDate> map = Map.of("date", LocalDate.now());
103116
}
104117
"""
105118
),
@@ -108,8 +121,9 @@ class Test {
108121
);
109122
}
110123

124+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/571")
111125
@Test
112-
void singletonMapAsArgument() {
126+
void shouldNotConvertLiteralNull() {
113127
//language=java
114128
rewriteRun(
115129
version(
@@ -118,20 +132,8 @@ void singletonMapAsArgument() {
118132
import java.util.*;
119133
120134
class Test {
121-
void take(Map<String, Object> m) {}
122-
void call(String key, Object value) {
123-
take(Collections.singletonMap(key, value));
124-
}
125-
}
126-
""",
127-
"""
128-
import java.util.Map;
129-
130-
class Test {
131-
void take(Map<String, Object> m) {}
132-
void call(String key, Object value) {
133-
take(Map.of(key, value));
134-
}
135+
Map<String, String> mapWithNullKey = Collections.singletonMap(null, "foo");
136+
Map<String, String> mapWithNullValue = Collections.singletonMap("bar", null);
135137
}
136138
"""
137139
),

0 commit comments

Comments
 (0)