From ac52d15599b054d1f6635cd919a4584cf4405070 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 14 Feb 2024 13:08:39 +0100 Subject: [PATCH] Prevent cast exception when plugin uses variable --- .../gradle/search/FindPlugins.java | 31 +++++++++++++++---- .../gradle/search/FindPluginsTest.java | 14 ++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindPlugins.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindPlugins.java index 53bda27a5b3..aa7fa210074 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindPlugins.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/search/FindPlugins.java @@ -17,12 +17,15 @@ import lombok.EqualsAndHashCode; import lombok.Value; +import org.jetbrains.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.gradle.IsBuildGradle; import org.openrewrite.gradle.IsSettingsGradle; import org.openrewrite.gradle.tree.GradlePlugin; +import org.openrewrite.groovy.tree.G; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.marker.SearchResult; @@ -88,12 +91,15 @@ public static List find(J j, String pluginIdPattern) { MethodMatcher versionMatcher = new MethodMatcher("Plugin version(..)", false); List pluginsWithVersion = plugins.stream().flatMap(plugin -> { if (versionMatcher.matches(plugin) && idMatcher.matches(plugin.getSelect())) { - return Stream.of(new GradlePlugin( - plugin, - requireNonNull(((J.Literal) requireNonNull(((J.MethodInvocation) plugin.getSelect())) - .getArguments().get(0)).getValue()).toString(), - requireNonNull(((J.Literal) plugin.getArguments().get(0)).getValue()).toString() - )); + String pluginVersion = getPluginVersion(plugin.getArguments().get(0)); + if (pluginVersion != null) { + return Stream.of(new GradlePlugin( + plugin, + requireNonNull(((J.Literal) requireNonNull(((J.MethodInvocation) plugin.getSelect())) + .getArguments().get(0)).getValue()).toString(), + pluginVersion + )); + } } return Stream.empty(); }).collect(toList()); @@ -115,4 +121,17 @@ public static List find(J j, String pluginIdPattern) { result.addAll(pluginsWithoutVersion); return result; } + + @Nullable + private static String getPluginVersion(Expression expression) { + if (expression instanceof J.Literal) { + return requireNonNull(((J.Literal) expression).getValue()).toString(); + } else if (expression instanceof G.GString) { + List strings = ((G.GString) expression).getStrings(); + if (!strings.isEmpty() && strings.get(0) instanceof G.GString.Value) { + return ((G.GString.Value) strings.get(0)).getTree().toString(); + } + } + return null; + } } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java index 985d8ff3789..7a5155c3490 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java @@ -60,6 +60,12 @@ void findPlugin() { @Test void findPluginWithVariable() { rewriteRun( + properties( + """ + jfrogBintrayVersion=1.8.5 + """, + spec -> spec.path("gradle.properties") + ), buildGradle( """ plugins { @@ -77,14 +83,8 @@ void findPluginWithVariable() { .isNotEmpty() .anySatisfy(p -> { assertThat(p.getPluginId()).isEqualTo("com.jfrog.bintray"); - assertThat(p.getVersion()).isEqualTo("1.8.5"); + assertThat(p.getVersion()).isEqualTo("jfrogBintrayVersion"); })) - ), - properties( - """ - jfrogBintrayVersion=1.8.5 - """, - spec -> spec.path("gradle.properties") ) ); }