下面我们开始从启动探寻 PMD 的源码设计。
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";
@Override
public 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 通过注解和继承,注册到 pico 的命令集中。
调用逻辑如下:
cli -> command -> PmdAnalysis -> pmd.runAndReturnStats() -> performAnalysis() -> performAnalysisImpl() -> launchAnalysis(analysisTask)) -> net.sourceforge.pmd.lang.impl.MultiThreadProcessor#processFiles -> net.sourceforge.pmd.lang.impl.PmdRunnable#processSource
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);
}
规则的调用核心: 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);
}
}
}