| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package com.puppycrawl.tools.checkstyle.checks; |
| 20 | |
|
| 21 | |
import com.google.common.collect.Lists; |
| 22 | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 23 | |
import com.puppycrawl.tools.checkstyle.api.FullIdent; |
| 24 | |
import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 25 | |
import java.util.List; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public final class CheckUtils |
| 35 | |
{ |
| 36 | |
|
| 37 | |
private CheckUtils() |
| 38 | 0 | { |
| 39 | 0 | throw new UnsupportedOperationException(); |
| 40 | |
} |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public static boolean isEqualsMethod(DetailAST aAST) |
| 49 | |
{ |
| 50 | 24 | if (aAST.getType() != TokenTypes.METHOD_DEF) { |
| 51 | |
|
| 52 | 0 | return false; |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | 24 | final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS); |
| 57 | 24 | if (modifiers.branchContains(TokenTypes.LITERAL_STATIC) |
| 58 | |
|| modifiers.branchContains(TokenTypes.ABSTRACT)) |
| 59 | |
{ |
| 60 | 1 | return false; |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | 23 | final DetailAST nameNode = aAST.findFirstToken(TokenTypes.IDENT); |
| 65 | 23 | final String name = nameNode.getText(); |
| 66 | 23 | if (!"equals".equals(name)) { |
| 67 | 6 | return false; |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | 17 | final DetailAST paramsNode = aAST.findFirstToken(TokenTypes.PARAMETERS); |
| 72 | 17 | return (paramsNode.getChildCount() == 1); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public static boolean isElseIf(DetailAST aAST) |
| 81 | |
{ |
| 82 | 68 | final DetailAST parentAST = aAST.getParent(); |
| 83 | |
|
| 84 | 68 | return (aAST.getType() == TokenTypes.LITERAL_IF) |
| 85 | |
&& (isElse(parentAST) || isElseWithCurlyBraces(parentAST)); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
private static boolean isElse(DetailAST aAST) |
| 94 | |
{ |
| 95 | 112 | return aAST.getType() == TokenTypes.LITERAL_ELSE; |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
private static boolean isElseWithCurlyBraces(DetailAST aAST) |
| 105 | |
{ |
| 106 | 68 | return (aAST.getType() == TokenTypes.SLIST) |
| 107 | |
&& (aAST.getChildCount() == 2) |
| 108 | |
&& isElse(aAST.getParent()); |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public static FullIdent createFullType(DetailAST aTypeAST) |
| 117 | |
{ |
| 118 | 117 | final DetailAST arrayDeclAST = |
| 119 | |
aTypeAST.findFirstToken(TokenTypes.ARRAY_DECLARATOR); |
| 120 | |
|
| 121 | 117 | return createFullTypeNoArrays(arrayDeclAST == null ? aTypeAST |
| 122 | |
: arrayDeclAST); |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
private static FullIdent createFullTypeNoArrays(DetailAST aTypeAST) |
| 130 | |
{ |
| 131 | 117 | return FullIdent.createFullIdent(aTypeAST.getFirstChild()); |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
private static final int BASE_8 = 8; |
| 137 | |
|
| 138 | |
|
| 139 | |
private static final int BASE_10 = 10; |
| 140 | |
|
| 141 | |
|
| 142 | |
private static final int BASE_16 = 16; |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public static double parseDouble(String aText, int aType) |
| 153 | |
{ |
| 154 | 515 | String txt = aText; |
| 155 | 515 | double result = 0; |
| 156 | 515 | switch (aType) { |
| 157 | |
case TokenTypes.NUM_FLOAT: |
| 158 | |
case TokenTypes.NUM_DOUBLE: |
| 159 | 62 | result = Double.parseDouble(txt); |
| 160 | 62 | break; |
| 161 | |
case TokenTypes.NUM_INT: |
| 162 | |
case TokenTypes.NUM_LONG: |
| 163 | 453 | int radix = BASE_10; |
| 164 | 453 | if (txt.startsWith("0x") || txt.startsWith("0X")) { |
| 165 | 72 | radix = BASE_16; |
| 166 | 72 | txt = txt.substring(2); |
| 167 | |
} |
| 168 | 381 | else if (txt.charAt(0) == '0') { |
| 169 | 77 | radix = BASE_8; |
| 170 | 77 | txt = txt.substring(1); |
| 171 | |
} |
| 172 | 453 | if ((txt.endsWith("L")) || (txt.endsWith("l"))) { |
| 173 | 85 | txt = txt.substring(0, txt.length() - 1); |
| 174 | |
} |
| 175 | 453 | if (txt.length() > 0) { |
| 176 | 430 | if (aType == TokenTypes.NUM_INT) { |
| 177 | 358 | result = parseInt(txt, radix); |
| 178 | |
} |
| 179 | |
else { |
| 180 | 72 | result = parseLong(txt, radix); |
| 181 | |
} |
| 182 | |
} |
| 183 | |
break; |
| 184 | |
default: |
| 185 | |
break; |
| 186 | |
} |
| 187 | 515 | return result; |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
public static int parseInt(String aText, int aRadix) |
| 202 | |
{ |
| 203 | 358 | int result = 0; |
| 204 | 358 | final int max = aText.length(); |
| 205 | 1059 | for (int i = 0; i < max; i++) { |
| 206 | 701 | final int digit = Character.digit(aText.charAt(i), aRadix); |
| 207 | 701 | result *= aRadix; |
| 208 | 701 | result += digit; |
| 209 | |
} |
| 210 | 358 | return result; |
| 211 | |
} |
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public static long parseLong(String aText, int aRadix) |
| 225 | |
{ |
| 226 | 72 | long result = 0; |
| 227 | 72 | final int max = aText.length(); |
| 228 | 666 | for (int i = 0; i < max; i++) { |
| 229 | 594 | final int digit = Character.digit(aText.charAt(i), aRadix); |
| 230 | 594 | result *= aRadix; |
| 231 | 594 | result += digit; |
| 232 | |
} |
| 233 | 72 | return result; |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public static double parseFloat(String aText, int aType) |
| 245 | |
{ |
| 246 | 10 | return (float) parseDouble(aText, aType); |
| 247 | |
} |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
public static DetailAST getFirstNode(final DetailAST aNode) |
| 255 | |
{ |
| 256 | 547 | DetailAST currentNode = aNode; |
| 257 | 547 | DetailAST child = aNode.getFirstChild(); |
| 258 | 1026 | while (child != null) { |
| 259 | 479 | final DetailAST newNode = getFirstNode(child); |
| 260 | 479 | if ((newNode.getLineNo() < currentNode.getLineNo()) |
| 261 | |
|| ((newNode.getLineNo() == currentNode.getLineNo()) |
| 262 | |
&& (newNode.getColumnNo() < currentNode.getColumnNo()))) |
| 263 | |
{ |
| 264 | 79 | currentNode = newNode; |
| 265 | |
} |
| 266 | 479 | child = child.getNextSibling(); |
| 267 | 479 | } |
| 268 | |
|
| 269 | 547 | return currentNode; |
| 270 | |
} |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
public static List<String> getTypeParameterNames(final DetailAST aNode) |
| 278 | |
{ |
| 279 | 217 | final DetailAST typeParameters = |
| 280 | |
aNode.findFirstToken(TokenTypes.TYPE_PARAMETERS); |
| 281 | |
|
| 282 | 217 | final List<String> typeParamNames = Lists.newArrayList(); |
| 283 | 217 | if (typeParameters != null) { |
| 284 | 27 | final DetailAST typeParam = |
| 285 | |
typeParameters.findFirstToken(TokenTypes.TYPE_PARAMETER); |
| 286 | 27 | typeParamNames.add( |
| 287 | |
typeParam.findFirstToken(TokenTypes.IDENT).getText()); |
| 288 | |
|
| 289 | 27 | DetailAST sibling = typeParam.getNextSibling(); |
| 290 | 72 | while (sibling != null) { |
| 291 | 45 | if (sibling.getType() == TokenTypes.TYPE_PARAMETER) { |
| 292 | 9 | typeParamNames.add( |
| 293 | |
sibling.findFirstToken(TokenTypes.IDENT).getText()); |
| 294 | |
} |
| 295 | 45 | sibling = sibling.getNextSibling(); |
| 296 | |
} |
| 297 | |
} |
| 298 | |
|
| 299 | 217 | return typeParamNames; |
| 300 | |
} |
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
public static List<DetailAST> getTypeParameters(final DetailAST aNode) |
| 308 | |
{ |
| 309 | 134 | final DetailAST typeParameters = |
| 310 | |
aNode.findFirstToken(TokenTypes.TYPE_PARAMETERS); |
| 311 | |
|
| 312 | 134 | final List<DetailAST> typeParams = Lists.newArrayList(); |
| 313 | 134 | if (typeParameters != null) { |
| 314 | 6 | final DetailAST typeParam = |
| 315 | |
typeParameters.findFirstToken(TokenTypes.TYPE_PARAMETER); |
| 316 | 6 | typeParams.add(typeParam); |
| 317 | |
|
| 318 | 6 | DetailAST sibling = typeParam.getNextSibling(); |
| 319 | 14 | while (sibling != null) { |
| 320 | 8 | if (sibling.getType() == TokenTypes.TYPE_PARAMETER) { |
| 321 | 1 | typeParams.add(sibling); |
| 322 | |
} |
| 323 | 8 | sibling = sibling.getNextSibling(); |
| 324 | |
} |
| 325 | |
} |
| 326 | |
|
| 327 | 134 | return typeParams; |
| 328 | |
} |
| 329 | |
} |