Skip to content

Commit af4b138

Browse files
cushonabashev
authored andcommitted
Accept a type annotation on the qualifier of a method reference
"Supplier<List<?>> a = @A ArrayList::new;" failed with 'did not generate token "@"'. The printing of a type annotation on a method reference's qualifier was already in place, but visitMemberReference first synced to the tree's start position, and javac's start position for a member reference leaves out the annotation, so the sync reported the annotation's tokens as never generated. This ports google/google-java-format#984 by Liam Miller-Cushon: visitMemberReference no longer syncs to the start position. The golden B308157568 comes from upstream, with the expected output in this project's style. The 15,747 files of the JDK 21 sources format exactly as before.
1 parent 746fb60 commit af4b138

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,8 @@ public boolean visitEnumDeclaration(ClassTree node) {
928928

929929
@Override
930930
public Void visitMemberReference(MemberReferenceTree node, Void unused) {
931-
sync(node);
931+
// No sync(node): javac's start position for a member reference leaves out a type annotation on its
932+
// qualifier ("@A ArrayList::new"), so syncing to it would report the annotation as never generated.
932933
builder.open(OpenOp.builder()
933934
.plusIndent(plusFour)
934935
.debugName("methodReference")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class C {
2+
@A(0x14)
3+
int f(Object o) {
4+
@A(0x40)
5+
int local;
6+
try (@A(0x41)
7+
JarFile jarFile = new JarFile("hello.jar")) {
8+
} catch (
9+
@A(0x42)
10+
IOException e) {
11+
}
12+
if (o instanceof @A(0x43) String) {}
13+
new @A(0x44) ArrayList<>();
14+
Supplier<List<?>> a = @A(0x45) ArrayList::new;
15+
Supplier<List<?>> b = @A(0x46) ImmutableList::of;
16+
String s = (@A(0x47) String) o;
17+
List<?> xs = new ArrayList<@A(0x48) String>();
18+
xs = ImmutableList.<@A(0x49) String>of();
19+
Supplier<List<?>> c = ArrayList<@A(0x4A) String>::new;
20+
Supplier<List<?>> d = ImmutableList::<@A(0x4B) String>of;
21+
return 0;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class C {
2+
@A(0x14)
3+
int f(Object o) {
4+
@A(0x40)
5+
int local;
6+
try (@A(0x41)
7+
JarFile jarFile = new JarFile("hello.jar")) {
8+
} catch (
9+
@A(0x42)
10+
IOException e) {
11+
}
12+
if (o instanceof @A(0x43) String) {}
13+
new @A(0x44) ArrayList<>();
14+
Supplier<List<?>> a = @A(0x45) ArrayList::new;
15+
Supplier<List<?>> b = @A(0x46) ImmutableList::of;
16+
String s = (@A(0x47) String) o;
17+
List<?> xs = new ArrayList<@A(0x48) String>();
18+
xs = ImmutableList.<@A(0x49) String>of();
19+
Supplier<List<?>> c = ArrayList<@A(0x4A) String>::new;
20+
Supplier<List<?>> d = ImmutableList::<@A(0x4B) String>of;
21+
return 0;
22+
}
23+
}

0 commit comments

Comments
 (0)