PMD在java中的文档空构造函数

San*_*kar 2 java coding-style pmd code-standards

我在我的 Java 项目中使用 PMD 插件。

当我运行 PMD 时,它将警告显示为“文档空构造函数”。

我的代码如下...

public class ExceptionHandlerImpl implements ExceptionHandler {

    private static final Logger log = Logger
            .getLogger(ExceptionHandlerImpl.class);

    /**
     * Default Constructor
     */
    public ExceptionHandlerImpl()
    {
        super();
    }
Run Code Online (Sandbox Code Playgroud)

在上面的构造函数代码中,它显示了“文档空构造函数”。

我如何解决这个问题以及为什么会发生这种情况?

Mic*_*das 5

在您的情况下,触发UncommentedEmptyConstructor规则。

它查找非私有构造函数不包含任何语句的地方,或者它只包含super()其中没有注释的地方。之前的 Javadoc 与此规则无关。

通过在空构造函数中提供注释,可以更容易地区分有意的(例如能够提供 Javadoc)和无意的空构造函数(有人忘记编写实现或者可以删除此构造函数)。

这条规则对你有这样的期望:

class MyConstructorIsNeededHere { 
  /** 
   * Creates instance of {@link Foo}.
   */ 
  MyConstructorIsNeededHere() { 
    // The explicit constructor is here, so that it is possible to provide Javadoc. 
  } 
} 
Run Code Online (Sandbox Code Playgroud)

并在以下情况下警告您:

class OhNoICanBeRemoved { 
  OhNoICanBeRemoved() { 
    super();
  } 
} 
Run Code Online (Sandbox Code Playgroud)