首页 > 解决方案 > 如何启用 checkstyle 的 MissingOverride 检查?

问题描述

我有一个

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>

最后我正在MissingOverride激活check_style.xml

<?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注释。

标签: javamavencheckstylemaven-checkstyle-plugin

解决方案


MissingOverride模块的文档指出:

当存在 {@inheritDoc} javadoc 标记时,验证 java.lang.Override 批注是否存在。

文档链接

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


推荐阅读