如何启用 checkstyle 的 MissingOverride 检查?

How to enable checkstyle's MissingOverride check?

我有一个

public interface Interface0 {

    void method0();
}

还有一个

public class Implementation0 implements Interface0 {

    public void method0() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

并且我通过声明

来使用maven-checkstyle-plugin
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>checkstyle</id>
            <phase>validate</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <configLocation>${basedir}/check_style.xml</configLocation>
    </configuration>
</plugin>

最后我在 check_style.xml 中激活 MissingOverride:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
   "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
   "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="MissingOverride"/>
    </module>
</module>

这是我在许多项目中的经验摘录,所有检查都工作正常,除了 MissingOverride 没有效果,即没有检测到单个缺失的 @Override 注释。

MissingOverride 模块的文档指出:

Verifies that the java.lang.Override annotation is present when the {@inheritDoc} javadoc tag is present.

Documentation link

换句话说,它不打算检查您希望它检查的内容 - 它仅检查 Javadoc 注释中 {@inheritDoc} 标记的使用是否与 @Override 注释配对。

如果您阅读 MissingOverride 的 Checkstyle documentation,它指出您还需要 {@inheritDoc} javadoc 标记才能使其工作,但它只会检查它与其他注释配对。

试试看。它对我有用:)

尽管如此,忘记包括那将与您原来的问题相同。