Skip to content

Commit 29392ec

Browse files
committed
Validate modifier reordering runtime support
1 parent 207fe52 commit 29392ec

5 files changed

Lines changed: 24 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ For available versions see [releases](https://github.com/sbt/sbt-java-formatter/
3838
* The `javafmtRemoveUnusedImports` setting controls whether unused imports are removed (`true` by default).
3939
* The `javafmtReflowLongStrings` setting controls whether long string literals are reflowed (`true` by default).
4040
* The `javafmtFormatJavadoc` setting controls whether Javadoc comments are reformatted (`true` by default).
41-
* The `javafmtReorderModifiers` setting controls whether modifiers are reordered into JLS order (`true` by default).
41+
* The `javafmtReorderModifiers` setting controls whether modifiers are reordered into JLS order (`true` by default). Disabling it requires `javafmtFormatterCompatibleJavaVersion := 21`.
4242
* The `javafmtFormatterCompatibleJavaVersion` setting selects which `google-java-format` runtime line to use (`21` by default).
4343
* The `javafmtJavaMaxHeap` setting controls the maximum heap passed to the forked `google-java-format` JVM (`Some("256m")` by default).
4444

plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ object JavaFormatterPlugin extends AutoPlugin {
143143
val iF = (javafmt / includeFilter).value
144144
val eF = (javafmt / excludeFilter).value
145145
val cache = streamz.cacheStoreFactory
146-
val options = javafmtOptions.value
146+
val options =
147+
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value)
147148
val formatterClasspath = javafmtFormatterClasspath.value.toVector
148149
val javaMaxHeap = javafmtJavaMaxHeap.value
149150
val sortImports = javafmtSortImports.value
@@ -173,7 +174,8 @@ object JavaFormatterPlugin extends AutoPlugin {
173174
val iF = (javafmt / includeFilter).value
174175
val eF = (javafmt / excludeFilter).value
175176
val cache = (javafmt / streams).value.cacheStoreFactory
176-
val options = javafmtOptions.value
177+
val options =
178+
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value)
177179
val formatterClasspath = javafmtFormatterClasspath.value.toVector
178180
val javaMaxHeap = javafmtJavaMaxHeap.value
179181
val sortImports = javafmtSortImports.value
@@ -203,7 +205,8 @@ object JavaFormatterPlugin extends AutoPlugin {
203205
val iF = (javafmt / includeFilter).value
204206
val eF = (javafmt / excludeFilter).value
205207
val cache = streamz.cacheStoreFactory
206-
val options = javafmtOptions.value
208+
val options =
209+
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value)
207210
val formatterClasspath = javafmtFormatterClasspath.value.toVector
208211
val javaMaxHeap = javafmtJavaMaxHeap.value
209212
val sortImports = javafmtSortImports.value
@@ -233,7 +236,8 @@ object JavaFormatterPlugin extends AutoPlugin {
233236
val iF = (javafmt / includeFilter).value
234237
val eF = (javafmt / excludeFilter).value
235238
val cache = (javafmt / streams).value.cacheStoreFactory
236-
val options = javafmtOptions.value
239+
val options =
240+
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value)
237241
val formatterClasspath = javafmtFormatterClasspath.value.toVector
238242
val javaMaxHeap = javafmtJavaMaxHeap.value
239243
val sortImports = javafmtSortImports.value
@@ -289,6 +293,17 @@ object JavaFormatterPlugin extends AutoPlugin {
289293
s"Unsupported javafmtFormatterCompatibleJavaVersion: $other. Expected one of: 11, 17, 21.")
290294
}
291295

296+
private def validateOptions(options: JavaFormatterOptions, compatibleJavaVersion: Int): JavaFormatterOptions = {
297+
if (!options.reorderModifiers() && compatibleJavaVersion != 21) {
298+
throw new MessageOnlyException(
299+
"Disabling modifier reordering requires " +
300+
"ThisBuild / javafmtFormatterCompatibleJavaVersion := 21 " +
301+
"because the Java 11 and Java 17 formatter runtime lines do not support " +
302+
"--skip-reordering-modifiers.")
303+
}
304+
options
305+
}
306+
292307
@transient
293308
private val javafmtDoFormatOnCompile =
294309
taskKey[Unit]("Format Java source files if javafmtOnCompile is on.")

plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/src/main/java/com/lightbend/BadFormatting.java

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
ThisBuild / javafmtFormatterCompatibleJavaVersion := 11
1+
ThisBuild / javafmtFormatterCompatibleJavaVersion := 21
22
ThisBuild / javafmtReorderModifiers := false

plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
> javafmtCheck
44

55
$ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java
6+
7+
> set ThisBuild / javafmtFormatterCompatibleJavaVersion := 17
8+
-> javafmtCheck

0 commit comments

Comments
 (0)