1 package com.puppycrawl.tools.checkstyle.checks; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.FullIdent; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 25 import java.util.List ; 26 import java.util.ArrayList ; 27 28 35 public final class CheckUtils 36 { 37 38 private CheckUtils() 39 { 40 throw new UnsupportedOperationException (); 41 } 42 43 49 public static boolean isEqualsMethod(DetailAST aAST) 50 { 51 if (aAST.getType() != TokenTypes.METHOD_DEF) { 52 return false; 54 } 55 56 final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS); 58 if (modifiers.branchContains(TokenTypes.LITERAL_STATIC) 59 || modifiers.branchContains(TokenTypes.ABSTRACT)) 60 { 61 return false; 62 } 63 64 final DetailAST nameNode = aAST.findFirstToken(TokenTypes.IDENT); 66 final String name = nameNode.getText(); 67 if (!"equals".equals(name)) { 68 return false; 69 } 70 71 final DetailAST paramsNode = aAST.findFirstToken(TokenTypes.PARAMETERS); 73 return (paramsNode.getChildCount() == 1); 74 } 75 76 81 public static boolean isElseIf(DetailAST aAST) 82 { 83 final DetailAST parentAST = aAST.getParent(); 84 85 return (aAST.getType() == TokenTypes.LITERAL_IF) 86 && (isElse(parentAST) || isElseWithCurlyBraces(parentAST)); 87 } 88 89 94 private static boolean isElse(DetailAST aAST) 95 { 96 return aAST.getType() == TokenTypes.LITERAL_ELSE; 97 } 98 99 105 private static boolean isElseWithCurlyBraces(DetailAST aAST) 106 { 107 return (aAST.getType() == TokenTypes.SLIST) 108 && (aAST.getChildCount() == 2) 109 && isElse(aAST.getParent()); 110 } 111 112 117 public static FullIdent createFullType(DetailAST aTypeAST) 118 { 119 final DetailAST arrayDeclAST = 120 aTypeAST.findFirstToken(TokenTypes.ARRAY_DECLARATOR); 121 122 return createFullTypeNoArrays(arrayDeclAST == null ? aTypeAST 123 : arrayDeclAST); 124 } 125 126 130 private static FullIdent createFullTypeNoArrays(DetailAST aTypeAST) 131 { 132 return FullIdent.createFullIdent((DetailAST) aTypeAST.getFirstChild()); 133 } 134 135 137 private static final int BASE_8 = 8; 138 139 140 private static final int BASE_10 = 10; 141 142 143 private static final int BASE_16 = 16; 144 145 153 public static double parseDouble(String aText, int aType) 154 { 155 double result = 0; 156 switch (aType) { 157 case TokenTypes.NUM_FLOAT: 158 case TokenTypes.NUM_DOUBLE: 159 result = Double.parseDouble(aText); 160 break; 161 case TokenTypes.NUM_INT: 162 case TokenTypes.NUM_LONG: 163 int radix = BASE_10; 164 if (aText.startsWith("0x") || aText.startsWith("0X")) { 165 radix = BASE_16; 166 aText = aText.substring(2); 167 } 168 else if (aText.charAt(0) == '0') { 169 radix = BASE_8; 170 aText = aText.substring(1); 171 } 172 if ((aText.endsWith("L")) || (aText.endsWith("l"))) { 173 aText = aText.substring(0, aText.length() - 1); 174 } 175 if (aText.length() > 0) { 176 if (aType == TokenTypes.NUM_INT) { 177 result = parseInt(aText, radix); 178 } 179 else { 180 result = parseLong(aText, radix); 181 } 182 } 183 break; 184 default: 185 break; 186 } 187 return result; 188 } 189 190 201 public static int parseInt(String aText, int aRadix) 202 { 203 int result = 0; 204 final int max = aText.length(); 205 for (int i = 0; i < max; i++) { 206 final int digit = Character.digit(aText.charAt(i), aRadix); 207 result *= aRadix; 208 result += digit; 209 } 210 return result; 211 } 212 213 224 public static long parseLong(String aText, int aRadix) 225 { 226 long result = 0; 227 final int max = aText.length(); 228 for (int i = 0; i < max; i++) { 229 final int digit = Character.digit(aText.charAt(i), aRadix); 230 result *= aRadix; 231 result += digit; 232 } 233 return result; 234 } 235 236 244 public static double parseFloat(String aText, int aType) 245 { 246 return (float) parseDouble(aText, aType); 247 } 248 249 254 public static DetailAST getFirstNode(final DetailAST aNode) 255 { 256 DetailAST currentNode = aNode; 257 DetailAST child = (DetailAST) aNode.getFirstChild(); 258 while (child != null) { 259 final DetailAST newNode = getFirstNode(child); 260 if ((newNode.getLineNo() < currentNode.getLineNo()) 261 || ((newNode.getLineNo() == currentNode.getLineNo()) 262 && (newNode.getColumnNo() < currentNode.getColumnNo()))) 263 { 264 currentNode = newNode; 265 } 266 child = (DetailAST) child.getNextSibling(); 267 } 268 269 return currentNode; 270 } 271 272 277 public static List getTypeParameterNames(final DetailAST aNode) 278 { 279 final DetailAST typeParameters = 280 aNode.findFirstToken(TokenTypes.TYPE_PARAMETERS); 281 282 final List typeParamNames = new ArrayList (); 283 if (typeParameters != null) { 284 final DetailAST typeParam = 285 typeParameters.findFirstToken(TokenTypes.TYPE_PARAMETER); 286 typeParamNames.add( 287 typeParam.findFirstToken(TokenTypes.IDENT).getText()); 288 289 DetailAST sibling = (DetailAST) typeParam.getNextSibling(); 290 while (sibling != null) { 291 if (sibling.getType() == TokenTypes.TYPE_PARAMETER) { 292 typeParamNames.add( 293 sibling.findFirstToken(TokenTypes.IDENT).getText()); 294 } 295 sibling = (DetailAST) sibling.getNextSibling(); 296 } 297 } 298 299 return typeParamNames; 300 } 301 302 307 public static List getTypeParameters(final DetailAST aNode) 308 { 309 final DetailAST typeParameters = 310 aNode.findFirstToken(TokenTypes.TYPE_PARAMETERS); 311 312 final List typeParams = new ArrayList (); 313 if (typeParameters != null) { 314 final DetailAST typeParam = 315 typeParameters.findFirstToken(TokenTypes.TYPE_PARAMETER); 316 typeParams.add(typeParam); 317 318 DetailAST sibling = (DetailAST) typeParam.getNextSibling(); 319 while (sibling != null) { 320 if (sibling.getType() == TokenTypes.TYPE_PARAMETER) { 321 typeParams.add(sibling); 322 } 323 sibling = (DetailAST) sibling.getNextSibling(); 324 } 325 } 326 327 return typeParams; 328 } 329 } 330 | Popular Tags |