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