30
votes

Is there any way to get a classes declared fields (and methods) in the order of declaration using reflection? According to the documentation, the ordering of Methods and Fields returned by getFields(), getDeclaredFields(), etc. is undefined.

Specifying something like an index would be possible using annotation as suggested in Java reflection: Is the order of class fields and methods standardized?

Are there any better options, i.e. not having to specify the index manually?

Now before you ask what I need this for: we have a method that takes a quite big data structure as input and performs a lengthy calculation on it. To create unit tests, we made a method that takes an input object and an output instance and creates the Java source code (setting up input, invoking the calculation method, and asserting the correct results afterwards) as output. This code is much more readable when fields are written in declaration order.

6

6 Answers

10
votes

With jdk 6, the reflected fields are in deed in their declaration order. In early jdk that wasn't the case. Apparently enough people have nagged.

Although not guaranteed by javadoc, I would still take this order as granted, and I assume the order will be kept in future jdks too.

In your app, like in most apps, the dependency on the declaration order is mostly vanity - your app won't fail if the order screws up, it just become a little uglier.

7
votes

I had this as an isolated problem, look at

https://github.com/wmacevoy/kiss/blob/master/src/main/java/kiss/util/Reflect.java

and the method

public static Method[] getDeclaredMethodsInOrder(Class clazz)

It gets the order by looking at the bytecode of the class. If you just want to use the libray, is would be kiss.util.Reflect.getDeclaredMethodsInOrder(Test.class)

5
votes

No, not possible with reflection. You could however solve it using a ProcessBuilder and the javap command:

Given a Test.java:

public abstract class Test {

    public void method1() {
    }

    public void method2() {
    }

    public static void main(String[] args) {
    }

    public String method3() {
        return "hello";
    }

    abstract void method4();

    final int method5() {
        return 0;
    }
}

The command javap Test prints:

...
public Test();
public void method1();
public void method2();
public static void main(java.lang.String[]);
public java.lang.String method3();
abstract void method4();
final int method5();
...
2
votes

I'm afraid it's impossible without modifying the compilation process. Normally, the field get written into the classfile in any order and the information about the declaration order gets lost.

Most probably you could use an annotation processor to write the order in an auxiliary file.

It should be quite easy. Look e.g. at interfacegen for an example, how an annotation processor can work. You may want to put the information in the same file, but this is much harder.

1
votes

You may think about using Javadoc with a custom Doclet, but this requires the source to be available.

There still is no guarantee about the order in the API (methods, fields, but every javadoc output I've ever seen has them in the right order, so I suppose the doclets get them in declaration order.

0
votes

You won't be able to get the information from the class file. As Adam said in an answer to the refrenced other question:

The elements in the array returned are not sorted and are not in any particular order.

And "no order" includes "no declaration order".

I once used a Java source file parser to get input data for code generators. And this way you'll have fields and methods in declaration order.