Skip to content

Commit

Permalink
Use a transient field for seen in FindJavaVersion (#533)
Browse files Browse the repository at this point in the history
Same as in sibling recipe `AboutJavaVersion`
  • Loading branch information
timtebeek authored Aug 14, 2024
1 parent b53a02d commit bc4e6ce
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
import org.openrewrite.java.tree.J;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

@Value
@EqualsAndHashCode(callSuper = false)
public class FindJavaVersion extends Recipe {

transient JavaVersionTable table = new JavaVersionTable(this);
private static Set<JavaVersion> seen = new HashSet<>();
transient Set<JavaVersion> seen = new HashSet<>();

@Override
public String getDisplayName() {
return "Find Java versions in use";
Expand All @@ -50,15 +50,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
@Override
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
Optional<JavaVersion> maybeJv = cu.getMarkers().findFirst(JavaVersion.class);
if(!maybeJv.isPresent()) {
return cu;
}
JavaVersion jv = maybeJv.get();
if(!seen.add(jv)) {
return cu;
}
table.insertRow(ctx, new JavaVersionTable.Row(jv.getSourceCompatibility(), jv.getTargetCompatibility()));
cu.getMarkers().findFirst(JavaVersion.class)
.filter(seen::add)
.map(jv -> new JavaVersionTable.Row(jv.getSourceCompatibility(), jv.getTargetCompatibility()))
.ifPresent(row -> table.insertRow(ctx, row));
return cu;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java.migrate.search;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.marker.JavaVersion;
import org.openrewrite.java.migrate.table.JavaVersionTable;
import org.openrewrite.test.RecipeSpec;
Expand All @@ -33,7 +34,8 @@ public void defaults(RecipeSpec spec) {
}

@Test
void test() {
@DocumentExample
void twoClassesWithSameMarkerLeadToOneRow() {
JavaVersion jv = new JavaVersion(randomId(), "Sam", "Shelter", "17", "8");
rewriteRun(
spec -> spec.dataTable(JavaVersionTable.Row.class, rows -> {
Expand Down

0 comments on commit bc4e6ce

Please sign in to comment.