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.coding; 021 022import java.util.Arrays; 023import java.util.Collections; 024import java.util.HashSet; 025import java.util.Set; 026import java.util.stream.Collectors; 027 028import com.puppycrawl.tools.checkstyle.StatelessCheck; 029import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 030import com.puppycrawl.tools.checkstyle.api.DetailAST; 031import com.puppycrawl.tools.checkstyle.api.FullIdent; 032import com.puppycrawl.tools.checkstyle.api.TokenTypes; 033import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; 034import com.puppycrawl.tools.checkstyle.utils.CheckUtil; 035 036/** 037 * <div> 038 * Checks that specified types are not declared to be thrown. 039 * Declaring that a method throws {@code java.lang.Error} or 040 * {@code java.lang.RuntimeException} is almost never acceptable. 041 * </div> 042 * 043 * @since 4.0 044 */ 045@StatelessCheck 046public final class IllegalThrowsCheck extends AbstractCheck { 047 048 /** 049 * A key is pointing to the warning message text in "messages.properties" 050 * file. 051 */ 052 public static final String MSG_KEY = "illegal.throw"; 053 054 /** Specify names of methods to ignore. */ 055 private final Set<String> ignoredMethodNames = 056 Arrays.stream(new String[] {"finalize", }).collect(Collectors.toCollection(HashSet::new)); 057 058 /** Specify throw class names to reject. */ 059 private final Set<String> illegalClassNames = Arrays.stream( 060 new String[] {"Error", "RuntimeException", "Throwable", "java.lang.Error", 061 "java.lang.RuntimeException", "java.lang.Throwable", }) 062 .collect(Collectors.toCollection(HashSet::new)); 063 064 /** 065 * Allow to ignore checking overridden methods (marked with {@code Override} 066 * or {@code java.lang.Override} annotation). 067 */ 068 private boolean ignoreOverriddenMethods = true; 069 070 /** 071 * Creates a new {@code IllegalThrowsCheck} instance. 072 */ 073 public IllegalThrowsCheck() { 074 // no code by default 075 } 076 077 /** 078 * Setter to specify throw class names to reject. 079 * 080 * @param classNames 081 * array of illegal exception classes 082 * @since 4.0 083 */ 084 public void setIllegalClassNames(final String... classNames) { 085 illegalClassNames.clear(); 086 illegalClassNames.addAll( 087 CheckUtil.parseClassNames(classNames)); 088 } 089 090 @Override 091 public int[] getDefaultTokens() { 092 return getRequiredTokens(); 093 } 094 095 @Override 096 public int[] getRequiredTokens() { 097 return new int[] {TokenTypes.LITERAL_THROWS}; 098 } 099 100 @Override 101 public int[] getAcceptableTokens() { 102 return getRequiredTokens(); 103 } 104 105 @Override 106 public void visitToken(DetailAST detailAST) { 107 final DetailAST methodDef = detailAST.getParent(); 108 // Check if the method with the given name should be ignored. 109 if (!isIgnorableMethod(methodDef)) { 110 DetailAST token = detailAST.getFirstChild(); 111 while (token != null) { 112 final FullIdent ident = FullIdent.createFullIdent(token); 113 final String identText = ident.getText(); 114 if (illegalClassNames.contains(identText)) { 115 log(token, MSG_KEY, identText); 116 } 117 token = token.getNextSibling(); 118 } 119 } 120 } 121 122 /** 123 * Checks if current method is ignorable due to Check's properties. 124 * 125 * @param methodDef {@link TokenTypes#METHOD_DEF METHOD_DEF} 126 * @return true if method is ignorable. 127 */ 128 private boolean isIgnorableMethod(DetailAST methodDef) { 129 return shouldIgnoreMethod(methodDef.findFirstToken(TokenTypes.IDENT).getText()) 130 || ignoreOverriddenMethods 131 && AnnotationUtil.hasOverrideAnnotation(methodDef); 132 } 133 134 /** 135 * Check if the method is specified in the ignore method list. 136 * 137 * @param name the name to check 138 * @return whether the method with the passed name should be ignored 139 */ 140 private boolean shouldIgnoreMethod(String name) { 141 return ignoredMethodNames.contains(name); 142 } 143 144 /** 145 * Setter to specify names of methods to ignore. 146 * 147 * @param methodNames array of ignored method names 148 * @since 5.4 149 */ 150 public void setIgnoredMethodNames(String... methodNames) { 151 ignoredMethodNames.clear(); 152 Collections.addAll(ignoredMethodNames, methodNames); 153 } 154 155 /** 156 * Setter to allow to ignore checking overridden methods 157 * (marked with {@code Override} or {@code java.lang.Override} annotation). 158 * 159 * @param ignoreOverriddenMethods Check's property. 160 * @since 6.4 161 */ 162 public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { 163 this.ignoreOverriddenMethods = ignoreOverriddenMethods; 164 } 165 166}