Code that relies on Class.getDeclaredMethods() returning the methods in source-code order is currently wrong not just in theory but in practice. That the JDK appears to return them in that order is a fragile coincidence, as I explained in this blog entry. Apparently unrelated changes anywhere else in the app can cause the order to change.

The proposed change could be made, but it is not just a spec change. All JVMs that don't currently conform to the spec would need to change, as Rémi Forax notes, and that includes the JDK and its derivatives. I'm not a HotSpot expert but my guess is that there's some pretty sensitive code involved that the HotSpot team might be reluctant to go poking around in.

Éamonn McManus · JMX Spec Lead · http://weblogs.java.net/blog/emcmanus


Florian Weimer wrote:
I suggest to replace:

| The elements [methods] in the array returned are not sorted and are
| not in any particular order.

with:

| If the class is a compiled Java class, the elements in the array are
| sorted according to the order of declaration in the source code.
| Otherwise, the elements are not in any particular order.

There is a growing amount of code which relies on predictable method
order, so backwards compatibility concerns implicitly dictate the
ordering.  The proposed change makes this explicit.

The other reflection methods should be updated in a similar fashion.

  

Reply via email to