From eb74491d015e92330cb6e6ccd0ba5b23fa49a83a Mon Sep 17 00:00:00 2001 From: Erko Risthein Date: Mon, 2 Nov 2026 13:33:12 +0200 Subject: [PATCH] #patch: Compile the Spock extension statically so it loads on Groovy 6 --- .../snapshots/spock/SnapshotExtension.groovy | 9 ++++++-- .../spock/SnapshotMethodInterceptor.groovy | 15 ++++++++---- .../GroovyRuntimeCompatibilitySpec.groovy | 23 +++++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/spock/GroovyRuntimeCompatibilitySpec.groovy diff --git a/java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotExtension.groovy b/java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotExtension.groovy index eba9305..dc2a4ad 100644 --- a/java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotExtension.groovy +++ b/java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotExtension.groovy @@ -4,9 +4,12 @@ import au.com.origin.snapshots.SnapshotVerifier import au.com.origin.snapshots.config.PropertyResolvingSnapshotConfig import au.com.origin.snapshots.config.SnapshotConfig import au.com.origin.snapshots.config.SnapshotConfigInjector +import groovy.transform.CompileStatic import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension +import org.spockframework.runtime.model.FeatureInfo import org.spockframework.runtime.model.SpecInfo +@CompileStatic class SnapshotExtension extends AbstractAnnotationDrivenExtension implements SnapshotConfigInjector { SnapshotVerifier snapshotVerifier; @@ -17,7 +20,9 @@ class SnapshotExtension extends AbstractAnnotationDrivenExtension - if (Expect.class == type) { + def parameterTypes = invocation.method.reflection.parameterTypes + for (int i = 0; i < parameterTypes.length; i++) { + if (Expect.class == parameterTypes[i]) { LoggingHelper.deprecatedV5(log, "Injecting 'Expect' via method a argument is no longer recommended. Consider using instance variable injection instead.") invocation.arguments[i] = new Expect(snapshotVerifier, invocation.feature.featureMethod.reflection) } @@ -38,8 +43,8 @@ class SnapshotMethodInterceptor extends AbstractMethodInterceptor { } private void updateInstanceVariable(Object testInstance, Method testMethod) { - ReflectionUtils.findFieldByPredicate(testInstance.class, { field -> field.getType() == Expect.class }) - .ifPresent({ field -> + ReflectionUtils.findFieldByPredicate(testInstance.class, { Field field -> field.getType() == Expect.class }) + .ifPresent({ Field field -> Expect expect = Expect.of(snapshotVerifier, testMethod); ReflectionUtils.makeAccessible(field); try { diff --git a/java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/spock/GroovyRuntimeCompatibilitySpec.groovy b/java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/spock/GroovyRuntimeCompatibilitySpec.groovy new file mode 100644 index 0000000..9a15a98 --- /dev/null +++ b/java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/spock/GroovyRuntimeCompatibilitySpec.groovy @@ -0,0 +1,23 @@ +package au.com.origin.snapshots.spock + +import spock.lang.Specification + +class GroovyRuntimeCompatibilitySpec extends Specification { + + private static final String REMOVED_IN_GROOVY_6 = 'org/codehaus/groovy/runtime/callsite/CallSiteArray' + + def "extension classes do not reference the call site cache that Groovy 6 removed"() { + given: + def classesRoot = new File(SnapshotExtension.protectionDomain.codeSource.location.toURI()) + def extensionClasses = [] + classesRoot.eachFileRecurse { file -> + if (file.name.endsWith('.class')) { + extensionClasses << file + } + } + + expect: + !extensionClasses.empty + extensionClasses.findAll { new String(it.bytes, 'ISO-8859-1').contains(REMOVED_IN_GROOVY_6) }.empty + } +}