1 package com.puppycrawl.tools.checkstyle.checks; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 import com.puppycrawl.tools.checkstyle.api.DetailAST; 24 25 36 public class FinalParametersCheck extends Check 37 { 38 39 public int[] getDefaultTokens() 40 { 41 return new int[] { 42 TokenTypes.METHOD_DEF, 43 TokenTypes.CTOR_DEF, 44 }; 45 } 46 47 48 public int[] getAcceptableTokens() 49 { 50 return new int[] { 51 TokenTypes.METHOD_DEF, 52 TokenTypes.CTOR_DEF, 53 TokenTypes.LITERAL_CATCH, 54 TokenTypes.FOR_EACH_CLAUSE, 55 }; 56 } 57 58 59 public void visitToken(DetailAST aAST) 60 { 61 final DetailAST container = aAST.getParent().getParent(); 63 if (container.getType() == TokenTypes.INTERFACE_DEF) { 64 return; 65 } 66 67 if (aAST.getType() == TokenTypes.LITERAL_CATCH) { 68 visitCatch(aAST); 69 } 70 else if (aAST.getType() == TokenTypes.FOR_EACH_CLAUSE) { 71 visitForEachClause(aAST); 72 } 73 else { 74 visitMethod(aAST); 75 } 76 } 77 78 82 private void visitMethod(final DetailAST aMethod) 83 { 84 if (!aMethod.branchContains(TokenTypes.PARAMETER_DEF)) { 86 return; 87 } 88 89 final DetailAST modifiers = 91 aMethod.findFirstToken(TokenTypes.MODIFIERS); 92 if (modifiers.branchContains(TokenTypes.ABSTRACT)) { 93 return; 94 } 95 96 final DetailAST parameters = 98 aMethod.findFirstToken(TokenTypes.PARAMETERS); 99 DetailAST child = (DetailAST) parameters.getFirstChild(); 100 while (child != null) { 101 if (child.getType() == TokenTypes.PARAMETER_DEF) { 103 checkParam(child); 104 } 105 child = (DetailAST) child.getNextSibling(); 106 } 107 } 108 109 113 private void visitCatch(final DetailAST aCatch) 114 { 115 checkParam(aCatch.findFirstToken(TokenTypes.PARAMETER_DEF)); 116 } 117 118 122 private void visitForEachClause(final DetailAST aForEachClause) 123 { 124 checkParam(aForEachClause.findFirstToken(TokenTypes.VARIABLE_DEF)); 125 } 126 127 131 private void checkParam(final DetailAST aParam) 132 { 133 if (!aParam.branchContains(TokenTypes.FINAL)) { 134 final DetailAST paramName = aParam.findFirstToken(TokenTypes.IDENT); 135 final DetailAST firstNode = CheckUtils.getFirstNode(aParam); 136 log(firstNode.getLineNo(), firstNode.getColumnNo(), 137 "final.parameter", paramName.getText()); 138 } 139 } 140 } 141 | Popular Tags |