[Git][java-team/groovy][master] 2 commits: Generate the super$ MOP methods in a reproducible order

Emmanuel Bourg (@ebourg) gitlab at salsa.debian.org
Tue Oct 20 10:53:34 BST 2026



Emmanuel Bourg pushed to branch master at Debian Java Maintainers / groovy


Commits:
bde55d5d by Emmanuel Bourg at 2026-10-20T11:51:33+02:00
Generate the super$ MOP methods in a reproducible order

- - - - -
d0c8998e by Emmanuel Bourg at 2026-10-20T11:53:09+02:00
Upload to unstable

- - - - -


3 changed files:

- debian/changelog
- + debian/patches/21_reproducible_reflection.patch
- debian/patches/series


Changes:

=====================================
debian/changelog
=====================================
@@ -1,3 +1,10 @@
+groovy (2.4.21-12) unstable; urgency=medium
+
+  * Team upload.
+  * Generate the super$ MOP methods in a reproducible order
+
+ -- Emmanuel Bourg <ebourg at apache.org>  Tue, 20 Oct 2026 11:53:00 +0200
+
 groovy (2.4.21-11) unstable; urgency=medium
 
   * Team upload.


=====================================
debian/patches/21_reproducible_reflection.patch
=====================================
@@ -0,0 +1,136 @@
+Description: Read the members of precompiled classes in a reproducible order
+ Class.getDeclaredMethods() and getDeclaredConstructors() return the members
+ in an unspecified order that varies from one JVM run to the next. This order
+ flows into the generated bytecode, for example the super$ MOP methods.
+Author: James Daugherty <jdaugherty at jdresources.net>
+Origin: backport, https://github.com/apache/groovy/commit/7aabc479f6bc03a6a6c5ecd31fc2c66d2cfb8e21
+Bug: https://issues.apache.org/jira/browse/GROOVY-12149
+Applied-Upstream: 5.0.8
+--- a/src/main/org/codehaus/groovy/reflection/ReflectionUtils.java
++++ b/src/main/org/codehaus/groovy/reflection/ReflectionUtils.java
+@@ -18,8 +18,12 @@
+  */
+ package org.codehaus.groovy.reflection;
+ 
++import java.lang.reflect.Constructor;
++import java.lang.reflect.Method;
++import java.util.Arrays;
+ import java.util.Collection;
+ import java.util.Collections;
++import java.util.Comparator;
+ import java.util.HashSet;
+ import java.util.Set;
+ 
+@@ -119,6 +123,75 @@
+         }
+     }
+ 
++    /**
++     * Returns the declared methods of a class in a deterministic order.
++     * <p>
++     * {@link Class#getDeclaredMethods()} does not guarantee an order, and
++     * HotSpot's varies between JVM runs. When members of a precompiled class
++     * are enumerated at compile time (annotation members copied to generated
++     * code, trait methods woven into implementing classes, MOP {@code super$}
++     * bridge methods, etc.), that order flows into the generated bytecode, so
++     * a nondeterministic order produces byte-different class files from
++     * identical sources. Callers whose output depends on member order should
++     * use this variant so builds are reproducible.
++     *
++     * @param type the class to introspect
++     * @return the declared methods, sorted by name then signature
++     */
++    public static Method[] getDeclaredMethodsSorted(final Class<?> type) {
++        Method[] methods = type.getDeclaredMethods();
++        Arrays.sort(methods, new Comparator<Method>() {
++            public int compare(Method m1, Method m2) {
++                return compareMethods(m1, m2);
++            }
++        });
++        return methods;
++    }
++
++    /**
++     * Returns the declared constructors of a class in a deterministic order.
++     * <p>
++     * {@link Class#getDeclaredConstructors()} does not guarantee an order, and
++     * a nondeterministic order can flow into generated bytecode; see
++     * {@link #getDeclaredMethodsSorted(Class)}.
++     *
++     * @param type the class to introspect
++     * @return the declared constructors, sorted by parameter types
++     */
++    public static Constructor<?>[] getDeclaredConstructorsSorted(final Class<?> type) {
++        Constructor<?>[] constructors = type.getDeclaredConstructors();
++        // name is always <init> and the return type always void, so the parameters decide
++        Arrays.sort(constructors, new Comparator<Constructor<?>>() {
++            public int compare(Constructor<?> c1, Constructor<?> c2) {
++                return compareParameterTypes(c1.getParameterTypes(), c2.getParameterTypes());
++            }
++        });
++        return constructors;
++    }
++
++    /**
++     * Orders by name, then parameter types, then return type. This is a total order:
++     * the JVM forbids two methods in one class sharing a name and descriptor, and the
++     * descriptor is exactly the parameter types plus the return type.
++     */
++    private static int compareMethods(final Method m1, final Method m2) {
++        int c = m1.getName().compareTo(m2.getName());
++        if (c != 0) return c;
++        c = compareParameterTypes(m1.getParameterTypes(), m2.getParameterTypes());
++        if (c != 0) return c;
++        return m1.getReturnType().getName().compareTo(m2.getReturnType().getName());
++    }
++
++    private static int compareParameterTypes(final Class<?>[] p1, final Class<?>[] p2) {
++        int c = Integer.compare(p1.length, p2.length);
++        if (c != 0) return c;
++        for (int i = 0; i < p1.length; i += 1) {
++            c = p1[i].getName().compareTo(p2[i].getName());
++            if (c != 0) return c;
++        }
++        return 0;
++    }
++
+     private static boolean superClassShouldBeIgnored(Class sc) {
+         return ((sc != null) && (sc.getPackage() != null) && "org.codehaus.groovy.runtime.callsite".equals(sc.getPackage().getName()));
+     }
+--- a/src/main/org/codehaus/groovy/vmplugin/v5/Java5.java
++++ b/src/main/org/codehaus/groovy/vmplugin/v5/Java5.java
+@@ -35,6 +35,7 @@
+ import org.codehaus.groovy.ast.expr.ListExpression;
+ import org.codehaus.groovy.ast.expr.PropertyExpression;
+ import org.codehaus.groovy.ast.stmt.ReturnStatement;
++import org.codehaus.groovy.reflection.ReflectionUtils;
+ import org.codehaus.groovy.vmplugin.VMPlugin;
+ 
+ import java.lang.annotation.Annotation;
+@@ -269,7 +270,7 @@
+         } else {
+             Method[] declaredMethods;
+             try {
+-                declaredMethods = type.getDeclaredMethods();
++                declaredMethods = ReflectionUtils.getDeclaredMethodsSorted(type);
+             } catch (SecurityException se) {
+                 declaredMethods = new Method[0];
+             }
+@@ -367,7 +368,7 @@
+                 setAnnotationMetaData(f.getAnnotations(), fn);
+                 classNode.addField(fn);
+             }
+-            Method[] methods = clazz.getDeclaredMethods();
++            Method[] methods = ReflectionUtils.getDeclaredMethodsSorted(clazz);
+             for (Method m : methods) {
+                 ClassNode ret = makeClassNode(compileUnit, m.getGenericReturnType(), m.getReturnType());
+                 Parameter[] params = makeParameters(compileUnit, m.getGenericParameterTypes(), m.getParameterTypes(), m.getParameterAnnotations());
+@@ -379,7 +380,7 @@
+                 mn.setGenericsTypes(configureTypeVariable(m.getTypeParameters()));
+                 classNode.addMethod(mn);
+             }
+-            Constructor[] constructors = clazz.getDeclaredConstructors();
++            Constructor[] constructors = ReflectionUtils.getDeclaredConstructorsSorted(clazz);
+             for (Constructor ctor : constructors) {
+                 Parameter[] params = makeParameters(
+                         compileUnit,


=====================================
debian/patches/series
=====================================
@@ -21,3 +21,4 @@ update-to-JLine2-2.13.patch
 18_reproducible_groovydoc.patch
 19_reproducible_delegate.patch
 20_reproducible_traits.patch
+21_reproducible_reflection.patch



View it on GitLab: https://salsa.debian.org/java-team/groovy/-/compare/b922b484e694a7299cec1ab4793bb8708171b819...d0c8998e6007c3a6896312a65427390ff88ebbea

-- 
View it on GitLab: https://salsa.debian.org/java-team/groovy/-/compare/b922b484e694a7299cec1ab4793bb8708171b819...d0c8998e6007c3a6896312a65427390ff88ebbea
You're receiving this email because of your account on salsa.debian.org. Manage all notifications: https://salsa.debian.org/-/profile/notifications | Help: https://salsa.debian.org/help


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20261020/bdc6655d/attachment.htm>


More information about the pkg-java-commits mailing list