java - A guaranteed way to get source-order of member fields at runtime? -
i'm looking way retrieve (at runtime) fields of class in source-order, can perform own "initialization processing" based on order of declaration. know javadoc class.getdeclaredfields()
explicitly states no order guaranteed.
some answers on point javassist
can find no evidence javassist
has such guarantee in absence of line number information.
yet "source-order" used java compiler, code not compile:
private int = 10 * b; private int b = 5;
clearly, value of b
isn't known @ moment a
being declared.
this initialization order must present in bytecode since @ runtime initialization must happen in same order (granted, requirement these edge cases :-( yet leads me think natural thing store source order inside .class
file.
questions:
how jvm/byte code go initializing member fields in declared order, , can information perhaps used reconstruct source-order of fields?
is there other guaranteed way of achieving same. third-party tools javassist ok must "guaranteed" or @ least "guaranteed under specific conditions".
is there specific java implementation guarantee order on
class.getdeclaredfields()
(perhaps under specific conditions (which ones))?
for information, need source order reconstruct behavior of legacy language order important. don't adding order explicitly e.g. adding arrays or annotations, want keep source readable possible.
-- edit -- important note may fields need "traverse" annotated, e.g. @mycomplextype(len = 4)
. parent class need meta-information construct kind of memory map. yet don't want clutter annotation ordering information, find hinders readability , maintainability.
concerning second , third question, possible retrieve fields in order using kind of dirty hack:
in bytecode, fields of class file not stored in order, , neither methods. don't know why case (even though made own jvm compiler), believe java compiler decides that. class.getdeclaredfields
returns fields in order in read bytecode, why states no order guaranteed.
if still want them in order, try following: use bytecode parser library such javassist or asm read class file, , skip constructors (and static {}
if want sort static fields). encounter putfield
or putstatic
instruction owner
class inspecting, current line available through debug information stored in bytecode, , use sort fields. problem technique inefficiency , fact relies on line number attributes, not present in class files. furthermore, find put*
instructions fields explicitly initialized, default ones such as
protected int modifiers;
are not initialized compiler, no instruction , no line number information available in bytecode. in case or when there no linenumber attributes in general, unfortunately out of luck. @ point, solution come read actual source code of class.
depending on class trying inspect, might have trouble getting actual bytecode of class, question in , on itself.
Comments
Post a Comment