Skip to content

Commit fe95aad

Browse files
committed
provide help for encapsulation error
1 parent b2f527f commit fe95aad

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/FormattingException.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,4 @@
55

66
package 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)

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/GoogleFormatter.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import com.google.googlejavaformat.java.Formatter
99
import com.google.googlejavaformat.java.JavaFormatterOptions
1010
import 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

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/writer/java/GoogleFormatterSpec.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
package io.openapiprocessor.core.writer.java
77

8+
import com.google.googlejavaformat.java.Formatter
9+
import io.kotest.assertions.throwables.shouldThrow
810
import io.kotest.core.spec.style.StringSpec
911
import io.kotest.matchers.shouldBe
12+
import io.mockk.every
13+
import io.mockk.mockk
1014

1115
class 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
})

0 commit comments

Comments
 (0)