1

I'm doing an experiment and I have to map all the classes and its attributes and methods to their respective code in Java. For now I'd like to know what's the best approach to parse a .java file and find which classes there are in those files, as well as their attributes and methods. I've seen in some other topics that building my own lexical analyser is not a good idea, as there're some libraries that already do this hard work. I found that it's possible to do this with files which are already included in the project just by calling some methods in Java, but not from external .java files.

Can anyone light me up? Thanks in advance

Marcelo Noguti
  • 830
  • 1
  • 9
  • 17
  • 1
    You can use javaparser: http://code.google.com/p/javaparser/ – Eldar Agalarov Aug 13 '13 at 00:03
  • Note that javaparser only supports Java 5. From the above link's accepted answer: "Since Java 6, the compiler has an API included in the JDK. Through it you can access the results of the Java parser through the javax.lang.model APIs." – Jason C Aug 13 '13 at 00:14
  • as @Jason mentioned, javaparser only supports Java 5, I just gave it a try and doesn't seem to work properly. On the other hand javax.lang.model seems to be what I'm looking for. I'll try this one. Thanks for helping me with my kick off. If you could put that as an answer I'd approve it – Marcelo Noguti Aug 13 '13 at 00:38
  • It's not my answer. :) You could upvote the accepted answer on that other thread if you'd like. – Jason C Aug 13 '13 at 00:50

1 Answers1

1

You can use source code scan tool like PMD, PMD checks source code against rules and produces a report. Like this:

  • Something passes a file name and a RuleSet into PMD
  • PMD hands an InputStream to the file off to a JavaCC-generated parser
  • PMD gets a reference to an Abstract Syntax Tree back from the parser
  • PMD hands the AST off to the symbol table layer which builds scopes, finds declarations, and find usages.
  • If any rules need data flow analysis, PMD hands the AST over to the DFA layer for building control flow graphs and data flow nodes.
  • Each Rule in the RuleSet gets to traverse the AST and check for problems. The rules can also poke around the symbol table and DFA nodes.
  • The Report is now filled with RuleViolations, and those get printed out in XML or HTML or whatever
smilingleo
  • 121
  • 4
  • The Java compiler's parsing and code analysis tools are packaged with the JDK; see https://today.java.net/pub/a/today/2008/04/10/source-code-analysis-using-java-6-compiler-apis.html (old but applicable and a good starting point) -- from the question I linked to in the comments. – Jason C Aug 13 '13 at 00:19