1919package org .codehaus .groovy .transform .trait ;
2020
2121import groovy .transform .CompileStatic ;
22+ import org .apache .groovy .ast .tools .MethodNodeUtils ;
2223import org .codehaus .groovy .ast .ASTNode ;
2324import org .codehaus .groovy .ast .AnnotationNode ;
2425import org .codehaus .groovy .ast .ClassHelper ;
5354import java .util .ArrayList ;
5455import java .util .Arrays ;
5556import java .util .Collections ;
57+ import java .util .Comparator ;
5658import java .util .Iterator ;
5759import java .util .LinkedHashSet ;
5860import 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