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.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.JavadocCommentsTokenTypes;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.internal.annotation.PreserveOrder;
34  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
35  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
36  
37  /**
38   * <div>
39   * Checks the order of
40   * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF">
41   * javadoc block-tags or javadoc tags</a>.
42   * </div>
43   *
44   * <p>
45   * Note: Google used the term "at-clauses" for block tags in their guide till 2017-02-28.
46   * </p>
47   *
48   * @since 6.0
49   */
50  @StatelessCheck
51  public class AtclauseOrderCheck extends AbstractJavadocCheck {
52  
53      /**
54       * A key is pointing to the warning message text in "messages.properties"
55       * file.
56       */
57      public static final String MSG_KEY = "at.clause.order";
58  
59      /**
60       * Default order of atclauses.
61       */
62      private static final String[] DEFAULT_ORDER = {
63          "@author", "@version",
64          "@param", "@return",
65          "@throws", "@exception",
66          "@see", "@since",
67          "@serial", "@serialField",
68          "@serialData", "@deprecated",
69      };
70  
71      /**
72       * Specify block tags targeted.
73       */
74      @XdocsPropertyType(PropertyType.TOKEN_ARRAY)
75      private BitSet target = TokenUtil.asBitSet(
76          TokenTypes.CLASS_DEF,
77          TokenTypes.INTERFACE_DEF,
78          TokenTypes.ENUM_DEF,
79          TokenTypes.METHOD_DEF,
80          TokenTypes.CTOR_DEF,
81          TokenTypes.VARIABLE_DEF,
82          TokenTypes.RECORD_DEF,
83          TokenTypes.COMPACT_CTOR_DEF
84      );
85  
86      /**
87       * Specify the order by tags.
88       * Default value is
89       * {@literal @}author, {@literal @}version, {@literal @}param, {@literal @}return,
90       * {@literal @}throws, {@literal @}exception, {@literal @}see, {@literal @}since,
91       * {@literal @}serial, {@literal @}serialField, {@literal @}serialData, {@literal @}deprecated.
92       */
93      @PreserveOrder
94      private List<String> tagOrder = Arrays.asList(DEFAULT_ORDER);
95  
96      /**
97       * Setter to specify block tags targeted.
98       *
99       * @param targets user's targets.
100      * @since 6.0
101      */
102     public void setTarget(String... targets) {
103         target = TokenUtil.asBitSet(targets);
104     }
105 
106     /**
107      * Setter to specify the order by tags.
108      *
109      * @param orders user's orders.
110      * @since 6.0
111      */
112     public void setTagOrder(String... orders) {
113         tagOrder = Arrays.asList(orders);
114     }
115 
116     @Override
117     public int[] getDefaultJavadocTokens() {
118         return new int[] {
119             JavadocCommentsTokenTypes.JAVADOC_CONTENT,
120         };
121     }
122 
123     @Override
124     public int[] getRequiredJavadocTokens() {
125         return getAcceptableJavadocTokens();
126     }
127 
128     @Override
129     public void visitJavadocToken(DetailNode ast) {
130         final int parentType = getParentType(getBlockCommentAst());
131 
132         if (target.get(parentType)) {
133             checkOrderInTagSection(ast);
134         }
135     }
136 
137     /**
138      * Checks order of atclauses in tag section node.
139      *
140      * @param javadoc Javadoc root node.
141      */
142     private void checkOrderInTagSection(DetailNode javadoc) {
143         int maxIndexOfPreviousTag = 0;
144         DetailNode node = javadoc.getFirstChild();
145 
146         while (node != null) {
147             if (node.getType() == JavadocCommentsTokenTypes.JAVADOC_BLOCK_TAG) {
148                 final String tagText = JavadocUtil.getTagName(node);
149                 final int indexOfCurrentTag = tagOrder.indexOf("@" + tagText);
150 
151                 if (indexOfCurrentTag != -1) {
152                     if (indexOfCurrentTag < maxIndexOfPreviousTag) {
153                         log(node.getLineNumber(), MSG_KEY, tagOrder.toString());
154                     }
155                     else {
156                         maxIndexOfPreviousTag = indexOfCurrentTag;
157                     }
158                 }
159             }
160             node = node.getNextSibling();
161         }
162     }
163 
164     /**
165      * Returns type of parent node.
166      *
167      * @param commentBlock child node.
168      * @return parent type.
169      */
170     private static int getParentType(DetailAST commentBlock) {
171         final DetailAST parentNode = commentBlock.getParent();
172         int result = parentNode.getType();
173         if (result == TokenTypes.TYPE || result == TokenTypes.MODIFIERS) {
174             result = parentNode.getParent().getType();
175         }
176         else if (parentNode.getParent() != null
177                 && parentNode.getParent().getType() == TokenTypes.MODIFIERS) {
178             result = parentNode.getParent().getParent().getType();
179         }
180         return result;
181     }
182 
183 }