View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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       * Creates a new {@code AtclauseOrderCheck} instance.
98       */
99      public AtclauseOrderCheck() {
100         // no code by default
101     }
102 
103     /**
104      * Setter to specify block tags targeted.
105      *
106      * @param targets user's targets.
107      * @since 6.0
108      */
109     public void setTarget(String... targets) {
110         target = TokenUtil.asBitSet(targets);
111     }
112 
113     /**
114      * Setter to specify the order by tags.
115      *
116      * @param orders user's orders.
117      * @since 6.0
118      */
119     public void setTagOrder(String... orders) {
120         tagOrder = Arrays.asList(orders);
121     }
122 
123     @Override
124     public int[] getDefaultJavadocTokens() {
125         return new int[] {
126             JavadocCommentsTokenTypes.JAVADOC_CONTENT,
127         };
128     }
129 
130     @Override
131     public int[] getRequiredJavadocTokens() {
132         return getAcceptableJavadocTokens();
133     }
134 
135     @Override
136     public void visitJavadocToken(DetailNode ast) {
137         final int parentType = getParentType(getBlockCommentAst());
138 
139         if (target.get(parentType)) {
140             checkOrderInTagSection(ast);
141         }
142     }
143 
144     /**
145      * Checks order of atclauses in tag section node.
146      *
147      * @param javadoc Javadoc root node.
148      */
149     private void checkOrderInTagSection(DetailNode javadoc) {
150         int maxIndexOfPreviousTag = 0;
151         DetailNode node = javadoc.getFirstChild();
152 
153         while (node != null) {
154             if (node.getType() == JavadocCommentsTokenTypes.JAVADOC_BLOCK_TAG) {
155                 final String tagText = JavadocUtil.getTagName(node);
156                 final int indexOfCurrentTag = tagOrder.indexOf("@" + tagText);
157 
158                 if (indexOfCurrentTag != -1) {
159                     if (indexOfCurrentTag < maxIndexOfPreviousTag) {
160                         log(node.getLineNumber(), MSG_KEY, tagOrder.toString());
161                     }
162                     else {
163                         maxIndexOfPreviousTag = indexOfCurrentTag;
164                     }
165                 }
166             }
167             node = node.getNextSibling();
168         }
169     }
170 
171     /**
172      * Returns type of parent node.
173      *
174      * @param commentBlock child node.
175      * @return parent type.
176      */
177     private static int getParentType(DetailAST commentBlock) {
178         final DetailAST parentNode = commentBlock.getParent();
179         int result = parentNode.getType();
180         if (result == TokenTypes.TYPE || result == TokenTypes.MODIFIERS) {
181             result = parentNode.getParent().getType();
182         }
183         else if (parentNode.getParent() != null
184                 && parentNode.getParent().getType() == TokenTypes.MODIFIERS) {
185             result = parentNode.getParent().getParent().getType();
186         }
187         return result;
188     }
189 
190 }