diff --git a/core/src/main/java/com/google/googlejavaformat/java/DimensionHelpers.java b/core/src/main/java/com/google/googlejavaformat/java/DimensionHelpers.java index 3bf4793c3..4d5b40966 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/DimensionHelpers.java +++ b/core/src/main/java/com/google/googlejavaformat/java/DimensionHelpers.java @@ -14,6 +14,8 @@ package com.google.googlejavaformat.java; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.ImmutableList; import com.sun.source.tree.AnnotatedTypeTree; import com.sun.source.tree.AnnotationTree; @@ -25,25 +27,22 @@ import java.util.Collections; import java.util.Deque; import java.util.List; +import org.jspecify.annotations.Nullable; /** * Utilities for working with array dimensions. * *

javac's parser does not preserve concrete syntax for mixed-notation arrays, so we have to - * re-lex the input to extra it. + * re-lex the input to extract it. * *

For example, {@code int [] a;} cannot be distinguished from {@code int [] a [];} in the AST. */ -class DimensionHelpers { +final class DimensionHelpers { /** The array dimension specifiers (including any type annotations) associated with a type. */ - static class TypeWithDims { - final Tree node; - final ImmutableList> dims; - - public TypeWithDims(Tree node, ImmutableList> dims) { - this.node = node; - this.dims = dims; + record TypeWithDims(@Nullable Tree node, ImmutableList> dims) { + TypeWithDims { + requireNonNull(dims, "dims"); } } @@ -120,4 +119,6 @@ private static Tree extractDims(Deque> dims, Tree node) { default -> node; }; } + + private DimensionHelpers() {} } diff --git a/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java b/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java index 7cec1fca8..de53679d2 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java +++ b/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java @@ -14,7 +14,8 @@ package com.google.googlejavaformat.java; -import com.google.auto.value.AutoValue; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.Range; import com.google.common.collect.RangeSet; import com.google.common.collect.TreeRangeSet; @@ -28,26 +29,25 @@ */ class FormatFileCallable implements Callable { - @AutoValue - abstract static class Result { - abstract @Nullable Path path(); - - abstract String input(); - - abstract @Nullable String output(); + record Result( + @Nullable Path path, + String input, + @Nullable String output, + @Nullable FormatterException exception) { + Result { + requireNonNull(input, "input"); + } boolean changed() { return !input().equals(output()); } - abstract @Nullable FormatterException exception(); - static Result create( @Nullable Path path, String input, @Nullable String output, @Nullable FormatterException exception) { - return new AutoValue_FormatFileCallable_Result(path, input, output, exception); + return new Result(path, input, output, exception); } } diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java index 67c13d0b3..509e0d33d 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java @@ -14,7 +14,9 @@ package com.google.googlejavaformat.java; -import com.google.auto.value.AutoValue; +import static java.util.Objects.requireNonNull; + +import com.google.auto.value.AutoBuilder; import com.google.errorprone.annotations.Immutable; /** @@ -26,10 +28,14 @@ *

The goal of google-java-format is to provide consistent formatting, and to free developers * from arguments over style choices. It is an explicit non-goal to support developers' individual * preferences, and in fact it would work directly against our primary goals. + * + * @param style Returns the code style. */ @Immutable -@AutoValue -public abstract class JavaFormatterOptions { +public record JavaFormatterOptions(boolean formatJavadoc, boolean reorderModifiers, Style style) { + public JavaFormatterOptions { + requireNonNull(style, "style"); + } public enum Style { /** The default Google Java Style configuration. */ @@ -54,13 +60,6 @@ public int indentationMultiplier() { return style().indentationMultiplier(); } - public abstract boolean formatJavadoc(); - - public abstract boolean reorderModifiers(); - - /** Returns the code style. */ - public abstract Style style(); - /** Returns the default formatting options. */ public static JavaFormatterOptions defaultOptions() { return builder().build(); @@ -68,14 +67,14 @@ public static JavaFormatterOptions defaultOptions() { /** Returns a builder for {@link JavaFormatterOptions}. */ public static Builder builder() { - return new AutoValue_JavaFormatterOptions.Builder() + return new AutoBuilder_JavaFormatterOptions_Builder() .style(Style.GOOGLE) .formatJavadoc(true) .reorderModifiers(true); } /** A builder for {@link JavaFormatterOptions}. */ - @AutoValue.Builder + @AutoBuilder public abstract static class Builder { public abstract Builder style(Style style); diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index 9ff42cce6..64855bfdc 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -42,11 +42,11 @@ import static com.sun.source.tree.Tree.Kind.STRING_LITERAL; import static com.sun.source.tree.Tree.Kind.UNION_TYPE; import static com.sun.source.tree.Tree.Kind.VARIABLE; +import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import com.google.auto.value.AutoOneOf; -import com.google.auto.value.AutoValue; import com.google.common.base.MoreObjects; import com.google.common.base.Predicate; import com.google.common.base.Throwables; @@ -508,14 +508,14 @@ public Void visitNewArray(NewArrayTree node, Void unused) { builder.space(); TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES); - Tree base = extractedDims.node; + Tree base = extractedDims.node(); Deque dimExpressions = new ArrayDeque<>(node.getDimensions()); Deque> annotations = new ArrayDeque<>(); annotations.add(ImmutableList.copyOf(node.getAnnotations())); annotations.addAll(node.getDimAnnotations()); - annotations.addAll(extractedDims.dims); + annotations.addAll(extractedDims.dims()); scan(base, null); builder.open(ZERO); @@ -635,8 +635,8 @@ public Void visitArrayType(ArrayTypeTree node, Void unused) { private void visitAnnotatedArrayType(Tree node) { TypeWithDims extractedDims = DimensionHelpers.extractDims(node, SortedDims.YES); builder.open(plusFour); - scan(extractedDims.node, null); - Deque> dims = new ArrayDeque<>(extractedDims.dims); + scan(extractedDims.node(), null); + Deque> dims = new ArrayDeque<>(extractedDims.dims()); maybeAddDims(dims); Verify.verify(dims.isEmpty()); builder.close(); @@ -1088,7 +1088,8 @@ private static TypeWithDims variableFragmentDims( } TypeWithDims dims = DimensionHelpers.extractDims(type, SortedDims.NO); return new TypeWithDims( - null, leadingDims > 0 ? dims.dims.subList(0, dims.dims.size() - leadingDims) : dims.dims); + null, + leadingDims > 0 ? dims.dims().subList(0, dims.dims().size() - leadingDims) : dims.dims()); } @Override @@ -1536,8 +1537,8 @@ public Void visitMethod(MethodTree node, Void unused) { if (node.getReturnType() != null) { TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getReturnType(), SortedDims.YES); - baseReturnType = extractedDims.node; - dims = new ArrayDeque<>(extractedDims.dims); + baseReturnType = extractedDims.node(); + dims = new ArrayDeque<>(extractedDims.dims()); } else { verticalAnnotations(typeAnnotations); typeAnnotations = ImmutableList.of(); @@ -2554,17 +2555,18 @@ public int compareTo(AnnotationOrModifier o) { * {@code @Deprecated public} as declaration modifiers, and {@code @Nullable} as a type annotation * on the return type. */ - @AutoValue - abstract static class DeclarationModifiersAndTypeAnnotations { - abstract ImmutableList declarationModifiers(); - - abstract ImmutableList typeAnnotations(); + record DeclarationModifiersAndTypeAnnotations( + ImmutableList declarationModifiers, + ImmutableList typeAnnotations) { + DeclarationModifiersAndTypeAnnotations { + requireNonNull(declarationModifiers, "declarationModifiers"); + requireNonNull(typeAnnotations, "typeAnnotations"); + } static DeclarationModifiersAndTypeAnnotations create( ImmutableList declarationModifiers, ImmutableList typeAnnotations) { - return new AutoValue_JavaInputAstVisitor_DeclarationModifiersAndTypeAnnotations( - declarationModifiers, typeAnnotations); + return new DeclarationModifiersAndTypeAnnotations(declarationModifiers, typeAnnotations); } static DeclarationModifiersAndTypeAnnotations empty() { @@ -2968,7 +2970,7 @@ private void visitToDeclare( if (node.getType() != null) { TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES); typeWithDims = Optional.of(extractedDims); - type = extractedDims.node; + type = extractedDims.node(); } else { typeWithDims = Optional.empty(); type = null; @@ -3656,7 +3658,7 @@ protected int declareOne( } Deque> dims = - new ArrayDeque<>(typeWithDims.isPresent() ? typeWithDims.get().dims : ImmutableList.of()); + new ArrayDeque<>(typeWithDims.isPresent() ? typeWithDims.get().dims() : ImmutableList.of()); int baseDims = 0; // preprocess to separate declaration annotations + modifiers, type annotations @@ -3688,8 +3690,8 @@ protected int declareOne( visitAnnotations(annotations, BreakOrNot.NO, BreakOrNot.YES); if (isVar) { token("var"); - } else if (typeWithDims.isPresent() && typeWithDims.get().node != null) { - scan(typeWithDims.get().node, null); + } else if (typeWithDims.isPresent() && typeWithDims.get().node() != null) { + scan(typeWithDims.get().node(), null); int totalDims = dims.size(); builder.open(plusFour); maybeAddDims(dims); @@ -3838,8 +3840,8 @@ private void declareMany(List fragments, Direction annotationDirec builder.open(plusFour); builder.open(ZERO); TypeWithDims extractedDims = DimensionHelpers.extractDims(type, SortedDims.YES); - Deque> dims = new ArrayDeque<>(extractedDims.dims); - scan(extractedDims.node, null); + Deque> dims = new ArrayDeque<>(extractedDims.dims()); + scan(extractedDims.node(), null); int baseDims = dims.size(); maybeAddDims(dims); baseDims = baseDims - dims.size(); @@ -3850,7 +3852,7 @@ private void declareMany(List fragments, Direction annotationDirec } TypeWithDims fragmentDims = variableFragmentDims(afterFirstToken, baseDims, fragment.getType()); - dims = new ArrayDeque<>(fragmentDims.dims); + dims = new ArrayDeque<>(fragmentDims.dims()); builder.breakOp(" "); builder.open(ZERO); maybeAddDims(dims);