• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Reflection...Incorrect sequence of methods returned.  RSS feed

 
Ishu Saxena
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers

I have been using the



to retrieve the list of test methods from a class.

The list of these methods is displayed in a tree structure as tree nodes.
This is where the problem begins.The sequence of methods appearing in the tree structure should be the same as the sequence in which the methods are implemented in the test class and all its superclasses.

But as per the API , the getMethods() returns methods in no particular order . ie. the methods list may not be sorted in the order in which they appear in the test class.

To maintained the sequence of the methods, i parsed the source files and all its superclasses(which are packed into zip files) and retrieved the list of test methods and rearranged the list returned by getMethods() in the same order...
So far so good, the problem got sorted out.

Now we have another scenario wherein, we have only the class files(bundled up into a jar).
Is there any workaround through which i can get the correct sequence of methods in the file(considering we only have access to the class files(from the jar)and not the source files), to be able to rearrange the list returned by getMethods().

PS : Moderators please move this to the appropriate section if this thread does not belong here..

Thank You..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried an experiment with a small program:

I compiled this, then saved the class file, swapped the firstMethod and secondMethod, and compiled it again.

I looked at the two class files with a hex editor and I see that the methods are swapped in both class files. So, the good news is that the compiler does seem to preserve the order in which the methods were defined. (I did this with JDK 1.6.0_06 on Windows XP; there's no guarantee that other Java compilers do the same!).

If you really want to know the order, you'll need to examine the class files. You could use a library such as BCEL to do that.
 
Ishu Saxena
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper..Thanks for your reply

Yes you are right..In most cases, the compiler does seem to preserve the order of methods...Even i did a test program to verify this.
Although somehow, in some classes in the application,the complier would completely ignore the sequence of method declaration.
Moreover the same code executed on different machines would produce entirely different results.

Back to the original problem , the determination of the original sequence of test methods in the class files(bundled into a jar) needs to be done at runtime only, so i guess using BCEL would not be an option for me (?).
Is there is workaround for this ?

Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really quite odd. This question was first posted more than 24 hours ago, and I responded to it at that time; when I pressed "Add Reply", though, I got an error because the thread was gone. Now, a day later, it was posted again. I hope that this time my answer won't be discarded.

There's no guarantee at all that the methods in a class file will be in any particular order, and in fact, many class files will contain methods that don't appear as methods in the source at all, like default constructors, static initializers, access methods, etc. So this isn't a crisply defined problem.

But if you use BCEL or something like it, then you can get enough information to answer your questions exactly. First of all, methods that don't appear in the source will have a "Synthetic" attribute set, so you can identify them. Second, class files will most of the time contain a LineNumberTable attribute for each non-synthetic method. The LineNumberTable attribute tells you which source-file line numbers correspond to each compiled method, and therefore you can sort the methods according to their LineNumberTables. This attribute is mainly used by debuggers, but this is another possible use.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!