Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get declared methods in order they appear in source code

The situation seems to be abnormal, but I was asked to build serializer that will parse an object into string by concatenating results of "get" methods. The values should appear in the same order as their "get" equivalent is declared in source code file.

So, for example, we have

 Class testBean1{
  public String getValue1(){
   return "value1";
  }

  public String getValue2(){
   return "value2";
  }
 }

The result should be:

"value1 - value2"

An not

"value2 - value1"

It can't be done with Class object according to the documentation. But I wonder if I can find this information in "*.class" file or is it lost? If such data exists, maybe, someone knows a ready to use tool for that purpose? If such information can't be found, please, suggest the most professional way of achieving the goal. I thought about adding some kind of custom annotations to the getters of the class that should be serialized.

like image 829
Denis Avatar asked Jun 30 '10 10:06

Denis


1 Answers

If you want that you have to parse the source code, not the byte code.

There are a number of libraries that parse a source file into a node tree, my favorite is the javaparser (hosted at code.google.com), which, in a slightly modified version, is also used by spring roo.

On the usage page you can find some samples. Basically you will want to use a Visitor that listens for MethodDefinitions.

like image 138
Sean Patrick Floyd Avatar answered Oct 26 '22 07:10

Sean Patrick Floyd