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.coding; 21 22 import java.util.Arrays; 23 import java.util.HashSet; 24 import java.util.Set; 25 import java.util.stream.Collectors; 26 27 import com.puppycrawl.tools.checkstyle.StatelessCheck; 28 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 29 import com.puppycrawl.tools.checkstyle.api.DetailAST; 30 import com.puppycrawl.tools.checkstyle.api.FullIdent; 31 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 32 import com.puppycrawl.tools.checkstyle.utils.CheckUtil; 33 34 /** 35 * <p> 36 * Checks that certain exception types do not appear in a {@code catch} statement. 37 * </p> 38 * <p> 39 * Rationale: catching {@code java.lang.Exception}, {@code java.lang.Error} or 40 * {@code java.lang.RuntimeException} is almost never acceptable. 41 * Novice developers often simply catch Exception in an attempt to handle 42 * multiple exception classes. This unfortunately leads to code that inadvertently 43 * catches {@code NullPointerException}, {@code OutOfMemoryError}, etc. 44 * </p> 45 * <ul> 46 * <li> 47 * Property {@code illegalClassNames} - Specify exception class names to reject. 48 * Type is {@code java.lang.String[]}. 49 * Default value is {@code Error, Exception, RuntimeException, Throwable, java.lang.Error, 50 * java.lang.Exception, java.lang.RuntimeException, java.lang.Throwable}. 51 * </li> 52 * </ul> 53 * <p> 54 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 55 * </p> 56 * <p> 57 * Violation Message Keys: 58 * </p> 59 * <ul> 60 * <li> 61 * {@code illegal.catch} 62 * </li> 63 * </ul> 64 * 65 * @since 3.2 66 */ 67 @StatelessCheck 68 public final class IllegalCatchCheck extends AbstractCheck { 69 70 /** 71 * A key is pointing to the warning message text in "messages.properties" 72 * file. 73 */ 74 public static final String MSG_KEY = "illegal.catch"; 75 76 /** Specify exception class names to reject. */ 77 private final Set<String> illegalClassNames = Arrays.stream(new String[] {"Exception", "Error", 78 "RuntimeException", "Throwable", "java.lang.Error", "java.lang.Exception", 79 "java.lang.RuntimeException", "java.lang.Throwable", }) 80 .collect(Collectors.toCollection(HashSet::new)); 81 82 /** 83 * Setter to specify exception class names to reject. 84 * 85 * @param classNames 86 * array of illegal exception classes 87 * @since 3.2 88 */ 89 public void setIllegalClassNames(final String... classNames) { 90 illegalClassNames.clear(); 91 illegalClassNames.addAll( 92 CheckUtil.parseClassNames(classNames)); 93 } 94 95 @Override 96 public int[] getDefaultTokens() { 97 return getRequiredTokens(); 98 } 99 100 @Override 101 public int[] getRequiredTokens() { 102 return new int[] {TokenTypes.LITERAL_CATCH}; 103 } 104 105 @Override 106 public int[] getAcceptableTokens() { 107 return getRequiredTokens(); 108 } 109 110 @Override 111 public void visitToken(DetailAST detailAST) { 112 final DetailAST parameterDef = 113 detailAST.findFirstToken(TokenTypes.PARAMETER_DEF); 114 final DetailAST excTypeParent = 115 parameterDef.findFirstToken(TokenTypes.TYPE); 116 117 DetailAST currentNode = excTypeParent.getFirstChild(); 118 while (currentNode != null) { 119 final FullIdent ident = FullIdent.createFullIdent(currentNode); 120 final String identText = ident.getText(); 121 if (illegalClassNames.contains(identText)) { 122 log(detailAST, MSG_KEY, identText); 123 } 124 currentNode = currentNode.getNextSibling(); 125 } 126 } 127 }