1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import java.util.ArrayList; import java.util.List;
public class ParameterNoAssignCheck extends Check {
private int[] assignTokenTypes = { TokenTypes.ASSIGN, TokenTypes.PLUS_ASSIGN, TokenTypes.MINUS_ASSIGN, TokenTypes.STAR_ASSIGN, TokenTypes.DIV_ASSIGN, TokenTypes.MOD_ASSIGN, TokenTypes.SR_ASSIGN, TokenTypes.BSR_ASSIGN, TokenTypes.SL_ASSIGN, TokenTypes.BAND_ASSIGN, TokenTypes.BXOR_ASSIGN, TokenTypes.BOR_ASSIGN };
@Override public int[] getDefaultTokens() { return assignTokenTypes; } @Override public void visitToken(DetailAST assignAST) { if (null == assignAST) { return; } DetailAST leftVarAST = assignAST.findFirstToken(TokenTypes.IDENT); if (null == leftVarAST) { return; } String leftVar = leftVarAST.getText(); DetailAST methodDefAST = findParentMethodDefBy(assignAST); if (null != methodDefAST) { List<String> parameters = findMethodParameterNames(methodDefAST); if (parameters.contains(leftVar)) { log(leftVarAST.getLineNo(), "Method parameter [" + leftVar + "] should not assign!"); } } } private List<String> findMethodParameterNames(DetailAST methodDefAST) { List<String> parameters = new ArrayList<String>(); if (null != methodDefAST) { DetailAST parametersAST = methodDefAST.findFirstToken(TokenTypes.PARAMETERS); if (null != parametersAST) { DetailAST parameterDefAST = parametersAST.getFirstChild(); while (null != parameterDefAST) { if (parameterDefAST.getType() == TokenTypes.PARAMETER_DEF) { String parameterName = parameterDefAST.findFirstToken(TokenTypes.IDENT).getText(); parameters.add(parameterName); } parameterDefAST = parameterDefAST.getNextSibling(); } } } return parameters; }
private DetailAST findParentMethodDefBy(DetailAST aAST) { if (null == aAST || aAST.getType() == TokenTypes.METHOD_DEF) { return aAST; } else { return findParentMethodDefBy(aAST.getParent()); } }
}
|