View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
8   import com.puppycrawl.tools.checkstyle.api.DetailAST;
9   import com.puppycrawl.tools.checkstyle.api.FileContents;
10  import com.puppycrawl.tools.checkstyle.api.TextBlock;
11  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
12  import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
13  
14  /**
15   * Checks that there is no Javadoc for inherited methods.
16   * Users may have a different understanding of your method
17   * based on whether they examine the method in the supertype
18   * or the subtype and it may cause confusion.
19   * @since 0.16
20   */
21  public final class NoJavadocForOverriddenMethodsCheck extends AbstractCheck {
22  
23      @Override
24      public int[] getDefaultTokens() {
25          return new int[] {
26              TokenTypes.METHOD_DEF,
27          };
28      }
29  
30      @Override
31      public int[] getAcceptableTokens() {
32          return this.getDefaultTokens();
33      }
34  
35      @Override
36      public int[] getRequiredTokens() {
37          return this.getDefaultTokens();
38      }
39  
40      @Override
41      @SuppressWarnings("deprecation")
42      public void visitToken(final DetailAST ast) {
43          if (AnnotationUtil.containsAnnotation(ast, "Override")) {
44              final FileContents contents = getFileContents();
45              final TextBlock javadoc = contents.getJavadocBefore(
46                  ast.getLineNo()
47              );
48              if (javadoc != null) {
49                  log(ast, "Overridden methods should not have Javadoc");
50              }
51          }
52      }
53  }