001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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.design;
021
022import java.util.ArrayDeque;
023import java.util.Deque;
024import java.util.regex.Pattern;
025
026import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
027import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
028import com.puppycrawl.tools.checkstyle.api.DetailAST;
029import com.puppycrawl.tools.checkstyle.api.TokenTypes;
030
031/**
032 * <p> Ensures that exceptions (classes with names conforming to some regular
033 * expression and explicitly extending classes with names conforming to other
034 * regular expression) are immutable. That is, they have only final fields.</p>
035 * <p> Rationale: Exception instances should represent an error
036 * condition. Having non final fields not only allows the state to be
037 * modified by accident and therefore mask the original condition but
038 * also allows developers to accidentally forget to initialise state
039 * thereby leading to code catching the exception to draw incorrect
040 * conclusions based on the state.</p>
041 *
042 */
043@FileStatefulCheck
044public final class MutableExceptionCheck extends AbstractCheck {
045
046    /**
047     * A key is pointing to the warning message text in "messages.properties"
048     * file.
049     */
050    public static final String MSG_KEY = "mutable.exception";
051
052    /** Default value for format and extendedClassNameFormat properties. */
053    private static final String DEFAULT_FORMAT = "^.*Exception$|^.*Error$|^.*Throwable$";
054    /** Stack of checking information for classes. */
055    private final Deque<Boolean> checkingStack = new ArrayDeque<>();
056    /** Pattern for class name that is being extended. */
057    private Pattern extendedClassNameFormat = Pattern.compile(DEFAULT_FORMAT);
058    /** Should we check current class or not. */
059    private boolean checking;
060    /** The regexp to match against. */
061    private Pattern format = Pattern.compile(DEFAULT_FORMAT);
062
063    /**
064     * Sets the format of extended class name to the specified regular expression.
065     * @param extendedClassNameFormat a {@code String} value
066     */
067    public void setExtendedClassNameFormat(Pattern extendedClassNameFormat) {
068        this.extendedClassNameFormat = extendedClassNameFormat;
069    }
070
071    /**
072     * Set the format for the specified regular expression.
073     * @param pattern the new pattern
074     */
075    public void setFormat(Pattern pattern) {
076        format = pattern;
077    }
078
079    @Override
080    public int[] getDefaultTokens() {
081        return getRequiredTokens();
082    }
083
084    @Override
085    public int[] getRequiredTokens() {
086        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF};
087    }
088
089    @Override
090    public int[] getAcceptableTokens() {
091        return getRequiredTokens();
092    }
093
094    @Override
095    public void visitToken(DetailAST ast) {
096        switch (ast.getType()) {
097            case TokenTypes.CLASS_DEF:
098                visitClassDef(ast);
099                break;
100            case TokenTypes.VARIABLE_DEF:
101                visitVariableDef(ast);
102                break;
103            default:
104                throw new IllegalStateException(ast.toString());
105        }
106    }
107
108    @Override
109    public void leaveToken(DetailAST ast) {
110        if (ast.getType() == TokenTypes.CLASS_DEF) {
111            leaveClassDef();
112        }
113    }
114
115    /**
116     * Called when we start processing class definition.
117     * @param ast class definition node
118     */
119    private void visitClassDef(DetailAST ast) {
120        checkingStack.push(checking);
121        checking = isNamedAsException(ast) && isExtendedClassNamedAsException(ast);
122    }
123
124    /** Called when we leave class definition. */
125    private void leaveClassDef() {
126        checking = checkingStack.pop();
127    }
128
129    /**
130     * Checks variable definition.
131     * @param ast variable def node for check
132     */
133    private void visitVariableDef(DetailAST ast) {
134        if (checking && ast.getParent().getType() == TokenTypes.OBJBLOCK) {
135            final DetailAST modifiersAST =
136                ast.findFirstToken(TokenTypes.MODIFIERS);
137
138            if (modifiersAST.findFirstToken(TokenTypes.FINAL) == null) {
139                log(ast, MSG_KEY, ast.findFirstToken(TokenTypes.IDENT).getText());
140            }
141        }
142    }
143
144    /**
145     * Checks that a class name conforms to specified format.
146     * @param ast class definition node
147     * @return true if a class name conforms to specified format
148     */
149    private boolean isNamedAsException(DetailAST ast) {
150        final String className = ast.findFirstToken(TokenTypes.IDENT).getText();
151        return format.matcher(className).find();
152    }
153
154    /**
155     * Checks that if extended class name conforms to specified format.
156     * @param ast class definition node
157     * @return true if extended class name conforms to specified format
158     */
159    private boolean isExtendedClassNamedAsException(DetailAST ast) {
160        boolean result = false;
161        final DetailAST extendsClause = ast.findFirstToken(TokenTypes.EXTENDS_CLAUSE);
162        if (extendsClause != null) {
163            DetailAST currentNode = extendsClause;
164            while (currentNode.getLastChild() != null) {
165                currentNode = currentNode.getLastChild();
166            }
167            final String extendedClassName = currentNode.getText();
168            result = extendedClassNameFormat.matcher(extendedClassName).matches();
169        }
170        return result;
171    }
172
173}