Skip to content

Commit

Permalink
Prevent cast exception when plugin uses variable
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Feb 14, 2024
1 parent 10766cc commit ac52d15
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -88,12 +91,15 @@ public static List<GradlePlugin> find(J j, String pluginIdPattern) {
MethodMatcher versionMatcher = new MethodMatcher("Plugin version(..)", false);
List<GradlePlugin> 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());
Expand All @@ -115,4 +121,17 @@ public static List<GradlePlugin> 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<J> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ void findPlugin() {
@Test
void findPluginWithVariable() {
rewriteRun(
properties(
"""
jfrogBintrayVersion=1.8.5
""",
spec -> spec.path("gradle.properties")
),
buildGradle(
"""
plugins {
Expand All @@ -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")
)
);
}
Expand Down

0 comments on commit ac52d15

Please sign in to comment.