001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 javax.annotation.Nullable;
023
024import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
025import com.puppycrawl.tools.checkstyle.api.DetailNode;
026import com.puppycrawl.tools.checkstyle.api.JavadocCommentsTokenTypes;
027import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
028
029/**
030 * <div>
031 * Checks that multiple {@code @throws} and {@code @exception} Javadoc tags are listed
032 * alphabetically by exception name.
033 * </div>
034 *
035 * @since 14.1.0
036 */
037@FileStatefulCheck
038public class JavadocThrowsOrderCheck extends AbstractJavadocCheck {
039
040    /**
041     * A key is pointing to the warning message text in "messages.properties" file.
042     */
043    public static final String MSG_KEY = "javadoc.throws.order";
044
045    /** The greatest exception name found so far in the current Javadoc tree. */
046    @Nullable
047    private String previousExceptionName;
048
049    /**
050     * Creates a new {@code JavadocThrowsOrderCheck} instance.
051     */
052    public JavadocThrowsOrderCheck() {
053        // no code by default
054    }
055
056    @Override
057    public int[] getDefaultJavadocTokens() {
058        return getRequiredJavadocTokens();
059    }
060
061    @Override
062    public int[] getRequiredJavadocTokens() {
063        return new int[] {
064            JavadocCommentsTokenTypes.THROWS_BLOCK_TAG,
065            JavadocCommentsTokenTypes.EXCEPTION_BLOCK_TAG,
066        };
067    }
068
069    @Override
070    public void visitJavadocToken(DetailNode ast) {
071        final DetailNode identifier = JavadocUtil.findFirstToken(
072                ast, JavadocCommentsTokenTypes.IDENTIFIER);
073
074        if (identifier != null) {
075            final String exceptionName = identifier.getText();
076            final String previousName = previousExceptionName;
077            if (previousName != null && exceptionName.compareTo(previousName) < 0) {
078                final String tagName = getTagName(ast);
079                log(ast, MSG_KEY, tagName, exceptionName, previousName);
080            }
081            else {
082                previousExceptionName = exceptionName;
083            }
084        }
085    }
086
087    @Override
088    public void beginJavadocTree(DetailNode rootAst) {
089        previousExceptionName = null;
090    }
091
092    /**
093     * Gets the tag name from a throws or exception block tag.
094     *
095     * @param ast throws or exception block tag
096     * @return tag name
097     */
098    private static String getTagName(DetailNode ast) {
099        return JavadocUtil.findFirstToken(ast, JavadocCommentsTokenTypes.TAG_NAME).getText();
100    }
101
102}