1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 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.HashSet;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27
28 import com.puppycrawl.tools.checkstyle.StatelessCheck;
29 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
30 import com.puppycrawl.tools.checkstyle.api.DetailAST;
31 import com.puppycrawl.tools.checkstyle.api.FullIdent;
32 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33 import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
34 import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
35
36 /**
37 * <div>
38 * Checks that specified types are not declared to be thrown.
39 * Declaring that a method throws {@code java.lang.Error} or
40 * {@code java.lang.RuntimeException} is almost never acceptable.
41 * </div>
42 *
43 * @since 4.0
44 */
45 @StatelessCheck
46 public final class IllegalThrowsCheck extends AbstractCheck {
47
48 /**
49 * A key is pointing to the warning message text in "messages.properties"
50 * file.
51 */
52 public static final String MSG_KEY = "illegal.throw";
53
54 /** Specify names of methods to ignore. */
55 private final Set<String> ignoredMethodNames =
56 Arrays.stream(new String[] {"finalize", }).collect(Collectors.toCollection(HashSet::new));
57
58 /** Specify throw class names to reject. */
59 private final Set<String> illegalClassNames = Arrays.stream(
60 new String[] {"Error", "RuntimeException", "Throwable", "java.lang.Error",
61 "java.lang.RuntimeException", "java.lang.Throwable", })
62 .collect(Collectors.toCollection(HashSet::new));
63
64 /**
65 * Allow to ignore checking overridden methods (marked with {@code Override}
66 * or {@code java.lang.Override} annotation).
67 */
68 private boolean ignoreOverriddenMethods = true;
69
70 /**
71 * Setter to specify throw class names to reject.
72 *
73 * @param classNames
74 * array of illegal exception classes
75 * @since 4.0
76 */
77 public void setIllegalClassNames(final String... classNames) {
78 illegalClassNames.clear();
79 illegalClassNames.addAll(
80 CheckUtil.parseClassNames(classNames));
81 }
82
83 @Override
84 public int[] getDefaultTokens() {
85 return getRequiredTokens();
86 }
87
88 @Override
89 public int[] getRequiredTokens() {
90 return new int[] {TokenTypes.LITERAL_THROWS};
91 }
92
93 @Override
94 public int[] getAcceptableTokens() {
95 return getRequiredTokens();
96 }
97
98 @Override
99 public void visitToken(DetailAST detailAST) {
100 final DetailAST methodDef = detailAST.getParent();
101 // Check if the method with the given name should be ignored.
102 if (!isIgnorableMethod(methodDef)) {
103 DetailAST token = detailAST.getFirstChild();
104 while (token != null) {
105 final FullIdent ident = FullIdent.createFullIdent(token);
106 final String identText = ident.getText();
107 if (illegalClassNames.contains(identText)) {
108 log(token, MSG_KEY, identText);
109 }
110 token = token.getNextSibling();
111 }
112 }
113 }
114
115 /**
116 * Checks if current method is ignorable due to Check's properties.
117 *
118 * @param methodDef {@link TokenTypes#METHOD_DEF METHOD_DEF}
119 * @return true if method is ignorable.
120 */
121 private boolean isIgnorableMethod(DetailAST methodDef) {
122 return shouldIgnoreMethod(methodDef.findFirstToken(TokenTypes.IDENT).getText())
123 || ignoreOverriddenMethods
124 && AnnotationUtil.hasOverrideAnnotation(methodDef);
125 }
126
127 /**
128 * Check if the method is specified in the ignore method list.
129 *
130 * @param name the name to check
131 * @return whether the method with the passed name should be ignored
132 */
133 private boolean shouldIgnoreMethod(String name) {
134 return ignoredMethodNames.contains(name);
135 }
136
137 /**
138 * Setter to specify names of methods to ignore.
139 *
140 * @param methodNames array of ignored method names
141 * @since 5.4
142 */
143 public void setIgnoredMethodNames(String... methodNames) {
144 ignoredMethodNames.clear();
145 Collections.addAll(ignoredMethodNames, methodNames);
146 }
147
148 /**
149 * Setter to allow to ignore checking overridden methods
150 * (marked with {@code Override} or {@code java.lang.Override} annotation).
151 *
152 * @param ignoreOverriddenMethods Check's property.
153 * @since 6.4
154 */
155 public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) {
156 this.ignoreOverriddenMethods = ignoreOverriddenMethods;
157 }
158
159 }