001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.Arrays;
023import java.util.LinkedList;
024import java.util.List;
025import java.util.Set;
026import java.util.stream.Collectors;
027
028import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
029import com.puppycrawl.tools.checkstyle.api.DetailAST;
030import com.puppycrawl.tools.checkstyle.api.FullIdent;
031import com.puppycrawl.tools.checkstyle.api.TokenTypes;
032import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
033
034/**
035 * Catching java.lang.Exception, java.lang.Error or java.lang.RuntimeException
036 * is almost never acceptable.
037 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
038 * @author <a href="mailto:IliaDubinin91@gmail.com">Ilja Dubinin</a>
039 */
040public final class IllegalCatchCheck extends AbstractCheck {
041
042    /**
043     * A key is pointing to the warning message text in "messages.properties"
044     * file.
045     */
046    public static final String MSG_KEY = "illegal.catch";
047
048    /** Illegal class names. */
049    private final Set<String> illegalClassNames = Arrays.stream(new String[] {"Exception", "Error",
050        "RuntimeException", "Throwable", "java.lang.Error", "java.lang.Exception",
051        "java.lang.RuntimeException", "java.lang.Throwable", }).collect(Collectors.toSet());
052
053    /**
054     * Set the list of illegal classes.
055     *
056     * @param classNames
057     *            array of illegal exception classes
058     */
059    public void setIllegalClassNames(final String... classNames) {
060        illegalClassNames.clear();
061        illegalClassNames.addAll(
062                CheckUtils.parseClassNames(classNames));
063    }
064
065    @Override
066    public int[] getDefaultTokens() {
067        return new int[] {TokenTypes.LITERAL_CATCH};
068    }
069
070    @Override
071    public int[] getRequiredTokens() {
072        return getDefaultTokens();
073    }
074
075    @Override
076    public int[] getAcceptableTokens() {
077        return new int[] {TokenTypes.LITERAL_CATCH};
078    }
079
080    @Override
081    public void visitToken(DetailAST detailAST) {
082        final DetailAST parameterDef =
083            detailAST.findFirstToken(TokenTypes.PARAMETER_DEF);
084        final DetailAST excTypeParent =
085                parameterDef.findFirstToken(TokenTypes.TYPE);
086        final List<DetailAST> excTypes = getAllExceptionTypes(excTypeParent);
087
088        for (DetailAST excType : excTypes) {
089            final FullIdent ident = FullIdent.createFullIdent(excType);
090
091            if (illegalClassNames.contains(ident.getText())) {
092                log(detailAST, MSG_KEY, ident.getText());
093            }
094        }
095    }
096
097    /**
098     * Finds all exception types in current catch.
099     * We need it till we can have few different exception types into one catch.
100     * @param parentToken - parent node for types (TYPE or BOR)
101     * @return list, that contains all exception types in current catch
102     */
103    private static List<DetailAST> getAllExceptionTypes(DetailAST parentToken) {
104        DetailAST currentNode = parentToken.getFirstChild();
105        final List<DetailAST> exceptionTypes = new LinkedList<>();
106        if (currentNode.getType() == TokenTypes.BOR) {
107            exceptionTypes.addAll(getAllExceptionTypes(currentNode));
108            currentNode = currentNode.getNextSibling();
109            if (currentNode != null) {
110                exceptionTypes.add(currentNode);
111            }
112        }
113        else {
114            exceptionTypes.add(currentNode);
115            currentNode = currentNode.getNextSibling();
116            while (currentNode != null) {
117                exceptionTypes.add(currentNode);
118                currentNode = currentNode.getNextSibling();
119            }
120        }
121        return exceptionTypes;
122    }
123}