Search code examples
javacode-analysischeckstyleeclipse-jdt

CheckStyle API usage without Check Rules


Anyone knows how to use Checkstyle API in standalone Java project to access information about AST or Cyclomatic number about a java file or project?

Because what I found are all about using it as a whole and defined rules for evaluation, but I only need to retrieve the statistical information programmatically without using evaluation. I know that using JDT AST is OK for this, but it doesn't provide those functions for calculating some metrics.


Solution

  • Well ... Checkstyle isn't really meant to be a toolkit for building third-party static code analysis tools. I would suggest taking a look at the ANTLR project instead. It's a really good Java parser which may already do what you need.

    Checkstyle uses ANTLR under the hood. You can take a look at how they do it by downloading the Checkstyle sources. This also reveals the logic behind their cyclomatic complexity implementation.

    However, it is generally possible to use Checkstyle as a framework for something not Checkstyle. You could do that by writing a Checkstyle check, which basically means writing a subclass of Check. Then you can configure Checkstyle to run your class as explained in the link.
    If you want to simply gather metrics generated by Checkstyle to use in your own application on top of Checkstyle, then I would suggest you run Checkstyle normally and evaluate its report, e.g. by implementing a listener or by parsing Checkstyle's XML output created by calling it with the -f xml command line option.

    P.S. From your reference to JDT AST I am assuming you are using Java. If so, you may want to add the tag to your question to get more answers.