1 package com.puppycrawl.tools.checkstyle.checks.design; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; 24 25 import java.util.Stack ; 26 27 40 public final class MutableExceptionCheck extends AbstractFormatCheck 41 { 42 43 private static final String DEFAULT_FORMAT = "^.*Exception$|^.*Error$"; 44 45 private final Stack mCheckingStack = new Stack (); 46 47 private boolean mChecking; 48 49 50 public MutableExceptionCheck() 51 { 52 super(DEFAULT_FORMAT); 53 } 54 55 56 public int[] getDefaultTokens() 57 { 58 return new int[] {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF}; 59 } 60 61 62 public int[] getRequiredTokens() 63 { 64 return getDefaultTokens(); 65 } 66 67 68 public void visitToken(DetailAST aAST) 69 { 70 switch (aAST.getType()) { 71 case TokenTypes.CLASS_DEF: 72 visitClassDef(aAST); 73 break; 74 case TokenTypes.VARIABLE_DEF: 75 visitVariableDef(aAST); 76 break; 77 default: 78 throw new IllegalStateException (aAST.toString()); 79 } 80 } 81 82 83 public void leaveToken(DetailAST aAST) 84 { 85 switch (aAST.getType()) { 86 case TokenTypes.CLASS_DEF: 87 leaveClassDef(); 88 break; 89 default: 90 } 92 } 93 94 98 private void visitClassDef(DetailAST aAST) 99 { 100 mCheckingStack.push(mChecking ? Boolean.TRUE : Boolean.FALSE); 101 mChecking = 102 isExceptionClass(aAST.findFirstToken(TokenTypes.IDENT).getText()); 103 } 104 105 106 private void leaveClassDef() 107 { 108 mChecking = ((Boolean ) mCheckingStack.pop()).booleanValue(); 109 } 110 111 115 private void visitVariableDef(DetailAST aAST) 116 { 117 if (mChecking && (aAST.getParent().getType() == TokenTypes.OBJBLOCK)) { 118 final DetailAST modifiersAST = 119 aAST.findFirstToken(TokenTypes.MODIFIERS); 120 121 if (!(modifiersAST.findFirstToken(TokenTypes.FINAL) != null)) { 122 log(aAST.getLineNo(), aAST.getColumnNo(), "mutable.exception", 123 aAST.findFirstToken(TokenTypes.IDENT).getText()); 124 } 125 } 126 } 127 128 132 private boolean isExceptionClass(String aClassName) 133 { 134 return getRegexp().matcher(aClassName).find(); 135 } 136 } 137 | Popular Tags |