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