001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * <p> 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 * </p> 042 * <ul> 043 * <li> 044 * Property {@code ignoreOverriddenMethods} - Allow to ignore checking overridden methods 045 * (marked with {@code Override} or {@code java.lang.Override} annotation). 046 * Type is {@code boolean}. 047 * Default value is {@code true}. 048 * </li> 049 * <li> 050 * Property {@code ignoredMethodNames} - Specify names of methods to ignore. 051 * Type is {@code java.lang.String[]}. 052 * Default value is {@code finalize}. 053 * </li> 054 * <li> 055 * Property {@code illegalClassNames} - Specify throw class names to reject. 056 * Type is {@code java.lang.String[]}. 057 * Default value is {@code Error, RuntimeException, Throwable, java.lang.Error, 058 * java.lang.RuntimeException, java.lang.Throwable}. 059 * </li> 060 * </ul> 061 * <p> 062 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 063 * </p> 064 * <p> 065 * Violation Message Keys: 066 * </p> 067 * <ul> 068 * <li> 069 * {@code illegal.throw} 070 * </li> 071 * </ul> 072 * 073 * @since 4.0 074 */ 075@StatelessCheck 076public final class IllegalThrowsCheck extends AbstractCheck { 077 078 /** 079 * A key is pointing to the warning message text in "messages.properties" 080 * file. 081 */ 082 public static final String MSG_KEY = "illegal.throw"; 083 084 /** Specify names of methods to ignore. */ 085 private final Set<String> ignoredMethodNames = 086 Arrays.stream(new String[] {"finalize", }).collect(Collectors.toCollection(HashSet::new)); 087 088 /** Specify throw class names to reject. */ 089 private final Set<String> illegalClassNames = Arrays.stream( 090 new String[] {"Error", "RuntimeException", "Throwable", "java.lang.Error", 091 "java.lang.RuntimeException", "java.lang.Throwable", }) 092 .collect(Collectors.toCollection(HashSet::new)); 093 094 /** 095 * Allow to ignore checking overridden methods (marked with {@code Override} 096 * or {@code java.lang.Override} annotation). 097 */ 098 private boolean ignoreOverriddenMethods = true; 099 100 /** 101 * Setter to specify throw class names to reject. 102 * 103 * @param classNames 104 * array of illegal exception classes 105 * @since 4.0 106 */ 107 public void setIllegalClassNames(final String... classNames) { 108 illegalClassNames.clear(); 109 illegalClassNames.addAll( 110 CheckUtil.parseClassNames(classNames)); 111 } 112 113 @Override 114 public int[] getDefaultTokens() { 115 return getRequiredTokens(); 116 } 117 118 @Override 119 public int[] getRequiredTokens() { 120 return new int[] {TokenTypes.LITERAL_THROWS}; 121 } 122 123 @Override 124 public int[] getAcceptableTokens() { 125 return getRequiredTokens(); 126 } 127 128 @Override 129 public void visitToken(DetailAST detailAST) { 130 final DetailAST methodDef = detailAST.getParent(); 131 // Check if the method with the given name should be ignored. 132 if (!isIgnorableMethod(methodDef)) { 133 DetailAST token = detailAST.getFirstChild(); 134 while (token != null) { 135 final FullIdent ident = FullIdent.createFullIdent(token); 136 final String identText = ident.getText(); 137 if (illegalClassNames.contains(identText)) { 138 log(token, MSG_KEY, identText); 139 } 140 token = token.getNextSibling(); 141 } 142 } 143 } 144 145 /** 146 * Checks if current method is ignorable due to Check's properties. 147 * 148 * @param methodDef {@link TokenTypes#METHOD_DEF METHOD_DEF} 149 * @return true if method is ignorable. 150 */ 151 private boolean isIgnorableMethod(DetailAST methodDef) { 152 return shouldIgnoreMethod(methodDef.findFirstToken(TokenTypes.IDENT).getText()) 153 || ignoreOverriddenMethods 154 && AnnotationUtil.hasOverrideAnnotation(methodDef); 155 } 156 157 /** 158 * Check if the method is specified in the ignore method list. 159 * 160 * @param name the name to check 161 * @return whether the method with the passed name should be ignored 162 */ 163 private boolean shouldIgnoreMethod(String name) { 164 return ignoredMethodNames.contains(name); 165 } 166 167 /** 168 * Setter to specify names of methods to ignore. 169 * 170 * @param methodNames array of ignored method names 171 * @since 5.4 172 */ 173 public void setIgnoredMethodNames(String... methodNames) { 174 ignoredMethodNames.clear(); 175 Collections.addAll(ignoredMethodNames, methodNames); 176 } 177 178 /** 179 * Setter to allow to ignore checking overridden methods 180 * (marked with {@code Override} or {@code java.lang.Override} annotation). 181 * 182 * @param ignoreOverriddenMethods Check's property. 183 * @since 6.4 184 */ 185 public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { 186 this.ignoreOverriddenMethods = ignoreOverriddenMethods; 187 } 188 189}