diff --git a/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt b/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt index 239f1ffa..629a49bf 100644 --- a/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt +++ b/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt @@ -81,6 +81,12 @@ sealed class PatchOption( val validator: (T?) -> Boolean ) { var value: T? = default + get() { + if (field == null && required) { + throw RequirementNotMetException + } + return field + } set(value) { if (value == null && required) { throw RequirementNotMetException @@ -95,7 +101,11 @@ sealed class PatchOption( * Gets the value of the option. * Please note that using the wrong value type results in a runtime error. */ - operator fun getValue(thisRef: Any?, property: KProperty<*>) = value as T + inline operator fun getValue(thisRef: Any?, property: KProperty<*>) = + value as? V ?: throw InvalidTypeException( + V::class.java.canonicalName, + value?.let { it::class.java.canonicalName } ?: "null" + ) /** * Gets the value of the option. diff --git a/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt b/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt index 796d31bd..04f4e659 100644 --- a/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt +++ b/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt @@ -85,9 +85,16 @@ internal class PatchOptionsTest { } @Test - fun `should fail because of the requirement is not met`() { + fun `should fail because the requirement is not met`() { assertThrows { options.nullify("key1") } } + + @Test + fun `should fail because getting a non-initialized option is illegal`() { + assertThrows { + println(options["key6"].value) + } + } } \ No newline at end of file diff --git a/src/test/kotlin/app/revanced/patcher/usage/bytecode/ExampleBytecodePatch.kt b/src/test/kotlin/app/revanced/patcher/usage/bytecode/ExampleBytecodePatch.kt index 384ecb65..454283a9 100644 --- a/src/test/kotlin/app/revanced/patcher/usage/bytecode/ExampleBytecodePatch.kt +++ b/src/test/kotlin/app/revanced/patcher/usage/bytecode/ExampleBytecodePatch.kt @@ -196,5 +196,10 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) { "key5", File("test.txt").toPath(), "title", "description" ) ) + private var key6: String by option( + PatchOption.StringOption( + "key6", null, "title", "description", true + ) + ) } }