From 9b6d3345265901cb26f48599f417814717845311 Mon Sep 17 00:00:00 2001 From: cushon Date: Fri, 2 Jun 2017 11:38:01 -0700 Subject: [PATCH] Handle malformed annotations more gracefully javac's parser doesn't weed annotations with illegal combinations of assignment and non-assignment expressions, so don't crash when formatting them. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=157855678 --- .../googlejavaformat/java/JavaInputAstVisitor.java | 9 ++++++++- .../googlejavaformat/java/testdata/B38352414.input | 2 ++ .../googlejavaformat/java/testdata/B38352414.output | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/B38352414.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/B38352414.output 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 0c0a7b332..5bce89e7a 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -1232,7 +1232,11 @@ public Void visitAnnotation(AnnotationTree node, Void unused) { builder.breakOp(" "); } } - visitAnnotationArgument((AssignmentTree) argument); + if (argument instanceof AssignmentTree) { + visitAnnotationArgument((AssignmentTree) argument); + } else { + scan(argument, null); + } first = false; } builder.breakOp(UNIFIED, "", minusTwo, Optional.absent()); @@ -1253,6 +1257,9 @@ public Void visitAnnotation(AnnotationTree node, Void unused) { new Predicate() { @Override public boolean apply(ExpressionTree argument) { + if (!(argument instanceof AssignmentTree)) { + return false; + } ExpressionTree expression = ((AssignmentTree) argument).getExpression(); return expression instanceof NewArrayTree && ((NewArrayTree) expression).getType() == null; diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B38352414.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B38352414.input new file mode 100644 index 000000000..60726fb72 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B38352414.input @@ -0,0 +1,2 @@ +@Redacted(Redacted.REDACTED + "/redacted", redacted = Redacted.class) +class B38352414 {} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B38352414.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B38352414.output new file mode 100644 index 000000000..60726fb72 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B38352414.output @@ -0,0 +1,2 @@ +@Redacted(Redacted.REDACTED + "/redacted", redacted = Redacted.class) +class B38352414 {}