001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.design;
021
022import java.util.Objects;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028
029/**
030 * <p>
031 * Restricts throws statements to a specified count.
032 * Methods with "Override" or "java.lang.Override" annotation are skipped
033 * from validation as current class cannot change signature of these methods.
034 * </p>
035 * <p>
036 * Rationale:
037 * Exceptions form part of a method's interface. Declaring
038 * a method to throw too many differently rooted
039 * exceptions makes exception handling onerous and leads
040 * to poor programming practices such as writing code like
041 * {@code catch(Exception ex)}. 4 is the empirical value which is based
042 * on reports that we had for the ThrowsCountCheck over big projects
043 * such as OpenJDK. This check also forces developers to put exceptions
044 * into a hierarchy such that in the simplest
045 * case, only one type of exception need be checked for by
046 * a caller but any subclasses can be caught
047 * specifically if necessary. For more information on rules
048 * for the exceptions and their issues, see Effective Java:
049 * Programming Language Guide Second Edition
050 * by Joshua Bloch pages 264-273.
051 * </p>
052 * <p>
053 * <b>ignorePrivateMethods</b> - allows to skip private methods as they do
054 * not cause problems for other classes.
055 * </p>
056 * <ul>
057 * <li>
058 * Property {@code ignorePrivateMethods} - Allow private methods to be ignored.
059 * Type is {@code boolean}.
060 * Default value is {@code true}.
061 * </li>
062 * <li>
063 * Property {@code max} - Specify maximum allowed number of throws statements.
064 * Type is {@code int}.
065 * Default value is {@code 4}.
066 * </li>
067 * </ul>
068 * <p>
069 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
070 * </p>
071 * <p>
072 * Violation Message Keys:
073 * </p>
074 * <ul>
075 * <li>
076 * {@code throws.count}
077 * </li>
078 * </ul>
079 *
080 * @since 3.2
081 */
082@StatelessCheck
083public final class ThrowsCountCheck extends AbstractCheck {
084
085    /**
086     * A key is pointing to the warning message text in "messages.properties"
087     * file.
088     */
089    public static final String MSG_KEY = "throws.count";
090
091    /** Default value of max property. */
092    private static final int DEFAULT_MAX = 4;
093
094    /** Allow private methods to be ignored. */
095    private boolean ignorePrivateMethods = true;
096
097    /** Specify maximum allowed number of throws statements. */
098    private int max;
099
100    /** Creates new instance of the check. */
101    public ThrowsCountCheck() {
102        max = DEFAULT_MAX;
103    }
104
105    @Override
106    public int[] getDefaultTokens() {
107        return getRequiredTokens();
108    }
109
110    @Override
111    public int[] getRequiredTokens() {
112        return new int[] {
113            TokenTypes.LITERAL_THROWS,
114        };
115    }
116
117    @Override
118    public int[] getAcceptableTokens() {
119        return getRequiredTokens();
120    }
121
122    /**
123     * Setter to allow private methods to be ignored.
124     *
125     * @param ignorePrivateMethods whether private methods must be ignored.
126     * @since 6.7
127     */
128    public void setIgnorePrivateMethods(boolean ignorePrivateMethods) {
129        this.ignorePrivateMethods = ignorePrivateMethods;
130    }
131
132    /**
133     * Setter to specify maximum allowed number of throws statements.
134     *
135     * @param max maximum allowed throws statements.
136     * @since 3.2
137     */
138    public void setMax(int max) {
139        this.max = max;
140    }
141
142    @Override
143    public void visitToken(DetailAST ast) {
144        if (ast.getType() == TokenTypes.LITERAL_THROWS) {
145            visitLiteralThrows(ast);
146        }
147        else {
148            throw new IllegalStateException(ast.toString());
149        }
150    }
151
152    /**
153     * Checks number of throws statements.
154     *
155     * @param ast throws for check.
156     */
157    private void visitLiteralThrows(DetailAST ast) {
158        if ((!ignorePrivateMethods || !isInPrivateMethod(ast))
159                && !isOverriding(ast)) {
160            // Account for all the commas!
161            final int count = (ast.getChildCount() + 1) / 2;
162            if (count > max) {
163                log(ast, MSG_KEY, count, max);
164            }
165        }
166    }
167
168    /**
169     * Check if a method has annotation @Override.
170     *
171     * @param ast throws, which is being checked.
172     * @return true, if a method has annotation @Override.
173     */
174    private static boolean isOverriding(DetailAST ast) {
175        final DetailAST modifiers = ast.getParent().findFirstToken(TokenTypes.MODIFIERS);
176        boolean isOverriding = false;
177        DetailAST child = modifiers.getFirstChild();
178        while (child != null) {
179            if (child.getType() == TokenTypes.ANNOTATION
180                    && "Override".equals(getAnnotationName(child))) {
181                isOverriding = true;
182                break;
183            }
184            child = child.getNextSibling();
185        }
186        return isOverriding;
187    }
188
189    /**
190     * Gets name of an annotation.
191     *
192     * @param annotation to get name of.
193     * @return name of an annotation.
194     */
195    private static String getAnnotationName(DetailAST annotation) {
196        final DetailAST dotAst = annotation.findFirstToken(TokenTypes.DOT);
197        final DetailAST parent = Objects.requireNonNullElse(dotAst, annotation);
198        return parent.findFirstToken(TokenTypes.IDENT).getText();
199    }
200
201    /**
202     * Checks if method, which throws an exception is private.
203     *
204     * @param ast throws, which is being checked.
205     * @return true, if method, which throws an exception is private.
206     */
207    private static boolean isInPrivateMethod(DetailAST ast) {
208        final DetailAST methodModifiers = ast.getParent().findFirstToken(TokenTypes.MODIFIERS);
209        return methodModifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) != null;
210    }
211
212}