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