diff --git a/build.gradle.kts b/build.gradle.kts index 2a258eab5..209b454a9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,21 +14,25 @@ dependencies { implementation(platform("org.openrewrite:rewrite-bom:${rewriteVersion}")) implementation("org.openrewrite:rewrite-java") - implementation("org.openrewrite:rewrite-groovy:${rewriteVersion}") - implementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}") - implementation("org.openrewrite:rewrite-csharp:${rewriteVersion}") implementation("org.openrewrite.meta:rewrite-analysis:${rewriteVersion}") implementation("org.apache.commons:commons-text:latest.release") + // Limit transitive dependencies for downstream projects like rewrite-spring + compileOnly("org.openrewrite:rewrite-groovy") + compileOnly("org.openrewrite:rewrite-kotlin:${rewriteVersion}") + compileOnly("org.openrewrite:rewrite-csharp:${rewriteVersion}") + annotationProcessor("org.openrewrite:rewrite-templating:${rewriteVersion}") implementation("org.openrewrite:rewrite-templating:${rewriteVersion}") compileOnly("com.google.errorprone:error_prone_core:2.+:with-dependencies") { exclude("com.google.auto.service", "auto-service-annotations") } - testImplementation("org.jetbrains:annotations:24.+") testImplementation("org.openrewrite:rewrite-groovy") + testImplementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}") + testImplementation("org.openrewrite:rewrite-csharp:${rewriteVersion}") testImplementation("org.openrewrite:rewrite-test") + testImplementation("org.jetbrains:annotations:24.+") testImplementation("org.junit-pioneer:junit-pioneer:2.+") testImplementation("junit:junit:4.13.2") diff --git a/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java index b482eb9cc..6fac69715 100644 --- a/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java @@ -19,15 +19,18 @@ import org.openrewrite.Tree; import org.openrewrite.TreeVisitor; import org.openrewrite.csharp.tree.Cs; +import org.openrewrite.internal.ReflectionUtils; import org.openrewrite.marker.SearchResult; /** * Add a search marker if vising a CSharp file */ public class CSharpFileChecker

extends TreeVisitor { + private static final boolean IS_CSHARP_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.csharp.tree.Cs"); + @Override public @Nullable Tree visit(@Nullable Tree tree, P p) { - if (tree instanceof Cs.CompilationUnit) { + if (IS_CSHARP_AVAILABLE && tree instanceof Cs.CompilationUnit) { return SearchResult.found(tree); } return tree; diff --git a/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java index 6782c32f3..ad68da31c 100644 --- a/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java @@ -19,15 +19,18 @@ import org.openrewrite.Tree; import org.openrewrite.TreeVisitor; import org.openrewrite.groovy.tree.G; +import org.openrewrite.internal.ReflectionUtils; import org.openrewrite.marker.SearchResult; /** * Add a search marker if vising a Groovy file */ public class GroovyFileChecker

extends TreeVisitor { + private static final boolean IS_GROOVY_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.groovy.tree.G"); + @Override public @Nullable Tree visit(@Nullable Tree tree, P p) { - if (tree instanceof G.CompilationUnit) { + if (IS_GROOVY_AVAILABLE && tree instanceof G.CompilationUnit) { return SearchResult.found(tree); } return tree; diff --git a/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java index 880b11115..ae1e1bfb3 100644 --- a/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java @@ -18,6 +18,7 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.Tree; import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.ReflectionUtils; import org.openrewrite.kotlin.tree.K; import org.openrewrite.marker.SearchResult; @@ -25,9 +26,11 @@ * Add a search marker if vising a Kotlin file */ public class KotlinFileChecker

extends TreeVisitor { + private static final boolean IS_KOTLIN_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.kotlin.tree.K"); + @Override public @Nullable Tree visit(@Nullable Tree tree, P p) { - if (tree instanceof K.CompilationUnit) { + if (IS_KOTLIN_AVAILABLE && tree instanceof K.CompilationUnit) { return SearchResult.found(tree); } return tree; diff --git a/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java b/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java index 9397d7493..1b9e4e522 100644 --- a/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java @@ -979,7 +979,9 @@ void foo() { @Test void recordCompactConstructor() { rewriteRun( - version(java( + version( + //language=java + java( """ public record MyRecord( boolean bar, @@ -999,6 +1001,7 @@ public record MyRecord( @Test void removeKotlinUnusedLocalVariable() { rewriteRun( + //language=kotlin kotlin( """ class A (val b: String) { @@ -1101,12 +1104,13 @@ class Kotlin { @Test void retainUnusedLocalVariableWithNewClass() { rewriteRun( + //language=kotlin kotlin( """ class A {} class B { fun foo() { - val a = A(); + val a = A() } } """