Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-反射按声明顺序获取declaredMethods奇怪的行为_Java_Reflection - Fatal编程技术网

Java-反射按声明顺序获取declaredMethods奇怪的行为

Java-反射按声明顺序获取declaredMethods奇怪的行为,java,reflection,Java,Reflection,这是getDeclaredMethods发生的奇怪行为,下面是场景,一个名为Entity的类: public class Entity { private Object reference; /** * @return the reference */ public Object getReference() { return reference; } /** * @param reference the reference to set */ public void set

这是getDeclaredMethods发生的奇怪行为,下面是场景,一个名为Entity的类:

public class Entity {
private Object reference;

/**
 * @return the reference
 */
public Object getReference() {
    return reference;
}

/**
 * @param reference the reference to set
 */
public void setReference(Object reference) {
    this.reference = reference;
}

public Object getReference2() {
    return reference;
}

public void setReference2(Object reference) {
    this.reference = reference;
}
}
主要课程包括:

public static void main(String Args[]){
    try {
        Entity _entity = new Entity();


        Class _newClass = _entity.getClass();
        Method[] _method = _newClass.getDeclaredMethods();
        for (int i = 0; i < _method.length; i++) {
            System.out.println(_method[i]);
        }


    } catch (IllegalArgumentException | SecurityException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }
}
好的,它们是有序的,这很好,但是当您设置对某个对象的引用时会发生这种情况_entity.setReference100,输出:

public void Entities.Entity.setReference(java.lang.Object)
public java.lang.Object Entities.Entity.getReference()
public java.lang.Object Entities.Entity.getReference2()
public void Entities.Entity.setReference2(java.lang.Object)
所以。。。为什么setReference放在第一位?也许因为它有价值?无论我设置了哪些字段,如何保持声明的顺序与类文件中的顺序相同

无论我设置了哪些字段,如何保持声明的顺序与类文件中的顺序相同

你不能,用getDeclaredMethods。政府对此非常清楚:

返回数组中的元素没有排序,也没有任何特定的顺序

我不清楚字节码中是否存在顺序-您可能需要源代码来确定原始顺序

不过,最好根本不依赖于排序,或者按照您想要的确定顺序对数组进行排序

无论我设置了哪些字段,如何保持声明的顺序与类文件中的顺序相同

你不能,用getDeclaredMethods。政府对此非常清楚:

返回数组中的元素没有排序,也没有任何特定的顺序

我不清楚字节码中是否存在顺序-您可能需要源代码来确定原始顺序


不过,最好不要依赖于排序,或者按您想要的确定顺序对数组进行排序。

hmm,谢谢:D,但是。。。你能用注释标记这些方法,然后按注释顺序使用它们吗?@Alpha2k:当然-为什么你不能?要做到这一点,您需要编写一些代码,但这并不难。我假设你的意思是@Order0、@Order1等,然后按@Order值排序。你甚至可以更巧妙地使用代理/字节码转换器自动添加@Order注释,以便它们具有在源代码中显示的顺序…嗯,谢谢:D,但是。。。你能用注释标记这些方法,然后按注释顺序使用它们吗?@Alpha2k:当然-为什么你不能?要做到这一点,您需要编写一些代码,但这并不难。我假设你的意思是@Order0、@Order1等,然后按@Order值排序。你甚至可以特别花哨,使用代理/字节码转换器自动添加@Order注释,以便它们具有在源代码中显示的顺序。。。
public void Entities.Entity.setReference(java.lang.Object)
public java.lang.Object Entities.Entity.getReference()
public java.lang.Object Entities.Entity.getReference2()
public void Entities.Entity.setReference2(java.lang.Object)