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