Skip to content

Commit bc4e6ce

Browse files
authored
Use a transient field for seen in FindJavaVersion (#533)
Same as in sibling recipe `AboutJavaVersion`
1 parent b53a02d commit bc4e6ce

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
import org.openrewrite.java.tree.J;
2727

2828
import java.util.HashSet;
29-
import java.util.Optional;
3029
import java.util.Set;
3130

3231
@Value
3332
@EqualsAndHashCode(callSuper = false)
3433
public class FindJavaVersion extends Recipe {
3534

3635
transient JavaVersionTable table = new JavaVersionTable(this);
37-
private static Set<JavaVersion> seen = new HashSet<>();
36+
transient Set<JavaVersion> seen = new HashSet<>();
37+
3838
@Override
3939
public String getDisplayName() {
4040
return "Find Java versions in use";
@@ -50,15 +50,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5050
return new JavaVisitor<ExecutionContext>() {
5151
@Override
5252
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
53-
Optional<JavaVersion> maybeJv = cu.getMarkers().findFirst(JavaVersion.class);
54-
if(!maybeJv.isPresent()) {
55-
return cu;
56-
}
57-
JavaVersion jv = maybeJv.get();
58-
if(!seen.add(jv)) {
59-
return cu;
60-
}
61-
table.insertRow(ctx, new JavaVersionTable.Row(jv.getSourceCompatibility(), jv.getTargetCompatibility()));
53+
cu.getMarkers().findFirst(JavaVersion.class)
54+
.filter(seen::add)
55+
.map(jv -> new JavaVersionTable.Row(jv.getSourceCompatibility(), jv.getTargetCompatibility()))
56+
.ifPresent(row -> table.insertRow(ctx, row));
6257
return cu;
6358
}
6459
};

src/test/java/org/openrewrite/java/migrate/search/FindJavaVersionTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.migrate.search;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
1920
import org.openrewrite.java.marker.JavaVersion;
2021
import org.openrewrite.java.migrate.table.JavaVersionTable;
2122
import org.openrewrite.test.RecipeSpec;
@@ -33,7 +34,8 @@ public void defaults(RecipeSpec spec) {
3334
}
3435

3536
@Test
36-
void test() {
37+
@DocumentExample
38+
void twoClassesWithSameMarkerLeadToOneRow() {
3739
JavaVersion jv = new JavaVersion(randomId(), "Sam", "Shelter", "17", "8");
3840
rewriteRun(
3941
spec -> spec.dataTable(JavaVersionTable.Row.class, rows -> {

0 commit comments

Comments
 (0)