Java Reflection: Getting fields and methods in declaration order

前端 未结 6 941
不知归路
不知归路 2020-11-29 07:27

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 Fie

相关标签:
6条回答
  • 2020-11-29 07:45

    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.

    0 讨论(0)
  • 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();
    ...
    
    0 讨论(0)
  • 2020-11-29 07:57

    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.

    0 讨论(0)
  • 2020-11-29 07:57

    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 讨论(0)
  • 2020-11-29 08:04

    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)

    0 讨论(0)
  • 2020-11-29 08:07

    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.

    0 讨论(0)
提交回复
热议问题