Skip to content

Commit 26f6755

Browse files
authored
Merge pull request #98 from openjavaformat/records-instead-of-immutables
Use records instead of Immutables for plain value types
2 parents b1e3c41 + 4f407a9 commit 26f6755

9 files changed

Lines changed: 85 additions & 202 deletions

File tree

‎open-java-format-jdk-bootstrap/build.gradle‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ configurations {
88
}
99

1010
dependencies {
11-
annotationProcessor libs.immutables.value
12-
1311
api project(':open-java-format-spi')
14-
compileOnly variantOf(libs.immutables.value) { classifier('annotations') }
1512
implementation libs.jackson.databind
1613

1714
testImplementation project(':open-java-format')
@@ -26,11 +23,6 @@ dependencies {
2623
}
2724
}
2825

29-
// Immutables' processor keeps Gradle's incremental compilation working only when asked to.
30-
tasks.named('compileJava', JavaCompile) {
31-
options.compilerArgs.add('-Aimmutables.gradle.incremental')
32-
}
33-
3426
tasks.named('test', Test.class) {
3527
inputs.files(configurations.named('formatterNativeImage'))
3628
environment.put('NATIVE_IMAGE_CLASSPATH', configurations.formatterNativeImage.asPath)

‎open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java‎

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.List;
3434
import java.util.Optional;
3535
import java.util.stream.Collectors;
36-
import org.immutables.value.Value;
3736

3837
public final class BootstrappingFormatterService implements FormatterService {
3938
private static final ObjectMapper MAPPER =
@@ -80,13 +79,12 @@ public String fixImports(String input) throws FormatterException {
8079

8180
private ImmutableList<Replacement> getFormatReplacementsInternal(String input, Collection<Range<Integer>> ranges)
8281
throws IOException {
83-
FormatterCliArgs command = FormatterCliArgs.builder()
84-
.jdkPath(jdkPath)
85-
.withJvmArgsForVersion(jdkMajorVersion)
86-
.implementationClasspath(implementationClassPath)
87-
.outputReplacements(true)
88-
.characterRanges(ranges.stream().map(RangeUtils::toStringRange).collect(Collectors.toList()))
89-
.build();
82+
FormatterCliArgs command = new FormatterCliArgs(
83+
jdkPath,
84+
jvmArgsForVersion(jdkMajorVersion),
85+
implementationClassPath,
86+
/* outputReplacements= */ true,
87+
ranges.stream().map(RangeUtils::toStringRange).collect(Collectors.toList()));
9088

9189
@SuppressWarnings("for-rollout:NullAway")
9290
Optional<String> output =
@@ -98,43 +96,50 @@ private ImmutableList<Replacement> getFormatReplacementsInternal(String input, C
9896
}
9997

10098
private String runFormatterCommand(String input) throws IOException {
101-
FormatterCliArgs command = FormatterCliArgs.builder()
102-
.jdkPath(jdkPath)
103-
.withJvmArgsForVersion(jdkMajorVersion)
104-
.implementationClasspath(implementationClassPath)
105-
.outputReplacements(false)
106-
.build();
99+
FormatterCliArgs command = new FormatterCliArgs(
100+
jdkPath,
101+
jvmArgsForVersion(jdkMajorVersion),
102+
implementationClassPath,
103+
/* outputReplacements= */ false,
104+
/* characterRanges= */ List.of());
107105
return FormatterCommandRunner.runWithStdin(command.toArgs(), input, Optional.ofNullable(jdkPath.getParent()))
108106
.orElse(input);
109107
}
110108

111-
@Value.Immutable
112-
interface FormatterCliArgs {
113-
List<String> characterRanges();
114-
115-
boolean outputReplacements();
116-
117-
Path jdkPath();
118-
119-
List<Path> implementationClasspath();
109+
private static List<String> jvmArgsForVersion(int majorJvmVersion) {
110+
if (majorJvmVersion >= 16) {
111+
return List.of(
112+
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
113+
"--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
114+
"--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
115+
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
116+
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED");
117+
}
118+
return List.of();
119+
}
120120

121-
List<String> jvmArgs();
121+
record FormatterCliArgs(
122+
Path jdkPath,
123+
List<String> jvmArgs,
124+
List<Path> implementationClasspath,
125+
boolean outputReplacements,
126+
List<String> characterRanges) {
122127

123-
default List<String> toArgs() {
128+
List<String> toArgs() {
124129
ImmutableList.Builder<String> args = ImmutableList.<String>builder()
125-
.add(jdkPath().toAbsolutePath().toString())
126-
.addAll(jvmArgs())
130+
.add(jdkPath.toAbsolutePath().toString())
131+
.addAll(jvmArgs)
127132
.add(
128133
"-cp",
129-
implementationClasspath().stream()
134+
implementationClasspath.stream()
130135
.map(path -> path.toAbsolutePath().toString())
131136
.collect(Collectors.joining(System.getProperty("path.separator"))))
132137
.add(FORMATTER_MAIN_CLASS);
133138

134-
if (!characterRanges().isEmpty()) {
135-
args.add("--character-ranges", Joiner.on(',').join(characterRanges()));
139+
if (!characterRanges.isEmpty()) {
140+
args.add("--character-ranges", Joiner.on(',').join(characterRanges));
136141
}
137-
if (outputReplacements()) {
142+
if (outputReplacements) {
138143
args.add("--output-replacements");
139144
}
140145

@@ -143,23 +148,5 @@ default List<String> toArgs() {
143148
.add("-")
144149
.build();
145150
}
146-
147-
static Builder builder() {
148-
return new Builder();
149-
}
150-
151-
final class Builder extends ImmutableFormatterCliArgs.Builder {
152-
Builder withJvmArgsForVersion(Integer majorJvmVersion) {
153-
if (majorJvmVersion >= 16) {
154-
addJvmArgs(
155-
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
156-
"--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
157-
"--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
158-
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
159-
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED");
160-
}
161-
return this;
162-
}
163-
}
164151
}
165152
}

‎open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java‎

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.List;
3333
import java.util.Optional;
3434
import java.util.stream.Collectors;
35-
import org.immutables.value.Value;
3635

3736
public class NativeImageFormatterService implements FormatterService {
3837
private static final ObjectMapper MAPPER =
@@ -47,12 +46,10 @@ public NativeImageFormatterService(Path nativeImagePath) {
4746
public ImmutableList<Replacement> getFormatReplacements(String input, Collection<Range<Integer>> ranges) {
4847
Optional<String> output = Optional.empty();
4948
try {
50-
FormatterNativeImageArgs command = FormatterNativeImageArgs.builder()
51-
.nativeImagePath(nativeImagePath)
52-
.outputReplacements(true)
53-
.characterRanges(
54-
ranges.stream().map(RangeUtils::toStringRange).collect(Collectors.toList()))
55-
.build();
49+
FormatterNativeImageArgs command = new FormatterNativeImageArgs(
50+
nativeImagePath,
51+
/* outputReplacements= */ true,
52+
ranges.stream().map(RangeUtils::toStringRange).collect(Collectors.toList()));
5653

5754
output = FormatterCommandRunner.runWithStdin(
5855
command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent()));
@@ -85,32 +82,23 @@ public String fixImports(String input) {
8582
}
8683

8784
private String runFormatterCommand(String input) throws IOException {
88-
FormatterNativeImageArgs command = FormatterNativeImageArgs.builder()
89-
.nativeImagePath(nativeImagePath)
90-
.outputReplacements(false)
91-
.build();
85+
FormatterNativeImageArgs command = new FormatterNativeImageArgs(
86+
nativeImagePath, /* outputReplacements= */ false, /* characterRanges= */ List.of());
9287
return FormatterCommandRunner.runWithStdin(
9388
command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent()))
9489
.orElse(input);
9590
}
9691

97-
@Value.Immutable
98-
interface FormatterNativeImageArgs {
99-
100-
List<String> characterRanges();
101-
102-
boolean outputReplacements();
103-
104-
Path nativeImagePath();
92+
record FormatterNativeImageArgs(Path nativeImagePath, boolean outputReplacements, List<String> characterRanges) {
10593

106-
default List<String> toArgs() {
94+
List<String> toArgs() {
10795
ImmutableList.Builder<String> args = ImmutableList.<String>builder()
108-
.add(nativeImagePath().toAbsolutePath().toString());
96+
.add(nativeImagePath.toAbsolutePath().toString());
10997

110-
if (!characterRanges().isEmpty()) {
111-
args.add("--character-ranges", Joiner.on(',').join(characterRanges()));
98+
if (!characterRanges.isEmpty()) {
99+
args.add("--character-ranges", Joiner.on(',').join(characterRanges));
112100
}
113-
if (outputReplacements()) {
101+
if (outputReplacements) {
114102
args.add("--output-replacements");
115103
}
116104

@@ -119,11 +107,5 @@ default List<String> toArgs() {
119107
.add("-")
120108
.build();
121109
}
122-
123-
static FormatterNativeImageArgs.Builder builder() {
124-
return new FormatterNativeImageArgs.Builder();
125-
}
126-
127-
final class Builder extends ImmutableFormatterNativeImageArgs.Builder {}
128110
}
129111
}

‎open-java-format/src/main/java/com/palantir/javaformat/OpsBuilder.java‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.Iterables;
2222
import com.google.common.collect.Multimap;
23+
import com.google.errorprone.annotations.Immutable;
2324
import com.palantir.javaformat.Indent.Const;
2425
import com.palantir.javaformat.Input.Tok;
2526
import com.palantir.javaformat.doc.Break;
@@ -35,14 +36,9 @@
3536
import com.palantir.javaformat.java.FormatterDiagnostic;
3637
import com.palantir.javaformat.java.InputMetadata;
3738
import com.palantir.javaformat.java.InputMetadataBuilder;
38-
import java.lang.annotation.ElementType;
39-
import java.lang.annotation.Retention;
40-
import java.lang.annotation.RetentionPolicy;
41-
import java.lang.annotation.Target;
4239
import java.util.ArrayList;
4340
import java.util.List;
4441
import java.util.Optional;
45-
import org.immutables.value.Value;
4642

4743
/** An {@code OpsBuilder} creates a list of {@link Op}s, which is turned into a {@link Doc} by {@link DocBuilder}. */
4844
public final class OpsBuilder {
@@ -86,6 +82,7 @@ public Integer actualStartColumn(int position) {
8682
}
8783

8884
/** A request to add or remove a blank line in the output. */
85+
@Immutable
8986
public abstract static class BlankLineWanted {
9087

9188
/** Always emit a blank line. */
@@ -501,18 +498,7 @@ private static int getI(Input.Token token) {
501498

502499
private static final NonBreakingSpace SPACE = NonBreakingSpace.make();
503500

504-
@Target(ElementType.TYPE)
505-
@Retention(RetentionPolicy.SOURCE)
506-
@Value.Style(overshadowImplementation = true)
507-
@interface OpsOutputStyle {}
508-
509-
@OpsOutputStyle
510-
@Value.Immutable
511-
public interface OpsOutput {
512-
ImmutableList<Op> ops();
513-
514-
InputMetadata inputMetadata();
515-
}
501+
public record OpsOutput(ImmutableList<Op> ops, InputMetadata inputMetadata) {}
516502

517503
/** Build a list of {@link Op}s from the {@code OpsBuilder}. */
518504
public OpsOutput build() {
@@ -669,10 +655,7 @@ public OpsOutput build() {
669655
afterForcedBreak = isForcedBreak(op);
670656
}
671657
}
672-
return ImmutableOpsOutput.builder()
673-
.ops(newOps.build())
674-
.inputMetadata(inputMetadataBuilder.build())
675-
.build();
658+
return new OpsOutput(newOps.build(), inputMetadataBuilder.build());
676659
}
677660

678661
private static boolean isNonNlsComment(Input.Tok tokAfter) {

‎open-java-format/src/main/java/com/palantir/javaformat/doc/Comment.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public State computeBreaks(
9696
int column = lastLineStart == 0 ? state.column() + lastLineLength : lastLineLength;
9797
return state.withColumn(column)
9898
.addNewLines(Iterators.size(Newlines.lineOffsetIterator(text)))
99-
.withTokState(this, ImmutableTokState.of(text));
99+
.withTokState(this, new State.TokState(text));
100100
}
101101

102102
@Override

‎open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java‎

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
import com.palantir.javaformat.doc.Obs.ExplorationNode;
3737
import com.palantir.javaformat.doc.Obs.LevelNode;
3838
import com.palantir.javaformat.doc.StartsWithBreakVisitor.Result;
39-
import java.lang.annotation.ElementType;
40-
import java.lang.annotation.Retention;
41-
import java.lang.annotation.RetentionPolicy;
42-
import java.lang.annotation.Target;
4339
import java.util.ArrayList;
4440
import java.util.HashSet;
4541
import java.util.List;
@@ -49,7 +45,6 @@
4945
import java.util.stream.Collector;
5046
import java.util.stream.Collectors;
5147
import java.util.stream.Stream;
52-
import org.immutables.value.Value;
5348

5449
/** A {@code Level} inside a {@link Doc}. */
5550
public final class Level extends Doc {
@@ -127,7 +122,7 @@ protected Range<Integer> computeRange() {
127122
@Override
128123
public State computeBreaks(CommentsHelper commentsHelper, int maxWidth, State state, Obs.ExplorationNode observer) {
129124
return tryToFitOnOneLine(maxWidth, state, docs)
130-
.map(newWidth -> state.withColumn(newWidth).withLevelState(this, ImmutableLevelState.of(true)))
125+
.map(newWidth -> state.withColumn(newWidth).withLevelState(this, new State.LevelState(true)))
131126
.orElseGet(() -> {
132127
Obs.LevelNode childLevel = observer.newChildNode(this, state);
133128
State newState =
@@ -631,19 +626,20 @@ private State tryToLayOutLevelOnOneLine(
631626
}
632627

633628
private static SplitsBreaks splitByBreaks(List<Doc> docs) {
634-
ImmutableSplitsBreaks.Builder builder = ImmutableSplitsBreaks.builder();
629+
ImmutableList.Builder<ImmutableList<Doc>> splits = ImmutableList.builder();
630+
ImmutableList.Builder<Break> breaks = ImmutableList.builder();
635631
ImmutableList.Builder<Doc> currentSplit = ImmutableList.builder();
636632
for (Doc doc : docs) {
637633
if (doc instanceof Break b) {
638-
builder.addSplits(currentSplit.build());
634+
splits.add(currentSplit.build());
639635
currentSplit = ImmutableList.builder();
640-
builder.addBreaks(b);
636+
breaks.add(b);
641637
} else {
642638
currentSplit.add(doc);
643639
}
644640
}
645-
builder.addSplits(currentSplit.build());
646-
return builder.build();
641+
splits.add(currentSplit.build());
642+
return new SplitsBreaks(splits.build(), breaks.build());
647643
}
648644

649645
/** Compute breaks for a {@link Level} that spans multiple lines. */
@@ -837,18 +833,11 @@ public String toString() {
837833
.toString();
838834
}
839835

840-
@Target(ElementType.TYPE)
841-
@Retention(RetentionPolicy.SOURCE)
842-
@Value.Style(overshadowImplementation = true)
843-
@interface SplitsBreaksStyle {}
844-
845-
@SplitsBreaksStyle
846-
@Value.Immutable
847-
interface SplitsBreaks {
848-
/** Groups of {@link Doc}s that are children of the current {@link Level}, separated by {@link Break}s. */
849-
ImmutableList<ImmutableList<Doc>> splits();
850-
851-
/** {@link Break}s between {@link Doc}s in the current {@link Level}. */
852-
ImmutableList<Break> breaks();
853-
}
836+
/**
837+
* The children of the current {@link Level}, cut at its {@link Break}s.
838+
*
839+
* @param splits groups of {@link Doc}s that are children of the current level, separated by breaks
840+
* @param breaks the breaks between those groups
841+
*/
842+
record SplitsBreaks(ImmutableList<ImmutableList<Doc>> splits, ImmutableList<Break> breaks) {}
854843
}

0 commit comments

Comments
 (0)