Skip to content

Commit d68fdec

Browse files
committed
Make Groovy major version parsing more robust for groovy-eclipse-compiler
1 parent 8110e0f commit d68fdec

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

docs/release_notes.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ include::include.adoc[]
66

77
== 2.5 (tbd)
88

9+
10+
=== Misc
11+
12+
* Make Groovy major version parsing more robust for groovy-eclipse-compiler spockIssue:2282[]
13+
914
== 2.4 (2025-12-11)
1015

1116
_This is a summary of the highlights of the milestone releases_

spock-core/src/main/java/org/spockframework/runtime/GroovyRuntimeUtil.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,30 @@ public abstract class GroovyRuntimeUtil {
4242
private static final String GET = "get";
4343
private static final String IS = "is";
4444

45+
/**
46+
* Fallback Groovy version used, when the parsing of the real Groovy version fails.
47+
*/
48+
private static final int GROOVY_DEFAULT_FALLBACK_VERSION = 4;
4549
@Internal
46-
public static final int MAJOR_VERSION = parseInt(GroovySystem.getVersion().split("\\.", 2)[0]);
50+
public static final int MAJOR_VERSION = parseGroovyMajorVersion();
4751
public static Object[] EMPTY_ARGUMENTS = new Object[0];
4852

53+
/**
54+
* See <a href="https://github.com/spockframework/spock/issues/2282">Issue2282</a>.
55+
*
56+
* @return The Groovy major version or {@link #GROOVY_DEFAULT_FALLBACK_VERSION} if the parsing failed.
57+
*/
58+
private static int parseGroovyMajorVersion() {
59+
String version = GroovySystem.getVersion();
60+
if (version != null && !version.isEmpty()) {
61+
try {
62+
return parseInt(version.split("\\.", 2)[0]);
63+
} catch (Exception ignored) {
64+
}
65+
}
66+
return GROOVY_DEFAULT_FALLBACK_VERSION;
67+
}
68+
4969
public static boolean isTruthy(Object obj) {
5070
return DefaultTypeTransformation.castToBoolean(obj);
5171
}
@@ -306,7 +326,7 @@ public static boolean isVoidMethod(@Nullable Object target, String method, Objec
306326
// in the end it's probably best to rely on NullAwareInvokeMethodSpec to tell us if
307327
// everything is OK
308328
MetaClass metaClass = target instanceof Class ?
309-
InvokerHelper.getMetaClass((Class) target) : InvokerHelper.getMetaClass(target);
329+
InvokerHelper.getMetaClass((Class) target) : InvokerHelper.getMetaClass(target);
310330

311331
// seems to find more methods than getMetaMethod()
312332
MetaMethod metaMethod = metaClass.pickMethod(method, argTypes);

0 commit comments

Comments
 (0)