1 package com.puppycrawl.tools.checkstyle.checks.coding; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.DetailAST; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import java.util.Stack ; 27 28 42 public final class ParameterAssignmentCheck extends Check 43 { 44 45 private final Stack mParameterNamesStack = new Stack (); 46 47 private Set mParameterNames; 48 49 50 public int[] getDefaultTokens() 51 { 52 return new int[] { 53 TokenTypes.CTOR_DEF, 54 TokenTypes.METHOD_DEF, 55 TokenTypes.ASSIGN, 56 TokenTypes.PLUS_ASSIGN, 57 TokenTypes.MINUS_ASSIGN, 58 TokenTypes.STAR_ASSIGN, 59 TokenTypes.DIV_ASSIGN, 60 TokenTypes.MOD_ASSIGN, 61 TokenTypes.SR_ASSIGN, 62 TokenTypes.BSR_ASSIGN, 63 TokenTypes.SL_ASSIGN, 64 TokenTypes.BAND_ASSIGN, 65 TokenTypes.BXOR_ASSIGN, 66 TokenTypes.BOR_ASSIGN, 67 TokenTypes.INC, 68 TokenTypes.POST_INC, 69 TokenTypes.DEC, 70 TokenTypes.POST_DEC, 71 }; 72 } 73 74 75 public int[] getRequiredTokens() 76 { 77 return getDefaultTokens(); 78 } 79 80 81 public void beginTree(DetailAST aRootAST) 82 { 83 mParameterNamesStack.clear(); 85 mParameterNames = null; 86 } 87 88 89 public void visitToken(DetailAST aAST) 90 { 91 switch (aAST.getType()) { 92 case TokenTypes.CTOR_DEF: 93 case TokenTypes.METHOD_DEF: 94 visitMethodDef(aAST); 95 break; 96 case TokenTypes.ASSIGN: 97 case TokenTypes.PLUS_ASSIGN: 98 case TokenTypes.MINUS_ASSIGN: 99 case TokenTypes.STAR_ASSIGN: 100 case TokenTypes.DIV_ASSIGN: 101 case TokenTypes.MOD_ASSIGN: 102 case TokenTypes.SR_ASSIGN: 103 case TokenTypes.BSR_ASSIGN: 104 case TokenTypes.SL_ASSIGN: 105 case TokenTypes.BAND_ASSIGN: 106 case TokenTypes.BXOR_ASSIGN: 107 case TokenTypes.BOR_ASSIGN: 108 visitAssign(aAST); 109 break; 110 case TokenTypes.INC: 111 case TokenTypes.POST_INC: 112 case TokenTypes.DEC: 113 case TokenTypes.POST_DEC: 114 visitIncDec(aAST); 115 break; 116 default: 117 throw new IllegalStateException (aAST.toString()); 118 } 119 } 120 121 122 public void leaveToken(DetailAST aAST) 123 { 124 switch (aAST.getType()) { 125 case TokenTypes.CTOR_DEF: 126 case TokenTypes.METHOD_DEF: 127 leaveMethodDef(); 128 break; 129 case TokenTypes.ASSIGN: 130 case TokenTypes.PLUS_ASSIGN: 131 case TokenTypes.MINUS_ASSIGN: 132 case TokenTypes.STAR_ASSIGN: 133 case TokenTypes.DIV_ASSIGN: 134 case TokenTypes.MOD_ASSIGN: 135 case TokenTypes.SR_ASSIGN: 136 case TokenTypes.BSR_ASSIGN: 137 case TokenTypes.SL_ASSIGN: 138 case TokenTypes.BAND_ASSIGN: 139 case TokenTypes.BXOR_ASSIGN: 140 case TokenTypes.BOR_ASSIGN: 141 case TokenTypes.INC: 142 case TokenTypes.POST_INC: 143 case TokenTypes.DEC: 144 case TokenTypes.POST_DEC: 145 break; 147 default: 148 throw new IllegalStateException (aAST.toString()); 149 } 150 } 151 152 156 private void visitAssign(DetailAST aAST) 157 { 158 checkIdent(aAST); 159 } 160 161 165 private void visitIncDec(DetailAST aAST) 166 { 167 checkIdent(aAST); 168 } 169 170 174 private void checkIdent(DetailAST aAST) 175 { 176 if ((mParameterNames != null) && !mParameterNames.isEmpty()) { 177 final DetailAST identAST = (DetailAST) aAST.getFirstChild(); 178 179 if ((identAST != null) 180 && (identAST.getType() == TokenTypes.IDENT) 181 && mParameterNames.contains(identAST.getText())) 182 { 183 log(aAST.getLineNo(), aAST.getColumnNo(), 184 "parameter.assignment", identAST.getText()); 185 } 186 } 187 } 188 189 193 private void visitMethodDef(DetailAST aAST) 194 { 195 mParameterNamesStack.push(mParameterNames); 196 mParameterNames = new HashSet (); 197 198 visitMethodParameters(aAST.findFirstToken(TokenTypes.PARAMETERS)); 199 } 200 201 202 private void leaveMethodDef() 203 { 204 mParameterNames = (Set ) mParameterNamesStack.pop(); 205 } 206 207 211 private void visitMethodParameters(DetailAST aAST) 212 { 213 DetailAST parameterDefAST = 214 aAST.findFirstToken(TokenTypes.PARAMETER_DEF); 215 216 for (; parameterDefAST != null; 217 parameterDefAST = (DetailAST) parameterDefAST.getNextSibling()) 218 { 219 if (parameterDefAST.getType() == TokenTypes.PARAMETER_DEF) { 220 final DetailAST param = 221 parameterDefAST.findFirstToken(TokenTypes.IDENT); 222 mParameterNames.add(param.getText()); 223 } 224 } 225 } 226 } 227 | Popular Tags |