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