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