|
15 | 15 | package com.google.googlejavaformat.java.java14; |
16 | 16 |
|
17 | 17 | import static com.google.common.collect.ImmutableList.toImmutableList; |
18 | | -import static com.google.common.collect.MoreCollectors.onlyElement; |
| 18 | +import static com.google.common.collect.MoreCollectors.toOptional; |
19 | 19 |
|
20 | 20 | import com.google.common.base.Verify; |
| 21 | +import com.google.common.collect.ImmutableList; |
21 | 22 | import com.google.googlejavaformat.Op; |
22 | 23 | import com.google.googlejavaformat.OpsBuilder; |
23 | 24 | import com.google.googlejavaformat.java.JavaInputAstVisitor; |
|
26 | 27 | import com.sun.source.tree.ClassTree; |
27 | 28 | import com.sun.source.tree.ExpressionTree; |
28 | 29 | import com.sun.source.tree.InstanceOfTree; |
29 | | -import com.sun.source.tree.MethodTree; |
30 | 30 | import com.sun.source.tree.SwitchExpressionTree; |
31 | 31 | import com.sun.source.tree.Tree; |
32 | 32 | import com.sun.source.tree.YieldTree; |
33 | 33 | import com.sun.tools.javac.code.Flags; |
34 | 34 | import com.sun.tools.javac.tree.JCTree; |
35 | 35 | import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
| 36 | +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
36 | 37 | import com.sun.tools.javac.tree.TreeInfo; |
37 | 38 | import java.util.List; |
38 | 39 | import java.util.Optional; |
@@ -112,20 +113,17 @@ public void visitRecordDeclaration(ClassTree node) { |
112 | 113 | if (!node.getTypeParameters().isEmpty()) { |
113 | 114 | typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO); |
114 | 115 | } |
115 | | - MethodTree constructor = |
116 | | - node.getMembers().stream() |
117 | | - .filter(JCMethodDecl.class::isInstance) |
118 | | - .map(JCMethodDecl.class::cast) |
119 | | - .filter( |
120 | | - m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR) |
121 | | - .collect(onlyElement()); |
| 116 | + ImmutableList<JCVariableDecl> parameters = |
| 117 | + compactRecordConstructor(node) |
| 118 | + .map(m -> ImmutableList.copyOf(m.getParameters())) |
| 119 | + .orElseGet(() -> recordVariables(node)); |
122 | 120 | token("("); |
123 | | - if (!constructor.getParameters().isEmpty() || constructor.getReceiverParameter() != null) { |
| 121 | + if (!parameters.isEmpty()) { |
124 | 122 | // Break before args. |
125 | 123 | builder.breakToFill(""); |
126 | 124 | } |
127 | | - visitFormals( |
128 | | - Optional.ofNullable(constructor.getReceiverParameter()), constructor.getParameters()); |
| 125 | + // record headers can't declare receiver parameters |
| 126 | + visitFormals(/* receiver= */ Optional.empty(), parameters); |
129 | 127 | token(")"); |
130 | 128 | if (hasSuperInterfaceTypes) { |
131 | 129 | builder.breakToFill(" "); |
@@ -157,6 +155,22 @@ public void visitRecordDeclaration(ClassTree node) { |
157 | 155 | dropEmptyDeclarations(); |
158 | 156 | } |
159 | 157 |
|
| 158 | + private static Optional<JCMethodDecl> compactRecordConstructor(ClassTree node) { |
| 159 | + return node.getMembers().stream() |
| 160 | + .filter(JCMethodDecl.class::isInstance) |
| 161 | + .map(JCMethodDecl.class::cast) |
| 162 | + .filter(m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR) |
| 163 | + .collect(toOptional()); |
| 164 | + } |
| 165 | + |
| 166 | + private static ImmutableList<JCVariableDecl> recordVariables(ClassTree node) { |
| 167 | + return node.getMembers().stream() |
| 168 | + .filter(JCVariableDecl.class::isInstance) |
| 169 | + .map(JCVariableDecl.class::cast) |
| 170 | + .filter(m -> (m.mods.flags & RECORD) == RECORD) |
| 171 | + .collect(toImmutableList()); |
| 172 | + } |
| 173 | + |
160 | 174 | @Override |
161 | 175 | public Void visitInstanceOf(InstanceOfTree node, Void unused) { |
162 | 176 | sync(node); |
|
0 commit comments