6

我正在编写一个实用程序来检查修改后的文件的Atlassian Stash拉取请求 - 目标是通过 Checkstyle(或 Findbugs 或 PMD)运行每个 Java 文件,然后在有问题的每一行上添加插件注释。

为此,插件必须通过 Checkstyle(或其他代码样式强制实用程序)运行请求中每个已修改Java 文件的内容。Atlassian Stash API 使得获取修改文件的内容变得非常容易,但似乎无法通过 Checkstyle 以编程方式运行这些内容 - 唯一的方法是将临时文件保存在磁盘上并运行 Checkstyle通过调用Runtime.getRuntime().exec(...)命令来对抗它。

是否有任何简化的类似 Checkstyle 的实用程序可以使用 Java API 以编程方式运行?

我基本上需要一些可以做这样的事情:

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

for(Problem p : problems) {
    // p.getLine();
    // p.getDescription();
    // add comment to Stash
}
4

2 回答 2

2

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.

于 2014-07-01T20:10:15.060 回答
1

大约一年前,我尝试做类似的事情,但找不到任何避免临时文件的好方法。所以我最终通过 Stash CommandBuilderFactory() 使用“git cat-file --batch”来生成临时文件,然后将它们输入 Checkstyle。

于 2014-07-02T04:58:32.883 回答