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