gpt4 book ai didi

java - 在 checkstyle 中运行自定义检查时需要帮助

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:20 25 4
gpt4 key购买 nike

我想在 java 程序 samples/Test.java 上运行以 checkstyle 实现的简单自定义检查。我遇到了以下错误。

com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Unable to instantiate 'com.checkstyle.checks.IllegalExceptionThrowsCheck' class, it is also not possible to instantiate it as null.

请原谅,尽管这个错误是有道理的,但我无法在修复它方面取得任何进展。我在帖子末尾发布了完整的错误。下面是自定义检查的实现。 IllegalExceptionThrowsCheck.java

package com.checkstyle.checks;

import java.util.Arrays;
import java.util.List;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public final class IllegalExceptionThrowsCheck extends Check {

List<String> illegalExceptionThrows;

/** {@inheritDoc} */
public int[] getDefaultTokens() {
return new int[] { TokenTypes.LITERAL_THROWS };
}

/** {@inheritDoc} */
public int[] getRequiredTokens() {
return getDefaultTokens();
}

/** {@inheritDoc} */
public void visitToken(DetailAST aDetailAST) {
DetailAST identDetailAST = aDetailAST.findFirstToken(TokenTypes.IDENT);
if (identDetailAST == null) {
return;
}
if (illegalExceptionThrows.contains(identDetailAST.getText())) {
log(aDetailAST, "Illegal Throws Clause -> "
+ identDetailAST.getText());
}
}

public void setIllegalExceptionThrows(String illegalExceptionThrowsStr) {
this.illegalExceptionThrows = Arrays.asList(illegalExceptionThrowsStr
.split(","));
}
}

And the checkstyle configuration is custom-checkstyle.xml

   <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<property name="severity" value="error" />
<module name="TreeWalker">
<property name="severity" value="error" />
<module name="com.checkstyle.checks.IllegalExceptionThrowsCheck">
<property name="severity" value="error" />
<property name="illegalExceptionThrows" value="Exception" />
</module>
</module>
</module>

The package structure is

Project
|
|->src
| |
| |->com
| |
| |->checkstyle
| |
| |-> checks
| |
| |-> IllegalExceptionThrowsCheck.java
|
|
|-> lib
| |
| |->checkstyle-8.10.1-all.jar
|
|
|-> Samples
|
| -> Test.java

The command I ran to execute the check on java program was

java -jar lib/checkstyle-8.10.1-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml samples/Test.java

任何评论都会非常有帮助。

APPENDIX

完整的错误日志

com.puppycrawl.tools.checkstyle.api.CheckstyleException:无法初始化模块 TreeWalker - 无法实例化“com.checkstyle.checks.IllegalExceptionThrowsCheck”类,也无法将其实例化为 null。请重新检查类名是否指定为规范名称或阅读如何配置短名称使用 http://checkstyle.sourceforge.net/config.html#Packages .还请重新检查提供给 Checker 的 ClassLoader 是否配置正确。 在 com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:460) 在 com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:198) 在 com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:550) 在 com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:465) 在 com.puppycrawl.tools.checkstyle.Main.main(Main.java:219)Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: 无法实例化 'com.checkstyle.checks.IllegalExceptionThrowsCheck' 类,也无法将其实例化为 null。请重新检查类名是否指定为规范名称或阅读如何配置短名称使用 http://checkstyle.sourceforge.net/config.html#Packages .还请重新检查提供给 Checker 的 ClassLoader 是否配置正确。 在 com.puppycrawl.tools.checkstyle.PackageObjectFactory.createModule(PackageObjectFactory.java:208) 在 com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:151) 在 com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:198) 在 com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:455) ... 还有 4 个Checkstyle 以 1 个错误结束。

最佳答案

class IllegalExceptionThrowsCheck extends Check {

首先,基类不再是Check。它是 AbstractCheckCheck 不久前被删除了,你的 CLI 命令说你正在使用 8.10.1,所以我不知道你是如何得到这个来检查编译一个不存在的基类 检查

java -jar lib/checkstyle-8.10.1-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml samples/Test.java

其次,由于您没有在此命令行中提及您的项目/jar,checkstyle 将永远找不到您的类。您只告诉 JVM 关于 checkstyle-8.10.1-all.jar 而不是关于您的项目及其类文件。

我建议将您的项目打包到一个 jar 中,然后将其包含在您的命令行中。示例:java -classpath MyCustom.jar;checkstyle-8.10.1-all.jar com.puppycrawl.tools.checkstyle.Main -c config.xml Check.java
http://checkstyle.sourceforge.net/cmdline.html#Usage_by_Classpath_update

第三,您可以将您的 setter 更改为:

public void setIllegalExceptionThrows(String... illegalExceptionThrowsStr) {
this.illegalExceptionThrows = Arrays.asList(illegalExceptionThrowsStr);
}

关于java - 在 checkstyle 中运行自定义检查时需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50671790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com