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.Arrays;
23  import java.util.BitSet;
24  import java.util.List;
25  
26  import com.puppycrawl.tools.checkstyle.PropertyType;
27  import com.puppycrawl.tools.checkstyle.StatelessCheck;
28  import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
29  import com.puppycrawl.tools.checkstyle.api.DetailAST;
30  import com.puppycrawl.tools.checkstyle.api.DetailNode;
31  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
34  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
35  
36  /**
37   * <div>
38   * Checks the order of
39   * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF">
40   * javadoc block-tags or javadoc tags</a>.
41   * </div>
42   *
43   * <p>
44   * Note: Google used the term "at-clauses" for block tags in their guide till 2017-02-28.
45   * </p>
46   *
47   * <ul>
48   * <li>
49   * Property {@code tagOrder} - Specify the order by tags.
50   * Type is {@code java.lang.String[]}.
51   * Default value is
52   * {@code @author, @deprecated, @exception, @param, @return, @see, @serial, @serialData, @serialField, @since, @throws, @version}.
53   * </li>
54   * <li>
55   * Property {@code target} - Specify block tags targeted.
56   * Type is {@code java.lang.String[]}.
57   * Validation type is {@code tokenTypesSet}.
58   * Default value is
59   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
60   * CLASS_DEF</a>,
61   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMPACT_CTOR_DEF">
62   * COMPACT_CTOR_DEF</a>,
63   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">
64   * CTOR_DEF</a>,
65   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
66   * ENUM_DEF</a>,
67   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
68   * INTERFACE_DEF</a>,
69   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
70   * METHOD_DEF</a>,
71   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
72   * RECORD_DEF</a>,
73   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">
74   * VARIABLE_DEF</a>.
75   * </li>
76   * <li>
77   * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations if the
78   * Javadoc being examined by this check violates the tight html rules defined at
79   * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
80   * Type is {@code boolean}.
81   * Default value is {@code false}.
82   * </li>
83   * </ul>
84   *
85   * <p>
86   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
87   * </p>
88   *
89   * <p>
90   * Violation Message Keys:
91   * </p>
92   * <ul>
93   * <li>
94   * {@code at.clause.order}
95   * </li>
96   * <li>
97   * {@code javadoc.missed.html.close}
98   * </li>
99   * <li>
100  * {@code javadoc.parse.rule.error}
101  * </li>
102  * <li>
103  * {@code javadoc.unclosedHtml}
104  * </li>
105  * <li>
106  * {@code javadoc.wrong.singleton.html.tag}
107  * </li>
108  * </ul>
109  *
110  * @since 6.0
111  */
112 @StatelessCheck
113 public class AtclauseOrderCheck extends AbstractJavadocCheck {
114 
115     /**
116      * A key is pointing to the warning message text in "messages.properties"
117      * file.
118      */
119     public static final String MSG_KEY = "at.clause.order";
120 
121     /**
122      * Default order of atclauses.
123      */
124     private static final String[] DEFAULT_ORDER = {
125         "@author", "@version",
126         "@param", "@return",
127         "@throws", "@exception",
128         "@see", "@since",
129         "@serial", "@serialField",
130         "@serialData", "@deprecated",
131     };
132 
133     /**
134      * Specify block tags targeted.
135      */
136     @XdocsPropertyType(PropertyType.TOKEN_ARRAY)
137     private BitSet target = TokenUtil.asBitSet(
138         TokenTypes.CLASS_DEF,
139         TokenTypes.INTERFACE_DEF,
140         TokenTypes.ENUM_DEF,
141         TokenTypes.METHOD_DEF,
142         TokenTypes.CTOR_DEF,
143         TokenTypes.VARIABLE_DEF,
144         TokenTypes.RECORD_DEF,
145         TokenTypes.COMPACT_CTOR_DEF
146     );
147 
148     /**
149      * Specify the order by tags.
150      */
151     private List<String> tagOrder = Arrays.asList(DEFAULT_ORDER);
152 
153     /**
154      * Setter to specify block tags targeted.
155      *
156      * @param targets user's targets.
157      * @since 6.0
158      */
159     public void setTarget(String... targets) {
160         target = TokenUtil.asBitSet(targets);
161     }
162 
163     /**
164      * Setter to specify the order by tags.
165      *
166      * @param orders user's orders.
167      * @since 6.0
168      */
169     public void setTagOrder(String... orders) {
170         tagOrder = Arrays.asList(orders);
171     }
172 
173     @Override
174     public int[] getDefaultJavadocTokens() {
175         return new int[] {
176             JavadocTokenTypes.JAVADOC,
177         };
178     }
179 
180     @Override
181     public int[] getRequiredJavadocTokens() {
182         return getAcceptableJavadocTokens();
183     }
184 
185     @Override
186     public void visitJavadocToken(DetailNode ast) {
187         final int parentType = getParentType(getBlockCommentAst());
188 
189         if (target.get(parentType)) {
190             checkOrderInTagSection(ast);
191         }
192     }
193 
194     /**
195      * Checks order of atclauses in tag section node.
196      *
197      * @param javadoc Javadoc root node.
198      */
199     private void checkOrderInTagSection(DetailNode javadoc) {
200         int maxIndexOfPreviousTag = 0;
201 
202         for (DetailNode node : javadoc.getChildren()) {
203             if (node.getType() == JavadocTokenTypes.JAVADOC_TAG) {
204                 final String tagText = JavadocUtil.getFirstChild(node).getText();
205                 final int indexOfCurrentTag = tagOrder.indexOf(tagText);
206 
207                 if (indexOfCurrentTag != -1) {
208                     if (indexOfCurrentTag < maxIndexOfPreviousTag) {
209                         log(node.getLineNumber(), MSG_KEY, tagOrder.toString());
210                     }
211                     else {
212                         maxIndexOfPreviousTag = indexOfCurrentTag;
213                     }
214                 }
215             }
216         }
217     }
218 
219     /**
220      * Returns type of parent node.
221      *
222      * @param commentBlock child node.
223      * @return parent type.
224      */
225     private static int getParentType(DetailAST commentBlock) {
226         final DetailAST parentNode = commentBlock.getParent();
227         int result = parentNode.getType();
228         if (result == TokenTypes.TYPE || result == TokenTypes.MODIFIERS) {
229             result = parentNode.getParent().getType();
230         }
231         else if (parentNode.getParent() != null
232                 && parentNode.getParent().getType() == TokenTypes.MODIFIERS) {
233             result = parentNode.getParent().getParent().getType();
234         }
235         return result;
236     }
237 
238 }