1

Since java screw over in terms of ordering methods and fields, I want to determine the exact order they are declared in.

Since Java 6 (or so) the JVM usually returns the members in random order, I looked at Javassist, ASM and Bcel. Bcel does not support Java 8, Javassist is a pain since it seems to not value the right ordering between methods and fields (it uses fields first, methods later even if fields are in between methods).

So ASM seams to be the best bet but it has only string representation, which is a pain.

Does anyone knows a better way?

[Update]

The idea is to use annotations for the current method to bind content for the former method. I write a precompiler for C/ASM and thats how it should look like:

public class Test {
    public native int add(int a, int b);
    @C("return a + b;")

    public native int sub(int a, int b); @C({
       "int result = a - b;",
       "return result;"
    })

    public native int power2(int a); @ASM({
         "mov eax, a",
         "shl eax, 1"
    })

    private boolean eof;
}

As you can see the idea was to use a final field eof to bind the annotation of the last method to it. Well it seams I will make the eof field mandatory and use a convention on that.

[update2] I just updated the code to correct for and also show alternative formatting.

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77
  • 10
    Why do you want to do this? Depending on the ordering seems dangerous. – Necreaux Apr 15 '15 at 20:37
  • Is this what you want? http://stackoverflow.com/questions/2126714/java-get-all-variable-names-in-a-class – Shar1er80 Apr 15 '15 at 20:39
  • 5
    This sounds like an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Can you please tell us your overall aims? – christopher Apr 15 '15 at 20:43
  • Its not dangerous and problematic. I will update the question to make it more clear. – Martin Kersten Apr 15 '15 at 20:59
  • 1
    I am not quite sure what your idea about annotation is. Annotations are bound to a class, a method, a member or a parameter (I might be forgetting some cases here), so in your example it looks like the first annotation is bound to `sub`, the second to `power2` and the third one to `eof`. I seem to understand that that's not what you want. – Giovanni Botta Apr 15 '15 at 21:11
  • 1
    To expand on @GiovanniBotta's comment - whatever it is you're trying to do can almost certainly be done by using `java.lang.reflect.Method.getAnnotations` to find the annotations for a particular method, if you move your annotations so that they're before the method they apply to. – Sbodd Apr 15 '15 at 21:34
  • And since an Annotation must be before a method you our out of luck for my scenario. The only other chance is to use source code which is even harder. So I just simplify it. – Martin Kersten Apr 16 '15 at 04:54
  • I don't understand what difference it makes if your annotation is before the method instead of after? You need the annotation to be tied to the method right? Why does it matter where it is in the source code? – Giovanni Botta Apr 22 '15 at 14:40
  • @Giovanni I am still puzzled how it is impossible to understand the difference by looking at the example code. Please just move the @C annotations above the methods and you will see it feels crazy and weird. That's why. – Martin Kersten Apr 22 '15 at 15:19
  • It would be a lot "crazier and weirder" if adding annotations below a method/field was allowed. The annotations are as close as possible to the method signature. If the method had a body (which yours don't), it would be very confusing to see annotation at the bottom of a (possibly long) method body. I honestly think you are nitpicking a little here... – Giovanni Botta Apr 22 '15 at 15:26
  • As I said, it is the best solution I can think of and it feels almost natural this way. Sure if the method has a body... Best would be to have a comment that is embedded in the class file triggered by marker annotation for the class. Something like /** */ after a method but recognized in the class. I did the same thing for GWT and works also flawless. In GWT one needs to have access to the java source but since this should compile on the server, I think I settle with the current solution. It is better this way. – Martin Kersten Apr 23 '15 at 00:02

1 Answers1

0

Well the big problem is solved. Both APIs (ASM and Javassist) do not support getting the correct ordering of fields and(!) methods since - well - the class file format does not support this. It just has a collection of fields and a collection of methods. (https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html)

This kills the idea I had in mind about using annotations in a particular way.

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77