From fa01637c25dfb3277096c058727e200558a9b484 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Sun, 11 Apr 2021 15:01:32 -0700 Subject: [PATCH] Handle `final` variables in `instanceof` patterns Fixes https://github.com/google/google-java-format/issues/588 PiperOrigin-RevId: 367902205 --- .../java/java14/Java14InputAstVisitor.java | 11 ++++++++--- .../java/FormatterIntegrationTest.java | 7 ++++++- .../google/googlejavaformat/java/testdata/I588.input | 8 ++++++++ .../google/googlejavaformat/java/testdata/I588.output | 8 ++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I588.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I588.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 2a90b939d..a9273ce7b 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 @@ -27,6 +27,7 @@ import com.sun.source.tree.ClassTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.InstanceOfTree; +import com.sun.source.tree.ModifiersTree; import com.sun.source.tree.SwitchExpressionTree; import com.sun.source.tree.Tree; import com.sun.source.tree.VariableTree; @@ -56,12 +57,13 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) { try { VariableTree variableTree = (VariableTree) BindingPatternTree.class.getMethod("getVariable").invoke(node); - visitBindingPattern(variableTree.getType(), variableTree.getName()); + visitBindingPattern( + variableTree.getModifiers(), variableTree.getType(), variableTree.getName()); } catch (ReflectiveOperationException e1) { try { Tree type = (Tree) BindingPatternTree.class.getMethod("getType").invoke(node); Name name = (Name) BindingPatternTree.class.getMethod("getName").invoke(node); - visitBindingPattern(type, name); + visitBindingPattern(/* modifiers= */ null, type, name); } catch (ReflectiveOperationException e2) { e2.addSuppressed(e1); throw new LinkageError(e2.getMessage(), e2); @@ -70,7 +72,10 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) { return null; } - private void visitBindingPattern(Tree type, Name name) { + private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) { + if (modifiers != null) { + builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty())); + } scan(type, null); builder.breakOp(" "); visit(name); 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 fdcf6f8a8..39f31e484 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -50,6 +50,8 @@ public class FormatterIntegrationTest { private static final ImmutableSet JAVA14_TESTS = ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch"); + private static final ImmutableSet JAVA16_TESTS = ImmutableSet.of("I588"); + @Parameters(name = "{index}: {0}") public static Iterable data() throws IOException { Path testDataPath = Paths.get("com/google/googlejavaformat/java/testdata"); @@ -76,7 +78,7 @@ public static Iterable data() throws IOException { case "output": outputs.put(baseName, contents); break; - default: + default: // fall out } } } @@ -90,6 +92,9 @@ public static Iterable data() throws IOException { if (JAVA14_TESTS.contains(fileName) && getMajor() < 14) { continue; } + if (JAVA16_TESTS.contains(fileName) && getMajor() < 16) { + continue; + } testInputs.add(new Object[] {fileName, input, expectedOutput}); } return testInputs; diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I588.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I588.input new file mode 100644 index 000000000..9c8f992ea --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I588.input @@ -0,0 +1,8 @@ +class T { + int f(Object x) { + if (x instanceof final Integer i) { + return i; + } + return -1; + } +} \ No newline at end of file diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I588.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I588.output new file mode 100644 index 000000000..37ff2f50d --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I588.output @@ -0,0 +1,8 @@ +class T { + int f(Object x) { + if (x instanceof final Integer i) { + return i; + } + return -1; + } +}