Class.getFields() 返回的字段顺序

发布于 2024-10-20 05:26:06 字数 168 浏览 2 评论 0原文

Class.getFields() 的 Javadoc 说:“返回的数组中的元素没有排序,也没有任何特定的顺序。”

关于顺序实际上是如何确定的有什么提示吗?当我执行此方法两次时,是否有可能以不同的顺序获取字段?换句话说,对于给定的编译类,甚至在同一源文件的编译之间,顺序是否稳定?

Javadoc for Class.getFields() say: "The elements in the array returned are not sorted and are not in any particular order."

Any hints on how the order actually is determined? Is it possible that when I execute this method twice, I get fields in different order? In other words, is the order stable for given compiled class, or even between compilations of the same source file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拿命拼未来 2024-10-27 05:26:06

它应该是稳定的,对于 Oracle 的 JVM 来说,它是它们声明的顺序,但您不应该依赖于此。

您应该根据字段名称(以及可能声明的类)而不是位置进行查找。

It should be stable, and for Oracle's JVM its the order they are declared, but you should not rely on this.

You should base lookup on the field's name (and possibly declaring class) rather than position.

橙幽之幻 2024-10-27 05:26:06

至少在我的 JVM 上,

Class.getFields() 按声明顺序返回字段。

另一方面,Class.getMethods() 并不总是如此。它按照(我相信)类加载器看到字符串的顺序返回它们。因此,如果两个类具有相同的方法名称,则第二个加载的类将在其他方法之前返回共享方法名称。

javap 确认编译器按声明顺序编写字段和方法。

请参阅此代码示例的输出。

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class OrderTest {
    public static void main(String[] args) {
        // fields are in declaration order
        for (Field field : C1.class.getDeclaredFields()) {
            System.out.println(field.getName());
        }
        for (Field field : C2.class.getDeclaredFields()) {
            System.out.println(field.getName());
        }

        // methods, on the other hand, are not necessarily in declaration order.
        for (Method method : C1.class.getDeclaredMethods()) {
            System.out.println(method.getName());
        }
        for (Method method : C2.class.getDeclaredMethods()) {
            System.out.println(method.getName());
        }
    }
}

class C1 {
    public int foo;
    public int bar;
    public int getFoo() { return foo; }
    public int getBar() { return bar; }
}

class C2 {
    public int bar;
    public int foo;
    public int getBar() { return bar; }
    public int getFoo() { return foo; }
}

在我的 JVM(1.7.0_45,Windows)上返回

foo
bar
bar
foo
getFoo
getBar
getFoo
getBar

On my JVM, at least,

Class.getFields() returns fields in declaration order.

Class.getMethods(), on the other hand, doesn't always. It returns them in (I believe) the order the classloader sees the strings. So if two classes have the same method name, the second-loaded class will return the shared method name before its other methods.

javap confirms the compiler wrote both fields and methods in declaration order.

See the output of this code sample.

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class OrderTest {
    public static void main(String[] args) {
        // fields are in declaration order
        for (Field field : C1.class.getDeclaredFields()) {
            System.out.println(field.getName());
        }
        for (Field field : C2.class.getDeclaredFields()) {
            System.out.println(field.getName());
        }

        // methods, on the other hand, are not necessarily in declaration order.
        for (Method method : C1.class.getDeclaredMethods()) {
            System.out.println(method.getName());
        }
        for (Method method : C2.class.getDeclaredMethods()) {
            System.out.println(method.getName());
        }
    }
}

class C1 {
    public int foo;
    public int bar;
    public int getFoo() { return foo; }
    public int getBar() { return bar; }
}

class C2 {
    public int bar;
    public int foo;
    public int getBar() { return bar; }
    public int getFoo() { return foo; }
}

on my JVM (1.7.0_45, Windows) this returns

foo
bar
bar
foo
getFoo
getBar
getFoo
getBar
梦境 2024-10-27 05:26:06

创建一个返回排序列表的辅助方法,并在需要字段列表时使用该方法。或者按名称而不是索引查找。

Create a helper method that returns a sorted list, and use that instead whenever you need the list of fields. Or lookup by name instead of index.

单身情人 2024-10-27 05:26:06

属性的自然顺序使用 readKeys() 方法为 Ujorm 框架 提供其键值对象。
结果的每个项目都具有类似的功能,例如字段,包括从对象读取值和向对象写入值。例如,请参阅下一个代码:

 public class User extends AbstractUjo implements Serializable {

     /** Factory */
     private static final KeyFactory<User> f = newFactory(User.class);
     /** Keys: */
     public static final Key<User, Long> PID = f.newKey();
     public static final Key<User, Integer> CODE = f.newKey();
     public static final Key<User, String> NAME = f.newKey();
     public static final Key<User, Double> CASH = f.newKey();

     static {
         f.lock();
     }

     // Setters:
     public void setPid(Long pid) {
         PID.setValue(this, pid);
     }

     public void setCode(Integer code) {
         CODE.setValue(this, code);
     }

     public void setName(String name) {
         NAME.setValue(this, name);
     }

     public void setCash(Double cash) {
         CASH.setValue(this, cash);
     }

     // Getters ...
 }

键的自然顺序可以通过以下方式迭代:

 for (Key key : new User().readKeys()) {
     System.out.println("Key: " + key);
 }

请参阅 文档了解更多信息。

An natural order of properties offers the Ujorm framework with its key-value objects using the readKeys() method.
Each item of the result have got similar features like the Field including reading and writting values from/to the object. For example see the next code:

 public class User extends AbstractUjo implements Serializable {

     /** Factory */
     private static final KeyFactory<User> f = newFactory(User.class);
     /** Keys: */
     public static final Key<User, Long> PID = f.newKey();
     public static final Key<User, Integer> CODE = f.newKey();
     public static final Key<User, String> NAME = f.newKey();
     public static final Key<User, Double> CASH = f.newKey();

     static {
         f.lock();
     }

     // Setters:
     public void setPid(Long pid) {
         PID.setValue(this, pid);
     }

     public void setCode(Integer code) {
         CODE.setValue(this, code);
     }

     public void setName(String name) {
         NAME.setValue(this, name);
     }

     public void setCash(Double cash) {
         CASH.setValue(this, cash);
     }

     // Getters ...
 }

The natural order of keys can be iterated by:

 for (Key key : new User().readKeys()) {
     System.out.println("Key: " + key);
 }

See the documentation for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文