简体   繁体   中英

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
}

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.

I tried doing something similar about a year ago and couldn't find any good way to avoid temporary files. So I end up using "git cat-file --batch" via Stash CommandBuilderFactory() to generate temporary files and then feed them into Checkstyle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM