Skip to content

Commit

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

PiperOrigin-RevId: 367902654
  • Loading branch information
cushon authored and google-java-format Team committed Apr 12, 2021
1 parent 5defb58 commit 4c0c23b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 23 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Expand Up @@ -29,19 +29,19 @@ jobs:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
java: [ 15, 11 ]
java: [ 16, 14, 11 ]
experimental: [ false ]
include:
# Only test on macos and windows with a single recent JDK to avoid a
# combinatorial explosion of test configurations.
- os: macos-latest
java: 15
java: 16
experimental: false
- os: windows-latest
java: 15
java: 16
experimental: false
- os: ubuntu-latest
java: 16-ea
java: 17-ea
experimental: true
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
Expand Down
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 @@ -27,13 +26,13 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.SwitchExpressionTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
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 All @@ -56,12 +55,13 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
try {
VariableTree variableTree =
(VariableTree) BindingPatternTree.class.getMethod("getVariable").invoke(node);
visitBindingPattern(variableTree.getType(), variableTree.getName());
visitBindingPattern(
variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
} catch (ReflectiveOperationException e1) {
try {
Tree type = (Tree) BindingPatternTree.class.getMethod("getType").invoke(node);
Name name = (Name) BindingPatternTree.class.getMethod("getName").invoke(node);
visitBindingPattern(type, name);
visitBindingPattern(/* modifiers= */ null, type, name);
} catch (ReflectiveOperationException e2) {
e2.addSuppressed(e1);
throw new LinkageError(e2.getMessage(), e2);
Expand All @@ -70,7 +70,10 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
return null;
}

private void visitBindingPattern(Tree type, Name name) {
private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
if (modifiers != null) {
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
}
scan(type, null);
builder.breakOp(" ");
visit(name);
Expand Down Expand Up @@ -136,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 @@ -178,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,9 @@
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");

@Parameters(name = "{index}: {0}")
public static Iterable<Object[]> data() throws IOException {
Expand Down Expand Up @@ -76,7 +78,7 @@ public static Iterable<Object[]> data() throws IOException {
case "output":
outputs.put(baseName, contents);
break;
default:
default: // fall out
}
}
}
Expand All @@ -90,6 +92,9 @@ public static Iterable<Object[]> data() throws IOException {
if (JAVA14_TESTS.contains(fileName) && getMajor() < 14) {
continue;
}
if (JAVA16_TESTS.contains(fileName) && getMajor() < 16) {
continue;
}
testInputs.add(new Object[] {fileName, input, expectedOutput});
}
return testInputs;
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,0 +1,8 @@
class T {
int f(Object x) {
if (x instanceof final Integer i) {
return i;
}
return -1;
}
}
@@ -0,0 +1,8 @@
class T {
int f(Object x) {
if (x instanceof final Integer i) {
return i;
}
return -1;
}
}

0 comments on commit 4c0c23b

Please sign in to comment.