View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.javadoc;
21  
22  import java.util.Set;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.api.FileContents;
28  import com.puppycrawl.tools.checkstyle.api.Scope;
29  import com.puppycrawl.tools.checkstyle.api.TextBlock;
30  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31  import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
34  
35  /**
36   * <div>
37   * Checks for missing Javadoc comments for class, enum, interface, and annotation interface
38   * definitions. The scope to verify is specified using the {@code Scope} class and defaults
39   * to {@code Scope.PUBLIC}. To verify another scope, set property scope to one of the
40   * {@code Scope} constants.
41   * </div>
42   *
43   * @since 8.20
44   */
45  @StatelessCheck
46  public class MissingJavadocTypeCheck extends AbstractCheck {
47  
48      /**
49       * A key is pointing to the warning message text in "messages.properties"
50       * file.
51       */
52      public static final String MSG_JAVADOC_MISSING = "javadoc.missing";
53  
54      /** Specify the visibility scope where Javadoc comments are checked. */
55      private Scope scope = Scope.PUBLIC;
56      /** Specify the visibility scope where Javadoc comments are not checked. */
57      private Scope excludeScope;
58  
59      /**
60       * Specify annotations that allow missed documentation.
61       * If annotation is present in target sources in multiple forms of qualified
62       * name, all forms should be listed in this property.
63       */
64      private Set<String> skipAnnotations = Set.of("Generated");
65  
66      /**
67       * Setter to specify the visibility scope where Javadoc comments are checked.
68       *
69       * @param scope a scope.
70       * @since 8.20
71       */
72      public void setScope(Scope scope) {
73          this.scope = scope;
74      }
75  
76      /**
77       * Setter to specify the visibility scope where Javadoc comments are not checked.
78       *
79       * @param excludeScope a scope.
80       * @since 8.20
81       */
82      public void setExcludeScope(Scope excludeScope) {
83          this.excludeScope = excludeScope;
84      }
85  
86      /**
87       * Setter to specify annotations that allow missed documentation.
88       * If annotation is present in target sources in multiple forms of qualified
89       * name, all forms should be listed in this property.
90       *
91       * @param userAnnotations user's value.
92       * @since 8.20
93       */
94      public void setSkipAnnotations(String... userAnnotations) {
95          skipAnnotations = Set.of(userAnnotations);
96      }
97  
98      @Override
99      public int[] getDefaultTokens() {
100         return getAcceptableTokens();
101     }
102 
103     @Override
104     public int[] getAcceptableTokens() {
105         return new int[] {
106             TokenTypes.INTERFACE_DEF,
107             TokenTypes.CLASS_DEF,
108             TokenTypes.ENUM_DEF,
109             TokenTypes.ANNOTATION_DEF,
110             TokenTypes.RECORD_DEF,
111         };
112     }
113 
114     @Override
115     public int[] getRequiredTokens() {
116         return CommonUtil.EMPTY_INT_ARRAY;
117     }
118 
119     // suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
120     @Override
121     @SuppressWarnings("deprecation")
122     public void visitToken(DetailAST ast) {
123         if (shouldCheck(ast)) {
124             final FileContents contents = getFileContents();
125             final int lineNo = ast.getLineNo();
126             final TextBlock textBlock = contents.getJavadocBefore(lineNo);
127             if (textBlock == null) {
128                 log(ast, MSG_JAVADOC_MISSING);
129             }
130         }
131     }
132 
133     /**
134      * Whether we should check this node.
135      *
136      * @param ast a given node.
137      * @return whether we should check a given node.
138      */
139     private boolean shouldCheck(final DetailAST ast) {
140         final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast);
141 
142         return surroundingScope.isIn(scope)
143                 && (excludeScope == null || !surroundingScope.isIn(excludeScope))
144                 && !AnnotationUtil.containsAnnotation(ast, skipAnnotations);
145     }
146 
147 }