All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openrewrite.Preconditions Maven / Gradle / Ivy

There is a newer version: 8.40.2
Show newest version
/*
 * Copyright 2022 the original author or authors.
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* https://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.openrewrite; import lombok.Getter; import org.jspecify.annotations.Nullable; import org.openrewrite.marker.SearchResult; import java.util.function.Supplier; public class Preconditions { public static TreeVisitor check(Recipe check, TreeVisitor v) { if (check instanceof ScanningRecipe) { throw new IllegalArgumentException("ScanningRecipe is not supported as a check"); } return new RecipeCheck(check, v); } public static TreeVisitor check(TreeVisitor check, TreeVisitor v) { return new Check(check, v); } public static TreeVisitor check(boolean check, TreeVisitor v) { return check ? v : TreeVisitor.noop(); } @Incubating(since = "8.0.0") @SafeVarargs public static TreeVisitor firstAcceptable(TreeVisitor... vs) { return new TreeVisitor() { @Override public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof SourceFile) { for (TreeVisitor v : vs) { if (v.isAcceptable((SourceFile) tree, ctx)) { return v.visit(tree, ctx); } } } return tree; } }; } public static TreeVisitor not(TreeVisitor v) { return new TreeVisitor() { @Override public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { SourceFile sourceFile = tree instanceof SourceFile ? (SourceFile) tree : null; // calling `isAcceptable()` in case `v` overrides `visit(Tree, P)` if (sourceFile != null && !v.isAcceptable(sourceFile, ctx)) { return SearchResult.found(tree); } Tree t2 = v.visit(tree, ctx); return tree == t2 && tree != null ? SearchResult.found(tree) : tree; } }; } @SafeVarargs public static TreeVisitor or(TreeVisitor... vs) { return new TreeVisitor() { @Override public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { SourceFile sourceFile = tree instanceof SourceFile ? (SourceFile) tree : null; for (TreeVisitor v : vs) { // calling `isAcceptable()` in case `v` overrides `visit(Tree, P)` if (sourceFile != null && !v.isAcceptable(sourceFile, ctx)) { continue; } Tree t2 = v.visit(tree, ctx); if (tree != t2) { return t2; } } return tree; } }; } @SafeVarargs public static TreeVisitor and(TreeVisitor... vs) { return new TreeVisitor() { @Override public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { SourceFile sourceFile = tree instanceof SourceFile ? (SourceFile) tree : null; Tree t2 = tree; for (TreeVisitor v : vs) { // calling `isAcceptable()` in case `v` overrides `visit(Tree, P)` if (sourceFile != null && !v.isAcceptable(sourceFile, ctx)) { continue; } t2 = v.visit(tree, ctx); if (tree == t2) { return tree; } } return t2; } }; } @SafeVarargs public static Supplier> and(Supplier>... svs) { 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 getRecipe() { return check; } } public static class Check extends TreeVisitor { @Getter 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; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy