mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-05 18:25:28 +08:00
Modify change find Field, Method, Constructor function exception to createException function and fix declared member exception interrupt the finding process bug in ReflectionTool
This commit is contained in:
@@ -35,6 +35,9 @@ import com.highcapable.yukihookapi.hook.core.finder.members.data.MethodRulesData
|
|||||||
import com.highcapable.yukihookapi.hook.factory.hasExtends
|
import com.highcapable.yukihookapi.hook.factory.hasExtends
|
||||||
import com.highcapable.yukihookapi.hook.store.ReflectsCacheStore
|
import com.highcapable.yukihookapi.hook.store.ReflectsCacheStore
|
||||||
import com.highcapable.yukihookapi.hook.type.defined.UndefinedType
|
import com.highcapable.yukihookapi.hook.type.defined.UndefinedType
|
||||||
|
import com.highcapable.yukihookapi.hook.type.java.NoClassDefFoundErrorClass
|
||||||
|
import com.highcapable.yukihookapi.hook.type.java.NoSuchFieldErrorClass
|
||||||
|
import com.highcapable.yukihookapi.hook.type.java.NoSuchMethodErrorClass
|
||||||
import com.highcapable.yukihookapi.hook.utils.conditions
|
import com.highcapable.yukihookapi.hook.utils.conditions
|
||||||
import com.highcapable.yukihookapi.hook.utils.let
|
import com.highcapable.yukihookapi.hook.utils.let
|
||||||
import com.highcapable.yukihookapi.hook.utils.takeIf
|
import com.highcapable.yukihookapi.hook.utils.takeIf
|
||||||
@@ -79,10 +82,7 @@ internal object ReflectionTool {
|
|||||||
loader == null -> Class.forName(name)
|
loader == null -> Class.forName(name)
|
||||||
else -> loader.loadClass(name)
|
else -> loader.loadClass(name)
|
||||||
}.also { ReflectsCacheStore.putClass(hashCode, it) }
|
}.also { ReflectsCacheStore.putClass(hashCode, it) }
|
||||||
}.getOrNull() ?: throw NoClassDefFoundError(
|
}.getOrNull() ?: throw createException(loader ?: AppParasitics.baseClassLoader, name = "Class", "name:[$name]")
|
||||||
"Can't find this Class --> " +
|
|
||||||
"name:[$name] in [${loader ?: AppParasitics.baseClassLoader}] by $TAG"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,7 +97,7 @@ internal object ReflectionTool {
|
|||||||
if (type == UndefinedType) error("Field match type class is not found")
|
if (type == UndefinedType) error("Field match type class is not found")
|
||||||
if (classSet == null) return@createResult hashSetOf()
|
if (classSet == null) return@createResult hashSetOf()
|
||||||
ReflectsCacheStore.findFields(hashCode(classSet)) ?: hashSetOf<Field>().also { fields ->
|
ReflectsCacheStore.findFields(hashCode(classSet)) ?: hashSetOf<Field>().also { fields ->
|
||||||
classSet.declaredFields.also { declares ->
|
classSet.existFields?.also { declares ->
|
||||||
var iType = -1
|
var iType = -1
|
||||||
var iName = -1
|
var iName = -1
|
||||||
var iModify = -1
|
var iModify = -1
|
||||||
@@ -153,7 +153,7 @@ internal object ReflectionTool {
|
|||||||
paramTypes?.takeIf { it.isNotEmpty() }
|
paramTypes?.takeIf { it.isNotEmpty() }
|
||||||
?.forEachIndexed { p, it -> if (it == UndefinedType) error("Method match paramType[$p] class is not found") }
|
?.forEachIndexed { p, it -> if (it == UndefinedType) error("Method match paramType[$p] class is not found") }
|
||||||
ReflectsCacheStore.findMethods(hashCode(classSet)) ?: hashSetOf<Method>().also { methods ->
|
ReflectsCacheStore.findMethods(hashCode(classSet)) ?: hashSetOf<Method>().also { methods ->
|
||||||
classSet.declaredMethods.also { declares ->
|
classSet.existMethods?.also { declares ->
|
||||||
var iReturnType = -1
|
var iReturnType = -1
|
||||||
var iParamTypes = -1
|
var iParamTypes = -1
|
||||||
var iParamCount = -1
|
var iParamCount = -1
|
||||||
@@ -234,7 +234,7 @@ internal object ReflectionTool {
|
|||||||
paramTypes?.takeIf { it.isNotEmpty() }
|
paramTypes?.takeIf { it.isNotEmpty() }
|
||||||
?.forEachIndexed { p, it -> if (it == UndefinedType) error("Constructor match paramType[$p] class is not found") }
|
?.forEachIndexed { p, it -> if (it == UndefinedType) error("Constructor match paramType[$p] class is not found") }
|
||||||
ReflectsCacheStore.findConstructors(hashCode(classSet)) ?: hashSetOf<Constructor<*>>().also { constructors ->
|
ReflectsCacheStore.findConstructors(hashCode(classSet)) ?: hashSetOf<Constructor<*>>().also { constructors ->
|
||||||
classSet.declaredConstructors.also { declares ->
|
classSet.existConstructors?.also { declares ->
|
||||||
var iParamTypes = -1
|
var iParamTypes = -1
|
||||||
var iParamCount = -1
|
var iParamCount = -1
|
||||||
var iParamCountRange = -1
|
var iParamCountRange = -1
|
||||||
@@ -350,72 +350,93 @@ internal object ReflectionTool {
|
|||||||
* @throws IllegalStateException 如果 [BaseRulesData] 的类型错误
|
* @throws IllegalStateException 如果 [BaseRulesData] 的类型错误
|
||||||
*/
|
*/
|
||||||
private fun BaseRulesData.throwNotFoundError(instanceSet: Any?): Nothing = when (this) {
|
private fun BaseRulesData.throwNotFoundError(instanceSet: Any?): Nothing = when (this) {
|
||||||
is FieldRulesData -> throw NoSuchFieldError(
|
is FieldRulesData -> throw createException(
|
||||||
"Can't find this Field --> " +
|
instanceSet, name = "Field",
|
||||||
when {
|
"name:[${name.takeIf { it.isNotBlank() } ?: "unspecified"}]",
|
||||||
orderIndex == null -> ""
|
nameConditions?.let { "nameConditions:$it" } ?: "",
|
||||||
orderIndex!!.second.not() -> "orderIndex:[last] "
|
"type:[${type ?: "unspecified"}] ",
|
||||||
else -> "orderIndex:[${orderIndex!!.first}] "
|
"modifiers:${modifiers ?: "[]"} ",
|
||||||
} +
|
"name:[${name.takeIf { it.isNotBlank() } ?: "unspecified"}]",
|
||||||
when {
|
orderIndex?.let { it.takeIf { it.second }?.let { e -> "orderIndex:[${e.first}]" } ?: "orderIndex:[last]" } ?: "",
|
||||||
matchIndex == null -> ""
|
matchIndex?.let { it.takeIf { it.second }?.let { e -> "matchIndex:[${e.first}]" } ?: "matchIndex:[last]" } ?: ""
|
||||||
matchIndex!!.second.not() -> "matchIndex:[last] "
|
|
||||||
else -> "matchIndex:[${matchIndex!!.first}] "
|
|
||||||
} +
|
|
||||||
when (nameConditions) {
|
|
||||||
null -> ""
|
|
||||||
else -> "nameConditions:${nameConditions} "
|
|
||||||
} +
|
|
||||||
"name:[${name.takeIf { it.isNotBlank() } ?: "unspecified"}] " +
|
|
||||||
"type:[${type ?: "unspecified"}] " +
|
|
||||||
"modifiers:${modifiers ?: "[]"} " +
|
|
||||||
"in [$instanceSet] by $TAG"
|
|
||||||
)
|
)
|
||||||
is MethodRulesData -> throw NoSuchMethodError(
|
is MethodRulesData -> throw createException(
|
||||||
"Can't find this Method --> " +
|
instanceSet, name = "Method",
|
||||||
when {
|
"name:[${name.takeIf { it.isNotBlank() } ?: "unspecified"}]",
|
||||||
orderIndex == null -> ""
|
nameConditions?.let { "nameConditions:$it" } ?: "",
|
||||||
orderIndex!!.second.not() -> "orderIndex:[last] "
|
"paramCount:[${paramCount.takeIf { it >= 0 } ?: "unspecified"}]",
|
||||||
else -> "orderIndex:[${orderIndex!!.first}] "
|
"paramCountRange:[${paramCountRange.takeIf { it.isEmpty().not() } ?: "unspecified"}]",
|
||||||
} +
|
"paramTypes:[${paramTypes.typeOfString()}]",
|
||||||
when {
|
"returnType:[${returnType ?: "unspecified"}]",
|
||||||
matchIndex == null -> ""
|
"modifiers:${modifiers ?: "[]"}",
|
||||||
matchIndex!!.second.not() -> "matchIndex:[last] "
|
orderIndex?.let { it.takeIf { it.second }?.let { e -> "orderIndex:[${e.first}]" } ?: "orderIndex:[last]" } ?: "",
|
||||||
else -> "matchIndex:[${matchIndex!!.first}] "
|
matchIndex?.let { it.takeIf { it.second }?.let { e -> "matchIndex:[${e.first}]" } ?: "matchIndex:[last]" } ?: ""
|
||||||
} +
|
|
||||||
when (nameConditions) {
|
|
||||||
null -> ""
|
|
||||||
else -> "nameConditions:${nameConditions} "
|
|
||||||
} +
|
|
||||||
"name:[${name.takeIf { it.isNotBlank() } ?: "unspecified"}] " +
|
|
||||||
"paramCount:[${paramCount.takeIf { it >= 0 } ?: "unspecified"}] " +
|
|
||||||
"paramCountRange:[${paramCountRange.takeIf { it.isEmpty().not() } ?: "unspecified"}] " +
|
|
||||||
"paramTypes:[${paramTypes.typeOfString()}] " +
|
|
||||||
"returnType:[${returnType ?: "unspecified"}] " +
|
|
||||||
"modifiers:${modifiers ?: "[]"} " +
|
|
||||||
"in [$instanceSet] by $TAG"
|
|
||||||
)
|
)
|
||||||
is ConstructorRulesData -> throw NoSuchMethodError(
|
is ConstructorRulesData -> throw createException(
|
||||||
"Can't find this Constructor --> " +
|
instanceSet, name = "Constructor",
|
||||||
when {
|
"paramCount:[${paramCount.takeIf { it >= 0 } ?: "unspecified"}]",
|
||||||
orderIndex == null -> ""
|
"paramCountRange:[${paramCountRange.takeIf { it.isEmpty().not() } ?: "unspecified"}]",
|
||||||
orderIndex!!.second.not() -> "orderIndex:[last] "
|
"paramTypes:[${paramTypes.typeOfString()}]",
|
||||||
else -> "orderIndex:[${orderIndex!!.first}] "
|
"modifiers:${modifiers ?: "[]"}",
|
||||||
} +
|
orderIndex?.let { it.takeIf { it.second }?.let { e -> "orderIndex:[${e.first}]" } ?: "orderIndex:[last]" } ?: "",
|
||||||
when {
|
matchIndex?.let { it.takeIf { it.second }?.let { e -> "matchIndex:[${e.first}]" } ?: "matchIndex:[last]" } ?: ""
|
||||||
matchIndex == null -> ""
|
|
||||||
matchIndex!!.second.not() -> "matchIndex:[last] "
|
|
||||||
else -> "matchIndex:[${matchIndex!!.first}] "
|
|
||||||
} +
|
|
||||||
"paramCount:[${paramCount.takeIf { it >= 0 } ?: "unspecified"}] " +
|
|
||||||
"paramCountRange:[${paramCountRange.takeIf { it.isEmpty().not() } ?: "unspecified"}] " +
|
|
||||||
"paramTypes:[${paramTypes.typeOfString()}] " +
|
|
||||||
"modifiers:${modifiers ?: "[]"} " +
|
|
||||||
"in [$instanceSet] by $TAG"
|
|
||||||
)
|
)
|
||||||
else -> error("Type [$this] not allowed")
|
else -> error("Type [$this] not allowed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个异常
|
||||||
|
* @param instanceSet 所在 [ClassLoader] or [Class]
|
||||||
|
* @param name 实例名称
|
||||||
|
* @param content 异常内容
|
||||||
|
* @return [Throwable]
|
||||||
|
*/
|
||||||
|
private fun createException(instanceSet: Any?, name: String, vararg content: String): Throwable {
|
||||||
|
/**
|
||||||
|
* 获取 [Class.getName] 长度的空格数量并使用 "->" 拼接
|
||||||
|
* @return [String]
|
||||||
|
*/
|
||||||
|
fun Class<*>.space(): String {
|
||||||
|
var space = ""
|
||||||
|
for (i in 0..this.name.length) space += " "
|
||||||
|
return "$space -> "
|
||||||
|
}
|
||||||
|
if (content.isEmpty()) return IllegalStateException("Exception content is null")
|
||||||
|
val space = when (name) {
|
||||||
|
"Class" -> NoClassDefFoundErrorClass.space()
|
||||||
|
"Field" -> NoSuchFieldErrorClass.space()
|
||||||
|
"Method", "Constructor" -> NoSuchMethodErrorClass.space()
|
||||||
|
else -> error("Invalid Exception type")
|
||||||
|
}
|
||||||
|
var splicing = ""
|
||||||
|
content.forEach { if (it.isNotBlank()) splicing += "$space$it\n" }
|
||||||
|
val template = "Can't find this $name in [$instanceSet]:\n${splicing}Generated by $TAG"
|
||||||
|
return when (name) {
|
||||||
|
"Class" -> NoClassDefFoundError(template)
|
||||||
|
"Field" -> NoSuchFieldError(template)
|
||||||
|
"Method", "Constructor" -> NoSuchMethodError(template)
|
||||||
|
else -> error("Invalid Exception type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前 [Class] 中存在的 [Field] 数组
|
||||||
|
* @return [Array]<[Field]>
|
||||||
|
*/
|
||||||
|
private val Class<*>.existFields get() = runCatching { declaredFields }.getOrNull()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前 [Class] 中存在的 [Method] 数组
|
||||||
|
* @return [Array]<[Method]>
|
||||||
|
*/
|
||||||
|
private val Class<*>.existMethods get() = runCatching { declaredMethods }.getOrNull()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前 [Class] 中存在的 [Constructor] 数组
|
||||||
|
* @return [Array]<[Constructor]>
|
||||||
|
*/
|
||||||
|
private val Class<*>.existConstructors get() = runCatching { declaredConstructors }.getOrNull()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取参数数组文本化内容
|
* 获取参数数组文本化内容
|
||||||
* @return [String]
|
* @return [String]
|
||||||
|
Reference in New Issue
Block a user