001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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>
033 * Ensures that exception classes (classes with names conforming to some regular
034 * expression and explicitly extending classes with names conforming to other
035 * regular expression) are immutable, that is, that they have only final fields.
036 * </p>
037 * <p>
038 * The current algorithm is very simple: it checks that all members of exception are final.
039 * The user can still mutate an exception's instance (e.g. Throwable has a method called
040 * {@code setStackTrace} which changes the exception's stack trace). But, at least, all
041 * information provided by this exception type is unchangeable.
042 * </p>
043 * <p>
044 * Rationale: Exception instances should represent an error
045 * condition. Having non final fields not only allows the state to be
046 * modified by accident and therefore mask the original condition but
047 * also allows developers to accidentally forget to set the initial state.
048 * In both cases, code catching the exception could draw incorrect
049 * conclusions based on the state.
050 * </p>
051 * <ul>
052 * <li>
053 * Property {@code format} - Specify pattern for exception class names.
054 * Default value is {@code "^.*Exception$|^.*Error$|^.*Throwable$"}.
055 * </li>
056 * <li>
057 * Property {@code extendedClassNameFormat} - Specify pattern for extended class names.
058 * Default value is {@code "^.*Exception$|^.*Error$|^.*Throwable$"}.
059 * </li>
060 * </ul>
061 * <p>
062 * To configure the check:
063 * </p>
064 * <pre>
065 * &lt;module name=&quot;MutableException&quot;/&gt;
066 * </pre>
067 *
068 * @since 3.2
069 */
070@FileStatefulCheck
071public final class MutableExceptionCheck extends AbstractCheck {
072
073    /**
074     * A key is pointing to the warning message text in "messages.properties"
075     * file.
076     */
077    public static final String MSG_KEY = "mutable.exception";
078
079    /** Default value for format and extendedClassNameFormat properties. */
080    private static final String DEFAULT_FORMAT = "^.*Exception$|^.*Error$|^.*Throwable$";
081    /** Stack of checking information for classes. */
082    private final Deque<Boolean> checkingStack = new ArrayDeque<>();
083    /** Specify pattern for extended class names. */
084    private Pattern extendedClassNameFormat = Pattern.compile(DEFAULT_FORMAT);
085    /** Should we check current class or not. */
086    private boolean checking;
087    /** Specify pattern for exception class names. */
088    private Pattern format = Pattern.compile(DEFAULT_FORMAT);
089
090    /**
091     * Setter to specify pattern for extended class names.
092     *
093     * @param extendedClassNameFormat a {@code String} value
094     */
095    public void setExtendedClassNameFormat(Pattern extendedClassNameFormat) {
096        this.extendedClassNameFormat = extendedClassNameFormat;
097    }
098
099    /**
100     * Setter to specify pattern for exception class names.
101     *
102     * @param pattern the new pattern
103     */
104    public void setFormat(Pattern pattern) {
105        format = pattern;
106    }
107
108    @Override
109    public int[] getDefaultTokens() {
110        return getRequiredTokens();
111    }
112
113    @Override
114    public int[] getRequiredTokens() {
115        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF};
116    }
117
118    @Override
119    public int[] getAcceptableTokens() {
120        return getRequiredTokens();
121    }
122
123    @Override
124    public void visitToken(DetailAST ast) {
125        switch (ast.getType()) {
126            case TokenTypes.CLASS_DEF:
127                visitClassDef(ast);
128                break;
129            case TokenTypes.VARIABLE_DEF:
130                visitVariableDef(ast);
131                break;
132            default:
133                throw new IllegalStateException(ast.toString());
134        }
135    }
136
137    @Override
138    public void leaveToken(DetailAST ast) {
139        if (ast.getType() == TokenTypes.CLASS_DEF) {
140            leaveClassDef();
141        }
142    }
143
144    /**
145     * Called when we start processing class definition.
146     *
147     * @param ast class definition node
148     */
149    private void visitClassDef(DetailAST ast) {
150        checkingStack.push(checking);
151        checking = isNamedAsException(ast) && isExtendedClassNamedAsException(ast);
152    }
153
154    /** Called when we leave class definition. */
155    private void leaveClassDef() {
156        checking = checkingStack.pop();
157    }
158
159    /**
160     * Checks variable definition.
161     *
162     * @param ast variable def node for check
163     */
164    private void visitVariableDef(DetailAST ast) {
165        if (checking && ast.getParent().getType() == TokenTypes.OBJBLOCK) {
166            final DetailAST modifiersAST =
167                ast.findFirstToken(TokenTypes.MODIFIERS);
168
169            if (modifiersAST.findFirstToken(TokenTypes.FINAL) == null) {
170                log(ast, MSG_KEY, ast.findFirstToken(TokenTypes.IDENT).getText());
171            }
172        }
173    }
174
175    /**
176     * Checks that a class name conforms to specified format.
177     *
178     * @param ast class definition node
179     * @return true if a class name conforms to specified format
180     */
181    private boolean isNamedAsException(DetailAST ast) {
182        final String className = ast.findFirstToken(TokenTypes.IDENT).getText();
183        return format.matcher(className).find();
184    }
185
186    /**
187     * Checks that if extended class name conforms to specified format.
188     *
189     * @param ast class definition node
190     * @return true if extended class name conforms to specified format
191     */
192    private boolean isExtendedClassNamedAsException(DetailAST ast) {
193        boolean result = false;
194        final DetailAST extendsClause = ast.findFirstToken(TokenTypes.EXTENDS_CLAUSE);
195        if (extendsClause != null) {
196            DetailAST currentNode = extendsClause;
197            while (currentNode.getLastChild() != null) {
198                currentNode = currentNode.getLastChild();
199            }
200            final String extendedClassName = currentNode.getText();
201            result = extendedClassNameFormat.matcher(extendedClassName).matches();
202        }
203        return result;
204    }
205
206}