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