Skip to main content

Ordering of getFields(), getMethods(), getDeclaredFields(), etc.

6 replies [Last post]
regexguy
Offline
Joined: 2003-06-20
Points: 0

The documentation says that the values returned by getFields() etc. are not returned in any particular order.

It would really be helpful, for certain applications of reflection based code, if they would be guaranteed to be returned in the order they were declared in the source file.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
regexguy
Offline
Joined: 2003-06-20
Points: 0

OK, I was ignorant!

If you compile with apt (instead of javac), you can get all the fields in the source and sort them by line number.

I didn't realize how powerful apt is!

brucechapman
Offline
Joined: 2004-03-18
Points: 0

One more convert :), one less Mustang RFE, so SUN can get on with the stuff we can't do ourselves :)

brucechapman
Offline
Joined: 2004-03-18
Points: 0

But the runtime only knows the order that they are in, in the bytecode, and jikes seems to have a different order in the bytecode than in the sourcecode (javac seems to keep the order intact)

You could maybe work it out from the linenumber table IF THAT WAS PRESENT in the bytecode, but it isn't always.

What if the class was compiled from some other language where there was more than one file (compilation unit), contributing to the class (AOP aspect injection at class loading time comes to mind here), what should the order be?

regexguy
Offline
Joined: 2003-06-20
Points: 0

Right. That's why this is a feature I'd like to see in a future version of java.

With that feature you could, using only reflection, take a simple class and write it out to a socket as if it were a C struct, figuring out byte padding, etc.

You could similarly serialize a class to a CSV file using this mechanism (if you could rely on the order).

Perhaps rather than making them all return the declared order, one could have a tag @OrderedFields or something that would mark a class as having this property.

brucechapman
Offline
Joined: 2004-03-18
Points: 0

If this is a requirement, I think your suggestion of using a custom annotation to specify an ordering is the correct way to go.

If you really must have source code ordering (rather than a deterministic order), an "apt" annotation processor could use the source line position to determine the order.

Also, if you are using JNI to access C Structs, maybe a better option is to generate a java wrapper from the C source. I have done this (almost), I generate java source from a simple text file that basically contains the C-Struct information (in this case it was easier to re-write the information, than to find/implement a c parser).

regexguy
Offline
Joined: 2003-06-20
Points: 0

I was thinking of the annotation being a special one the compiler understood... hacking this up with apt would be awkward. I'd have to either tag every field, or parse the source code once I saw the tag on the class.

In my example I was not thinking of JNI, I was thinking about having java talk to a C program over a socket where the C program just writes a struct out.

But how hard would it really be for the compiler writers to just keep the source order in the class file? Naively, I think it would be simple.