Skip to content

Commit fdfd3fa

Browse files
committed
GROOVY-11674: Grails reproducible build (order trait methods)
1 parent 9364652 commit fdfd3fa

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

src/main/java/org/codehaus/groovy/transform/trait/TraitComposer.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.codehaus.groovy.transform.trait;
2020

2121
import groovy.transform.CompileStatic;
22+
import org.apache.groovy.ast.tools.MethodNodeUtils;
2223
import org.codehaus.groovy.ast.ASTNode;
2324
import org.codehaus.groovy.ast.AnnotationNode;
2425
import org.codehaus.groovy.ast.ClassHelper;
@@ -53,6 +54,7 @@
5354
import java.util.ArrayList;
5455
import java.util.Arrays;
5556
import java.util.Collections;
57+
import java.util.Comparator;
5658
import java.util.Iterator;
5759
import java.util.LinkedHashSet;
5860
import java.util.LinkedList;
@@ -123,7 +125,11 @@ private static void applyTrait(final ClassNode trait, final ClassNode cNode, fin
123125
ClassNode staticFieldHelperClassNode = helpers.getStaticFieldHelper();
124126
Map<String, ClassNode> genericsSpec = GenericsUtils.createGenericsSpec(trait, GenericsUtils.createGenericsSpec(cNode));
125127

126-
for (MethodNode methodNode : helperClassNode.getMethods()) {
128+
List<MethodNode> hMethods = helperClassNode.getMethods();
129+
if (!hMethods.isEmpty()) {
130+
hMethods.sort(Comparator.comparing(MethodNodeUtils::methodDescriptorWithoutReturnType));
131+
}
132+
for (MethodNode methodNode : hMethods) {
127133
String name = methodNode.getName();
128134
Parameter[] helperMethodParams = methodNode.getParameters();
129135
int nParams = helperMethodParams.length;
@@ -172,7 +178,11 @@ private static void applyTrait(final ClassNode trait, final ClassNode cNode, fin
172178
// implementation of methods
173179
List<MethodNode> declaredMethods = new LinkedList<>();
174180
int pos = 0; // keep direct getters at start but in declaration order
175-
for (MethodNode declaredMethod : fieldHelperClassNode.getMethods()) {
181+
List<MethodNode> fhMethods = fieldHelperClassNode.getMethods();
182+
if (!fhMethods.isEmpty()) {
183+
fhMethods.sort(Comparator.comparing(MethodNodeUtils::methodDescriptorWithoutReturnType));
184+
}
185+
for (MethodNode declaredMethod : fhMethods) {
176186
if (declaredMethod.getName().endsWith(Traits.DIRECT_GETTER_SUFFIX)) {
177187
declaredMethods.add(pos++, declaredMethod);
178188
} else {
@@ -181,7 +191,11 @@ private static void applyTrait(final ClassNode trait, final ClassNode cNode, fin
181191
}
182192

183193
if (staticFieldHelperClassNode != null) {
184-
for (MethodNode declaredMethod : staticFieldHelperClassNode.getMethods()) {
194+
List<MethodNode> sfhMethods = staticFieldHelperClassNode.getMethods();
195+
if (!sfhMethods.isEmpty()) {
196+
sfhMethods.sort(Comparator.comparing(MethodNodeUtils::methodDescriptorWithoutReturnType));
197+
}
198+
for (MethodNode declaredMethod : sfhMethods) {
185199
if (declaredMethod.getName().endsWith(Traits.DIRECT_GETTER_SUFFIX)) {
186200
declaredMethods.add(pos++, declaredMethod);
187201
} else {

0 commit comments

Comments
 (0)