View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * <ul>
43   * <li>
44   * Property {@code excludeScope} - Specify the visibility scope where Javadoc comments are not
45   * checked.
46   * Type is {@code com.puppycrawl.tools.checkstyle.api.Scope}.
47   * Default value is {@code null}.
48   * </li>
49   * <li>
50   * Property {@code scope} - Specify the visibility scope where Javadoc comments are checked.
51   * Type is {@code com.puppycrawl.tools.checkstyle.api.Scope}.
52   * Default value is {@code public}.
53   * </li>
54   * <li>
55   * Property {@code skipAnnotations} - Specify annotations that allow missed
56   * documentation. If annotation is present in target sources in multiple forms of qualified
57   * name, all forms should be listed in this property.
58   * Type is {@code java.lang.String[]}.
59   * Default value is {@code Generated}.
60   * </li>
61   * <li>
62   * Property {@code tokens} - tokens to check
63   * Type is {@code java.lang.String[]}.
64   * Validation type is {@code tokenSet}.
65   * Default value is:
66   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
67   * INTERFACE_DEF</a>,
68   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
69   * CLASS_DEF</a>,
70   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
71   * ENUM_DEF</a>,
72   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
73   * ANNOTATION_DEF</a>,
74   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
75   * RECORD_DEF</a>.
76   * </li>
77   * </ul>
78   *
79   * <p>
80   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
81   * </p>
82   *
83   * <p>
84   * Violation Message Keys:
85   * </p>
86   * <ul>
87   * <li>
88   * {@code javadoc.missing}
89   * </li>
90   * </ul>
91   *
92   * @since 8.20
93   */
94  @StatelessCheck
95  public class MissingJavadocTypeCheck extends AbstractCheck {
96  
97      /**
98       * A key is pointing to the warning message text in "messages.properties"
99       * file.
100      */
101     public static final String MSG_JAVADOC_MISSING = "javadoc.missing";
102 
103     /** Specify the visibility scope where Javadoc comments are checked. */
104     private Scope scope = Scope.PUBLIC;
105     /** Specify the visibility scope where Javadoc comments are not checked. */
106     private Scope excludeScope;
107 
108     /**
109      * Specify annotations that allow missed documentation.
110      * If annotation is present in target sources in multiple forms of qualified
111      * name, all forms should be listed in this property.
112      */
113     private Set<String> skipAnnotations = Set.of("Generated");
114 
115     /**
116      * Setter to specify the visibility scope where Javadoc comments are checked.
117      *
118      * @param scope a scope.
119      * @since 8.20
120      */
121     public void setScope(Scope scope) {
122         this.scope = scope;
123     }
124 
125     /**
126      * Setter to specify the visibility scope where Javadoc comments are not checked.
127      *
128      * @param excludeScope a scope.
129      * @since 8.20
130      */
131     public void setExcludeScope(Scope excludeScope) {
132         this.excludeScope = excludeScope;
133     }
134 
135     /**
136      * Setter to specify annotations that allow missed documentation.
137      * If annotation is present in target sources in multiple forms of qualified
138      * name, all forms should be listed in this property.
139      *
140      * @param userAnnotations user's value.
141      * @since 8.20
142      */
143     public void setSkipAnnotations(String... userAnnotations) {
144         skipAnnotations = Set.of(userAnnotations);
145     }
146 
147     @Override
148     public int[] getDefaultTokens() {
149         return getAcceptableTokens();
150     }
151 
152     @Override
153     public int[] getAcceptableTokens() {
154         return new int[] {
155             TokenTypes.INTERFACE_DEF,
156             TokenTypes.CLASS_DEF,
157             TokenTypes.ENUM_DEF,
158             TokenTypes.ANNOTATION_DEF,
159             TokenTypes.RECORD_DEF,
160         };
161     }
162 
163     @Override
164     public int[] getRequiredTokens() {
165         return CommonUtil.EMPTY_INT_ARRAY;
166     }
167 
168     // suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
169     @SuppressWarnings("deprecation")
170     @Override
171     public void visitToken(DetailAST ast) {
172         if (shouldCheck(ast)) {
173             final FileContents contents = getFileContents();
174             final int lineNo = ast.getLineNo();
175             final TextBlock textBlock = contents.getJavadocBefore(lineNo);
176             if (textBlock == null) {
177                 log(ast, MSG_JAVADOC_MISSING);
178             }
179         }
180     }
181 
182     /**
183      * Whether we should check this node.
184      *
185      * @param ast a given node.
186      * @return whether we should check a given node.
187      */
188     private boolean shouldCheck(final DetailAST ast) {
189         final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast);
190 
191         return surroundingScope.isIn(scope)
192                 && (excludeScope == null || !surroundingScope.isIn(excludeScope))
193                 && !AnnotationUtil.containsAnnotation(ast, skipAnnotations);
194     }
195 
196 }