// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package wrongway;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ReflectionX {
    private static void testPlain(Object o) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Method[] allm = o.getClass().getDeclaredMethods();
        for(Method m : allm) {
            System.out.println(m.getName());
        }
    }
    public static class MethodOrderPair {
        private Method method;
        private int order;
        public MethodOrderPair(Method method, int order) {
            this.method = method;
            this.order = order;
        }
        public Method getMethod() {
            return method;
        }
        public int getOrder() {
            return order;
        }
    }
    public static List<MethodOrderPair> getMethodsWithOrder(Object o) {
        List<MethodOrderPair> res = new ArrayList<>();
        for(Method m : o.getClass().getDeclaredMethods()) {
            res.add(new MethodOrderPair(m, m.getAnnotation(Order.class).value()));
        }
        return res;
    }
    private static void testOrdered(Object o) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<MethodOrderPair> allmop = getMethodsWithOrder(o);
        allmop.sort((m1,m2) -> m1.getOrder() - m2.getOrder());
        for(MethodOrderPair mop : allmop) {
            System.out.println(mop.getMethod().getName());
        }
        
    }
    public static void main(String[] args) throws Exception {
        testPlain(new SomeClass());
        testOrdered(new SomeClass());
    }
}
