title: Java Reflection: Getting fields and methods in declaration order
body: <p>Is there any way to get a classes declared fields (and methods) in the order of declaration using reflection? According to the documentation, the ordering of Methods and Fields returned by <code>getFields()</code>, <code>getDeclaredFields()</code>, etc. is undefined.</p>
  
  <p>Specifying something like an index would be possible using annotation as suggested in <a href="https://stackoverflow.com/questions/1097807/java-reflection-is-the-order-of-class-fields-and-methods-standardized">Java reflection: Is the order of class fields and methods standardized?</a></p>
  
  <p>Are there any better options, i.e. not having to specify the index manually?</p>
  
  <p>Now before you ask what I need this for: we have a method that takes a quite big data structure as input and performs a lengthy calculation on it. To create unit tests, we made a method that takes an input object and an output instance and creates the Java source code (setting up input, invoking the calculation method, and asserting the correct results afterwards) as output. This code is much more readable when fields are written in declaration order.</p>