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, @since, @serial, @serialField, @serialData, @deprecated}.
091     */
092    private List<String> tagOrder = Arrays.asList(DEFAULT_ORDER);
093
094    /**
095     * Setter to specify block tags targeted.
096     *
097     * @param targets user's targets.
098     * @since 6.0
099     */
100    public void setTarget(String... targets) {
101        target = TokenUtil.asBitSet(targets);
102    }
103
104    /**
105     * Setter to specify the order by tags.
106     *
107     * @param orders user's orders.
108     * @since 6.0
109     */
110    public void setTagOrder(String... orders) {
111        tagOrder = Arrays.asList(orders);
112    }
113
114    @Override
115    public int[] getDefaultJavadocTokens() {
116        return new int[] {
117            JavadocCommentsTokenTypes.JAVADOC_CONTENT,
118        };
119    }
120
121    @Override
122    public int[] getRequiredJavadocTokens() {
123        return getAcceptableJavadocTokens();
124    }
125
126    @Override
127    public void visitJavadocToken(DetailNode ast) {
128        final int parentType = getParentType(getBlockCommentAst());
129
130        if (target.get(parentType)) {
131            checkOrderInTagSection(ast);
132        }
133    }
134
135    /**
136     * Checks order of atclauses in tag section node.
137     *
138     * @param javadoc Javadoc root node.
139     */
140    private void checkOrderInTagSection(DetailNode javadoc) {
141        int maxIndexOfPreviousTag = 0;
142        DetailNode node = javadoc.getFirstChild();
143
144        while (node != null) {
145            if (node.getType() == JavadocCommentsTokenTypes.JAVADOC_BLOCK_TAG) {
146                final String tagText = JavadocUtil.getTagName(node);
147                final int indexOfCurrentTag = tagOrder.indexOf("@" + tagText);
148
149                if (indexOfCurrentTag != -1) {
150                    if (indexOfCurrentTag < maxIndexOfPreviousTag) {
151                        log(node.getLineNumber(), MSG_KEY, tagOrder.toString());
152                    }
153                    else {
154                        maxIndexOfPreviousTag = indexOfCurrentTag;
155                    }
156                }
157            }
158            node = node.getNextSibling();
159        }
160    }
161
162    /**
163     * Returns type of parent node.
164     *
165     * @param commentBlock child node.
166     * @return parent type.
167     */
168    private static int getParentType(DetailAST commentBlock) {
169        final DetailAST parentNode = commentBlock.getParent();
170        int result = parentNode.getType();
171        if (result == TokenTypes.TYPE || result == TokenTypes.MODIFIERS) {
172            result = parentNode.getParent().getType();
173        }
174        else if (parentNode.getParent() != null
175                && parentNode.getParent().getType() == TokenTypes.MODIFIERS) {
176            result = parentNode.getParent().getParent().getType();
177        }
178        return result;
179    }
180
181}