From 2ca9cce7935a4bbcd86c799be65e01a2f39c68df Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 28 May 2026 12:16:29 +0200 Subject: [PATCH] Skip Python sources in `CombineSemanticallyEqualCatchBlocks` The recipe's `CommentVisitor` is a `JavaIsoVisitor` that crashes in `TreeVisitorAdapter` when adapted to Python `Py.CompilationUnit` sources. Add a `PythonFileChecker` and use it as a precondition to exclude Python files while keeping Java, Kotlin, and Groovy support. --- build.gradle.kts | 1 + .../CombineSemanticallyEqualCatchBlocks.java | 3 +- .../python/PythonFileChecker.java | 38 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/openrewrite/staticanalysis/python/PythonFileChecker.java diff --git a/build.gradle.kts b/build.gradle.kts index a642fc0ef..aecade022 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,6 +24,7 @@ dependencies { provided("org.openrewrite:rewrite-javascript:${rewriteVersion}") provided("org.openrewrite:rewrite-kotlin:${rewriteVersion}") provided("org.openrewrite:rewrite-csharp:${rewriteVersion}") + provided("org.openrewrite:rewrite-python:${rewriteVersion}") annotationProcessor("org.openrewrite:rewrite-templating:${rewriteVersion}") implementation("org.openrewrite:rewrite-templating:${rewriteVersion}") diff --git a/src/main/java/org/openrewrite/staticanalysis/CombineSemanticallyEqualCatchBlocks.java b/src/main/java/org/openrewrite/staticanalysis/CombineSemanticallyEqualCatchBlocks.java index 1548316ef..18873bd8e 100644 --- a/src/main/java/org/openrewrite/staticanalysis/CombineSemanticallyEqualCatchBlocks.java +++ b/src/main/java/org/openrewrite/staticanalysis/CombineSemanticallyEqualCatchBlocks.java @@ -25,6 +25,7 @@ import org.openrewrite.java.search.SemanticallyEqual; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; +import org.openrewrite.staticanalysis.python.PythonFileChecker; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -50,7 +51,7 @@ public class CombineSemanticallyEqualCatchBlocks extends Recipe { @Override public TreeVisitor getVisitor() { - return new CombineSemanticallyEqualCatchBlocksVisitor(); + return Preconditions.check(Preconditions.not(new PythonFileChecker<>()), new CombineSemanticallyEqualCatchBlocksVisitor()); } private static class CombineSemanticallyEqualCatchBlocksVisitor extends JavaVisitor { diff --git a/src/main/java/org/openrewrite/staticanalysis/python/PythonFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/python/PythonFileChecker.java new file mode 100644 index 000000000..78828651f --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/python/PythonFileChecker.java @@ -0,0 +1,38 @@ +/* + * Copyright 2026 the original author or authors. + *

+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://docs.moderne.io/licensing/moderne-source-available-license + *

+ * 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.staticanalysis.python; + +import org.jspecify.annotations.Nullable; +import org.openrewrite.Tree; +import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.ReflectionUtils; +import org.openrewrite.marker.SearchResult; +import org.openrewrite.python.tree.Py; + +/** + * Add a search marker if visiting a Python file + */ +public class PythonFileChecker

extends TreeVisitor { + private static final boolean IS_PYTHON_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.python.tree.Py"); + + @Override + public @Nullable Tree visit(@Nullable Tree tree, P p) { + if (IS_PYTHON_AVAILABLE && tree instanceof Py.CompilationUnit) { + return SearchResult.found(tree); + } + return tree; + } +}