方法由javac在生成的类文件中排序

时间:2021-01-18 09:57:10

With JDK7, the reflection API has changed and now the methods returned by getDeclaredMethods() are not returned in the order in which they are declared in the source file.

使用JDK7,反射API已更改,现在getDeclaredMethods()返回的方法不会按照在源文件中声明的顺序返回。

Now my question is, does the .class file generated by javac contains methods in the same order in which they were defined in the source file OR it can write methods in random order too?

现在我的问题是,javac生成的.class文件是否包含与源文件中定义的方法相同的方法,还是可以按随机顺序编写方法?

2 个解决方案

#1


8  

The Binary Compatibility chapter of the Java Language Specification is explicit about the fact that reordering of elements in the class files is permitted:

Java语言规范的二进制兼容性章节明确指出允许对类文件中的元素进行重新排序的事实:

[...] here is a list of some important binary compatible changes that the Java programming language supports:

[...]这里列出了Java编程语言支持的一些重要的二进制兼容更改:

  • [...]

    [...]

  • Reordering the fields, methods, or constructors in an existing type declaration.

    重新排序现有类型声明中的字段,方法或构造函数。

  • [...]

    [...]

  • Reordering the list of direct superinterfaces of a class or interface.

    重新排序类或接口的直接超接口列表。

That means that the order in which they appear in the .class file is not dictated by the specifications. If you want to rely on it, you have to either (1) know for a fact that your specific implementation uses the same order as the definition order (testing it, like you've done, is a good idea but does not guarantee anything), or (2) change the order yourself.

这意味着它们出现在.class文件中的顺序不是由规范决定的。如果你想依赖它,你必须要么(1)知道你的具体实现使用与定义顺序相同的顺序(测试它,就像你做的那样,是一个好主意,但不能保证任何东西) ),或(2)自己更改订单。

#2


3  

Class.getDeclaredMethods API is clear about this "...The elements in the array returned are not sorted and are not in any particular order...". Most likely the reason of that is that javac is not obliged to generate methods in .class in any particular order.

Class.getDeclaredMethods API很清楚这个“...返回的数组中的元素没有排序,并且没有按任何特定的顺序......”。最有可能的原因是javac没有义务以任何特定顺序在.class中生成方法。

#1


8  

The Binary Compatibility chapter of the Java Language Specification is explicit about the fact that reordering of elements in the class files is permitted:

Java语言规范的二进制兼容性章节明确指出允许对类文件中的元素进行重新排序的事实:

[...] here is a list of some important binary compatible changes that the Java programming language supports:

[...]这里列出了Java编程语言支持的一些重要的二进制兼容更改:

  • [...]

    [...]

  • Reordering the fields, methods, or constructors in an existing type declaration.

    重新排序现有类型声明中的字段,方法或构造函数。

  • [...]

    [...]

  • Reordering the list of direct superinterfaces of a class or interface.

    重新排序类或接口的直接超接口列表。

That means that the order in which they appear in the .class file is not dictated by the specifications. If you want to rely on it, you have to either (1) know for a fact that your specific implementation uses the same order as the definition order (testing it, like you've done, is a good idea but does not guarantee anything), or (2) change the order yourself.

这意味着它们出现在.class文件中的顺序不是由规范决定的。如果你想依赖它,你必须要么(1)知道你的具体实现使用与定义顺序相同的顺序(测试它,就像你做的那样,是一个好主意,但不能保证任何东西) ),或(2)自己更改订单。

#2


3  

Class.getDeclaredMethods API is clear about this "...The elements in the array returned are not sorted and are not in any particular order...". Most likely the reason of that is that javac is not obliged to generate methods in .class in any particular order.

Class.getDeclaredMethods API很清楚这个“...返回的数组中的元素没有排序,并且没有按任何特定的顺序......”。最有可能的原因是javac没有义务以任何特定顺序在.class中生成方法。