001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.ArrayList; 023import java.util.Arrays; 024import java.util.List; 025 026import com.puppycrawl.tools.checkstyle.StatelessCheck; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.DetailNode; 029import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 030import com.puppycrawl.tools.checkstyle.api.TokenTypes; 031import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 032import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 033 034/** 035 * <p> 036 * Checks the order of 037 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF"> 038 * javadoc block-tags or javadoc tags</a>. 039 * </p> 040 * <p> 041 * Note: Google used the term "at-clauses" for block tags in their guide till 2017-02-28. 042 * </p> 043 * 044 * <ul> 045 * <li> 046 * Property {@code violateExecutionOnNonTightHtml} - If turned on, will print violations if the 047 * Javadoc being examined by this check violates the tight html rules defined at 048 * <a href="writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>. 049 * Default value is {@code false}. 050 * </li> 051 * <li> 052 * Property {@code target} - Specify the list of targets to check at-clauses. Default value is 053 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">CLASS_DEF</a>, 054 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">INTERFACE_DEF</a>, 055 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">ENUM_DEF</a>, 056 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">METHOD_DEF</a>, 057 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">CTOR_DEF</a>, 058 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a>. 059 * </li> 060 * <li> 061 * Property {@code tagOrder} - Specify the order by tags. Default value is 062 * {@code @author, @version, @param, @return, @throws, @exception, @see, @since, @serial, @serialField, @serialData, @deprecated}. 063 * </li> 064 * </ul> 065 * <p> 066 * Default configuration 067 * </p> 068 * <pre> 069 * <module name="AtclauseOrder"> 070 * <property name="tagOrder" value="@author, @version, @param, 071 * @return, @throws, @exception, @see, @since, @serial, 072 * @serialField, @serialData, @deprecated"/> 073 * <property name="target" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, 074 * METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/> 075 * </module> 076 * </pre> 077 * 078 * @since 6.0 079 */ 080@StatelessCheck 081public class AtclauseOrderCheck extends AbstractJavadocCheck { 082 083 /** 084 * A key is pointing to the warning message text in "messages.properties" 085 * file. 086 */ 087 public static final String MSG_KEY = "at.clause.order"; 088 089 /** 090 * Default order of atclauses. 091 */ 092 private static final String[] DEFAULT_ORDER = { 093 "@author", "@version", 094 "@param", "@return", 095 "@throws", "@exception", 096 "@see", "@since", 097 "@serial", "@serialField", 098 "@serialData", "@deprecated", 099 }; 100 101 /** 102 * Specify the list of targets to check at-clauses. 103 */ 104 private List<Integer> target = Arrays.asList( 105 TokenTypes.CLASS_DEF, 106 TokenTypes.INTERFACE_DEF, 107 TokenTypes.ENUM_DEF, 108 TokenTypes.METHOD_DEF, 109 TokenTypes.CTOR_DEF, 110 TokenTypes.VARIABLE_DEF 111 ); 112 113 /** 114 * Specify the order by tags. 115 */ 116 private List<String> tagOrder = Arrays.asList(DEFAULT_ORDER); 117 118 /** 119 * Setter to specify the list of targets to check at-clauses. 120 * @param targets user's targets. 121 */ 122 public void setTarget(String... targets) { 123 final List<Integer> customTarget = new ArrayList<>(); 124 for (String temp : targets) { 125 customTarget.add(TokenUtil.getTokenId(temp.trim())); 126 } 127 target = customTarget; 128 } 129 130 /** 131 * Setter to specify the order by tags. 132 * @param orders user's orders. 133 */ 134 public void setTagOrder(String... orders) { 135 final List<String> customOrder = new ArrayList<>(); 136 for (String order : orders) { 137 customOrder.add(order.trim()); 138 } 139 tagOrder = customOrder; 140 } 141 142 @Override 143 public int[] getDefaultJavadocTokens() { 144 return new int[] { 145 JavadocTokenTypes.JAVADOC, 146 }; 147 } 148 149 @Override 150 public int[] getRequiredJavadocTokens() { 151 return getAcceptableJavadocTokens(); 152 } 153 154 @Override 155 public void visitJavadocToken(DetailNode ast) { 156 final int parentType = getParentType(getBlockCommentAst()); 157 158 if (target.contains(parentType)) { 159 checkOrderInTagSection(ast); 160 } 161 } 162 163 /** 164 * Checks order of atclauses in tag section node. 165 * @param javadoc Javadoc root node. 166 */ 167 private void checkOrderInTagSection(DetailNode javadoc) { 168 int maxIndexOfPreviousTag = 0; 169 170 for (DetailNode node : javadoc.getChildren()) { 171 if (node.getType() == JavadocTokenTypes.JAVADOC_TAG) { 172 final String tagText = JavadocUtil.getFirstChild(node).getText(); 173 final int indexOfCurrentTag = tagOrder.indexOf(tagText); 174 175 if (indexOfCurrentTag != -1) { 176 if (indexOfCurrentTag < maxIndexOfPreviousTag) { 177 log(node.getLineNumber(), MSG_KEY, tagOrder.toString()); 178 } 179 else { 180 maxIndexOfPreviousTag = indexOfCurrentTag; 181 } 182 } 183 } 184 } 185 } 186 187 /** 188 * Returns type of parent node. 189 * @param commentBlock child node. 190 * @return parent type. 191 */ 192 private static int getParentType(DetailAST commentBlock) { 193 final DetailAST parentNode = commentBlock.getParent(); 194 int type = parentNode.getType(); 195 if (type == TokenTypes.TYPE || type == TokenTypes.MODIFIERS) { 196 type = parentNode.getParent().getType(); 197 } 198 return type; 199 } 200 201}