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