001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.coding; 021 022import java.util.LinkedList; 023import java.util.List; 024 025import com.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.api.FullIdent; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028 029/** 030 * Catching java.lang.Exception, java.lang.Error or java.lang.RuntimeException 031 * is almost never acceptable. 032 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> 033 * @author <a href="mailto:IliaDubinin91@gmail.com">Ilja Dubinin</a> 034 */ 035public final class IllegalCatchCheck extends AbstractIllegalCheck { 036 037 /** 038 * A key is pointing to the warning message text in "messages.properties" 039 * file. 040 */ 041 public static final String MSG_KEY = "illegal.catch"; 042 043 /** Creates new instance of the check. */ 044 public IllegalCatchCheck() { 045 super("Exception", "Error", "RuntimeException", "Throwable", "java.lang.Error", 046 "java.lang.Exception", "java.lang.RuntimeException", "java.lang.Throwable"); 047 } 048 049 @Override 050 public int[] getDefaultTokens() { 051 return new int[] {TokenTypes.LITERAL_CATCH}; 052 } 053 054 @Override 055 public int[] getRequiredTokens() { 056 return getDefaultTokens(); 057 } 058 059 @Override 060 public int[] getAcceptableTokens() { 061 return new int[] {TokenTypes.LITERAL_CATCH}; 062 } 063 064 @Override 065 public void visitToken(DetailAST detailAST) { 066 final DetailAST parameterDef = 067 detailAST.findFirstToken(TokenTypes.PARAMETER_DEF); 068 final DetailAST excTypeParent = 069 parameterDef.findFirstToken(TokenTypes.TYPE); 070 final List<DetailAST> excTypes = getAllExceptionTypes(excTypeParent); 071 072 for (DetailAST excType : excTypes) { 073 final FullIdent ident = FullIdent.createFullIdent(excType); 074 075 if (isIllegalClassName(ident.getText())) { 076 log(detailAST, MSG_KEY, ident.getText()); 077 } 078 } 079 } 080 081 /** 082 * Finds all exception types in current catch. 083 * We need it till we can have few different exception types into one catch. 084 * @param parentToken - parent node for types (TYPE or BOR) 085 * @return list, that contains all exception types in current catch 086 */ 087 public static List<DetailAST> getAllExceptionTypes(DetailAST parentToken) { 088 DetailAST currentNode = parentToken.getFirstChild(); 089 final List<DetailAST> exceptionTypes = new LinkedList<>(); 090 if (currentNode.getType() == TokenTypes.BOR) { 091 exceptionTypes.addAll(getAllExceptionTypes(currentNode)); 092 currentNode = currentNode.getNextSibling(); 093 if (currentNode != null) { 094 exceptionTypes.add(currentNode); 095 } 096 } 097 else { 098 exceptionTypes.add(currentNode); 099 currentNode = currentNode.getNextSibling(); 100 while (currentNode != null) { 101 exceptionTypes.add(currentNode); 102 currentNode = currentNode.getNextSibling(); 103 } 104 } 105 return exceptionTypes; 106 } 107}