View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle;
32  
33  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
34  import com.puppycrawl.tools.checkstyle.api.DetailAST;
35  import com.puppycrawl.tools.checkstyle.api.FileContents;
36  import com.puppycrawl.tools.checkstyle.api.TextBlock;
37  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38  import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
39  
40  /**
41   * Checks that there is no Javadoc for inherited methods.
42   * Users may have a different understanding of your method
43   * based on whether they examine the method in the supertype
44   * or the subtype and it may cause confusion.
45   *
46   * @since 0.16
47   */
48  public final class NoJavadocForOverriddenMethodsCheck extends AbstractCheck {
49  
50      @Override
51      public int[] getDefaultTokens() {
52          return new int[] {
53              TokenTypes.METHOD_DEF,
54          };
55      }
56  
57      @Override
58      public int[] getAcceptableTokens() {
59          return this.getDefaultTokens();
60      }
61  
62      @Override
63      public int[] getRequiredTokens() {
64          return this.getDefaultTokens();
65      }
66  
67      @Override
68      @SuppressWarnings("deprecation")
69      public void visitToken(final DetailAST ast) {
70          if (AnnotationUtil.containsAnnotation(ast, "Override")) {
71              final FileContents contents = getFileContents();
72              final TextBlock javadoc = contents.getJavadocBefore(
73                  ast.getLineNo()
74              );
75              if (javadoc != null) {
76                  log(ast, "Overridden methods should not have Javadoc");
77              }
78          }
79      }
80  }