diff --git a/rewrite-core/src/main/java/org/openrewrite/Preconditions.java b/rewrite-core/src/main/java/org/openrewrite/Preconditions.java index 9e0b32e4a7a..c54e6e50c87 100644 --- a/rewrite-core/src/main/java/org/openrewrite/Preconditions.java +++ b/rewrite-core/src/main/java/org/openrewrite/Preconditions.java @@ -18,9 +18,7 @@ import org.openrewrite.internal.lang.Nullable; import org.openrewrite.marker.SearchResult; -import java.util.Arrays; import java.util.function.Supplier; -import java.util.stream.Collectors; public class Preconditions { @@ -28,34 +26,11 @@ public static TreeVisitor check(Recipe check, TreeVisitor check(TreeVisitor check, TreeVisitor v) { - return new TreeVisitor() { - @Override - public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { - return check.isAcceptable(sourceFile, ctx) && v.isAcceptable(sourceFile, ctx); - } - - @Override - public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { - // if tree isn't an instanceof SourceFile, then a precondition visitor may - // not be able to do its work because it may assume we are starting from the root level - return !(tree instanceof SourceFile) || check.visit(tree, ctx) != tree ? - v.visit(tree, ctx) : - tree; - } - - @Override - public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx, Cursor parent) { - // if tree isn't an instanceof SourceFile, then a precondition visitor may - // not be able to do its work because it may assume we are starting from the root level - return !(tree instanceof SourceFile) || check.visit(tree, ctx, parent) != tree ? - v.visit(tree, ctx, parent) : - tree; - } - }; + return new Check(check, v); } public static TreeVisitor check(boolean check, TreeVisitor v) { @@ -139,14 +114,63 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { } }; } + @SafeVarargs public static Supplier> and(Supplier>... svs) { - return new Supplier>() { - @Override - public TreeVisitor get() { - TreeVisitor[] visitors = Arrays.stream(svs).map(Supplier::get).collect(Collectors.toList()).toArray(new TreeVisitor[]{}); - return and(visitors); + return () -> { + //noinspection unchecked + TreeVisitor[] visitors = new TreeVisitor[svs.length]; + for (int i = 0; i < svs.length; i++) { + Supplier> sv = svs[i]; + visitors[i] = sv.get(); } + return and(visitors); }; } + + public static class RecipeCheck extends Check { + private final Recipe check; + + public RecipeCheck(Recipe check, TreeVisitor v) { + super(check.getVisitor(), v); + this.check = check; + } + + public Recipe getCheck() { + return check; + } + } + + public static class Check extends TreeVisitor { + private final TreeVisitor check; + private final TreeVisitor v; + + public Check(TreeVisitor check, TreeVisitor v) { + this.check = check; + this.v = v; + } + + @Override + public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { + return check.isAcceptable(sourceFile, ctx) && v.isAcceptable(sourceFile, ctx); + } + + @Override + public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { + // if tree isn't an instanceof SourceFile, then a precondition visitor may + // not be able to do its work because it may assume we are starting from the root level + return !(tree instanceof SourceFile) || check.visit(tree, ctx) != tree ? + v.visit(tree, ctx) : + tree; + } + + @Override + public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx, Cursor parent) { + // if tree isn't an instanceof SourceFile, then a precondition visitor may + // not be able to do its work because it may assume we are starting from the root level + return !(tree instanceof SourceFile) || check.visit(tree, ctx, parent) != tree ? + v.visit(tree, ctx, parent) : + tree; + } + } }