From 6156aac557d7be6a21fd7eca03e6e3ec8ab522fc Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 24 Oct 2026 14:05:01 +0200 Subject: [PATCH] Use `JavaFileChecker` preconditions instead of `isAcceptable` overrides --- .../testing/junit5/AssertThrowsOnLastStatement.java | 12 +++++------- .../junit5/ExpectedSystemExitToCatchSystemExit.java | 8 ++------ ...JUnitSoftAssertionsToSoftAssertionsExtension.java | 12 ++++-------- .../testing/junit5/SystemPropertyRulesToPioneer.java | 9 ++------- .../testing/junit5/SystemRulesToSystemStubs.java | 8 ++------ 5 files changed, 15 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java b/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java index c024f59f4..b9614b01b 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java @@ -28,6 +28,8 @@ import org.openrewrite.kotlin.KotlinTemplate; import org.openrewrite.kotlin.tree.K; import org.openrewrite.staticanalysis.LambdaBlockToExpression; +import org.openrewrite.staticanalysis.java.JavaFileChecker; +import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker; import java.util.*; import java.util.regex.Matcher; @@ -55,14 +57,10 @@ public class AssertThrowsOnLastStatement extends Recipe { public TreeVisitor getVisitor() { MethodMatcher assertThrowsMatcher = new MethodMatcher( "org.junit.jupiter.api.Assertions assertThrows(java.lang.Class, org.junit.jupiter.api.function.Executable, ..)"); - return Preconditions.check(new UsesMethod<>(assertThrowsMatcher), new JavaIsoVisitor() { - @Override - public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { + return Preconditions.check(Preconditions.and( // Only Java and Kotlin are supported, as the extracted variable declaration is rendered per language - return (sourceFile instanceof J.CompilationUnit || sourceFile instanceof K.CompilationUnit) && - super.isAcceptable(sourceFile, ctx); - } - + Preconditions.or(new JavaFileChecker<>(), new KotlinFileChecker<>()), + new UsesMethod<>(assertThrowsMatcher)), new JavaIsoVisitor() { @Override public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl, ExecutionContext ctx) { J.MethodDeclaration m = super.visitMethodDeclaration(methodDecl, ctx); diff --git a/src/main/java/org/openrewrite/java/testing/junit5/ExpectedSystemExitToCatchSystemExit.java b/src/main/java/org/openrewrite/java/testing/junit5/ExpectedSystemExitToCatchSystemExit.java index 913326bf6..67d9b2473 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/ExpectedSystemExitToCatchSystemExit.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/ExpectedSystemExitToCatchSystemExit.java @@ -24,6 +24,7 @@ import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; +import org.openrewrite.staticanalysis.java.JavaFileChecker; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -52,16 +53,11 @@ public class ExpectedSystemExitToCatchSystemExit extends Recipe { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>(EXPECTED_SYSTEM_EXIT, false), new JavaVisitor() { + return Preconditions.check(Preconditions.and(new JavaFileChecker<>(), new UsesType<>(EXPECTED_SYSTEM_EXIT, false)), new JavaVisitor() { private final Set migrated = new HashSet<>(); private final Map todos = new HashMap<>(); private final Map plans = new HashMap<>(); - @Override - public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { - return sourceFile instanceof J.CompilationUnit; - } - @Override public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { migrated.clear(); diff --git a/src/main/java/org/openrewrite/java/testing/junit5/JUnitSoftAssertionsToSoftAssertionsExtension.java b/src/main/java/org/openrewrite/java/testing/junit5/JUnitSoftAssertionsToSoftAssertionsExtension.java index 08f994cb0..9e44cf7fe 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/JUnitSoftAssertionsToSoftAssertionsExtension.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/JUnitSoftAssertionsToSoftAssertionsExtension.java @@ -21,7 +21,6 @@ import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; -import org.openrewrite.SourceFile; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.ListUtils; import org.openrewrite.java.AnnotationMatcher; @@ -34,6 +33,7 @@ import org.openrewrite.java.trait.Annotated; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.TypeUtils; +import org.openrewrite.staticanalysis.java.JavaFileChecker; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -79,19 +79,15 @@ public class JUnitSoftAssertionsToSoftAssertionsExtension extends Recipe { public TreeVisitor getVisitor() { return Preconditions.check( Preconditions.and( + // Kotlin properties would need `lateinit var` rather than a dropped `final` and initializer, and + // the `ChangeType` below is driven from a `J.CompilationUnit` that a Kotlin source does not have. + new JavaFileChecker<>(), new UsesType<>(RULE, false), Preconditions.or( new UsesType<>(JUNIT_SOFT_ASSERTIONS, false), new UsesType<>(JUNIT_BDD_SOFT_ASSERTIONS, false))), new JavaIsoVisitor() { - // Kotlin properties would need `lateinit var` rather than a dropped `final` and initializer, and - // the `ChangeType` below is driven from a `J.CompilationUnit` that a Kotlin source does not have. - @Override - public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { - return sourceFile instanceof J.CompilationUnit; - } - @Override public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { getCursor().putMessage(UNCONVERTIBLE_TYPES, unconvertibleRuleTypes(cu)); diff --git a/src/main/java/org/openrewrite/java/testing/junit5/SystemPropertyRulesToPioneer.java b/src/main/java/org/openrewrite/java/testing/junit5/SystemPropertyRulesToPioneer.java index 5a95dbec3..3290d10aa 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/SystemPropertyRulesToPioneer.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/SystemPropertyRulesToPioneer.java @@ -22,7 +22,6 @@ import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; -import org.openrewrite.SourceFile; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.ListUtils; import org.openrewrite.java.JavaIsoVisitor; @@ -37,6 +36,7 @@ import org.openrewrite.java.tree.JavaType; import org.openrewrite.java.tree.Statement; import org.openrewrite.java.tree.TypeUtils; +import org.openrewrite.staticanalysis.java.JavaFileChecker; import java.util.*; @@ -63,12 +63,7 @@ public class SystemPropertyRulesToPioneer extends Recipe { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>(PACKAGE + ".*", false), new JavaIsoVisitor() { - @Override - public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { - return sourceFile instanceof J.CompilationUnit; - } - + return Preconditions.check(Preconditions.and(new JavaFileChecker<>(), new UsesType<>(PACKAGE + ".*", false)), new JavaIsoVisitor() { @Override public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); diff --git a/src/main/java/org/openrewrite/java/testing/junit5/SystemRulesToSystemStubs.java b/src/main/java/org/openrewrite/java/testing/junit5/SystemRulesToSystemStubs.java index 8e5f73ff7..f22450235 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/SystemRulesToSystemStubs.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/SystemRulesToSystemStubs.java @@ -29,6 +29,7 @@ import org.openrewrite.java.service.AnnotationService; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; +import org.openrewrite.staticanalysis.java.JavaFileChecker; import java.util.*; @@ -119,15 +120,10 @@ private static class Migration { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>(PACKAGE + ".*", false), new JavaVisitor() { + return Preconditions.check(Preconditions.and(new JavaFileChecker<>(), new UsesType<>(PACKAGE + ".*", false)), new JavaVisitor() { private final Map migrations = new HashMap<>(); private final Map todos = new HashMap<>(); - @Override - public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { - return sourceFile instanceof J.CompilationUnit; - } - @Override public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { migrations.clear();