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