33

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.

Community
  • 1
  • 1
Axel
  • 13,939
  • 5
  • 50
  • 79

6 Answers6

12

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.

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • I choose this answer. Even if not guaranteed by the documentation, it works for me. – Axel Mar 03 '11 at 21:57
  • 4
    @irreputable, could you provide a link to this ? or was this observation based on your experience only ? – Sergio Aug 04 '12 at 09:51
  • 37
    Running on Oracle JVM version 1.7 I found the ordering did not match the declaration order. – Nick Sawadsky Nov 13 '13 at 16:40
  • 2
    Dalvik doesn't match the declaration order also – Kevin D. Jul 08 '15 at 07:55
  • 2
    They are not in the order declared. I just tested this. – gshauger Dec 07 '15 at 15:35
  • 1
    Regardless of how the particular JRE’s Reflection implementation handles this, there isn’t even a guaranty about how the *compiler* stores the members in the class files, hence, the JRE wouldn’t have a chance to reconstitute the declaration order, if the order has been lost at compile-time already. So even with that particular JDK6 implementation that seems to happen to maintain the order, there is no guaranty. – Holger Jun 21 '17 at 13:36
8

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)

Warren MacEvoy
  • 898
  • 9
  • 14
  • But there is no guaranty that the bytecode of the class reflects the order of the source code… – Holger Jun 21 '17 at 13:39
  • Agreed; but the declaration section of the bytecode in fact does have them in declaration order, as tested on JDK 1.7 and 1.8. Android's Jack and Jill is not parsed (and so not sorted), but Android does return the methods in declared order, so the code works on all major current contexts. – Warren MacEvoy Jun 22 '17 at 15:08
5

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();
...
aioobe
  • 413,195
  • 112
  • 811
  • 826
2

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.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
1

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.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
0

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.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268