001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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.Set; 025import java.util.stream.Collectors; 026 027import com.puppycrawl.tools.checkstyle.StatelessCheck; 028import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 029import com.puppycrawl.tools.checkstyle.api.DetailAST; 030import com.puppycrawl.tools.checkstyle.api.FullIdent; 031import com.puppycrawl.tools.checkstyle.api.TokenTypes; 032import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; 033import com.puppycrawl.tools.checkstyle.utils.CheckUtil; 034 035/** 036 * <p> 037 * Checks that specified types are not declared to be thrown. 038 * Declaring that a method throws {@code java.lang.Error} or 039 * {@code java.lang.RuntimeException} is almost never acceptable. 040 * </p> 041 * <ul> 042 * <li> 043 * Property {@code illegalClassNames} - Specify throw class names to reject. 044 * Type is {@code java.lang.String[]}. 045 * Default value is {@code Error, RuntimeException, Throwable, java.lang.Error, 046 * java.lang.RuntimeException, java.lang.Throwable}. 047 * </li> 048 * <li> 049 * Property {@code ignoredMethodNames} - Specify names of methods to ignore. 050 * Type is {@code java.lang.String[]}. 051 * Default value is {@code finalize}. 052 * </li> 053 * <li> 054 * Property {@code ignoreOverriddenMethods} - allow to ignore checking overridden methods 055 * (marked with {@code Override} or {@code java.lang.Override} annotation). 056 * Type is {@code boolean}. 057 * Default value is {@code true}. 058 * </li> 059 * </ul> 060 * <p> 061 * To configure the check: 062 * </p> 063 * <pre> 064 * <module name="IllegalThrows"/> 065 * </pre> 066 * <p> 067 * To configure the check rejecting throws NullPointerException from methods: 068 * </p> 069 * <pre> 070 * <module name="IllegalThrows"> 071 * <property name="illegalClassNames" value="NullPointerException"/> 072 * </module> 073 * </pre> 074 * <p> 075 * To configure the check ignoring method named "foo()": 076 * </p> 077 * <pre> 078 * <module name="IllegalThrows"> 079 * <property name="ignoredMethodNames" value="foo"/> 080 * </module> 081 * </pre> 082 * <p> 083 * To configure the check to warn on overridden methods: 084 * </p> 085 * <pre> 086 * <module name="IllegalThrows"> 087 * <property name="ignoreOverriddenMethods" value="false"/> 088 * </module> 089 * </pre> 090 * <p> 091 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 092 * </p> 093 * <p> 094 * Violation Message Keys: 095 * </p> 096 * <ul> 097 * <li> 098 * {@code illegal.throw} 099 * </li> 100 * </ul> 101 * 102 * @since 4.0 103 */ 104@StatelessCheck 105public final class IllegalThrowsCheck extends AbstractCheck { 106 107 /** 108 * A key is pointing to the warning message text in "messages.properties" 109 * file. 110 */ 111 public static final String MSG_KEY = "illegal.throw"; 112 113 /** Specify names of methods to ignore. */ 114 private final Set<String> ignoredMethodNames = 115 Arrays.stream(new String[] {"finalize", }).collect(Collectors.toSet()); 116 117 /** Specify throw class names to reject. */ 118 private final Set<String> illegalClassNames = Arrays.stream( 119 new String[] {"Error", "RuntimeException", "Throwable", "java.lang.Error", 120 "java.lang.RuntimeException", "java.lang.Throwable", }) 121 .collect(Collectors.toSet()); 122 123 /** 124 * Allow to ignore checking overridden methods (marked with {@code Override} 125 * or {@code java.lang.Override} annotation). 126 */ 127 private boolean ignoreOverriddenMethods = true; 128 129 /** 130 * Setter to specify throw class names to reject. 131 * 132 * @param classNames 133 * array of illegal exception classes 134 */ 135 public void setIllegalClassNames(final String... classNames) { 136 illegalClassNames.clear(); 137 illegalClassNames.addAll( 138 CheckUtil.parseClassNames(classNames)); 139 } 140 141 @Override 142 public int[] getDefaultTokens() { 143 return getRequiredTokens(); 144 } 145 146 @Override 147 public int[] getRequiredTokens() { 148 return new int[] {TokenTypes.LITERAL_THROWS}; 149 } 150 151 @Override 152 public int[] getAcceptableTokens() { 153 return getRequiredTokens(); 154 } 155 156 @Override 157 public void visitToken(DetailAST detailAST) { 158 final DetailAST methodDef = detailAST.getParent(); 159 // Check if the method with the given name should be ignored. 160 if (!isIgnorableMethod(methodDef)) { 161 DetailAST token = detailAST.getFirstChild(); 162 while (token != null) { 163 final FullIdent ident = FullIdent.createFullIdent(token); 164 if (illegalClassNames.contains(ident.getText())) { 165 log(token, MSG_KEY, ident.getText()); 166 } 167 token = token.getNextSibling(); 168 } 169 } 170 } 171 172 /** 173 * Checks if current method is ignorable due to Check's properties. 174 * 175 * @param methodDef {@link TokenTypes#METHOD_DEF METHOD_DEF} 176 * @return true if method is ignorable. 177 */ 178 private boolean isIgnorableMethod(DetailAST methodDef) { 179 return shouldIgnoreMethod(methodDef.findFirstToken(TokenTypes.IDENT).getText()) 180 || ignoreOverriddenMethods 181 && (AnnotationUtil.containsAnnotation(methodDef, "Override") 182 || AnnotationUtil.containsAnnotation(methodDef, "java.lang.Override")); 183 } 184 185 /** 186 * Check if the method is specified in the ignore method list. 187 * 188 * @param name the name to check 189 * @return whether the method with the passed name should be ignored 190 */ 191 private boolean shouldIgnoreMethod(String name) { 192 return ignoredMethodNames.contains(name); 193 } 194 195 /** 196 * Setter to specify names of methods to ignore. 197 * 198 * @param methodNames array of ignored method names 199 */ 200 public void setIgnoredMethodNames(String... methodNames) { 201 ignoredMethodNames.clear(); 202 Collections.addAll(ignoredMethodNames, methodNames); 203 } 204 205 /** 206 * Setter to allow to ignore checking overridden methods 207 * (marked with {@code Override} or {@code java.lang.Override} annotation). 208 * 209 * @param ignoreOverriddenMethods Check's property. 210 */ 211 public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { 212 this.ignoreOverriddenMethods = ignoreOverriddenMethods; 213 } 214 215}