Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Checkstyle, Findbugs, or PMD programmatically on file *content*

I'm writing a utility which checks Atlassian Stash pull requests for modified files - the goal is to run each Java file through Checkstyle (or Findbugs, or PMD), and then have the plugin comment on each line with problems.

To accomplish this, the plugin must run the contents of each modified Java file in the request through Checkstyle (or other code style enforcement utility). The Atlassian Stash API makes it quite easy to get the contents of modified files, but it doesn't appear possible to run those contents through Checkstyle programmatically - the only way to do it would be to save a temp file on disk and run the Checkstyle jar against it by calling a Runtime.getRuntime().exec(...) command.

Are there any simplified Checkstyle-like utilities which can be run programmatically using a Java API?

I essentially need something that can do something like this:

String contentsOfJavaFile = ; //stuff
List<Problem> problems = CheckstyleOrSomethingElse.analyze(contentsOfJavaFile);

for(Problem p : problems) {
    // p.getLine();
    // p.getDescription();
    // add comment to Stash
}
like image 840
Mike Cialowicz Avatar asked Jul 01 '14 01:07

Mike Cialowicz


1 Answers

This is probably only a partial answer to your question, but it may still save others some time:

I have looked at this problem for Checkstyle. Checkstyle (as of the 5.7 sources) does not offer a way to analyze anything other than a file. The only relevant Checker method is int process(List<File> aFiles). I also looked at the Eclipse-CS and Checkstyle-IDEA plugins to see how they handle this. Both go through (temp) files.

So you will have to create a temp file if your tool is to be Checkstyle.

Let me add (even if you didn't ask for this explicitly) that I believe you can only seriously pursue the "free, individual tools" approach if it includes Checkstyle. Using only FindBugs and PMD without Checkstyle will probably not suffice (and they might also require temp files). So, you can either create the temp files, or go with a completely different solution such as the free SonarQube or a commercial solution that integrates with your Atlassian tool suite.

like image 102
barfuin Avatar answered Oct 12 '22 00:10

barfuin