From 53390d99b56edae23cfab5adcafd7df28d9984c8 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Mon, 30 Oct 2023 08:22:01 -0700 Subject: [PATCH] Handle type annotations on method reference qualifiers e.g. ``` Supplier> a = @A(0x45) ArrayList::new; Supplier> b = @A(0x46) ImmutableList::of; ``` The necessary logic is already there, but the start position information for the method reference doesn't include the leading type annotations, which was resulting in an assertion to fail. PiperOrigin-RevId: 577850593 --- .../java/JavaInputAstVisitor.java | 1 - .../java/testdata/B308157568.input | 23 +++++++++++++++++++ .../java/testdata/B308157568.output | 23 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.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 89c944c5b..5a75afa0e 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -930,7 +930,6 @@ public boolean visitEnumDeclaration(ClassTree node) { @Override public Void visitMemberReference(MemberReferenceTree node, Void unused) { - sync(node); builder.open(plusFour); scan(node.getQualifierExpression(), null); builder.breakOp(); diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.input new file mode 100644 index 000000000..089a3f1e8 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.input @@ -0,0 +1,23 @@ +class C { + @A(0x14) + int f(Object o) { + @A(0x40) + int local; + try (@A(0x41) + JarFile jarFile = new JarFile("hello.jar")) { + } catch ( + @A(0x42) + IOException e) { + } + if (o instanceof @A(0x43) String) {} + new @A(0x44) ArrayList<>(); + Supplier> a = @A(0x45) ArrayList::new; + Supplier> b = @A(0x46) ImmutableList::of; + String s = (@A(0x47) String) o; + List xs = new ArrayList<@A(0x48) String>(); + xs = ImmutableList.<@A(0x49) String>of(); + Supplier> c = ArrayList<@A(0x4A) String>::new; + Supplier> d = ImmutableList::<@A(0x4B) String>of; + return 0; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.output new file mode 100644 index 000000000..089a3f1e8 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.output @@ -0,0 +1,23 @@ +class C { + @A(0x14) + int f(Object o) { + @A(0x40) + int local; + try (@A(0x41) + JarFile jarFile = new JarFile("hello.jar")) { + } catch ( + @A(0x42) + IOException e) { + } + if (o instanceof @A(0x43) String) {} + new @A(0x44) ArrayList<>(); + Supplier> a = @A(0x45) ArrayList::new; + Supplier> b = @A(0x46) ImmutableList::of; + String s = (@A(0x47) String) o; + List xs = new ArrayList<@A(0x48) String>(); + xs = ImmutableList.<@A(0x49) String>of(); + Supplier> c = ArrayList<@A(0x4A) String>::new; + Supplier> d = ImmutableList::<@A(0x4B) String>of; + return 0; + } +}