001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2023 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.javadoc; 021 022import java.util.Arrays; 023import java.util.BitSet; 024import java.util.List; 025 026import com.puppycrawl.tools.checkstyle.PropertyType; 027import com.puppycrawl.tools.checkstyle.StatelessCheck; 028import com.puppycrawl.tools.checkstyle.XdocsPropertyType; 029import com.puppycrawl.tools.checkstyle.api.DetailAST; 030import com.puppycrawl.tools.checkstyle.api.DetailNode; 031import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 032import com.puppycrawl.tools.checkstyle.api.TokenTypes; 033import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 034import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 035 036/** 037 * <p> 038 * Checks the order of 039 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF"> 040 * javadoc block-tags or javadoc tags</a>. 041 * </p> 042 * <p> 043 * Note: Google used the term "at-clauses" for block tags in their guide till 2017-02-28. 044 * </p> 045 * 046 * <ul> 047 * <li> 048 * Property {@code tagOrder} - Specify the order by tags. 049 * Type is {@code java.lang.String[]}. 050 * Default value is 051 * {@code @author, @deprecated, @exception, @param, @return, @see, @serial, @serialData, @serialField, @since, @throws, @version}. 052 * </li> 053 * <li> 054 * Property {@code target} - Specify block tags targeted. 055 * Type is {@code java.lang.String[]}. 056 * Validation type is {@code tokenTypesSet}. 057 * Default value is 058 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF"> 059 * CLASS_DEF</a>, 060 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMPACT_CTOR_DEF"> 061 * COMPACT_CTOR_DEF</a>, 062 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF"> 063 * CTOR_DEF</a>, 064 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF"> 065 * ENUM_DEF</a>, 066 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF"> 067 * INTERFACE_DEF</a>, 068 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF"> 069 * METHOD_DEF</a>, 070 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF"> 071 * RECORD_DEF</a>, 072 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF"> 073 * VARIABLE_DEF</a>. 074 * </li> 075 * <li> 076 * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations if the 077 * Javadoc being examined by this check violates the tight html rules defined at 078 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>. 079 * Type is {@code boolean}. 080 * Default value is {@code false}. 081 * </li> 082 * </ul> 083 * <p> 084 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 085 * </p> 086 * <p> 087 * Violation Message Keys: 088 * </p> 089 * <ul> 090 * <li> 091 * {@code at.clause.order} 092 * </li> 093 * <li> 094 * {@code javadoc.missed.html.close} 095 * </li> 096 * <li> 097 * {@code javadoc.parse.rule.error} 098 * </li> 099 * <li> 100 * {@code javadoc.wrong.singleton.html.tag} 101 * </li> 102 * </ul> 103 * 104 * @since 6.0 105 */ 106@StatelessCheck 107public class AtclauseOrderCheck extends AbstractJavadocCheck { 108 109 /** 110 * A key is pointing to the warning message text in "messages.properties" 111 * file. 112 */ 113 public static final String MSG_KEY = "at.clause.order"; 114 115 /** 116 * Default order of atclauses. 117 */ 118 private static final String[] DEFAULT_ORDER = { 119 "@author", "@version", 120 "@param", "@return", 121 "@throws", "@exception", 122 "@see", "@since", 123 "@serial", "@serialField", 124 "@serialData", "@deprecated", 125 }; 126 127 /** 128 * Specify block tags targeted. 129 */ 130 @XdocsPropertyType(PropertyType.TOKEN_ARRAY) 131 private BitSet target = TokenUtil.asBitSet( 132 TokenTypes.CLASS_DEF, 133 TokenTypes.INTERFACE_DEF, 134 TokenTypes.ENUM_DEF, 135 TokenTypes.METHOD_DEF, 136 TokenTypes.CTOR_DEF, 137 TokenTypes.VARIABLE_DEF, 138 TokenTypes.RECORD_DEF, 139 TokenTypes.COMPACT_CTOR_DEF 140 ); 141 142 /** 143 * Specify the order by tags. 144 */ 145 private List<String> tagOrder = Arrays.asList(DEFAULT_ORDER); 146 147 /** 148 * Setter to specify block tags targeted. 149 * 150 * @param targets user's targets. 151 * @since 6.0 152 */ 153 public void setTarget(String... targets) { 154 target = TokenUtil.asBitSet(targets); 155 } 156 157 /** 158 * Setter to specify the order by tags. 159 * 160 * @param orders user's orders. 161 * @since 6.0 162 */ 163 public void setTagOrder(String... orders) { 164 tagOrder = Arrays.asList(orders); 165 } 166 167 @Override 168 public int[] getDefaultJavadocTokens() { 169 return new int[] { 170 JavadocTokenTypes.JAVADOC, 171 }; 172 } 173 174 @Override 175 public int[] getRequiredJavadocTokens() { 176 return getAcceptableJavadocTokens(); 177 } 178 179 @Override 180 public void visitJavadocToken(DetailNode ast) { 181 final int parentType = getParentType(getBlockCommentAst()); 182 183 if (target.get(parentType)) { 184 checkOrderInTagSection(ast); 185 } 186 } 187 188 /** 189 * Checks order of atclauses in tag section node. 190 * 191 * @param javadoc Javadoc root node. 192 */ 193 private void checkOrderInTagSection(DetailNode javadoc) { 194 int maxIndexOfPreviousTag = 0; 195 196 for (DetailNode node : javadoc.getChildren()) { 197 if (node.getType() == JavadocTokenTypes.JAVADOC_TAG) { 198 final String tagText = JavadocUtil.getFirstChild(node).getText(); 199 final int indexOfCurrentTag = tagOrder.indexOf(tagText); 200 201 if (indexOfCurrentTag != -1) { 202 if (indexOfCurrentTag < maxIndexOfPreviousTag) { 203 log(node.getLineNumber(), MSG_KEY, tagOrder.toString()); 204 } 205 else { 206 maxIndexOfPreviousTag = indexOfCurrentTag; 207 } 208 } 209 } 210 } 211 } 212 213 /** 214 * Returns type of parent node. 215 * 216 * @param commentBlock child node. 217 * @return parent type. 218 */ 219 private static int getParentType(DetailAST commentBlock) { 220 final DetailAST parentNode = commentBlock.getParent(); 221 int result = parentNode.getType(); 222 if (result == TokenTypes.TYPE || result == TokenTypes.MODIFIERS) { 223 result = parentNode.getParent().getType(); 224 } 225 else if (parentNode.getParent() != null 226 && parentNode.getParent().getType() == TokenTypes.MODIFIERS) { 227 result = parentNode.getParent().getParent().getType(); 228 } 229 return result; 230 } 231 232}