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