private static boolean shouldUseUnderscore(String literalValue) { String value = LiteralUtils.trimLongSuffix(literalValue); Base base = Base.ofLiteralValue(value); return value.length() >= (base.minimalLength + base.prefix.length()); } }
private static boolean shouldUseUnderscore(String literalValue) { String value = LiteralUtils.trimLongSuffix(literalValue); Base base = Base.ofLiteralValue(value); return value.length() >= (base.minimalLength + base.prefix.length()); } }
@CheckForNull public static Long longLiteralValue(ExpressionTree tree) { ExpressionTree expression = tree; int sign = tree.is(Kind.UNARY_MINUS) ? -1 : 1; if (tree.is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) { expression = ((UnaryExpressionTree) tree).expression(); } if (expression.is(Kind.INT_LITERAL, Kind.LONG_LITERAL)) { String value = trimLongSuffix(((LiteralTree) expression).value()); // long as hexadecimal can be written using underscore to separate groups value = value.replaceAll("\\_", ""); try { if (value.startsWith("0b") || value.startsWith("0B")) { return sign * Long.valueOf(value.substring(2), 2); } return sign * Long.decode(value); } catch (NumberFormatException e) { // Long.decode() may fail in case of very large long number written in hexadecimal. In such situation, we ignore the number. // Note that Long.MAX_VALUE = "0x7FFF_FFFF_FFFF_FFFFL", but it is possible to write larger numbers in hexadecimal // to be used as mask in bitwise operation. For instance: // 0x8000_0000_0000_0000L (MAX_VALUE + 1), // 0xFFFF_FFFF_FFFF_FFFFL (only ones), // 0xFFFF_FFFF_FFFF_FFFEL (only ones except least significant bit), ... } } return null; }
@CheckForNull public static Long longLiteralValue(ExpressionTree tree) { ExpressionTree expression = tree; int sign = tree.is(Kind.UNARY_MINUS) ? -1 : 1; if (tree.is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) { expression = ((UnaryExpressionTree) tree).expression(); } if (expression.is(Kind.INT_LITERAL, Kind.LONG_LITERAL)) { String value = trimLongSuffix(((LiteralTree) expression).value()); // long as hexadecimal can be written using underscore to separate groups value = value.replaceAll("\\_", ""); try { if (value.startsWith("0b") || value.startsWith("0B")) { return sign * Long.valueOf(value.substring(2), 2); } return sign * Long.decode(value); } catch (NumberFormatException e) { // Long.decode() may fail in case of very large long number written in hexadecimal. In such situation, we ignore the number. // Note that Long.MAX_VALUE = "0x7FFF_FFFF_FFFF_FFFFL", but it is possible to write larger numbers in hexadecimal // to be used as mask in bitwise operation. For instance: // 0x8000_0000_0000_0000L (MAX_VALUE + 1), // 0xFFFF_FFFF_FFFF_FFFFL (only ones), // 0xFFFF_FFFF_FFFF_FFFEL (only ones except least significant bit), ... } } return null; }
@CheckForNull public static Long longLiteralValue(ExpressionTree tree) { ExpressionTree expression = tree; int sign = tree.is(Kind.UNARY_MINUS) ? -1 : 1; if (tree.is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) { expression = ((UnaryExpressionTree) tree).expression(); } if (expression.is(Kind.INT_LITERAL, Kind.LONG_LITERAL)) { String value = LiteralUtils.trimLongSuffix(((LiteralTree) expression).value()); if (value != null) { // long as hexadecimal can be written using underscore to separate groups value = value.replaceAll("\\_", ""); try { return sign * Long.decode(value); } catch (NumberFormatException e) { // Long.decode() may fail in case of very large long number written in hexadecimal. In such situation, we ignore the number. // Note that Long.MAX_VALUE = "0x7FFF_FFFF_FFFF_FFFFL", but it is possible to write larger numbers in hexadecimal // to be used as mask in bitwise operation. For instance: // 0x8000_0000_0000_0000L (MAX_VALUE + 1), // 0xFFFF_FFFF_FFFF_FFFFL (only ones), // 0xFFFF_FFFF_FFFF_FFFEL (only ones except least significant bit), ... } } } return null; }
@CheckForNull public static Long longLiteralValue(ExpressionTree tree) { ExpressionTree expression = tree; int sign = tree.is(Kind.UNARY_MINUS) ? -1 : 1; if (tree.is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) { expression = ((UnaryExpressionTree) tree).expression(); } if (expression.is(Kind.INT_LITERAL, Kind.LONG_LITERAL)) { String value = LiteralUtils.trimLongSuffix(((LiteralTree) expression).value()); if (value != null) { // long as hexadecimal can be written using underscore to separate groups value = value.replaceAll("\\_", ""); try { return sign * Long.decode(value); } catch (NumberFormatException e) { // Long.decode() may fail in case of very large long number written in hexadecimal. In such situation, we ignore the number. // Note that Long.MAX_VALUE = "0x7FFF_FFFF_FFFF_FFFFL", but it is possible to write larger numbers in hexadecimal // to be used as mask in bitwise operation. For instance: // 0x8000_0000_0000_0000L (MAX_VALUE + 1), // 0xFFFF_FFFF_FFFF_FFFFL (only ones), // 0xFFFF_FFFF_FFFF_FFFEL (only ones except least significant bit), ... } } } return null; }