From 32b63aa2e4675cb03fb853ea84cbc37792e442a4 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 15 Apr 2021 14:41:27 -0700 Subject: [PATCH] Fix a crash in `instanceof` pattern handling Fixes https://github.com/google/google-java-format PiperOrigin-RevId: 368720966 --- .../java/java14/Java14InputAstVisitor.java | 2 +- .../googlejavaformat/java/FormatterIntegrationTest.java | 2 +- .../com/google/googlejavaformat/java/testdata/I594.input | 7 +++++++ .../com/google/googlejavaformat/java/testdata/I594.output | 7 +++++++ 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I594.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I594.output diff --git a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java index 3517c3511..8293fd4b0 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java @@ -60,7 +60,7 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) { } catch (ReflectiveOperationException e1) { try { Tree type = (Tree) BindingPatternTree.class.getMethod("getType").invoke(node); - Name name = (Name) BindingPatternTree.class.getMethod("getName").invoke(node); + Name name = (Name) BindingPatternTree.class.getMethod("getBinding").invoke(node); visitBindingPattern(/* modifiers= */ null, type, name); } catch (ReflectiveOperationException e2) { e2.addSuppressed(e1); diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index b6549f4a8..31d59f171 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -48,7 +48,7 @@ public class FormatterIntegrationTest { private static final ImmutableSet JAVA14_TESTS = - ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574"); + ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574", "I594"); private static final ImmutableSet JAVA16_TESTS = ImmutableSet.of("I588"); diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I594.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I594.input new file mode 100644 index 000000000..98f667e39 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I594.input @@ -0,0 +1,7 @@ +public class I594 { + public void thisIsNotFormattedCorrectly(Object something){ + if(something instanceof String somethingAsString){ + return; + } + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I594.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I594.output new file mode 100644 index 000000000..7c519a2d9 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I594.output @@ -0,0 +1,7 @@ +public class I594 { + public void thisIsNotFormattedCorrectly(Object something) { + if (something instanceof String somethingAsString) { + return; + } + } +}