Skip to content

Commit

Permalink
Fix handling of annotations in compact record constructors
Browse files Browse the repository at this point in the history
Fixes #574

PiperOrigin-RevId: 367902654
  • Loading branch information
cushon authored and google-java-format Team committed Apr 13, 2021
1 parent fe5ac46 commit ede926e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
Expand Up @@ -15,7 +15,6 @@
package com.google.googlejavaformat.java.java14;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.MoreCollectors.toOptional;

import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
Expand All @@ -34,7 +33,6 @@
import com.sun.source.tree.YieldTree;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.TreeInfo;
import java.util.List;
Expand Down Expand Up @@ -141,10 +139,7 @@ public void visitRecordDeclaration(ClassTree node) {
if (!node.getTypeParameters().isEmpty()) {
typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO);
}
ImmutableList<JCVariableDecl> parameters =
compactRecordConstructor(node)
.map(m -> ImmutableList.copyOf(m.getParameters()))
.orElseGet(() -> recordVariables(node));
ImmutableList<JCVariableDecl> parameters = recordVariables(node);
token("(");
if (!parameters.isEmpty()) {
// Break before args.
Expand Down Expand Up @@ -183,14 +178,6 @@ public void visitRecordDeclaration(ClassTree node) {
dropEmptyDeclarations();
}

private static Optional<JCMethodDecl> compactRecordConstructor(ClassTree node) {
return node.getMembers().stream()
.filter(JCMethodDecl.class::isInstance)
.map(JCMethodDecl.class::cast)
.filter(m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR)
.collect(toOptional());
}

private static ImmutableList<JCVariableDecl> recordVariables(ClassTree node) {
return node.getMembers().stream()
.filter(JCVariableDecl.class::isInstance)
Expand Down
Expand Up @@ -48,7 +48,7 @@
public class FormatterIntegrationTest {

private static final ImmutableSet<String> JAVA14_TESTS =
ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch");
ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574");

private static final ImmutableSet<String> JAVA16_TESTS = ImmutableSet.of("I588");

Expand Down
@@ -0,0 +1,6 @@
public record Record(@NotNull Object o) {

public Record {
this.o = o;
}
}
@@ -0,0 +1,6 @@
public record Record(@NotNull Object o) {

public Record {
this.o = o;
}
}

0 comments on commit ede926e

Please sign in to comment.