Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use provided configuration for optional language dependencies #411

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
12 changes: 8 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<P> extends TreeVisitor<Tree, P> {
private static final boolean IS_CSHARP_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.csharp.tree.Cs");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make an isLanguageAvailable function part of ReflectionUtils or otherwise available in the rewrite core modules as this functionality will end up used in many places


@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<P> extends TreeVisitor<Tree, P> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
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;

/**
* Add a search marker if vising a Kotlin file
*/
public class KotlinFileChecker<P> extends TreeVisitor<Tree, P> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,9 @@ void foo() {
@Test
void recordCompactConstructor() {
rewriteRun(
version(java(
version(
//language=java
java(
"""
public record MyRecord(
boolean bar,
Expand All @@ -999,6 +1001,7 @@ public record MyRecord(
@Test
void removeKotlinUnusedLocalVariable() {
rewriteRun(
//language=kotlin
kotlin(
"""
class A (val b: String) {
Expand Down Expand Up @@ -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()
}
}
"""
Expand Down
Loading