1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2025 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.checks.design; 21 22 import java.util.ArrayDeque; 23 import java.util.Deque; 24 import java.util.regex.Pattern; 25 26 import com.puppycrawl.tools.checkstyle.FileStatefulCheck; 27 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 28 import com.puppycrawl.tools.checkstyle.api.DetailAST; 29 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 30 31 /** 32 * <div> 33 * Ensures that exception classes (classes with names conforming to some pattern 34 * and explicitly extending classes with names conforming to other 35 * pattern) are immutable, that is, that they have only final fields. 36 * </div> 37 * 38 * <p> 39 * The current algorithm is very simple: it checks that all members of exception are final. 40 * The user can still mutate an exception's instance (e.g. Throwable has a method called 41 * {@code setStackTrace} which changes the exception's stack trace). But, at least, all 42 * information provided by this exception type is unchangeable. 43 * </p> 44 * 45 * <p> 46 * Rationale: Exception instances should represent an error 47 * condition. Having non-final fields not only allows the state to be 48 * modified by accident and therefore mask the original condition but 49 * also allows developers to accidentally forget to set the initial state. 50 * In both cases, code catching the exception could draw incorrect 51 * conclusions based on the state. 52 * </p> 53 * <ul> 54 * <li> 55 * Property {@code extendedClassNameFormat} - Specify pattern for extended class names. 56 * Type is {@code java.util.regex.Pattern}. 57 * Default value is {@code "^.*Exception$|^.*Error$|^.*Throwable$"}. 58 * </li> 59 * <li> 60 * Property {@code format} - Specify pattern for exception class names. 61 * Type is {@code java.util.regex.Pattern}. 62 * Default value is {@code "^.*Exception$|^.*Error$|^.*Throwable$"}. 63 * </li> 64 * </ul> 65 * 66 * <p> 67 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 68 * </p> 69 * 70 * <p> 71 * Violation Message Keys: 72 * </p> 73 * <ul> 74 * <li> 75 * {@code mutable.exception} 76 * </li> 77 * </ul> 78 * 79 * @since 3.2 80 */ 81 @FileStatefulCheck 82 public final class MutableExceptionCheck extends AbstractCheck { 83 84 /** 85 * A key is pointing to the warning message text in "messages.properties" 86 * file. 87 */ 88 public static final String MSG_KEY = "mutable.exception"; 89 90 /** Default value for format and extendedClassNameFormat properties. */ 91 private static final String DEFAULT_FORMAT = "^.*Exception$|^.*Error$|^.*Throwable$"; 92 /** Stack of checking information for classes. */ 93 private final Deque<Boolean> checkingStack = new ArrayDeque<>(); 94 /** Specify pattern for extended class names. */ 95 private Pattern extendedClassNameFormat = Pattern.compile(DEFAULT_FORMAT); 96 /** Should we check current class or not. */ 97 private boolean checking; 98 /** Specify pattern for exception class names. */ 99 private Pattern format = extendedClassNameFormat; 100 101 /** 102 * Setter to specify pattern for extended class names. 103 * 104 * @param extendedClassNameFormat a {@code String} value 105 * @since 6.2 106 */ 107 public void setExtendedClassNameFormat(Pattern extendedClassNameFormat) { 108 this.extendedClassNameFormat = extendedClassNameFormat; 109 } 110 111 /** 112 * Setter to specify pattern for exception class names. 113 * 114 * @param pattern the new pattern 115 * @since 3.2 116 */ 117 public void setFormat(Pattern pattern) { 118 format = pattern; 119 } 120 121 @Override 122 public int[] getDefaultTokens() { 123 return getRequiredTokens(); 124 } 125 126 @Override 127 public int[] getRequiredTokens() { 128 return new int[] {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF}; 129 } 130 131 @Override 132 public int[] getAcceptableTokens() { 133 return getRequiredTokens(); 134 } 135 136 @Override 137 public void visitToken(DetailAST ast) { 138 switch (ast.getType()) { 139 case TokenTypes.CLASS_DEF -> visitClassDef(ast); 140 case TokenTypes.VARIABLE_DEF -> visitVariableDef(ast); 141 default -> throw new IllegalStateException(ast.toString()); 142 } 143 } 144 145 @Override 146 public void leaveToken(DetailAST ast) { 147 if (ast.getType() == TokenTypes.CLASS_DEF) { 148 leaveClassDef(); 149 } 150 } 151 152 /** 153 * Called when we start processing class definition. 154 * 155 * @param ast class definition node 156 */ 157 private void visitClassDef(DetailAST ast) { 158 checkingStack.push(checking); 159 checking = isNamedAsException(ast) && isExtendedClassNamedAsException(ast); 160 } 161 162 /** Called when we leave class definition. */ 163 private void leaveClassDef() { 164 checking = checkingStack.pop(); 165 } 166 167 /** 168 * Checks variable definition. 169 * 170 * @param ast variable def node for check 171 */ 172 private void visitVariableDef(DetailAST ast) { 173 if (checking && ast.getParent().getType() == TokenTypes.OBJBLOCK) { 174 final DetailAST modifiersAST = 175 ast.findFirstToken(TokenTypes.MODIFIERS); 176 177 if (modifiersAST.findFirstToken(TokenTypes.FINAL) == null) { 178 log(ast, MSG_KEY, ast.findFirstToken(TokenTypes.IDENT).getText()); 179 } 180 } 181 } 182 183 /** 184 * Checks that a class name conforms to specified format. 185 * 186 * @param ast class definition node 187 * @return true if a class name conforms to specified format 188 */ 189 private boolean isNamedAsException(DetailAST ast) { 190 final String className = ast.findFirstToken(TokenTypes.IDENT).getText(); 191 return format.matcher(className).find(); 192 } 193 194 /** 195 * Checks that if extended class name conforms to specified format. 196 * 197 * @param ast class definition node 198 * @return true if extended class name conforms to specified format 199 */ 200 private boolean isExtendedClassNamedAsException(DetailAST ast) { 201 boolean result = false; 202 final DetailAST extendsClause = ast.findFirstToken(TokenTypes.EXTENDS_CLAUSE); 203 if (extendsClause != null) { 204 DetailAST currentNode = extendsClause; 205 while (currentNode.getLastChild() != null) { 206 currentNode = currentNode.getLastChild(); 207 } 208 final String extendedClassName = currentNode.getText(); 209 result = extendedClassNameFormat.matcher(extendedClassName).matches(); 210 } 211 return result; 212 } 213 214 }