001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.coding; 020 021import java.util.LinkedList; 022import java.util.List; 023 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.FullIdent; 026import com.puppycrawl.tools.checkstyle.api.TokenTypes; 027 028/** 029 * Catching java.lang.Exception, java.lang.Error or java.lang.RuntimeException 030 * is almost never acceptable. 031 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> 032 * @author <a href="mailto:IliaDubinin91@gmail.com">Ilja Dubinin</a> 033 */ 034public final class IllegalCatchCheck extends AbstractIllegalCheck 035{ 036 /** Creates new instance of the check. */ 037 public IllegalCatchCheck() 038 { 039 super(new String[] {"Exception", "Error", 040 "RuntimeException", "Throwable", 041 "java.lang.Error", 042 "java.lang.Exception", 043 "java.lang.RuntimeException", 044 "java.lang.Throwable", 045 }); 046 } 047 048 @Override 049 public int[] getDefaultTokens() 050 { 051 return new int[] {TokenTypes.LITERAL_CATCH}; 052 } 053 054 @Override 055 public int[] getRequiredTokens() 056 { 057 return getDefaultTokens(); 058 } 059 060 @Override 061 public void visitToken(DetailAST detailAST) 062 { 063 final DetailAST paradef = 064 detailAST.findFirstToken(TokenTypes.PARAMETER_DEF); 065 final DetailAST excTypeParent = 066 paradef.findFirstToken(TokenTypes.TYPE); 067 final List<DetailAST> excTypes = getAllExceptionTypes(excTypeParent); 068 069 for (DetailAST excType : excTypes) { 070 final FullIdent ident = FullIdent.createFullIdent(excType); 071 072 if (isIllegalClassName(ident.getText())) { 073 log(detailAST, "illegal.catch", ident.getText()); 074 } 075 } 076 } 077 078 /** 079 * Finds all exception types in current catch. 080 * We need it till we can have few different exception types into one catch. 081 * @param parentToken - parent node for types (TYPE or BOR) 082 * @return list, that contains all exception types in current catch 083 */ 084 public List<DetailAST> getAllExceptionTypes(DetailAST parentToken) 085 { 086 DetailAST currentNode = parentToken.getFirstChild(); 087 final List<DetailAST> exceptionTypes = new LinkedList<DetailAST>(); 088 if (currentNode.getType() == TokenTypes.BOR) { 089 exceptionTypes.addAll(getAllExceptionTypes(currentNode)); 090 currentNode = currentNode.getNextSibling(); 091 if (currentNode != null) { 092 exceptionTypes.add(currentNode); 093 } 094 } 095 else { 096 exceptionTypes.add(currentNode); 097 while ((currentNode = currentNode.getNextSibling()) != null) { 098 exceptionTypes.add(currentNode); 099 } 100 } 101 return exceptionTypes; 102 } 103}