Document that read/write methods may throw IAE and make sure this is true:

- Replace !! with requireNotNull()
- Add try/catch to wrap all other exceptions (mainly protobuf parser ones)
- Add tests for malformed input for both read and write
- Hide InconsistentMetadataException from public API
- Replace its external usages with IllegalArgumentException
This commit is contained in:
Leonid Startsev
2023-01-24 13:38:58 +01:00
committed by Space Team
parent 64838d5ec9
commit 4b524b8589
10 changed files with 168 additions and 67 deletions
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.kotlinp
import kotlinx.metadata.InconsistentKotlinMetadataException
import kotlinx.metadata.jvm.KotlinClassMetadata
import kotlinx.metadata.jvm.KotlinModuleMetadata
import kotlinx.metadata.jvm.UnstableMetadataApi
@@ -30,7 +29,7 @@ class Kotlinp(private val settings: KotlinpSettings) {
val header = file.readKotlinClassHeader() ?: throw KotlinpException("file is not a Kotlin class file: $file")
return try {
KotlinClassMetadata.read(header)
} catch (e: InconsistentKotlinMetadataException) {
} catch (e: IllegalArgumentException) {
throw KotlinpException("inconsistent Kotlin metadata: ${e.message}")
}
}
@@ -171,7 +171,7 @@ private fun printType(type: KmType): String {
} else {
val (variance, argumentType) = argument
if (variance == null || argumentType == null)
throw InconsistentKotlinMetadataException("Variance and type must be set for non-star type projection")
throw IllegalArgumentException("Variance and type must be set for non-star type projection")
val argumentTypeString = printType(argumentType)
buildString {
if (variance != KmVariance.INVARIANT) {