View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * <ul>
43   * <li>
44   * Property {@code ignoreOverriddenMethods} - Allow to ignore checking overridden methods
45   * (marked with {@code Override} or {@code java.lang.Override} annotation).
46   * Type is {@code boolean}.
47   * Default value is {@code true}.
48   * </li>
49   * <li>
50   * Property {@code ignoredMethodNames} - Specify names of methods to ignore.
51   * Type is {@code java.lang.String[]}.
52   * Default value is {@code finalize}.
53   * </li>
54   * <li>
55   * Property {@code illegalClassNames} - Specify throw class names to reject.
56   * Type is {@code java.lang.String[]}.
57   * Default value is {@code Error, RuntimeException, Throwable, java.lang.Error,
58   * java.lang.RuntimeException, java.lang.Throwable}.
59   * </li>
60   * </ul>
61   *
62   * <p>
63   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
64   * </p>
65   *
66   * <p>
67   * Violation Message Keys:
68   * </p>
69   * <ul>
70   * <li>
71   * {@code illegal.throw}
72   * </li>
73   * </ul>
74   *
75   * @since 4.0
76   */
77  @StatelessCheck
78  public final class IllegalThrowsCheck extends AbstractCheck {
79  
80      /**
81       * A key is pointing to the warning message text in "messages.properties"
82       * file.
83       */
84      public static final String MSG_KEY = "illegal.throw";
85  
86      /** Specify names of methods to ignore. */
87      private final Set<String> ignoredMethodNames =
88          Arrays.stream(new String[] {"finalize", }).collect(Collectors.toCollection(HashSet::new));
89  
90      /** Specify throw class names to reject. */
91      private final Set<String> illegalClassNames = Arrays.stream(
92          new String[] {"Error", "RuntimeException", "Throwable", "java.lang.Error",
93                        "java.lang.RuntimeException", "java.lang.Throwable", })
94          .collect(Collectors.toCollection(HashSet::new));
95  
96      /**
97       * Allow to ignore checking overridden methods (marked with {@code Override}
98       * or {@code java.lang.Override} annotation).
99       */
100     private boolean ignoreOverriddenMethods = true;
101 
102     /**
103      * Setter to specify throw class names to reject.
104      *
105      * @param classNames
106      *            array of illegal exception classes
107      * @since 4.0
108      */
109     public void setIllegalClassNames(final String... classNames) {
110         illegalClassNames.clear();
111         illegalClassNames.addAll(
112                 CheckUtil.parseClassNames(classNames));
113     }
114 
115     @Override
116     public int[] getDefaultTokens() {
117         return getRequiredTokens();
118     }
119 
120     @Override
121     public int[] getRequiredTokens() {
122         return new int[] {TokenTypes.LITERAL_THROWS};
123     }
124 
125     @Override
126     public int[] getAcceptableTokens() {
127         return getRequiredTokens();
128     }
129 
130     @Override
131     public void visitToken(DetailAST detailAST) {
132         final DetailAST methodDef = detailAST.getParent();
133         // Check if the method with the given name should be ignored.
134         if (!isIgnorableMethod(methodDef)) {
135             DetailAST token = detailAST.getFirstChild();
136             while (token != null) {
137                 final FullIdent ident = FullIdent.createFullIdent(token);
138                 final String identText = ident.getText();
139                 if (illegalClassNames.contains(identText)) {
140                     log(token, MSG_KEY, identText);
141                 }
142                 token = token.getNextSibling();
143             }
144         }
145     }
146 
147     /**
148      * Checks if current method is ignorable due to Check's properties.
149      *
150      * @param methodDef {@link TokenTypes#METHOD_DEF METHOD_DEF}
151      * @return true if method is ignorable.
152      */
153     private boolean isIgnorableMethod(DetailAST methodDef) {
154         return shouldIgnoreMethod(methodDef.findFirstToken(TokenTypes.IDENT).getText())
155             || ignoreOverriddenMethods
156                && AnnotationUtil.hasOverrideAnnotation(methodDef);
157     }
158 
159     /**
160      * Check if the method is specified in the ignore method list.
161      *
162      * @param name the name to check
163      * @return whether the method with the passed name should be ignored
164      */
165     private boolean shouldIgnoreMethod(String name) {
166         return ignoredMethodNames.contains(name);
167     }
168 
169     /**
170      * Setter to specify names of methods to ignore.
171      *
172      * @param methodNames array of ignored method names
173      * @since 5.4
174      */
175     public void setIgnoredMethodNames(String... methodNames) {
176         ignoredMethodNames.clear();
177         Collections.addAll(ignoredMethodNames, methodNames);
178     }
179 
180     /**
181      * Setter to allow to ignore checking overridden methods
182      * (marked with {@code Override} or {@code java.lang.Override} annotation).
183      *
184      * @param ignoreOverriddenMethods Check's property.
185      * @since 6.4
186      */
187     public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) {
188         this.ignoreOverriddenMethods = ignoreOverriddenMethods;
189     }
190 
191 }