0

I am trying to write a simple code analyzer / metadata extractor in Java. For the beginning, all I want to do is extract and differentiate instance and local vars, and methods and their signatures and associate local vars to methods they belong to in much the same way Eclipse displays them in its Outline view. Of course, this is possible to do by simple text parsing but that is obviously an extremely onerous and tedious method, definitely reinventing the wheel. Since the compiler already parses the source code, I would like to leverage its functionality to extract the metadata in a clean, easy, and elegant way.

Any idea which API to use to accomplish this? Can anyone point to an API within Eclipse that I can use to do this (as I am sure it exists)?

Thanks

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • 3
    You may want to consider compiling the source programmatically using [JavaCompiler](http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html), that way you'd have access to all the data the virtual machine would have. See [this answer](http://stackoverflow.com/a/7989365/477453) for pointers, it's great stuff. – SáT Jan 30 '13 at 23:19

3 Answers3

1

try it: http://www.steike.com/code/java-reverse-engineering/

You can use reflection, which is a bit tricky

User user = new User();
Field[] fields = user.getClass().getDeclaredFields();
Edgard Leal
  • 2,592
  • 26
  • 30
1

Eclipse ASTParser seems like a good place to start.

jdb
  • 4,419
  • 21
  • 21
1

If you are using javac, you should be able to do all that you want with a combination of the Compiler API, the Compiler Tree API, and the Language Model API. See http://docs.oracle.com/javase/7/docs/technotes/guides/javac/

Use the Compiler API to invoke javac. Use the Compiler Tree API to examine the ASTs Use the Language Model API to look at the semantics of the tree.