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.ScopeUtils; 24 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 25 26 import com.puppycrawl.tools.checkstyle.checks.CheckUtils; 27 28 51 public class ExplicitInitializationCheck extends Check 52 { 53 54 public final int[] getDefaultTokens() 55 { 56 return new int[] {TokenTypes.VARIABLE_DEF}; 57 } 58 59 60 public final int[] getRequiredTokens() 61 { 62 return getDefaultTokens(); 63 } 64 65 66 public void visitToken(DetailAST aAST) 67 { 68 if (ScopeUtils.isLocalVariableDef(aAST) 71 || ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) 72 { 73 return; 74 } 75 76 final DetailAST assign = aAST.findFirstToken(TokenTypes.ASSIGN); 77 if (assign == null) { 78 return; 80 } 81 82 final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS); 83 if ((modifiers != null) 84 && modifiers.branchContains(TokenTypes.FINAL)) 85 { 86 return; 88 } 89 90 final DetailAST type = aAST.findFirstToken(TokenTypes.TYPE); 91 final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); 92 final DetailAST exprStart = 93 (DetailAST) assign.getFirstChild().getFirstChild(); 94 if (isObjectType(type) 95 && (exprStart.getType() == TokenTypes.LITERAL_NULL)) 96 { 97 log(ident, "explicit.init", ident.getText(), "null"); 98 } 99 100 final int primitiveType = type.getFirstChild().getType(); 101 if ((primitiveType == TokenTypes.LITERAL_BOOLEAN) 102 && (exprStart.getType() == TokenTypes.LITERAL_FALSE)) 103 { 104 log(ident, "explicit.init", ident.getText(), "false"); 105 } 106 if (isNumericType(primitiveType) && isZero(exprStart)) { 107 log(ident, "explicit.init", ident.getText(), "0"); 108 } 109 if ((primitiveType == TokenTypes.LITERAL_CHAR) 110 && (isZero(exprStart) 111 || ((exprStart.getType() == TokenTypes.CHAR_LITERAL) 112 && "'\\0'".equals(exprStart.getText())))) 113 { 114 log(ident, "explicit.init", ident.getText(), "\\0"); 115 } 116 } 117 118 123 private boolean isObjectType(DetailAST aType) 124 { 125 final int type = aType.getFirstChild().getType(); 126 return ((type == TokenTypes.IDENT) || (type == TokenTypes.DOT) 127 || (type == TokenTypes.ARRAY_DECLARATOR)); 128 } 129 130 136 private boolean isNumericType(int aType) 137 { 138 return ((aType == TokenTypes.LITERAL_BYTE) 139 || (aType == TokenTypes.LITERAL_SHORT) 140 || (aType == TokenTypes.LITERAL_INT) 141 || (aType == TokenTypes.LITERAL_FLOAT) 142 || (aType == TokenTypes.LITERAL_LONG) 143 || (aType == TokenTypes.LITERAL_DOUBLE)); 144 } 145 146 150 private boolean isZero(DetailAST aExpr) 151 { 152 final int type = aExpr.getType(); 153 switch (type) { 154 case TokenTypes.NUM_FLOAT: 155 case TokenTypes.NUM_DOUBLE: 156 case TokenTypes.NUM_INT: 157 case TokenTypes.NUM_LONG: 158 final String text = aExpr.getText(); 159 return (0 == CheckUtils.parseFloat(text, type)); 160 default: 161 return false; 162 } 163 } 164 } 165 | Popular Tags |