Skip to content

Commit c1aa242

Browse files
authored
Handle GString compiler args in Gradle plugin (#801) (#968)
1 parent e7ead89 commit c1aa242

4 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎scip-gradle-plugin/src/main/java/org/scip_code/scip_java/gradle/ScipGradlePlugin.java‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,34 @@ private void configureProject(Project project) {
5151
task.getOptions().setIncremental(false);
5252

5353
if (pluginAdded) {
54-
List<String> args = task.getOptions().getCompilerArgs();
54+
// Groovy build scripts can append interpolated strings
55+
// (GString) to compilerArgs, so despite the List<String>
56+
// signature the list may hold non-String elements. Iterate
57+
// through a wildcard reference so elements are not implicitly
58+
// cast to String, which throws ClassCastException (issue
59+
// #801).
60+
List<?> args = task.getOptions().getCompilerArgs();
5561

5662
// It's important we don't add the plugin configuration more
5763
// than once, as javac considers that an error.
5864
boolean alreadyAdded =
59-
args.stream().anyMatch(arg -> arg.startsWith("-Xplugin:scip"));
65+
args.stream()
66+
.anyMatch(arg -> String.valueOf(arg).startsWith("-Xplugin:scip"));
6067
if (!alreadyAdded) {
6168
// The random timestamp ensures the sources are _always_
6269
// recompiled, so Gradle doesn't cache any state.
6370
// TODO: before this plugin is published to Maven Central, we
6471
// will need to revert this change - as it can have
6572
// detrimental effect on people's builds.
66-
args.add(
67-
"-Xplugin:scip -targetroot:"
68-
+ targetRoot
69-
+ " -sourceroot:"
70-
+ sourceRoot
71-
+ " -randomtimestamp="
72-
+ System.nanoTime());
73+
task.getOptions()
74+
.getCompilerArgs()
75+
.add(
76+
"-Xplugin:scip -targetroot:"
77+
+ targetRoot
78+
+ " -sourceroot:"
79+
+ sourceRoot
80+
+ " -randomtimestamp="
81+
+ System.nanoTime());
7382
}
7483
}
7584
});

‎scip-java/src/test/kotlin/tests/GradleBuildToolTest.kt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ class GradleBuildToolTest : BuildToolHarness() {
7171
add(
7272
checkGradleBuild("build-with-Werror", "gradle/build-with-Werror", expectedScipFiles = 2)
7373
)
74+
// Regression test: Groovy string interpolation puts GString elements
75+
// (not java.lang.String) into compilerArgs, which used to crash the
76+
// plugin with a ClassCastException.
77+
// https://github.com/scip-code/scip-java/issues/801
78+
add(
79+
checkGradleBuild(
80+
"gstring-compiler-args",
81+
"gradle/gstring-compiler-args",
82+
expectedScipFiles = 1,
83+
)
84+
)
7485
add(
7586
checkGradleBuild(
7687
"publishing",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id 'java'
3+
}
4+
repositories {
5+
mavenCentral()
6+
}
7+
// Groovy string interpolation produces GString elements, not java.lang.String,
8+
// so compilerArgs may hold non-String values despite its List<String>
9+
// signature. Regression test for
10+
// https://github.com/scip-code/scip-java/issues/801
11+
def maxErrors = 500
12+
compileJava {
13+
options.compilerArgs << "-Xmaxerrs" << "${maxErrors}"
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package example;
2+
3+
public class Example {
4+
public String greet(String name) {
5+
return "Hello, " + name + "!";
6+
}
7+
}

0 commit comments

Comments
 (0)