ensure matched before accessing indices

This commit is contained in:
oSumAtrIX
2026-01-28 13:35:36 +01:00
parent d7d676d5e4
commit b45faaf459

View File

@@ -131,7 +131,8 @@ fun Iterable<Method>.firstMutableMethodOrNull(methodReference: MethodReference)
@JvmName("firstMutableMethodInMethods")
context(_: BytecodePatchContext)
fun Iterable<Method>.firstMutableMethod(methodReference: MethodReference) = requireNotNull(firstMutableMethodOrNull(methodReference))
fun Iterable<Method>.firstMutableMethod(methodReference: MethodReference) =
requireNotNull(firstMutableMethodOrNull(methodReference))
@JvmName("firstMethodOrNullInMethods")
fun Iterable<Method>.firstMethodOrNull(
@@ -209,7 +210,8 @@ fun Iterable<ClassDef>.firstMethodOrNull(methodReference: MethodReference) =
asSequence().flatMap { it.methods.asSequence() }.asIterable().firstMethodOrNull(methodReference)
@JvmName("firstMethodInClassDefs")
fun Iterable<ClassDef>.firstMethod(methodReference: MethodReference) = requireNotNull(firstMethodOrNull(methodReference))
fun Iterable<ClassDef>.firstMethod(methodReference: MethodReference) =
requireNotNull(firstMethodOrNull(methodReference))
@JvmName("firstMutableMethodOrNullInClassDefs")
context(_: BytecodePatchContext)
@@ -218,7 +220,8 @@ fun Iterable<ClassDef>.firstMutableMethodOrNull(methodReference: MethodReference
@JvmName("firstMutableMethodInClassDefs")
context(_: BytecodePatchContext)
fun Iterable<ClassDef>.firstMutableMethod(methodReference: MethodReference) = requireNotNull(firstMutableMethodOrNull(methodReference))
fun Iterable<ClassDef>.firstMutableMethod(methodReference: MethodReference) =
requireNotNull(firstMutableMethodOrNull(methodReference))
@JvmName("firstMethodOrNullInClassDefs")
fun Iterable<ClassDef>.firstMethodOrNull(predicate: MethodPredicate = { true }) =
@@ -293,11 +296,13 @@ fun ClassDef.firstMethod(methodReference: MethodReference) = requireNotNull(firs
@JvmName("firstMutableMethodOrNullInClassDef")
context(_: BytecodePatchContext)
fun ClassDef.firstMutableMethodOrNull(methodReference: MethodReference) = methods.firstMutableMethodOrNull(methodReference)
fun ClassDef.firstMutableMethodOrNull(methodReference: MethodReference) =
methods.firstMutableMethodOrNull(methodReference)
@JvmName("firstMutableMethodInClassDef")
context(_: BytecodePatchContext)
fun ClassDef.firstMutableMethod(methodReference: MethodReference) = requireNotNull(firstMutableMethodOrNull(methodReference))
fun ClassDef.firstMutableMethod(methodReference: MethodReference) =
requireNotNull(firstMutableMethodOrNull(methodReference))
@JvmName("firstMethodOrNullInClassDef")
fun ClassDef.firstMethodOrNull(
@@ -476,7 +481,8 @@ fun gettingFirstMethodOrNull(method: MethodReference) = cachedReadOnlyProperty {
fun gettingFirstMethod(method: MethodReference) = cachedReadOnlyProperty { firstMethod(method) }
fun gettingFirstMutableMethodOrNull(method: MethodReference) = cachedReadOnlyProperty { firstMutableMethodOrNull(method) }
fun gettingFirstMutableMethodOrNull(method: MethodReference) =
cachedReadOnlyProperty { firstMutableMethodOrNull(method) }
fun gettingFirstMutableMethod(method: MethodReference) = cachedReadOnlyProperty { firstMutableMethod(method) }
@@ -953,9 +959,9 @@ fun MutablePredicateList<Method>.definingClass(
fun MutablePredicateList<Method>.parameterTypes(vararg parameterTypePrefixes: String) =
predicate {
parameterTypes.size == parameterTypePrefixes.size &&
parameterTypes
.zip(parameterTypePrefixes)
.all { (a, b) -> a.startsWith(b) }
parameterTypes
.zip(parameterTypePrefixes)
.all { (a, b) -> a.startsWith(b) }
}
fun MutablePredicateList<Method>.strings(build: Function<IndexedMatcher<Instruction>>) {
@@ -1033,7 +1039,8 @@ fun MutablePredicateList<Method>.opcodes(vararg opcodes: Opcode) = instructions
inline fun <reified T : Instruction> `is`(crossinline predicate: Predicate<T> = { true }): IndexedMatcherPredicate<Instruction> =
{ _, _, _ -> (this as? T)?.predicate() == true }
fun instruction(predicate: Predicate<Instruction> = { true }): IndexedMatcherPredicate<Instruction> = { _, _, _ -> predicate() }
fun instruction(predicate: Predicate<Instruction> = { true }): IndexedMatcherPredicate<Instruction> =
{ _, _, _ -> predicate() }
fun registers(predicate: Predicate<IntArray> = { true }): IndexedMatcherPredicate<Instruction> =
{ _, _, _ ->
@@ -1077,7 +1084,8 @@ fun registers(
},
) = registers({ compare(registers) })
fun literal(predicate: Predicate<Long> = { true }): IndexedMatcherPredicate<Instruction> = { _, _, _ -> wideLiteral?.predicate() == true }
fun literal(predicate: Predicate<Long> = { true }): IndexedMatcherPredicate<Instruction> =
{ _, _, _ -> wideLiteral?.predicate() == true }
fun literal(
literal: Long,
@@ -1104,7 +1112,8 @@ fun field(
compare: String.(String) -> Boolean = String::equals,
) = field { this.name.compare(name) }
fun type(predicate: Predicate<String> = { true }): IndexedMatcherPredicate<Instruction> = { _, _, _ -> type?.predicate() == true }
fun type(predicate: Predicate<String> = { true }): IndexedMatcherPredicate<Instruction> =
{ _, _, _ -> type?.predicate() == true }
fun type(
type: String,
@@ -1149,13 +1158,13 @@ operator fun String.invoke(compare: String.(String) -> Boolean = String::equals)
operator fun Opcode.invoke(): IndexedMatcherPredicate<Instruction> = { _, _, _ -> opcode == this@invoke }
typealias BuildCompositeDeclarativePredicate<Method> =
context(
context(
BytecodePatchContext,
PredicateContext,
IndexedMatcher<Instruction>,
MutableList<String>
)
MutablePredicateList<Method>.() -> Unit
)
MutablePredicateList<Method>.() -> Unit
fun firstMethodComposite(
vararg strings: String,
@@ -1164,7 +1173,7 @@ fun firstMethodComposite(
class MatchBuilder private constructor(
private val strings: MutableList<String>,
indexedMatcher: IndexedMatcher<Instruction> = indexedMatcher(),
private val indexedMatcher: IndexedMatcher<Instruction> = indexedMatcher(),
build: BuildCompositeDeclarativePredicate<Method> = { },
) {
internal constructor(
@@ -1176,7 +1185,12 @@ class MatchBuilder private constructor(
context(strings, indexedMatcher) { build() }
}
val indices = indexedMatcher.indices
context(_: BytecodePatchContext)
val indices: List<Int>
get() {
immutableMethod // Ensure matched.
return indexedMatcher.indices
}
private val BytecodePatchContext.cachedImmutableMethodOrNull by gettingFirstMethodDeclarativelyOrNull(
strings = strings.toTypedArray(),
@@ -1231,9 +1245,9 @@ class MatchBuilder private constructor(
}
class Match(
val context: BytecodePatchContext,
private val context: BytecodePatchContext,
val immutableMethodOrNull: Method?,
val indices: List<Int>,
private val _indices: List<Int>,
) {
val immutableMethod by lazy { requireNotNull(immutableMethodOrNull) }
@@ -1252,6 +1266,13 @@ class Match(
}
val classDef by lazy { requireNotNull(classDefOrNull) }
context(_: BytecodePatchContext)
val indices: List<Int>
get() {
immutableMethod // Ensure matched.
return _indices
}
}
context(context: BytecodePatchContext)