| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2018 the original author or authors. | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | ||
| 20 | package com.puppycrawl.tools.checkstyle.checks.coding; | |
| 21 | ||
| 22 | import java.util.Arrays; | |
| 23 | import java.util.Collections; | |
| 24 | import java.util.Set; | |
| 25 | import java.util.stream.Collectors; | |
| 26 | ||
| 27 | import com.puppycrawl.tools.checkstyle.StatelessCheck; | |
| 28 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
| 29 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 30 | import com.puppycrawl.tools.checkstyle.api.FullIdent; | |
| 31 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 32 | import com.puppycrawl.tools.checkstyle.utils.AnnotationUtility; | |
| 33 | import com.puppycrawl.tools.checkstyle.utils.CheckUtils; | |
| 34 | ||
| 35 | /** | |
| 36 | * <p> | |
| 37 | * Throwing java.lang.Error or java.lang.RuntimeException | |
| 38 | * is almost never acceptable. | |
| 39 | * </p> | |
| 40 | * Check has following properties: | |
| 41 | * <p> | |
| 42 | * <b>illegalClassNames</b> - throw class names to reject. | |
| 43 | * </p> | |
| 44 | * <p> | |
| 45 | * <b>ignoredMethodNames</b> - names of methods to ignore. | |
| 46 | * </p> | |
| 47 | * <p> | |
| 48 | * <b>ignoreOverriddenMethods</b> - ignore checking overridden methods (marked with Override | |
| 49 | * or java.lang.Override annotation) default value is <b>true</b>. | |
| 50 | * </p> | |
| 51 | * | |
| 52 | * @author Oliver Burn | |
| 53 | * @author John Sirois | |
| 54 | * @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a> | |
| 55 | */ | |
| 56 | @StatelessCheck | |
| 57 | public final class IllegalThrowsCheck extends AbstractCheck { | |
| 58 | ||
| 59 | /** | |
| 60 | * A key is pointing to the warning message text in "messages.properties" | |
| 61 | * file. | |
| 62 | */ | |
| 63 | public static final String MSG_KEY = "illegal.throw"; | |
| 64 | ||
| 65 | /** Methods which should be ignored. */ | |
| 66 | private final Set<String> ignoredMethodNames = | |
| 67 | Arrays.stream(new String[] {"finalize", }).collect(Collectors.toSet()); | |
| 68 | ||
| 69 | /** Illegal class names. */ | |
| 70 | private final Set<String> illegalClassNames = Arrays.stream( | |
| 71 | new String[] {"Error", "RuntimeException", "Throwable", "java.lang.Error", | |
| 72 | "java.lang.RuntimeException", "java.lang.Throwable", }) | |
| 73 | .collect(Collectors.toSet()); | |
| 74 | ||
| 75 | /** Property for ignoring overridden methods. */ | |
| 76 | private boolean ignoreOverriddenMethods = true; | |
| 77 | ||
| 78 | /** | |
| 79 | * Set the list of illegal classes. | |
| 80 | * | |
| 81 | * @param classNames | |
| 82 | * array of illegal exception classes | |
| 83 | */ | |
| 84 | public void setIllegalClassNames(final String... classNames) { | |
| 85 |
1
1. setIllegalClassNames : removed call to java/util/Set::clear → KILLED |
illegalClassNames.clear(); |
| 86 | illegalClassNames.addAll( | |
| 87 | CheckUtils.parseClassNames(classNames)); | |
| 88 | } | |
| 89 | ||
| 90 | @Override | |
| 91 | public int[] getDefaultTokens() { | |
| 92 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/IllegalThrowsCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
| 93 | } | |
| 94 | ||
| 95 | @Override | |
| 96 | public int[] getRequiredTokens() { | |
| 97 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/IllegalThrowsCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.LITERAL_THROWS}; |
| 98 | } | |
| 99 | ||
| 100 | @Override | |
| 101 | public int[] getAcceptableTokens() { | |
| 102 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/IllegalThrowsCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
| 103 | } | |
| 104 | ||
| 105 | @Override | |
| 106 | public void visitToken(DetailAST detailAST) { | |
| 107 | final DetailAST methodDef = detailAST.getParent(); | |
| 108 | // Check if the method with the given name should be ignored. | |
| 109 |
1
1. visitToken : negated conditional → KILLED |
if (!isIgnorableMethod(methodDef)) { |
| 110 | DetailAST token = detailAST.getFirstChild(); | |
| 111 |
1
1. visitToken : negated conditional → KILLED |
while (token != null) { |
| 112 |
1
1. visitToken : negated conditional → KILLED |
if (token.getType() != TokenTypes.COMMA) { |
| 113 | final FullIdent ident = FullIdent.createFullIdent(token); | |
| 114 |
1
1. visitToken : negated conditional → KILLED |
if (illegalClassNames.contains(ident.getText())) { |
| 115 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/coding/IllegalThrowsCheck::log → KILLED |
log(token, MSG_KEY, ident.getText()); |
| 116 | } | |
| 117 | } | |
| 118 | token = token.getNextSibling(); | |
| 119 | } | |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Checks if current method is ignorable due to Check's properties. | |
| 125 | * @param methodDef {@link TokenTypes#METHOD_DEF METHOD_DEF} | |
| 126 | * @return true if method is ignorable. | |
| 127 | */ | |
| 128 | private boolean isIgnorableMethod(DetailAST methodDef) { | |
| 129 |
3
1. isIgnorableMethod : negated conditional → KILLED 2. isIgnorableMethod : negated conditional → KILLED 3. isIgnorableMethod : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return shouldIgnoreMethod(methodDef.findFirstToken(TokenTypes.IDENT).getText()) |
| 130 | || ignoreOverriddenMethods | |
| 131 |
1
1. isIgnorableMethod : negated conditional → KILLED |
&& (AnnotationUtility.containsAnnotation(methodDef, "Override") |
| 132 |
1
1. isIgnorableMethod : negated conditional → KILLED |
|| AnnotationUtility.containsAnnotation(methodDef, "java.lang.Override")); |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * Check if the method is specified in the ignore method list. | |
| 137 | * @param name the name to check | |
| 138 | * @return whether the method with the passed name should be ignored | |
| 139 | */ | |
| 140 | private boolean shouldIgnoreMethod(String name) { | |
| 141 |
1
1. shouldIgnoreMethod : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return ignoredMethodNames.contains(name); |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * Set the list of ignore method names. | |
| 146 | * @param methodNames array of ignored method names | |
| 147 | */ | |
| 148 | public void setIgnoredMethodNames(String... methodNames) { | |
| 149 |
1
1. setIgnoredMethodNames : removed call to java/util/Set::clear → KILLED |
ignoredMethodNames.clear(); |
| 150 | Collections.addAll(ignoredMethodNames, methodNames); | |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Sets <b>ignoreOverriddenMethods</b> property value. | |
| 155 | * @param ignoreOverriddenMethods Check's property. | |
| 156 | */ | |
| 157 | public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { | |
| 158 | this.ignoreOverriddenMethods = ignoreOverriddenMethods; | |
| 159 | } | |
| 160 | ||
| 161 | } | |
Mutations | ||
| 85 |
1.1 |
|
| 92 |
1.1 |
|
| 97 |
1.1 |
|
| 102 |
1.1 |
|
| 109 |
1.1 |
|
| 111 |
1.1 |
|
| 112 |
1.1 |
|
| 114 |
1.1 |
|
| 115 |
1.1 |
|
| 129 |
1.1 2.2 3.3 |
|
| 131 |
1.1 |
|
| 132 |
1.1 |
|
| 141 |
1.1 |
|
| 149 |
1.1 |