1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 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 * Creates a new {@code IllegalThrowsCheck} instance.
72 */
73 public IllegalThrowsCheck() {
74 // no code by default
75 }
76
77 /**
78 * Setter to specify throw class names to reject.
79 *
80 * @param classNames
81 * array of illegal exception classes
82 * @since 4.0
83 */
84 public void setIllegalClassNames(final String... classNames) {
85 illegalClassNames.clear();
86 illegalClassNames.addAll(
87 CheckUtil.parseClassNames(classNames));
88 }
89
90 @Override
91 public int[] getDefaultTokens() {
92 return getRequiredTokens();
93 }
94
95 @Override
96 public int[] getRequiredTokens() {
97 return new int[] {TokenTypes.LITERAL_THROWS};
98 }
99
100 @Override
101 public int[] getAcceptableTokens() {
102 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 if (!isIgnorableMethod(methodDef)) {
110 DetailAST token = detailAST.getFirstChild();
111 while (token != null) {
112 final FullIdent ident = FullIdent.createFullIdent(token);
113 final String identText = ident.getText();
114 if (illegalClassNames.contains(identText)) {
115 log(token, MSG_KEY, identText);
116 }
117 token = token.getNextSibling();
118 }
119 }
120 }
121
122 /**
123 * Checks if current method is ignorable due to Check's properties.
124 *
125 * @param methodDef {@link TokenTypes#METHOD_DEF METHOD_DEF}
126 * @return true if method is ignorable.
127 */
128 private boolean isIgnorableMethod(DetailAST methodDef) {
129 return shouldIgnoreMethod(methodDef.findFirstToken(TokenTypes.IDENT).getText())
130 || ignoreOverriddenMethods
131 && AnnotationUtil.hasOverrideAnnotation(methodDef);
132 }
133
134 /**
135 * Check if the method is specified in the ignore method list.
136 *
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 return ignoredMethodNames.contains(name);
142 }
143
144 /**
145 * Setter to specify names of methods to ignore.
146 *
147 * @param methodNames array of ignored method names
148 * @since 5.4
149 */
150 public void setIgnoredMethodNames(String... methodNames) {
151 ignoredMethodNames.clear();
152 Collections.addAll(ignoredMethodNames, methodNames);
153 }
154
155 /**
156 * Setter to allow to ignore checking overridden methods
157 * (marked with {@code Override} or {@code java.lang.Override} annotation).
158 *
159 * @param ignoreOverriddenMethods Check's property.
160 * @since 6.4
161 */
162 public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) {
163 this.ignoreOverriddenMethods = ignoreOverriddenMethods;
164 }
165
166 }