001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.JavadocCommentsTokenTypes; 032import com.puppycrawl.tools.checkstyle.api.TokenTypes; 033import com.puppycrawl.tools.checkstyle.internal.annotation.PreserveOrder; 034import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 035import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 036 037/** 038 * <div> 039 * Checks the order of 040 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF"> 041 * javadoc block-tags or javadoc tags</a>. 042 * </div> 043 * 044 * <p> 045 * Note: Google used the term "at-clauses" for block tags in their guide till 2017-02-28. 046 * </p> 047 * 048 * @since 6.0 049 */ 050@StatelessCheck 051public class AtclauseOrderCheck extends AbstractJavadocCheck { 052 053 /** 054 * A key is pointing to the warning message text in "messages.properties" 055 * file. 056 */ 057 public static final String MSG_KEY = "at.clause.order"; 058 059 /** 060 * Default order of atclauses. 061 */ 062 private static final String[] DEFAULT_ORDER = { 063 "@author", "@version", 064 "@param", "@return", 065 "@throws", "@exception", 066 "@see", "@since", 067 "@serial", "@serialField", 068 "@serialData", "@deprecated", 069 }; 070 071 /** 072 * Specify block tags targeted. 073 */ 074 @XdocsPropertyType(PropertyType.TOKEN_ARRAY) 075 private BitSet target = TokenUtil.asBitSet( 076 TokenTypes.CLASS_DEF, 077 TokenTypes.INTERFACE_DEF, 078 TokenTypes.ENUM_DEF, 079 TokenTypes.METHOD_DEF, 080 TokenTypes.CTOR_DEF, 081 TokenTypes.VARIABLE_DEF, 082 TokenTypes.RECORD_DEF, 083 TokenTypes.COMPACT_CTOR_DEF 084 ); 085 086 /** 087 * Specify the order by tags. 088 * Default value is 089 * {@literal @}author, {@literal @}version, {@literal @}param, {@literal @}return, 090 * {@literal @}throws, {@literal @}exception, {@literal @}see, {@literal @}since, 091 * {@literal @}serial, {@literal @}serialField, {@literal @}serialData, {@literal @}deprecated. 092 */ 093 @PreserveOrder 094 private List<String> tagOrder = Arrays.asList(DEFAULT_ORDER); 095 096 /** 097 * Creates a new {@code AtclauseOrderCheck} instance. 098 */ 099 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}