简体   繁体   English

有没有一种方法可以从Java程序中调用checkstyle?

[英]Is there a way to call checkstyle from within a Java program?

I have a Java program that creates a list of filepaths I want to run checkstyle on. 我有一个Java程序,该程序创建要在其上运行checkstyle的文件路径的列表。 I was curious if there is a way to directly run checkstyle from within my program or whether I will need to shell out to the CLI. 我很好奇,是否有一种方法可以直接从程序内部运行checkstyle,或者是否需要使用CLI。

Yes, that is possible, although it's not so much a documented API, but rather a set of calls that works, and that's kept reasonably stable. 是的,这是有可能的,尽管它不是一个文档化的API,而是一组有效的调用,并且保持了合理的稳定性。 In fact, IDE plugins go that way. 实际上,IDE插件就是这样。

For example, the Checkstyle plugin for IntelliJ has some code you can look at to get an idea: 例如,用于IntelliJ的Checkstyle插件提供了一些代码,您可以查看这些代码以了解概念:
https://github.com/jshiell/checkstyle-idea/tree/5.26.0/src/csaccess/java/org/infernus/idea/checkstyle/service/cmd https://github.com/jshiell/checkstyle-idea/tree/5.26.0/src/csaccess/java/org/infernus/idea/checkstyle/service/cmd

It may, however, be easier to just call Checkstyle as a command-line program (via zt-exec , for example) and parse its XML report. 但是,将Checkstyle作为命令行程序调用(例如,通过zt-exec )并解析其XML报告可能会更容易。 That is if you don't need to have the direct feedback provided via in-process AuditListeners . 那就是如果您不需要通过进程内AuditListeners提供的直接反馈。

I have a Java program 我有一个Java程序
is a way to directly run checkstyle from within my program 是一种直接在我的程序中运行checkstyle的方法

You can call any Java program from within another Java program. 您可以从另一个Java程序中调用任何Java程序。 When a Java program is called from the command line, it's main method is called that passes in all the command line parameters that is not for the java program itself. 从命令行调用Java程序时,将调用它的main方法,该方法传入所有非java程序本身的命令行参数。 All you need to do in your Java program is call the same main that the command line calls. 您在Java程序中所需要做的就是调用与命令行调用相同的main For checkstyle this is com.puppycrawl.tools.checkstyle.Main . 对于checkstyle,这是com.puppycrawl.tools.checkstyle.Main See https://github.com/checkstyle/checkstyle/blob/bd7621fae3b1b887d46b8a678600db7e6d03185c/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L100 . 参见https://github.com/checkstyle/checkstyle/blob/bd7621fae3b1b887d46b8a678600db7e6d03185c/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L100

The downside to checkstyle however is it calls System.exit when it is done so you will never return from your call. Checkstyle的缺点是完成后会调用System.exit ,因此您永远不会从调用中返回。 To prevent a System.exit from terminating the JVM completely see Java: How to test methods that call System.exit()? 要防止System.exit完全终止JVM,请参阅Java:如何测试调用System.exit()的方法? for the SecurityManager example. 对于SecurityManager示例。

You can avoid all this System.exit business but it would require you to duplicate a bunch of Checkstyle's code, which is also in the Main class. 您可以避免所有这些System.exit业务,但需要您复制一堆Checkstyle的代码,这些代码也在Main类中。 See https://github.com/checkstyle/checkstyle/blob/bd7621fae3b1b887d46b8a678600db7e6d03185c/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L332 . 参见https://github.com/checkstyle/checkstyle/blob/bd7621fae3b1b887d46b8a678600db7e6d03185c/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L332 It is up to you how you want to handle it. 取决于您如何处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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