Question

I am trying to write a program to analyze Java source code, for example, checking all the callers to some certain method. Since eclipse IDE provides this kind of feature, which is powerful and impressive, I am wondering if they provide some APIs so I can use those features in my program as well.

I checked some materials online. But nearly all of the documents I can find are related with eclipse plug-in development, which is not what I want. I want to use them in a stand-alone program (maybe as part of a compilation chain to do customized style checking).

Is this possible? If so, any link as entrypoint for me to start research would be very appreciated. If not, is there any alternative that I can try? (I used to think about using ANTLR, but it is merely a parser that is quite a few steps away from a source code analyzer)

Was it helpful?

Solution

I don't know about Eclipse itself, but here are some alternatives:

First, the relatively lightweight:

  • Findbugs. Relatively straightforward to write your own analyses if you're a decent Java programmer.
  • PMD. I haven't used it myself, but looks straightforward to write a Java analysis plus - added bonus - they support XPath-style queries of source-code ASTs.

These might be overkill, but the heavy guns for Java analysis are Soot from McGill U. and WALA from IBM.

OTHER TIPS

If you decide you like JDT, then it's not (terribly) difficult to develop an Eclipse RCP application that would only use the base Eclipse stuff with JDT and function as a standard command line application. There are a number of tutorials and papers on Eclipse RCP.

As an alterantive to Eclipse/ANTLR, see our DMS Software Reengineering Toolkit with its Java Front End. DMS provides general purpose parsing/tree building/pattern matching/source rewriting capabilities; the front end customizes DMS to know language details such as grammar, symbol tables, and flow analysis (for Java, presently limited to methods).

If you want to do "real" static analysis, I'd recommend going for one of the existing frameworks like Soot or Chord. The analysis you need is most likely already implemented there (although those tools usually prefer byte-code). On the other hand, especially the "find all callers"-analysis already exists within Eclipse; you can start looking from the CallHierarchyViewPart.

If you want to create a simple standalone program to do static code analysis then you seem to be on right path to use ANTLR.

Checkstyle also uses ANTLR to perform static code analysis. ANTLR will help you create abstract syntax tree (AST). But then you will have to write code to do different types of checks that you want. You can use 'walk' pattern of AST prepared by ANTLR to visit all tokens & then have a logic to execute on it & store or print the results. To get started, here is one very simple standalone example to perform class name check for abstract classes to make sure it starts with 'Abstract'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top