Axel
Axel

Reputation: 14149

Java Reflection: Getting fields and methods in declaration order

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.

Upvotes: 34

Views: 20537

Answers (6)

Warren MacEvoy
Warren MacEvoy

Reputation: 979

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)

Upvotes: 9

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

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.

Upvotes: 1

irreputable
irreputable

Reputation: 45433

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.

Upvotes: 12

aioobe
aioobe

Reputation: 420951

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();
...

Upvotes: 6

maaartinus
maaartinus

Reputation: 46382

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.

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114757

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.

Upvotes: 0

Related Questions