无法实例化 MatchXpath

Unable to instantiate MatchXpath

我使用 the Gradle plugin 设置了 Checkstyle。我正在尝试设置一个规则,强制在所有局部变量中使用 vars。

<module name="Checker">
        <module name="MatchXpath">
            <property name="query"
                      value="//VARIABLE_DEF[./ASSIGN/EXPR and not(./TYPE/IDENT[@text='var'])]"/>
            <message key="matchxpath.match"
                     value="Local variables should use 'var' keyword"/>
        </module>
    </module>
</module>

但是,上面的配置会抛出以下错误。所有其他规则都可以正常工作,因此它与“MatchXpath”模块有关。

Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - cannot initialize module MatchXpath - Unable to instantiate 'MatchXpath' class, it is also not possible to instantiate it as .MatchXpath, MatchXpathCheck, .MatchXpathCheck. Please recheck that class name is specified as canonical name or read how to configure short name usage https://checkstyle.org/config.html#Packages. Please also recheck that provided ClassLoader to Checker is configured correctly.
        at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:482)
        at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
        at com.puppycrawl.tools.checkstyle.ant.CheckstyleAntTask.createRootModule(CheckstyleAntTask.java:421)
        ... 149 more
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module MatchXpath - Unable to instantiate 'MatchXpath' class, it is also not possible to instantiate it as .MatchXpath, MatchXpathCheck, .MatchXpathCheck. Please recheck that class name is specified as canonical name or read how to configure short name usage https://checkstyle.org/config.html#Packages. Please also recheck that provided ClassLoader to Checker is configured correctly.
        at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:125)
        at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
        at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:477)
        ... 151 more
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to instantiate 'MatchXpath' class, it is also not possible to instantiate it as .MatchXpath, MatchXpathCheck, .MatchXpathCheck. Please recheck that class name is specified as canonical name or read how to configure short name usage https://checkstyle.org/config.html#Packages. Please also recheck that provided ClassLoader to Checker is configured correctly.
        at com.puppycrawl.tools.checkstyle.PackageObjectFactory.createModule(PackageObjectFactory.java:215)
        at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:116)
        ... 153 more

MatchXpath 与 Checkstyle 8.39 一起发布。如果您至少没有那个版本,那么您使用的版本太旧了。 https://checkstyle.org/config_coding.html#MatchXpath

您应该仔细检查您的 Checkstyle 版本运行,如果太低则升级它。

此外,您的配置缺失 TreeWalker,因此它不是完全有效的配置。配置应为:

<module name="Checker">
  <module name="TreeWalker">
        <module name="MatchXpath">
            <property name="query"
                      value="//VARIABLE_DEF[./ASSIGN/EXPR and not(./TYPE/IDENT[@text='var'])]"/>
            <message key="matchxpath.match"
                     value="Local variables should use 'var' keyword"/>
        </module>
  </module>
</module>