View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 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   *
20   * @since 0.16
21   */
22  public final class NoJavadocForOverriddenMethodsCheck extends AbstractCheck {
23  
24      @Override
25      public int[] getDefaultTokens() {
26          return new int[] {
27              TokenTypes.METHOD_DEF,
28          };
29      }
30  
31      @Override
32      public int[] getAcceptableTokens() {
33          return this.getDefaultTokens();
34      }
35  
36      @Override
37      public int[] getRequiredTokens() {
38          return this.getDefaultTokens();
39      }
40  
41      @Override
42      @SuppressWarnings("deprecation")
43      public void visitToken(final DetailAST ast) {
44          if (AnnotationUtil.containsAnnotation(ast, "Override")) {
45              final FileContents contents = getFileContents();
46              final TextBlock javadoc = contents.getJavadocBefore(
47                  ast.getLineNo()
48              );
49              if (javadoc != null) {
50                  log(ast, "Overridden methods should not have Javadoc");
51              }
52          }
53      }
54  }