Skip to content

Commit

Permalink
Fix formatting records without an explicit constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
fawind committed Nov 13, 2020
1 parent 2c05467 commit 8552645
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,8 @@ public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
// TODO(fawind): Use Flags.COMPACT_RECORD_CONSTRUCTOR once if/when we drop support for Java 11
protected static final long COMPACT_RECORD_CONSTRUCTOR = 1L << 51;

protected static final long RECORD = 1L << 61;

@Override
public Void visitMethod(MethodTree node, Void unused) {
sync(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package com.palantir.javaformat.java.java14;

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

import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.palantir.javaformat.Op;
import com.palantir.javaformat.OpsBuilder;
import com.palantir.javaformat.java.JavaInputAstVisitor;
Expand All @@ -29,13 +30,11 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.SwitchExpressionTree;
import com.sun.source.tree.Tree;
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.TreeInfo;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -111,17 +110,16 @@ public void visitRecordDeclaration(ClassTree node) {
if (!node.getTypeParameters().isEmpty()) {
typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO);
}
MethodTree constructor = node.getMembers().stream()
.filter(JCMethodDecl.class::isInstance)
.map(JCMethodDecl.class::cast)
.filter(m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR)
.collect(onlyElement());
ImmutableList<JCTree.JCVariableDecl> parameters = compactRecordConstructor(node)
.map(m -> ImmutableList.copyOf(m.getParameters()))
.orElseGet(() -> recordVariables(node));
token("(");
if (!constructor.getParameters().isEmpty() || constructor.getReceiverParameter() != null) {
if (!parameters.isEmpty()) {
// Break before args.
builder.breakToFill("");
}
visitFormals(Optional.ofNullable(constructor.getReceiverParameter()), constructor.getParameters());
// record headers can't declare receiver parameters
visitFormals(/* receiver= */ Optional.empty(), parameters);
token(")");
if (hasSuperInterfaceTypes) {
builder.breakToFill(" ");
Expand Down Expand Up @@ -152,6 +150,22 @@ 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)
.map(JCTree.JCVariableDecl.class::cast)
.filter(m -> (m.mods.flags & RECORD) == RECORD)
.collect(toImmutableList());
}

@Override
public Void visitInstanceOf(InstanceOfTree node, Void unused) {
sync(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
@Execution(ExecutionMode.CONCURRENT)
public final class FileBasedTests {
// Tests files that are only used when run with java 14 or higher
private static final ImmutableSet<String> JAVA_14_TESTS = ImmutableSet.of("java14");
private static final ImmutableSet<String> JAVA_14_TESTS =
ImmutableSet.of("ExpressionSwitch", "RSL", "Records", "Var");

private final Class<?> testClass;
/** The path prefix for all tests if loaded as resources. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ExpressionSwitch {
boolean odd(int x) {
return switch (x) {
case 0 -> true;
case 1 -> false;
default -> { int y = x - 1; return odd(y); }
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ExpressionSwitch {
boolean odd(int x) {
return switch (x) {
case 0 -> true;
case 1 -> false;
default -> {
int y = x - 1;
return odd(y);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RSLs {
String s = """
lorem
ipsum
""";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RSLs {
String s = """
lorem
ipsum
""";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Records {
record R1() {}

private record R2() {}

@Deprecated
private record R3() {}

record R4<T>() {}

record R5<T>(int x) {}

record R6<T>(@Deprecated int x) {}

record R7<T>(@Deprecated int x, int... y) {}

record R8<T>() implements Comparable<R8<T>> {
@Override
public int compareTo(R8<T> other) {
return 0;
}
}

record R9(int x) {
R9(int x) {
this.x = x;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Records {
record R1() {}

private record R2() {}

@Deprecated
private record R3() {}

record R4<T>() {}

record R5<T>(int x) {}

record R6<T>(@Deprecated int x) {}

record R7<T>(@Deprecated int x, int... y) {}

record R8<T>() implements Comparable<R8<T>> {
@Override
public int compareTo(R8<T> other) {
return 0;
}
}

record R9(int x) {
R9(int x) {
this.x = x;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Var {
void f() {
for (var x : ImmutableList.of(42)) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Var {
void f() {
for (var x : ImmutableList.of(42)) {}
}
}

This file was deleted.

This file was deleted.

0 comments on commit 8552645

Please sign in to comment.