Skip to content

Commit d38441a

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Fix handling of annotations in compact record constructors
Fixes #574 PiperOrigin-RevId: 367902654
1 parent 2396d08 commit d38441a

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.googlejavaformat.java.java14;
1616

1717
import static com.google.common.collect.ImmutableList.toImmutableList;
18-
import static com.google.common.collect.MoreCollectors.toOptional;
1918

2019
import com.google.common.base.Verify;
2120
import com.google.common.collect.ImmutableList;
@@ -27,13 +26,13 @@
2726
import com.sun.source.tree.ClassTree;
2827
import com.sun.source.tree.ExpressionTree;
2928
import com.sun.source.tree.InstanceOfTree;
29+
import com.sun.source.tree.ModifiersTree;
3030
import com.sun.source.tree.SwitchExpressionTree;
3131
import com.sun.source.tree.Tree;
3232
import com.sun.source.tree.VariableTree;
3333
import com.sun.source.tree.YieldTree;
3434
import com.sun.tools.javac.code.Flags;
3535
import com.sun.tools.javac.tree.JCTree;
36-
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
3736
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
3837
import com.sun.tools.javac.tree.TreeInfo;
3938
import java.util.List;
@@ -56,12 +55,13 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
5655
try {
5756
VariableTree variableTree =
5857
(VariableTree) BindingPatternTree.class.getMethod("getVariable").invoke(node);
59-
visitBindingPattern(variableTree.getType(), variableTree.getName());
58+
visitBindingPattern(
59+
variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
6060
} catch (ReflectiveOperationException e1) {
6161
try {
6262
Tree type = (Tree) BindingPatternTree.class.getMethod("getType").invoke(node);
6363
Name name = (Name) BindingPatternTree.class.getMethod("getName").invoke(node);
64-
visitBindingPattern(type, name);
64+
visitBindingPattern(/* modifiers= */ null, type, name);
6565
} catch (ReflectiveOperationException e2) {
6666
e2.addSuppressed(e1);
6767
throw new LinkageError(e2.getMessage(), e2);
@@ -70,7 +70,10 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
7070
return null;
7171
}
7272

73-
private void visitBindingPattern(Tree type, Name name) {
73+
private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
74+
if (modifiers != null) {
75+
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
76+
}
7477
scan(type, null);
7578
builder.breakOp(" ");
7679
visit(name);
@@ -136,10 +139,7 @@ public void visitRecordDeclaration(ClassTree node) {
136139
if (!node.getTypeParameters().isEmpty()) {
137140
typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO);
138141
}
139-
ImmutableList<JCVariableDecl> parameters =
140-
compactRecordConstructor(node)
141-
.map(m -> ImmutableList.copyOf(m.getParameters()))
142-
.orElseGet(() -> recordVariables(node));
142+
ImmutableList<JCVariableDecl> parameters = recordVariables(node);
143143
token("(");
144144
if (!parameters.isEmpty()) {
145145
// Break before args.
@@ -178,14 +178,6 @@ public void visitRecordDeclaration(ClassTree node) {
178178
dropEmptyDeclarations();
179179
}
180180

181-
private static Optional<JCMethodDecl> compactRecordConstructor(ClassTree node) {
182-
return node.getMembers().stream()
183-
.filter(JCMethodDecl.class::isInstance)
184-
.map(JCMethodDecl.class::cast)
185-
.filter(m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR)
186-
.collect(toOptional());
187-
}
188-
189181
private static ImmutableList<JCVariableDecl> recordVariables(ClassTree node) {
190182
return node.getMembers().stream()
191183
.filter(JCVariableDecl.class::isInstance)

core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
public class FormatterIntegrationTest {
4949

5050
private static final ImmutableSet<String> JAVA14_TESTS =
51-
ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch");
51+
ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574");
52+
53+
private static final ImmutableSet<String> JAVA16_TESTS = ImmutableSet.of("I588");
5254

5355
@Parameters(name = "{index}: {0}")
5456
public static Iterable<Object[]> data() throws IOException {
@@ -76,7 +78,7 @@ public static Iterable<Object[]> data() throws IOException {
7678
case "output":
7779
outputs.put(baseName, contents);
7880
break;
79-
default:
81+
default: // fall out
8082
}
8183
}
8284
}
@@ -90,6 +92,9 @@ public static Iterable<Object[]> data() throws IOException {
9092
if (JAVA14_TESTS.contains(fileName) && getMajor() < 14) {
9193
continue;
9294
}
95+
if (JAVA16_TESTS.contains(fileName) && getMajor() < 16) {
96+
continue;
97+
}
9398
testInputs.add(new Object[] {fileName, input, expectedOutput});
9499
}
95100
return testInputs;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public record Record(@NotNull Object o) {
2+
3+
public Record {
4+
this.o = o;
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public record Record(@NotNull Object o) {
2+
3+
public Record {
4+
this.o = o;
5+
}
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class T {
2+
int f(Object x) {
3+
if (x instanceof final Integer i) {
4+
return i;
5+
}
6+
return -1;
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class T {
2+
int f(Object x) {
3+
if (x instanceof final Integer i) {
4+
return i;
5+
}
6+
return -1;
7+
}
8+
}

0 commit comments

Comments
 (0)