jeudi 25 juin 2015

How does JVM do source-order initialization of member fields?

I'm looking for a way to retrieve (at runtime) the fields of a class in source-order. I know that Javadoc for Class.getDeclaredField explicitly states that no order is guaranteed.

Some answers on SO point to Javassist but I can find no evidence that javassist has any such guarantee in the absence of line number information.

Yet this "source-order" is used by the Java compiler, as this code does not compile:

private int a = 10 * b;
private int b = 5;

Clearly, the value of b isn't known at the moment a is being declared.

This initialization order must also be present at runtime, for the same reason. This leads me to think that the source order must somehow be available inside the .class file.

So how does the JVM go about initializing member fields in declared order, and can this information perhaps be used to reconstruct source-order of fields?

For your information, I need the source order to reconstruct behavior of a legacy language where order was important. I don't like adding order explicitly e.g. by adding arrays or annotations, as I want to keep the source as readable as possible.





Aucun commentaire:

Enregistrer un commentaire