001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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} - Control when to print violations if the
047 * Javadoc being examined by this check violates the tight html rules defined at
048 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
049 * Type is {@code boolean}.
050 * Default value is {@code false}.
051 * </li>
052 * <li>
053 * Property {@code target} - Specify the list of targets to check at-clauses.
054 * Type is {@code int[]}.
055 * Default value is
056 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
057 * CLASS_DEF</a>,
058 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
059 * INTERFACE_DEF</a>,
060 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
061 * ENUM_DEF</a>,
062 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
063 * METHOD_DEF</a>,
064 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">
065 * CTOR_DEF</a>,
066 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">
067 * VARIABLE_DEF</a>.
068 * </li>
069 * <li>
070 * Property {@code tagOrder} - Specify the order by tags.
071 * Type is {@code java.lang.String[]}.
072 * Default value is
073 * {@code @author, @deprecated, @exception, @param, @return, @see, @serial, @serialData, @serialField, @since, @throws, @version}.
074 * </li>
075 * </ul>
076 * <p>
077 * Default configuration
078 * </p>
079 * <pre>
080 * &lt;module name=&quot;AtclauseOrder&quot;&gt;
081 *   &lt;property name=&quot;tagOrder&quot; value=&quot;&#64;author, &#64;version, &#64;param,
082 *   &#64;return, &#64;throws, &#64;exception, &#64;see, &#64;since, &#64;serial,
083 *   &#64;serialField, &#64;serialData, &#64;deprecated&quot;/&gt;
084 *   &lt;property name=&quot;target&quot; value=&quot;CLASS_DEF, INTERFACE_DEF, ENUM_DEF,
085 *   METHOD_DEF, CTOR_DEF, VARIABLE_DEF&quot;/&gt;
086 * &lt;/module&gt;
087 * </pre>
088 * <p>
089 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
090 * </p>
091 * <p>
092 * Violation Message Keys:
093 * </p>
094 * <ul>
095 * <li>
096 * {@code at.clause.order}
097 * </li>
098 * <li>
099 * {@code javadoc.missed.html.close}
100 * </li>
101 * <li>
102 * {@code javadoc.parse.rule.error}
103 * </li>
104 * <li>
105 * {@code javadoc.wrong.singleton.html.tag}
106 * </li>
107 * </ul>
108 *
109 * @since 6.0
110 */
111@StatelessCheck
112public class AtclauseOrderCheck extends AbstractJavadocCheck {
113
114    /**
115     * A key is pointing to the warning message text in "messages.properties"
116     * file.
117     */
118    public static final String MSG_KEY = "at.clause.order";
119
120    /**
121     * Default order of atclauses.
122     */
123    private static final String[] DEFAULT_ORDER = {
124        "@author", "@version",
125        "@param", "@return",
126        "@throws", "@exception",
127        "@see", "@since",
128        "@serial", "@serialField",
129        "@serialData", "@deprecated",
130    };
131
132    /**
133     * Specify the list of targets to check at-clauses.
134     */
135    private List<Integer> target = Arrays.asList(
136        TokenTypes.CLASS_DEF,
137        TokenTypes.INTERFACE_DEF,
138        TokenTypes.ENUM_DEF,
139        TokenTypes.METHOD_DEF,
140        TokenTypes.CTOR_DEF,
141        TokenTypes.VARIABLE_DEF
142    );
143
144    /**
145     * Specify the order by tags.
146     */
147    private List<String> tagOrder = Arrays.asList(DEFAULT_ORDER);
148
149    /**
150     * Setter to specify the list of targets to check at-clauses.
151     *
152     * @param targets user's targets.
153     */
154    public void setTarget(String... targets) {
155        final List<Integer> customTarget = new ArrayList<>();
156        for (String temp : targets) {
157            customTarget.add(TokenUtil.getTokenId(temp.trim()));
158        }
159        target = customTarget;
160    }
161
162    /**
163     * Setter to specify the order by tags.
164     *
165     * @param orders user's orders.
166     */
167    public void setTagOrder(String... orders) {
168        final List<String> customOrder = new ArrayList<>();
169        for (String order : orders) {
170            customOrder.add(order.trim());
171        }
172        tagOrder = customOrder;
173    }
174
175    @Override
176    public int[] getDefaultJavadocTokens() {
177        return new int[] {
178            JavadocTokenTypes.JAVADOC,
179        };
180    }
181
182    @Override
183    public int[] getRequiredJavadocTokens() {
184        return getAcceptableJavadocTokens();
185    }
186
187    @Override
188    public void visitJavadocToken(DetailNode ast) {
189        final int parentType = getParentType(getBlockCommentAst());
190
191        if (target.contains(parentType)) {
192            checkOrderInTagSection(ast);
193        }
194    }
195
196    /**
197     * Checks order of atclauses in tag section node.
198     *
199     * @param javadoc Javadoc root node.
200     */
201    private void checkOrderInTagSection(DetailNode javadoc) {
202        int maxIndexOfPreviousTag = 0;
203
204        for (DetailNode node : javadoc.getChildren()) {
205            if (node.getType() == JavadocTokenTypes.JAVADOC_TAG) {
206                final String tagText = JavadocUtil.getFirstChild(node).getText();
207                final int indexOfCurrentTag = tagOrder.indexOf(tagText);
208
209                if (indexOfCurrentTag != -1) {
210                    if (indexOfCurrentTag < maxIndexOfPreviousTag) {
211                        log(node.getLineNumber(), MSG_KEY, tagOrder.toString());
212                    }
213                    else {
214                        maxIndexOfPreviousTag = indexOfCurrentTag;
215                    }
216                }
217            }
218        }
219    }
220
221    /**
222     * Returns type of parent node.
223     *
224     * @param commentBlock child node.
225     * @return parent type.
226     */
227    private static int getParentType(DetailAST commentBlock) {
228        final DetailAST parentNode = commentBlock.getParent();
229        int result = 0;
230        if (parentNode != null) {
231            result = parentNode.getType();
232            if (result == TokenTypes.TYPE || result == TokenTypes.MODIFIERS) {
233                result = parentNode.getParent().getType();
234            }
235        }
236        return result;
237    }
238
239}