Skip to content

Replace derive4j with a sealed interface and records - #99

Merged
abashev merged 1 commit into
mainfrom
break-behaviour-without-derive4j
Oct 2, 2026
Merged

abashev merged 1 commit into
mainfrom
break-behaviour-without-derive4j

Conversation

@abashev

@abashev abashev commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

Follows #98 and targets its branch, so this diff shows only its own change; once #98 is merged it goes to main.

derive4j generated BreakBehaviours for one type, BreakBehaviour: four cases, two of them with a keepIndentWhenInlined flag. Standard Java does the same job now, and open-java-format no longer runs the derive4j processor.

What changes

  • BreakBehaviour is a sealed interface whose cases are records: BreakThisLevel, PreferBreakingLastInnerLevel, InlineSuffix and BreakOnlyIfInnerLevelsThenFitOnOneLine. The factories the 25 call sites used moved from BreakBehaviours onto BreakBehaviour, with the same names.
  • Level picks the case with a switch over record patterns instead of the Cases visitor and the caseOf chains, so the compiler checks that every case is handled. The JSON serializer and LevelDelimitedFlatValueDocVisitor switch the same way.
  • Each case keeps derive4j's toString, such as preferBreakingLastInnerLevel(true), which the debug visitor prints, and the JSON keeps its shape: {"type": ..., "keepIndentWhenInlined": ...}.
  • Level.BreakImpl.preferBreakingLastInnerLevel never read the flag and now takes no parameter. Error Prone's UnusedVariable said so once the method no longer implemented an interface.

Checked

  • The 15,747 files of the JDK 21 sources format byte for byte as with Use records instead of Immutables for plain value types #98's head.
  • ./gradlew test: 1597 tests in :open-java-format, 10 in the IDEA plugin, 51 in the Gradle plugin.
  • With -PnativeImage=true, the native image compiles from this and the Gradle plugin's 60 tests and jdk-bootstrap's 8 pass against it.
  • formatDiff through the released plugin, which reshaped the new switches.

derive4j generated BreakBehaviours for one type, BreakBehaviour: four
cases, two of them with a keepIndentWhenInlined flag. BreakBehaviour is
now a sealed interface whose cases are records, and the factories the
call sites used moved onto it. Level picks the case with a switch over
record patterns instead of a Cases visitor and caseOf chains, so the
compiler checks that every case is handled; the JSON serializer and
LevelDelimitedFlatValueDocVisitor switch the same way. Each case keeps
derive4j's toString, which the debug visitor prints, and the JSON keeps
its shape.

Level.BreakImpl's preferBreakingLastInnerLevel never read the flag and
now takes no parameter. open-java-format no longer runs the derive4j
processor.

The 15,747 files of the JDK 21 sources format exactly as before, and the
plugins' tests pass against a native image built from this.
Base automatically changed from records-instead-of-immutables to main October 2, 2026 21:30
@abashev
abashev merged commit 2fbf175 into main Oct 2, 2026
12 checks passed
@abashev
abashev deleted the break-behaviour-without-derive4j branch October 2, 2026 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant