文章目录
- cli
- Command
- parser
- rule
下面我们开始从启动探寻 PMD 的源码设计。

cli
pmd 的启动类为 PmdCli,作为命令行的启动器, 其依赖 picocli 作为控制台命令框架。
picocli 官网:https://picocli.info/
@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {@Parameters(index = "0", description = "The file whose checksum to calculate.")private File file;@Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")private String algorithm = "SHA-256";@Overridepublic Integer call() throws Exception { // your business logic goes here...byte[] fileContents = Files.readAllBytes(file.toPath());byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));return 0;}// this example implements Callable, so parsing, error handling and handling user// requests for usage help or version help can be done with one line of code.public static void main(String... args) {int exitCode = new CommandLine(new CheckSum()).execute(args);System.exit(exitCode);}
}
Command
几个执行命令给抽出 Command 做解耦,实现方式还是很优雅的:
而且几个 Command 通过注解和继承,注册到 pico 的命令集中。
调用逻辑如下:
cli -> command -> PmdAnalysis -> pmd.runAndReturnStats() -> performAnalysis() -> performAnalysisImpl() -> launchAnalysis(analysisTask)) -> net.sourceforge.pmd.lang.impl.MultiThreadProcessor#processFiles -> net.sourceforge.pmd.lang.impl.PmdRunnable#processSource
以check 源码为例,对 java 源码检测:
在 parseArgs 的时候,对于命令的信息就已经进入了parseResult。
随后对 parse 的命令进行处理:
方法调用到 Command 中的 execute 方法,在该方法中,通过 PmdAnalysis.create(configuration); 建立起分析策略。
之后,随着方法调用深度加强,进行语言解析器选择和 AST 相关的代码,就出现了:
parser
private void processSource(FileAnalysisListener listener,TextDocument textDocument,RuleSets ruleSets) throws FileAnalysisException {SemanticErrorReporter reporter = SemanticErrorReporter.reportToLogger(task.getMessageReporter());@SuppressWarnings("PMD.CloseResource")LanguageProcessor processor = task.getLpRegistry().getProcessor(textDocument.getLanguageVersion().getLanguage());ParserTask parserTask = new ParserTask(textDocument,reporter,task.getLpRegistry());LanguageVersionHandler handler = processor.services();// 获取解析器Parser parser = handler.getParser();// 拿到抽象语法树根RootNode rootNode = parse(parser, parserTask);SemanticException semanticError = reporter.getFirstError();if (semanticError != null) {throw semanticError;}// 应用规则集ruleSets.apply(rootNode, listener);}
解析器有这么几类:
我们关注在 java 源码,JjtreeParserAdapter [ pmd-core ] —> JavaParser [pmd-java]
而且 源码也从 core 转移到 pmd-java 上,看一下工程,可以看到 pmd-java 使用的是 SPI 的机制,并在该包下定义了规则集,实现了语言的解耦。
rule
规则的调用核心: rule.apply(node, ctx);
private void applyOnIndex(TreeIndex idx, Collection<? extends Rule> rules, FileAnalysisListener listener) {for (Rule rule : rules) {if (!ruleSetApplies(rule, currentLangVer)) {continue; // No point in even trying to apply the rule}RuleContext ctx = InternalApiBridge.createRuleContext(listener, rule);rule.start(ctx);try (TimedOperation rcto = TimeTracker.startOperation(TimedOperationCategory.RULE, rule.getName())) {int nodeCounter = 0;Iterator<? extends Node> targets = rule.getTargetSelector().getVisitedNodes(idx);while (targets.hasNext()) {Node node = targets.next();try {nodeCounter++;rule.apply(node, ctx);} catch (RuntimeException e) {reportOrRethrow(listener, rule, node, AssertionUtil.contexted(e), true);} catch (StackOverflowError e) {reportOrRethrow(listener, rule, node, AssertionUtil.contexted(e), SystemProps.isErrorRecoveryMode());} catch (AssertionError e) {reportOrRethrow(listener, rule, node, AssertionUtil.contexted(e), SystemProps.isErrorRecoveryMode());}}rcto.close(nodeCounter);} finally {rule.end(ctx);}}}