From ae403f6867d744018a1203fa4bce999d8eb80937 Mon Sep 17 00:00:00 2001 From: lingenj Date: Mon, 23 Dec 2024 11:46:07 +0100 Subject: [PATCH 01/10] Set groovy/kotlin/csharp as comileOnly+testImplementation dependencies and add file-checkers with a reflection-class-exists-check --- build.gradle.kts | 9 ++++-- .../csharp/CSharpFileChecker.java | 5 +++- .../groovy/GroovyFileChecker.java | 5 +++- .../internal/ClassPathUtils.java | 30 +++++++++++++++++++ .../kotlin/KotlinFileChecker.java | 5 +++- 5 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java diff --git a/build.gradle.kts b/build.gradle.kts index 2a258eab5..314c49e10 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,9 +14,12 @@ 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}") + compileOnly("org.openrewrite:rewrite-groovy:${rewriteVersion}") + testImplementation("org.openrewrite:rewrite-groovy:${rewriteVersion}") + compileOnly("org.openrewrite:rewrite-kotlin:${rewriteVersion}") + testImplementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}") + compileOnly("org.openrewrite:rewrite-csharp:${rewriteVersion}") + testImplementation("org.openrewrite:rewrite-csharp:${rewriteVersion}") implementation("org.openrewrite.meta:rewrite-analysis:${rewriteVersion}") implementation("org.apache.commons:commons-text:latest.release") diff --git a/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java index b482eb9cc..0619807a2 100644 --- a/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java @@ -20,14 +20,17 @@ import org.openrewrite.TreeVisitor; import org.openrewrite.csharp.tree.Cs; import org.openrewrite.marker.SearchResult; +import org.openrewrite.staticanalysis.internal.ClassPathUtils; /** * Add a search marker if vising a CSharp file */ public class CSharpFileChecker

extends TreeVisitor { + private static final boolean IS_CSHARP_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.csharp.tree.Cs$CompilationUnit"); + @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..6ab40d98c 100644 --- a/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java @@ -20,14 +20,17 @@ import org.openrewrite.TreeVisitor; import org.openrewrite.groovy.tree.G; import org.openrewrite.marker.SearchResult; +import org.openrewrite.staticanalysis.internal.ClassPathUtils; /** * Add a search marker if vising a Groovy file */ public class GroovyFileChecker

extends TreeVisitor { + private static final boolean IS_GROOVY_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.groovy.tree.G$CompilationUnit"); + @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/internal/ClassPathUtils.java b/src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java new file mode 100644 index 000000000..3489f5206 --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java @@ -0,0 +1,30 @@ +/* + * Copyright 2024 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.internal; + +import lombok.experimental.UtilityClass; + +@UtilityClass +public class ClassPathUtils { + public static boolean isAvailable(String className) { + try { + Class.forName(className); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } +} diff --git a/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java index 880b11115..ae55d67ab 100644 --- a/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java @@ -20,14 +20,17 @@ import org.openrewrite.TreeVisitor; import org.openrewrite.kotlin.tree.K; import org.openrewrite.marker.SearchResult; +import org.openrewrite.staticanalysis.internal.ClassPathUtils; /** * Add a search marker if vising a Kotlin file */ public class KotlinFileChecker

extends TreeVisitor { + private static final boolean IS_KOTLIN_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.kotlin.tree.K$CompilationUnit"); + @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; From d150e5ad326b20702ac377da54a4dcc481418d13 Mon Sep 17 00:00:00 2001 From: Jacob van Lingen Date: Mon, 23 Dec 2024 13:05:52 +0100 Subject: [PATCH 02/10] Update build.gradle.kts Co-authored-by: Tim te Beek --- build.gradle.kts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 314c49e10..83271ec38 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,8 +14,8 @@ dependencies { implementation(platform("org.openrewrite:rewrite-bom:${rewriteVersion}")) implementation("org.openrewrite:rewrite-java") - compileOnly("org.openrewrite:rewrite-groovy:${rewriteVersion}") - testImplementation("org.openrewrite:rewrite-groovy:${rewriteVersion}") + compileOnly("org.openrewrite:rewrite-groovy") + testImplementation("org.openrewrite:rewrite-groovy") compileOnly("org.openrewrite:rewrite-kotlin:${rewriteVersion}") testImplementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}") compileOnly("org.openrewrite:rewrite-csharp:${rewriteVersion}") From 556eb36c47967d4d4d953fc0a815cf8f78c932fa Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 23 Dec 2024 15:48:55 +0100 Subject: [PATCH 03/10] Split off & document compileOnly dependencies; remove duplicate test dependency --- build.gradle.kts | 13 +++++++------ .../RemoveUnusedLocalVariablesTest.java | 8 ++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 83271ec38..209b454a9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,14 +14,13 @@ dependencies { implementation(platform("org.openrewrite:rewrite-bom:${rewriteVersion}")) implementation("org.openrewrite:rewrite-java") + 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") - testImplementation("org.openrewrite:rewrite-groovy") compileOnly("org.openrewrite:rewrite-kotlin:${rewriteVersion}") - testImplementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}") compileOnly("org.openrewrite:rewrite-csharp:${rewriteVersion}") - testImplementation("org.openrewrite:rewrite-csharp:${rewriteVersion}") - implementation("org.openrewrite.meta:rewrite-analysis:${rewriteVersion}") - implementation("org.apache.commons:commons-text:latest.release") annotationProcessor("org.openrewrite:rewrite-templating:${rewriteVersion}") implementation("org.openrewrite:rewrite-templating:${rewriteVersion}") @@ -29,9 +28,11 @@ 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/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() } } """ From 6237e0e2312388f052039a63d1d587c43fd27b85 Mon Sep 17 00:00:00 2001 From: Jacob van Lingen Date: Mon, 23 Dec 2024 16:21:44 +0100 Subject: [PATCH 04/10] Apply suggestions from code review Co-authored-by: Tim te Beek --- .../openrewrite/staticanalysis/csharp/CSharpFileChecker.java | 2 +- .../openrewrite/staticanalysis/groovy/GroovyFileChecker.java | 2 +- .../openrewrite/staticanalysis/kotlin/KotlinFileChecker.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java index 0619807a2..eb8a80294 100644 --- a/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java @@ -26,7 +26,7 @@ * Add a search marker if vising a CSharp file */ public class CSharpFileChecker

extends TreeVisitor { - private static final boolean IS_CSHARP_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.csharp.tree.Cs$CompilationUnit"); + private static final boolean IS_CSHARP_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.csharp.tree.Cs"); @Override public @Nullable Tree visit(@Nullable Tree tree, P p) { diff --git a/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java index 6ab40d98c..9724a9918 100644 --- a/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/groovy/GroovyFileChecker.java @@ -26,7 +26,7 @@ * Add a search marker if vising a Groovy file */ public class GroovyFileChecker

extends TreeVisitor { - private static final boolean IS_GROOVY_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.groovy.tree.G$CompilationUnit"); + private static final boolean IS_GROOVY_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.groovy.tree.G"); @Override public @Nullable Tree visit(@Nullable Tree tree, P p) { diff --git a/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java index ae55d67ab..51614d7a3 100644 --- a/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java @@ -26,7 +26,7 @@ * Add a search marker if vising a Kotlin file */ public class KotlinFileChecker

extends TreeVisitor { - private static final boolean IS_KOTLIN_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.kotlin.tree.K$CompilationUnit"); + private static final boolean IS_KOTLIN_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.kotlin.tree.K"); @Override public @Nullable Tree visit(@Nullable Tree tree, P p) { From 14346ee73525ebf6d41e37e12d7509bf369630c7 Mon Sep 17 00:00:00 2001 From: lingenj Date: Mon, 23 Dec 2024 16:44:59 +0100 Subject: [PATCH 05/10] Set groovy/kotlin/csharp as provided dependencies --- build.gradle.kts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 209b454a9..154cb7108 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,9 @@ plugins { id("org.openrewrite.build.recipe-library") version "latest.release" id("org.openrewrite.build.moderne-source-available-license") version "latest.release" + id("com.netflix.nebula.provided-base") version "10.0.1" } +apply(plugin = "com.netflix.nebula.provided-base") group = "org.openrewrite.recipe" description = "The first Static Analysis and REMEDIATION tool" @@ -17,22 +19,19 @@ dependencies { 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") { + compileOnly("com.google.errorprone:error_prone_core:2.+") { exclude("com.google.auto.service", "auto-service-annotations") } + provided("org.openrewrite:rewrite-groovy") + provided("org.openrewrite:rewrite-kotlin:${rewriteVersion}") + provided("org.openrewrite:rewrite-csharp:${rewriteVersion}") + + 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") @@ -41,3 +40,9 @@ dependencies { testRuntimeOnly("org.openrewrite:rewrite-java-17") testRuntimeOnly("com.google.code.findbugs:jsr305:latest.release") } + +tasks.register("listConfigurations") { + doLast { + configurations.forEach { println(it.name) } + } +} From 18c3a1db98efc40f125204f143b1101a8254b2bc Mon Sep 17 00:00:00 2001 From: lingenj Date: Mon, 23 Dec 2024 16:55:05 +0100 Subject: [PATCH 06/10] Set groovy/kotlin/csharp as provided dependencies --- build.gradle.kts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 154cb7108..57330e765 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -40,9 +40,3 @@ dependencies { testRuntimeOnly("org.openrewrite:rewrite-java-17") testRuntimeOnly("com.google.code.findbugs:jsr305:latest.release") } - -tasks.register("listConfigurations") { - doLast { - configurations.forEach { println(it.name) } - } -} From 2498e50e4304cfe0c5a83f93c254cd6f708d1b18 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 24 Dec 2024 09:43:14 +0100 Subject: [PATCH 07/10] Revert gradle file + add TODO --- build.gradle.kts | 17 +++++++++-------- .../staticanalysis/internal/ClassPathUtils.java | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 57330e765..209b454a9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,9 +1,7 @@ plugins { id("org.openrewrite.build.recipe-library") version "latest.release" id("org.openrewrite.build.moderne-source-available-license") version "latest.release" - id("com.netflix.nebula.provided-base") version "10.0.1" } -apply(plugin = "com.netflix.nebula.provided-base") group = "org.openrewrite.recipe" description = "The first Static Analysis and REMEDIATION tool" @@ -19,19 +17,22 @@ dependencies { 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.+") { + compileOnly("com.google.errorprone:error_prone_core:2.+:with-dependencies") { exclude("com.google.auto.service", "auto-service-annotations") } - provided("org.openrewrite:rewrite-groovy") - provided("org.openrewrite:rewrite-kotlin:${rewriteVersion}") - provided("org.openrewrite:rewrite-csharp:${rewriteVersion}") - - 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/internal/ClassPathUtils.java b/src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java index 3489f5206..615afd1da 100644 --- a/src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java +++ b/src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java @@ -17,6 +17,7 @@ import lombok.experimental.UtilityClass; +// TODO: Remove this class, use `ReflectionUtils.isClassAvailable`. @UtilityClass public class ClassPathUtils { public static boolean isAvailable(String className) { From 81faf84a54c3e11b4b175cab66de51a9d23c4f1e Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 24 Dec 2024 13:13:23 +0100 Subject: [PATCH 08/10] Use `ReflectionUtils.isClassAvailable` --- .../csharp/CSharpFileChecker.java | 4 +-- .../groovy/GroovyFileChecker.java | 4 +-- .../internal/ClassPathUtils.java | 31 ------------------- .../kotlin/KotlinFileChecker.java | 4 +-- 4 files changed, 6 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java diff --git a/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java index eb8a80294..6fac69715 100644 --- a/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/csharp/CSharpFileChecker.java @@ -19,14 +19,14 @@ import org.openrewrite.Tree; import org.openrewrite.TreeVisitor; import org.openrewrite.csharp.tree.Cs; +import org.openrewrite.internal.ReflectionUtils; import org.openrewrite.marker.SearchResult; -import org.openrewrite.staticanalysis.internal.ClassPathUtils; /** * Add a search marker if vising a CSharp file */ public class CSharpFileChecker

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

extends TreeVisitor { - private static final boolean IS_GROOVY_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.groovy.tree.G"); + private static final boolean IS_GROOVY_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.groovy.tree.G"); @Override public @Nullable Tree visit(@Nullable Tree tree, P p) { diff --git a/src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java b/src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java deleted file mode 100644 index 615afd1da..000000000 --- a/src/main/java/org/openrewrite/staticanalysis/internal/ClassPathUtils.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2024 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.internal; - -import lombok.experimental.UtilityClass; - -// TODO: Remove this class, use `ReflectionUtils.isClassAvailable`. -@UtilityClass -public class ClassPathUtils { - public static boolean isAvailable(String className) { - try { - Class.forName(className); - return true; - } catch (ClassNotFoundException e) { - return false; - } - } -} diff --git a/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java b/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java index 51614d7a3..ae1e1bfb3 100644 --- a/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java +++ b/src/main/java/org/openrewrite/staticanalysis/kotlin/KotlinFileChecker.java @@ -18,15 +18,15 @@ 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; -import org.openrewrite.staticanalysis.internal.ClassPathUtils; /** * Add a search marker if vising a Kotlin file */ public class KotlinFileChecker

extends TreeVisitor { - private static final boolean IS_KOTLIN_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.kotlin.tree.K"); + private static final boolean IS_KOTLIN_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.kotlin.tree.K"); @Override public @Nullable Tree visit(@Nullable Tree tree, P p) { From 1f28e1839ca80d434a232056b996ac63938839b2 Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Thu, 2 Jan 2025 15:19:57 -0800 Subject: [PATCH 09/10] Get things compiling --- .../java/org/openrewrite/staticanalysis/UnnecessaryCatch.java | 4 ++-- .../org/openrewrite/staticanalysis/UnnecessaryThrows.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/UnnecessaryCatch.java b/src/main/java/org/openrewrite/staticanalysis/UnnecessaryCatch.java index 6fc82c854..e8c758b26 100644 --- a/src/main/java/org/openrewrite/staticanalysis/UnnecessaryCatch.java +++ b/src/main/java/org/openrewrite/staticanalysis/UnnecessaryCatch.java @@ -76,7 +76,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) { public J.Try visitTry(J.Try tryable, ExecutionContext ctx) { J.Try t = super.visitTry(tryable, ctx); - List thrownExceptions = new ArrayList<>(); + List thrownExceptions = new ArrayList<>(); AtomicBoolean missingTypeInformation = new AtomicBoolean(false); //Collect any checked exceptions thrown from the try block. new JavaIsoVisitor() { @@ -108,7 +108,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ if (!includeJavaLangException && TypeUtils.isOfClassType(parameterType, "java.lang.Exception")) { return aCatch; } - for (JavaType.FullyQualified e : thrownExceptions) { + for (JavaType e : thrownExceptions) { if (TypeUtils.isAssignableTo(e, parameterType)) { return aCatch; } diff --git a/src/main/java/org/openrewrite/staticanalysis/UnnecessaryThrows.java b/src/main/java/org/openrewrite/staticanalysis/UnnecessaryThrows.java index f57de2f12..c0d3d41d7 100644 --- a/src/main/java/org/openrewrite/staticanalysis/UnnecessaryThrows.java +++ b/src/main/java/org/openrewrite/staticanalysis/UnnecessaryThrows.java @@ -115,7 +115,7 @@ public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { private void removeThrownTypes(JavaType.@Nullable Method type) { if (type != null) { - for (JavaType.FullyQualified thrownException : type.getThrownExceptions()) { + for (JavaType thrownException : type.getThrownExceptions()) { unusedThrows.removeIf(t -> TypeUtils.isAssignableTo(t, thrownException)); } } @@ -178,7 +178,7 @@ private Set findExceptionCandidates(J.@Nullable MethodD if (superMethod.isPresent()) { JavaType.Method baseMethod = superMethod.get(); baseMethod.getThrownExceptions(); - for (JavaType.FullyQualified baseException : baseMethod.getThrownExceptions()) { + for (JavaType baseException : baseMethod.getThrownExceptions()) { candidates.remove(baseException); } } From eaf83066c326fb382d3b8a0de3f2bb8bb019770f Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Thu, 2 Jan 2025 18:36:50 -0800 Subject: [PATCH 10/10] Use provided functionality I've built into the openrewrite publishing plugin: https://github.com/openrewrite/rewrite-build-gradle-plugin/commit/7de9d928f8675b7ddc18d92dc6ae4ad5f8375ad0 --- build.gradle.kts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 209b454a9..6ce6afa11 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,4 @@ +@file:Suppress("UnstableApiUsage") plugins { id("org.openrewrite.build.recipe-library") version "latest.release" id("org.openrewrite.build.moderne-source-available-license") version "latest.release" @@ -6,6 +7,7 @@ plugins { group = "org.openrewrite.recipe" description = "The first Static Analysis and REMEDIATION tool" +val provided = configurations.named("provided") val rewriteVersion = rewriteRecipe.rewriteVersion.get() dependencies { compileOnly("org.projectlombok:lombok:latest.release") @@ -18,9 +20,9 @@ dependencies { 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}") + provided("org.openrewrite:rewrite-groovy:${rewriteVersion}") + provided("org.openrewrite:rewrite-kotlin:${rewriteVersion}") + provided("org.openrewrite:rewrite-csharp:${rewriteVersion}") annotationProcessor("org.openrewrite:rewrite-templating:${rewriteVersion}") implementation("org.openrewrite:rewrite-templating:${rewriteVersion}") @@ -28,9 +30,6 @@ dependencies { exclude("com.google.auto.service", "auto-service-annotations") } - 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.+")