This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch 9.0.x-java-to-groovy
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit e55fd9e0eb35abbfa2b7bdf3836da518d6dbad65
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Sat Oct 10 11:43:00 2026 -0500

    fix(datamapping-core): keep Groovy runtime calls out of the transformer's 
class init
    
    DetachedCriteriaTransformer is instantiated by the global
    GlobalDetachedCriteriaASTTransformation, so its static initialiser runs in
    whichever Groovy compiles a source unit with this module on the classpath.
    The static compiler turns `keysAndValues.length % 2` into
    NumberMath.remainder(Number, Number), which only exists from Groovy 5 
(Groovy
    4 has mod), so under Gradle's embedded Groovy the class failed to initialise
    with NoSuchMethodError. A bitwise parity test compiles to plain iand.
    
    This is defence in depth behind the Forge change that keeps framework jars 
off
    the buildscript classpath; the new spec inspects the bytecode reachable from
    <clinit> so the next Groovy-version-specific helper does not slip back in.
    
    Co-Authored-By: Claude Fable 5.1 <[email protected]>
---
 .../transform/DetachedCriteriaTransformer.groovy   |  4 +-
 ...DetachedCriteriaTransformerClassInitSpec.groovy | 90 ++++++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)

diff --git 
a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformer.groovy
 
b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformer.groovy
index 5bd639cd42..bec0292127 100644
--- 
a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformer.groovy
+++ 
b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformer.groovy
@@ -1505,7 +1505,9 @@ class DetachedCriteriaTransformer extends 
ClassCodeVisitorSupport {
         if (keysAndValues == null) {
             return Collections.emptyMap()
         }
-        if (keysAndValues.length % 2 == 1) {
+        // a bitwise test rather than `% 2`: this class is loaded by whichever 
Groovy runs the compiler,
+        // and the static compiler turns `%` into a NumberMath call that older 
Groovy runtimes lack
+        if ((keysAndValues.length & 1) == 1) {
             throw new IllegalArgumentException('Must have an even number of 
keys and values')
         }
 
diff --git 
a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformerClassInitSpec.groovy
 
b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformerClassInitSpec.groovy
new file mode 100644
index 0000000000..11b9b5c87e
--- /dev/null
+++ 
b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformerClassInitSpec.groovy
@@ -0,0 +1,90 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.datastore.gorm.query.transform
+
+import groovyjarjarasm.asm.ClassReader
+import groovyjarjarasm.asm.ClassVisitor
+import groovyjarjarasm.asm.MethodVisitor
+import groovyjarjarasm.asm.Opcodes
+import spock.lang.Specification
+
+/**
+ * {@link GlobalDetachedCriteriaASTTransformation} is a global transformation: 
it runs inside
+ * whichever Groovy compiles a source unit that has this module on its 
classpath, which is not
+ * always the Groovy this module was compiled with (Gradle compiles build 
scripts with its own,
+ * older Groovy). Its first action is to instantiate {@link 
DetachedCriteriaTransformer}, so that
+ * class' static initialisation must not depend on Groovy runtime helpers that 
differ between
+ * Groovy versions, such as the {@code NumberMath} method the static compiler 
emits for {@code %}
+ * ({@code mod} up to Groovy 4, {@code remainder} from Groovy 5). The array 
accessors of
+ * {@code BytecodeInterface8} have been the same since Groovy 2 and are the 
only runtime calls allowed.
+ */
+class DetachedCriteriaTransformerClassInitSpec extends Specification {
+
+    private static final String GROOVY_RUNTIME_PACKAGE = 
'org/codehaus/groovy/runtime/'
+
+    private static final String ARRAY_ACCESS_HELPER = 
'org/codehaus/groovy/runtime/BytecodeInterface8.'
+
+    void "the static initialisation of the transformer makes no Groovy runtime 
calls"() {
+        given:
+        String internalName = DetachedCriteriaTransformer.name.replace('.', 
'/')
+        Map<String, List<String>> callsByMethod = [:]
+        ClassReader reader = new 
ClassReader(DetachedCriteriaTransformer.getResourceAsStream("/${internalName}.class").bytes)
+        reader.accept(new ClassVisitor(Opcodes.ASM9) {
+
+            @Override
+            MethodVisitor visitMethod(int access, String name, String 
descriptor, String signature, String[] exceptions) {
+                List<String> calls = []
+                callsByMethod[name + descriptor] = calls
+                return new MethodVisitor(Opcodes.ASM9) {
+
+                    @Override
+                    void visitMethodInsn(int opcode, String owner, String 
calledName, String calledDescriptor, boolean isInterface) {
+                        calls << 
"${owner}.${calledName}${calledDescriptor}".toString()
+                    }
+
+                }
+            }
+
+        }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES)
+
+        when: 'every method reachable from the static initialiser inside the 
class is followed'
+        Set<String> visited = []
+        List<String> pending = callsByMethod.keySet().findAll { 
it.startsWith('<clinit>') }.toList()
+        while (pending) {
+            String method = pending.pop()
+            if (visited.add(method)) {
+                callsByMethod[method].each { String call ->
+                    if (call.startsWith(internalName + '.')) {
+                        pending << call.substring(internalName.length() + 1)
+                    }
+                }
+            }
+        }
+        List<String> runtimeCalls = visited.collectMany { String method ->
+            callsByMethod[method]
+                    .findAll { it.startsWith(GROOVY_RUNTIME_PACKAGE) && 
!it.startsWith(ARRAY_ACCESS_HELPER) }
+                    .collect { "${method} -> ${it}".toString() }
+        }
+
+        then:
+        visited.any { it.startsWith('newMap(') }
+        runtimeCalls.empty
+    }
+
+}

Reply via email to