Home > Mobile >  Parsing .java file to extract all features
Parsing .java file to extract all features

Time:02-19

I need to parse .java file to do static analyze and extract information from the file like:

  1. variables
  2. methods
  3. annotations
  4. inner classes
  5. ...

I need to parse It at runtime something like this:

JavaClass c = parse("file.java");
c.getMethods();

I am not sure if there is any tool already exists, and if not can you please provide with some advice how to build it.

CodePudding user response:

You definitely need to use a good parser for that.

ANTLR is a parser generator that comes with a huge library of ready to use grammars, one of which is Java. With that one it is easy to transform a java source file into an abstract syntax tree. On that tree do whatever analysis or transformation you need.

CodePudding user response:

Have a look to the JavaDoc API. It provides everything you need, and you do not even need to write a parser of your own.

Ok, in the beginning it is far from being intuitive, but the good thing is, it works along the same line as an AnnotationProcessor, so you learn two things in one go.

  • Related