0

I'm trying to retrieve a list of methods inside a specific class, but it has to be in order. The second part is where I'm stuck and uncertain if it's even possible.

This is the method that retrieves the list:

public static ArrayList<Method> getAllMethods (MyClass myClass) {
    ArrayList<Method> methods = new ArrayList<Method>();
    Class c = myClass.getClass();
    while (c != Object.class) {
        Collections.addAll(methods, ClassReflection.getDeclaredMethods(c));
        c = c.getSuperclass();
    }
    return methods;
}

The result is that the list contains all the methods in a what seems to be random order. For my program that's very inconvenient. Is it possible to do this in a simple way? The only way I thought of was adding annotations and order each method by giving it a number. However, that seems like a lot of hassle for what I'm trying to achieve and I don't want to do it this way.

EDIT: I use the retrieved methods to get the annotations that belong to them. Next I display these annotations on screen in my game, because the methods are basically in-game commands. The commands are written in a logical order and some commands are grouped together so they need to be displayed next to each other and in the correct order. Here is how I display the annotation values:

@Command(description = "Shows all commands and their usages")
public void help() {
    for(Method command : Util.getAllMethods(this)) {
        Annotation anno = command.getDeclaredAnnotation(Command.class);
        if(command.isAnnotationPresent(Command.class)){
            Command cmd = anno.getAnnotation(Command.class);
            console.log(command.getName() + " " + cmd.param() + " : " + cmd.description());
        }
    }
}
Tipsi
  • 404
  • 4
  • 12
  • 2
    How do you define "correct order" to begin with? And why do you feel that you need this? – Hovercraft Full Of Eels Jul 23 '19 at 23:09
  • They're not officially ordered, but I think they usually are in practice. What's `ClassReflection` though? Please provide a [mcve]. – shmosel Jul 23 '19 at 23:10
  • @HovercraftFullOfEels The order would be lower line number is higher up in the list. I added some more information in the OP. – Tipsi Jul 23 '19 at 23:19
  • @shmosel I added an example in my OP. – Tipsi Jul 23 '19 at 23:20
  • @shmosel I think this is duplicate of https://stackoverflow.com/questions/3148274/get-declared-methods-in-order-they-appear-in-source-code as OP wants to this, he is not asking about order but if he can get the original one. – GotoFinal Jul 24 '19 at 21:30
  • @GotoFinal I don't know what you mean by "the original one", but that looks like the same question to me. – shmosel Jul 24 '19 at 21:33
  • @shmosel no, one of question is about how methods are ordered when fetched by reflections, and another one (and this one) is about how to use reflections to get methods in same order as in source. – GotoFinal Jul 24 '19 at 21:39
  • @GotoFinal Well, the answer is the same. Reflection doesn't guarantee an order. – shmosel Jul 24 '19 at 21:40
  • @shmosel it's not the same, as you can still get the original order under some conditions – GotoFinal Jul 24 '19 at 21:41
  • @GotoFinal Under which conditions? – shmosel Jul 24 '19 at 21:44
  • @shmosel that the class is loaded from some source you can read, like typical .jar, as you can then get order directly from .class file. Just like in answers from that other question. Tho it seems to be duplicate of https://stackoverflow.com/questions/5001172/java-reflection-getting-fields-and-methods-in-declaration-order too – GotoFinal Jul 24 '19 at 21:45
  • 1
    @GotoFinal That's not technically reflection. But I'll add your links as duplicates. – shmosel Jul 24 '19 at 21:48

0 Answers0