File tree Expand file tree Collapse file tree 3 files changed +30
-12
lines changed
openapi-processor-core/src
main/kotlin/io/openapiprocessor/core/writer/java
test/kotlin/io/openapiprocessor/core/writer/java Expand file tree Collapse file tree 3 files changed +30
-12
lines changed Original file line number Diff line number Diff line change 55
66package io.openapiprocessor.core.writer.java
77
8- class FormattingException (
9- private val source : String ,
10- cause : Throwable
11- ): java.lang.RuntimeException(cause) {
12-
13- override val message: String
14- get() = " failed to format the generated source: \n >>\n $source \n <<"
15-
16- }
8+ class FormattingException (override val message : String , cause : Throwable ): java.lang.RuntimeException(cause)
Original file line number Diff line number Diff line change @@ -9,8 +9,10 @@ import com.google.googlejavaformat.java.Formatter
99import com.google.googlejavaformat.java.JavaFormatterOptions
1010import io.openapiprocessor.core.writer.SourceFormatter
1111
12- class GoogleFormatter : SourceFormatter {
13- private lateinit var formatter: Formatter
12+ private const val addExportsLink: String = " https://openapiprocessor.io/oap/home/jdk.html"
13+
14+ open class GoogleFormatter : SourceFormatter {
15+ protected lateinit var formatter: Formatter
1416
1517 init {
1618 initFormatter()
@@ -19,8 +21,13 @@ class GoogleFormatter: SourceFormatter {
1921 override fun format (raw : String ): String {
2022 try {
2123 return correctLineFeed(formatter.formatSource(raw))
24+
25+ } catch (e: IllegalAccessError ) {
26+ // since java 16 the jdk.compiler module does not export com.sun.tools.javac.parser
27+ throw FormattingException (" looks like you may need $addExportsLink to make formatting work." , e)
28+
2229 } catch (e: Exception ) {
23- throw FormattingException (raw, e)
30+ throw FormattingException (" failed to format the generated source: \n >> \n $ raw\n << " , e)
2431 }
2532 }
2633
Original file line number Diff line number Diff line change 55
66package io.openapiprocessor.core.writer.java
77
8+ import com.google.googlejavaformat.java.Formatter
9+ import io.kotest.assertions.throwables.shouldThrow
810import io.kotest.core.spec.style.StringSpec
911import io.kotest.matchers.shouldBe
12+ import io.mockk.every
13+ import io.mockk.mockk
1014
1115class GoogleFormatterSpec : StringSpec ({
1216
@@ -19,4 +23,19 @@ class GoogleFormatterSpec : StringSpec({
1923 |
2024 """ .trimMargin()
2125 }
26+
27+ " formatter catches IllegalAccessError and re-throws with link to add-export note" {
28+ val f : Formatter = mockk<Formatter >()
29+ every { f.formatSource(any()) } throws IllegalAccessError ("fake illegal access error")
30+
31+ val formatter = object : GoogleFormatter () {
32+ init {
33+ formatter = f
34+ }
35+ }
36+
37+ shouldThrow<FormattingException > {
38+ formatter.format("....")
39+ }
40+ }
2241})
You can’t perform that action at this time.
0 commit comments