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