Don't crash on int/long min value
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=98233419
diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java
index 2ff3782..e00a0f1 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java
@@ -1403,7 +1403,22 @@
@Override
public boolean visit(NumberLiteral node) {
sync(node);
- token(node.getToken());
+ String value = node.getToken();
+ if (value.startsWith("-")) {
+ // jdt normally parses negative numeric literals as a unary minus on an
+ // unsigned literal, but in the case of Long.MIN_VALUE and
+ // Integer.MIN_VALUE it creates a signed literal without a unary minus
+ // expression.
+ //
+ // Unfortunately in both cases the input token stream will still have
+ // the '-' token followed by an unsigned numeric literal token. We
+ // hack around this by checking for a leading '-' in the text of the
+ // given numeric literal, and emitting two separate tokens if it is
+ // present to match the input stream.
+ token("-");
+ value = value.substring(1);
+ }
+ token(value);
return false;
}
diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22469536.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22469536.input
new file mode 100644
index 0000000..9f06d6f
--- /dev/null
+++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22469536.input
@@ -0,0 +1,6 @@
+public class Test {
+ public static void foo() {
+ long x = -9223372036854775808L;
+ int y = -2147483648;
+ }
+}
diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22469536.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22469536.output
new file mode 100644
index 0000000..9f06d6f
--- /dev/null
+++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22469536.output
@@ -0,0 +1,6 @@
+public class Test {
+ public static void foo() {
+ long x = -9223372036854775808L;
+ int y = -2147483648;
+ }
+}