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.HashSet;
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.CheckUtil;
33  
34  /**
35   * <div>
36   * Checks that certain exception types do not appear in a {@code catch} statement.
37   * </div>
38   *
39   * <p>
40   * Rationale: catching {@code java.lang.Exception}, {@code java.lang.Error} or
41   * {@code java.lang.RuntimeException} is almost never acceptable.
42   * Novice developers often simply catch Exception in an attempt to handle
43   * multiple exception classes. This unfortunately leads to code that inadvertently
44   * catches {@code NullPointerException}, {@code OutOfMemoryError}, etc.
45   * </p>
46   * <ul>
47   * <li>
48   * Property {@code illegalClassNames} - Specify exception class names to reject.
49   * Type is {@code java.lang.String[]}.
50   * Default value is {@code Error, Exception, RuntimeException, Throwable, java.lang.Error,
51   * java.lang.Exception, java.lang.RuntimeException, java.lang.Throwable}.
52   * </li>
53   * </ul>
54   *
55   * <p>
56   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
57   * </p>
58   *
59   * <p>
60   * Violation Message Keys:
61   * </p>
62   * <ul>
63   * <li>
64   * {@code illegal.catch}
65   * </li>
66   * </ul>
67   *
68   * @since 3.2
69   */
70  @StatelessCheck
71  public final class IllegalCatchCheck extends AbstractCheck {
72  
73      /**
74       * A key is pointing to the warning message text in "messages.properties"
75       * file.
76       */
77      public static final String MSG_KEY = "illegal.catch";
78  
79      /** Specify exception class names to reject. */
80      private final Set<String> illegalClassNames = Arrays.stream(new String[] {"Exception", "Error",
81          "RuntimeException", "Throwable", "java.lang.Error", "java.lang.Exception",
82          "java.lang.RuntimeException", "java.lang.Throwable", })
83              .collect(Collectors.toCollection(HashSet::new));
84  
85      /**
86       * Setter to specify exception class names to reject.
87       *
88       * @param classNames
89       *            array of illegal exception classes
90       * @since 3.2
91       */
92      public void setIllegalClassNames(final String... classNames) {
93          illegalClassNames.clear();
94          illegalClassNames.addAll(
95                  CheckUtil.parseClassNames(classNames));
96      }
97  
98      @Override
99      public int[] getDefaultTokens() {
100         return getRequiredTokens();
101     }
102 
103     @Override
104     public int[] getRequiredTokens() {
105         return new int[] {TokenTypes.LITERAL_CATCH};
106     }
107 
108     @Override
109     public int[] getAcceptableTokens() {
110         return getRequiredTokens();
111     }
112 
113     @Override
114     public void visitToken(DetailAST detailAST) {
115         final DetailAST parameterDef =
116             detailAST.findFirstToken(TokenTypes.PARAMETER_DEF);
117         final DetailAST excTypeParent =
118                 parameterDef.findFirstToken(TokenTypes.TYPE);
119 
120         DetailAST currentNode = excTypeParent.getFirstChild();
121         while (currentNode != null) {
122             final FullIdent ident = FullIdent.createFullIdent(currentNode);
123             final String identText = ident.getText();
124             if (illegalClassNames.contains(identText)) {
125                 log(detailAST, MSG_KEY, identText);
126             }
127             currentNode = currentNode.getNextSibling();
128         }
129     }
130 }