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 google/google-java-format#574

PiperOrigin-RevId: 368124032
  • Loading branch information
cushon authored and fawind committed Jan 10, 2022
1 parent fdeb4d1 commit 4e0de13
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
Expand Up @@ -17,7 +17,6 @@
package com.palantir.javaformat.java.java14;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.MoreCollectors.toOptional;
import static com.sun.source.tree.Tree.Kind.BLOCK;

import com.google.common.base.Verify;
Expand All @@ -43,6 +42,7 @@
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.JCVariableDecl;
import com.sun.tools.javac.tree.TreeInfo;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -161,9 +161,7 @@ public void visitRecordDeclaration(ClassTree node) {
if (!node.getTypeParameters().isEmpty()) {
typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO);
}
ImmutableList<JCTree.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 @@ -201,14 +199,6 @@ public void visitRecordDeclaration(ClassTree node) {
dropEmptyDeclarations();
}

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

private static ImmutableList<JCTree.JCVariableDecl> recordVariables(ClassTree node) {
return node.getMembers().stream()
.filter(JCTree.JCVariableDecl.class::isInstance)
Expand Down
Expand Up @@ -43,7 +43,7 @@
public final class FileBasedTests {
// Test files that are only used when run with a minimum Java version
private static final ImmutableSet<String> JAVA_14_TESTS =
ImmutableSet.of("ExpressionSwitch", "RSL", "Records", "Var");
ImmutableSet.of("ExpressionSwitch", "RSL", "Records", "Var", "I574");
private static final ImmutableSet<String> JAVA_16_TESTS = ImmutableSet.of("I588");

private final Class<?> testClass;
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 4e0de13

Please sign in to comment.