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.TokenTypes;
10  
11  /**
12   * Checks that there is no Javadoc for private methods, both static
13   * and non-static ones.
14   *
15   * <p>A private method is an implementation detail of its own class and
16   * has no users outside of it. Its name, the names of its parameters and
17   * its body say everything a reader needs, while a Javadoc block above it
18   * only repeats them and then rots, staying behind after the method is
19   * renamed or its contract changes.</p>
20   *
21   * <p>Private constructors are not affected, since a constructor is not
22   * a method and its Javadoc often carries the only explanation of why
23   * the class must not be instantiated.</p>
24   *
25   * @since 0.73.4
26   */
27  public final class NoJavadocForPrivateMethodsCheck extends AbstractCheck {
28  
29      /**
30       * Default constructor.
31       */
32      public NoJavadocForPrivateMethodsCheck() {
33          // nothing to initialize
34      }
35  
36      @Override
37      public int[] getDefaultTokens() {
38          return new int[] {TokenTypes.METHOD_DEF};
39      }
40  
41      @Override
42      public int[] getAcceptableTokens() {
43          return this.getDefaultTokens();
44      }
45  
46      @Override
47      public int[] getRequiredTokens() {
48          return this.getDefaultTokens();
49      }
50  
51      @Override
52      @SuppressWarnings("deprecation")
53      public void visitToken(final DetailAST ast) {
54          final boolean prv = ast.findFirstToken(TokenTypes.MODIFIERS)
55              .findFirstToken(TokenTypes.LITERAL_PRIVATE) != null;
56          if (prv && this.getFileContents().getJavadocBefore(ast.getLineNo()) != null) {
57              final DetailAST name = ast.findFirstToken(TokenTypes.IDENT);
58              this.log(
59                  name,
60                  String.format(
61                      "Private method \"%s\" must not have Javadoc",
62                      name.getText()
63                  )
64              );
65          }
66      }
67  }