@@ -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