1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2024 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.checks.design; 21 22 import java.util.Objects; 23 24 import com.puppycrawl.tools.checkstyle.StatelessCheck; 25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 26 import com.puppycrawl.tools.checkstyle.api.DetailAST; 27 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 28 29 /** 30 * <p> 31 * Restricts throws statements to a specified count. 32 * Methods with "Override" or "java.lang.Override" annotation are skipped 33 * from validation as current class cannot change signature of these methods. 34 * </p> 35 * <p> 36 * Rationale: 37 * Exceptions form part of a method's interface. Declaring 38 * a method to throw too many differently rooted 39 * exceptions makes exception handling onerous and leads 40 * to poor programming practices such as writing code like 41 * {@code catch(Exception ex)}. 4 is the empirical value which is based 42 * on reports that we had for the ThrowsCountCheck over big projects 43 * such as OpenJDK. This check also forces developers to put exceptions 44 * into a hierarchy such that in the simplest 45 * case, only one type of exception need be checked for by 46 * a caller but any subclasses can be caught 47 * specifically if necessary. For more information on rules 48 * for the exceptions and their issues, see Effective Java: 49 * Programming Language Guide Second Edition 50 * by Joshua Bloch pages 264-273. 51 * </p> 52 * <p> 53 * <b>ignorePrivateMethods</b> - allows to skip private methods as they do 54 * not cause problems for other classes. 55 * </p> 56 * <ul> 57 * <li> 58 * Property {@code ignorePrivateMethods} - Allow private methods to be ignored. 59 * Type is {@code boolean}. 60 * Default value is {@code true}. 61 * </li> 62 * <li> 63 * Property {@code max} - Specify maximum allowed number of throws statements. 64 * Type is {@code int}. 65 * Default value is {@code 4}. 66 * </li> 67 * </ul> 68 * <p> 69 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 70 * </p> 71 * <p> 72 * Violation Message Keys: 73 * </p> 74 * <ul> 75 * <li> 76 * {@code throws.count} 77 * </li> 78 * </ul> 79 * 80 * @since 3.2 81 */ 82 @StatelessCheck 83 public final class ThrowsCountCheck extends AbstractCheck { 84 85 /** 86 * A key is pointing to the warning message text in "messages.properties" 87 * file. 88 */ 89 public static final String MSG_KEY = "throws.count"; 90 91 /** Default value of max property. */ 92 private static final int DEFAULT_MAX = 4; 93 94 /** Allow private methods to be ignored. */ 95 private boolean ignorePrivateMethods = true; 96 97 /** Specify maximum allowed number of throws statements. */ 98 private int max; 99 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 }