Skip to content

Commit 4962993

Browse files
cushongoogle-java-format Team
authored andcommitted
Migrate to records in google-java-format
I was debugging something where having a `toString` implementation in `TypeWithDims` would have been helpful. PiperOrigin-RevId: 876329640
1 parent 075e025 commit 4962993

File tree

4 files changed

+56
-54
lines changed

4 files changed

+56
-54
lines changed

core/src/main/java/com/google/googlejavaformat/java/DimensionHelpers.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17+
import static java.util.Objects.requireNonNull;
18+
1719
import com.google.common.collect.ImmutableList;
1820
import com.sun.source.tree.AnnotatedTypeTree;
1921
import com.sun.source.tree.AnnotationTree;
@@ -25,25 +27,22 @@
2527
import java.util.Collections;
2628
import java.util.Deque;
2729
import java.util.List;
30+
import org.jspecify.annotations.Nullable;
2831

2932
/**
3033
* Utilities for working with array dimensions.
3134
*
3235
* <p>javac's parser does not preserve concrete syntax for mixed-notation arrays, so we have to
33-
* re-lex the input to extra it.
36+
* re-lex the input to extract it.
3437
*
3538
* <p>For example, {@code int [] a;} cannot be distinguished from {@code int [] a [];} in the AST.
3639
*/
37-
class DimensionHelpers {
40+
final class DimensionHelpers {
3841

3942
/** The array dimension specifiers (including any type annotations) associated with a type. */
40-
static class TypeWithDims {
41-
final Tree node;
42-
final ImmutableList<List<AnnotationTree>> dims;
43-
44-
public TypeWithDims(Tree node, ImmutableList<List<AnnotationTree>> dims) {
45-
this.node = node;
46-
this.dims = dims;
43+
record TypeWithDims(@Nullable Tree node, ImmutableList<List<AnnotationTree>> dims) {
44+
TypeWithDims {
45+
requireNonNull(dims, "dims");
4746
}
4847
}
4948

@@ -120,4 +119,6 @@ private static Tree extractDims(Deque<List<AnnotationTree>> dims, Tree node) {
120119
default -> node;
121120
};
122121
}
122+
123+
private DimensionHelpers() {}
123124
}

core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17-
import com.google.auto.value.AutoValue;
17+
import static java.util.Objects.requireNonNull;
18+
1819
import com.google.common.collect.Range;
1920
import com.google.common.collect.RangeSet;
2021
import com.google.common.collect.TreeRangeSet;
@@ -28,26 +29,25 @@
2829
*/
2930
class FormatFileCallable implements Callable<FormatFileCallable.Result> {
3031

31-
@AutoValue
32-
abstract static class Result {
33-
abstract @Nullable Path path();
34-
35-
abstract String input();
36-
37-
abstract @Nullable String output();
32+
record Result(
33+
@Nullable Path path,
34+
String input,
35+
@Nullable String output,
36+
@Nullable FormatterException exception) {
37+
Result {
38+
requireNonNull(input, "input");
39+
}
3840

3941
boolean changed() {
4042
return !input().equals(output());
4143
}
4244

43-
abstract @Nullable FormatterException exception();
44-
4545
static Result create(
4646
@Nullable Path path,
4747
String input,
4848
@Nullable String output,
4949
@Nullable FormatterException exception) {
50-
return new AutoValue_FormatFileCallable_Result(path, input, output, exception);
50+
return new Result(path, input, output, exception);
5151
}
5252
}
5353

core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17-
import com.google.auto.value.AutoValue;
17+
import static java.util.Objects.requireNonNull;
18+
19+
import com.google.auto.value.AutoBuilder;
1820
import com.google.errorprone.annotations.Immutable;
1921

2022
/**
@@ -26,10 +28,14 @@
2628
* <p>The goal of google-java-format is to provide consistent formatting, and to free developers
2729
* from arguments over style choices. It is an explicit non-goal to support developers' individual
2830
* preferences, and in fact it would work directly against our primary goals.
31+
*
32+
* @param style Returns the code style.
2933
*/
3034
@Immutable
31-
@AutoValue
32-
public abstract class JavaFormatterOptions {
35+
public record JavaFormatterOptions(boolean formatJavadoc, boolean reorderModifiers, Style style) {
36+
public JavaFormatterOptions {
37+
requireNonNull(style, "style");
38+
}
3339

3440
public enum Style {
3541
/** The default Google Java Style configuration. */
@@ -54,28 +60,21 @@ public int indentationMultiplier() {
5460
return style().indentationMultiplier();
5561
}
5662

57-
public abstract boolean formatJavadoc();
58-
59-
public abstract boolean reorderModifiers();
60-
61-
/** Returns the code style. */
62-
public abstract Style style();
63-
6463
/** Returns the default formatting options. */
6564
public static JavaFormatterOptions defaultOptions() {
6665
return builder().build();
6766
}
6867

6968
/** Returns a builder for {@link JavaFormatterOptions}. */
7069
public static Builder builder() {
71-
return new AutoValue_JavaFormatterOptions.Builder()
70+
return new AutoBuilder_JavaFormatterOptions_Builder()
7271
.style(Style.GOOGLE)
7372
.formatJavadoc(true)
7473
.reorderModifiers(true);
7574
}
7675

7776
/** A builder for {@link JavaFormatterOptions}. */
78-
@AutoValue.Builder
77+
@AutoBuilder
7978
public abstract static class Builder {
8079

8180
public abstract Builder style(Style style);

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
import static com.sun.source.tree.Tree.Kind.STRING_LITERAL;
4343
import static com.sun.source.tree.Tree.Kind.UNION_TYPE;
4444
import static com.sun.source.tree.Tree.Kind.VARIABLE;
45+
import static java.util.Objects.requireNonNull;
4546
import static java.util.stream.Collectors.joining;
4647
import static java.util.stream.Collectors.toList;
4748

4849
import com.google.auto.value.AutoOneOf;
49-
import com.google.auto.value.AutoValue;
5050
import com.google.common.base.MoreObjects;
5151
import com.google.common.base.Predicate;
5252
import com.google.common.base.Throwables;
@@ -508,14 +508,14 @@ public Void visitNewArray(NewArrayTree node, Void unused) {
508508
builder.space();
509509

510510
TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
511-
Tree base = extractedDims.node;
511+
Tree base = extractedDims.node();
512512

513513
Deque<ExpressionTree> dimExpressions = new ArrayDeque<>(node.getDimensions());
514514

515515
Deque<List<? extends AnnotationTree>> annotations = new ArrayDeque<>();
516516
annotations.add(ImmutableList.copyOf(node.getAnnotations()));
517517
annotations.addAll(node.getDimAnnotations());
518-
annotations.addAll(extractedDims.dims);
518+
annotations.addAll(extractedDims.dims());
519519

520520
scan(base, null);
521521
builder.open(ZERO);
@@ -635,8 +635,8 @@ public Void visitArrayType(ArrayTypeTree node, Void unused) {
635635
private void visitAnnotatedArrayType(Tree node) {
636636
TypeWithDims extractedDims = DimensionHelpers.extractDims(node, SortedDims.YES);
637637
builder.open(plusFour);
638-
scan(extractedDims.node, null);
639-
Deque<List<? extends AnnotationTree>> dims = new ArrayDeque<>(extractedDims.dims);
638+
scan(extractedDims.node(), null);
639+
Deque<List<? extends AnnotationTree>> dims = new ArrayDeque<>(extractedDims.dims());
640640
maybeAddDims(dims);
641641
Verify.verify(dims.isEmpty());
642642
builder.close();
@@ -1088,7 +1088,8 @@ private static TypeWithDims variableFragmentDims(
10881088
}
10891089
TypeWithDims dims = DimensionHelpers.extractDims(type, SortedDims.NO);
10901090
return new TypeWithDims(
1091-
null, leadingDims > 0 ? dims.dims.subList(0, dims.dims.size() - leadingDims) : dims.dims);
1091+
null,
1092+
leadingDims > 0 ? dims.dims().subList(0, dims.dims().size() - leadingDims) : dims.dims());
10921093
}
10931094

10941095
@Override
@@ -1536,8 +1537,8 @@ public Void visitMethod(MethodTree node, Void unused) {
15361537
if (node.getReturnType() != null) {
15371538
TypeWithDims extractedDims =
15381539
DimensionHelpers.extractDims(node.getReturnType(), SortedDims.YES);
1539-
baseReturnType = extractedDims.node;
1540-
dims = new ArrayDeque<>(extractedDims.dims);
1540+
baseReturnType = extractedDims.node();
1541+
dims = new ArrayDeque<>(extractedDims.dims());
15411542
} else {
15421543
verticalAnnotations(typeAnnotations);
15431544
typeAnnotations = ImmutableList.of();
@@ -2554,17 +2555,18 @@ public int compareTo(AnnotationOrModifier o) {
25542555
* {@code @Deprecated public} as declaration modifiers, and {@code @Nullable} as a type annotation
25552556
* on the return type.
25562557
*/
2557-
@AutoValue
2558-
abstract static class DeclarationModifiersAndTypeAnnotations {
2559-
abstract ImmutableList<AnnotationOrModifier> declarationModifiers();
2560-
2561-
abstract ImmutableList<AnnotationTree> typeAnnotations();
2558+
record DeclarationModifiersAndTypeAnnotations(
2559+
ImmutableList<AnnotationOrModifier> declarationModifiers,
2560+
ImmutableList<AnnotationTree> typeAnnotations) {
2561+
DeclarationModifiersAndTypeAnnotations {
2562+
requireNonNull(declarationModifiers, "declarationModifiers");
2563+
requireNonNull(typeAnnotations, "typeAnnotations");
2564+
}
25622565

25632566
static DeclarationModifiersAndTypeAnnotations create(
25642567
ImmutableList<AnnotationOrModifier> declarationModifiers,
25652568
ImmutableList<AnnotationTree> typeAnnotations) {
2566-
return new AutoValue_JavaInputAstVisitor_DeclarationModifiersAndTypeAnnotations(
2567-
declarationModifiers, typeAnnotations);
2569+
return new DeclarationModifiersAndTypeAnnotations(declarationModifiers, typeAnnotations);
25682570
}
25692571

25702572
static DeclarationModifiersAndTypeAnnotations empty() {
@@ -2968,7 +2970,7 @@ private void visitToDeclare(
29682970
if (node.getType() != null) {
29692971
TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
29702972
typeWithDims = Optional.of(extractedDims);
2971-
type = extractedDims.node;
2973+
type = extractedDims.node();
29722974
} else {
29732975
typeWithDims = Optional.empty();
29742976
type = null;
@@ -3656,7 +3658,7 @@ protected int declareOne(
36563658
}
36573659

36583660
Deque<List<? extends AnnotationTree>> dims =
3659-
new ArrayDeque<>(typeWithDims.isPresent() ? typeWithDims.get().dims : ImmutableList.of());
3661+
new ArrayDeque<>(typeWithDims.isPresent() ? typeWithDims.get().dims() : ImmutableList.of());
36603662
int baseDims = 0;
36613663

36623664
// preprocess to separate declaration annotations + modifiers, type annotations
@@ -3688,8 +3690,8 @@ protected int declareOne(
36883690
visitAnnotations(annotations, BreakOrNot.NO, BreakOrNot.YES);
36893691
if (isVar) {
36903692
token("var");
3691-
} else if (typeWithDims.isPresent() && typeWithDims.get().node != null) {
3692-
scan(typeWithDims.get().node, null);
3693+
} else if (typeWithDims.isPresent() && typeWithDims.get().node() != null) {
3694+
scan(typeWithDims.get().node(), null);
36933695
int totalDims = dims.size();
36943696
builder.open(plusFour);
36953697
maybeAddDims(dims);
@@ -3838,8 +3840,8 @@ private void declareMany(List<VariableTree> fragments, Direction annotationDirec
38383840
builder.open(plusFour);
38393841
builder.open(ZERO);
38403842
TypeWithDims extractedDims = DimensionHelpers.extractDims(type, SortedDims.YES);
3841-
Deque<List<? extends AnnotationTree>> dims = new ArrayDeque<>(extractedDims.dims);
3842-
scan(extractedDims.node, null);
3843+
Deque<List<? extends AnnotationTree>> dims = new ArrayDeque<>(extractedDims.dims());
3844+
scan(extractedDims.node(), null);
38433845
int baseDims = dims.size();
38443846
maybeAddDims(dims);
38453847
baseDims = baseDims - dims.size();
@@ -3850,7 +3852,7 @@ private void declareMany(List<VariableTree> fragments, Direction annotationDirec
38503852
}
38513853
TypeWithDims fragmentDims =
38523854
variableFragmentDims(afterFirstToken, baseDims, fragment.getType());
3853-
dims = new ArrayDeque<>(fragmentDims.dims);
3855+
dims = new ArrayDeque<>(fragmentDims.dims());
38543856
builder.breakOp(" ");
38553857
builder.open(ZERO);
38563858
maybeAddDims(dims);

0 commit comments

Comments
 (0)