mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-04 17:55:24 +08:00
Modify move "throw createException" content to function templates in FieldRulesData, MethodRulesData, ConstructorRulesData, MemberRulesData, BaseRulesData and make modifiers condition visible
This commit is contained in:
@@ -31,6 +31,7 @@ import com.highcapable.yukihookapi.hook.core.finder.base.rules.CountRules
|
||||
import com.highcapable.yukihookapi.hook.core.finder.base.rules.ModifierRules
|
||||
import com.highcapable.yukihookapi.hook.core.finder.base.rules.NameRules
|
||||
import com.highcapable.yukihookapi.hook.core.finder.type.factory.ModifierConditions
|
||||
import com.highcapable.yukihookapi.hook.type.defined.VagueType
|
||||
import java.lang.reflect.Member
|
||||
|
||||
/**
|
||||
@@ -46,6 +47,13 @@ internal abstract class BaseRulesData internal constructor(
|
||||
var matchIndex: Pair<Int, Boolean>? = null
|
||||
) {
|
||||
|
||||
/** 当前类唯一标识值 */
|
||||
internal var uniqueValue = 0L
|
||||
|
||||
init {
|
||||
uniqueValue = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
/**
|
||||
* [String] 转换为 [NameRules]
|
||||
* @return [NameRules]
|
||||
@@ -62,13 +70,34 @@ internal abstract class BaseRulesData internal constructor(
|
||||
* [Class] 转换为 [ModifierRules]
|
||||
* @return [ModifierRules]
|
||||
*/
|
||||
internal fun Class<*>.cast() = ModifierRules.with(this)
|
||||
internal fun Class<*>.cast() = ModifierRules.with(instance = this, uniqueValue)
|
||||
|
||||
/**
|
||||
* [Member] 转换为 [ModifierRules]
|
||||
* @return [ModifierRules]
|
||||
*/
|
||||
internal fun Member.cast() = ModifierRules.with(this)
|
||||
internal fun Member.cast() = ModifierRules.with(instance = this, uniqueValue)
|
||||
|
||||
/**
|
||||
* 获取参数数组文本化内容
|
||||
* @return [String]
|
||||
*/
|
||||
internal fun Array<out Class<*>>?.typeOfString() =
|
||||
StringBuilder("(").also { sb ->
|
||||
var isFirst = true
|
||||
if (this == null || isEmpty()) return "()"
|
||||
forEach {
|
||||
if (isFirst) isFirst = false else sb.append(", ")
|
||||
sb.append(it.takeIf { it.canonicalName != VagueType.canonicalName }?.canonicalName ?: "*vague*")
|
||||
}
|
||||
sb.append(")")
|
||||
}.toString()
|
||||
|
||||
/**
|
||||
* 获取规则对象模板字符串数组
|
||||
* @return [Array]<[String]>
|
||||
*/
|
||||
internal abstract val templates: Array<String>
|
||||
|
||||
/**
|
||||
* 获取规则对象名称
|
||||
|
@@ -45,15 +45,29 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
@PublishedApi
|
||||
internal companion object {
|
||||
|
||||
/** 当前实例数组 */
|
||||
private val instances = HashMap<Long, ModifierRules>()
|
||||
|
||||
/**
|
||||
* 获取模板字符串数组
|
||||
* @param value 唯一标识值
|
||||
* @return [ArrayList]<[String]>
|
||||
*/
|
||||
internal fun templates(value: Long) = instances[value]?.templates ?: arrayListOf()
|
||||
|
||||
/**
|
||||
* 创建实例
|
||||
* @param instance 实例对象
|
||||
* @param value 唯一标识值 - 默认 0
|
||||
* @return [ModifierRules]
|
||||
*/
|
||||
@PublishedApi
|
||||
internal fun with(instance: Any) = ModifierRules(instance)
|
||||
internal fun with(instance: Any, value: Long = 0) = ModifierRules(instance).apply { instances[value] = this }
|
||||
}
|
||||
|
||||
/** 当前模板字符串数组 */
|
||||
private val templates = ArrayList<String>()
|
||||
|
||||
/**
|
||||
* [Class]、[Member] 类型是否包含 public
|
||||
*
|
||||
@@ -64,7 +78,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isPublic get() = Modifier.isPublic(modifiers)
|
||||
val isPublic get() = Modifier.isPublic(modifiers).also { templates.add("<isPublic> ($it)") }
|
||||
|
||||
/**
|
||||
* [Class]、[Member] 类型是否包含 private
|
||||
@@ -76,7 +90,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isPrivate get() = Modifier.isPrivate(modifiers)
|
||||
val isPrivate get() = Modifier.isPrivate(modifiers).also { templates.add("<isPrivate> ($it)") }
|
||||
|
||||
/**
|
||||
* [Class]、[Member] 类型是否包含 protected
|
||||
@@ -88,7 +102,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isProtected get() = Modifier.isProtected(modifiers)
|
||||
val isProtected get() = Modifier.isProtected(modifiers).also { templates.add("<isProtected> ($it)") }
|
||||
|
||||
/**
|
||||
* [Class]、[Member] 类型是否包含 static
|
||||
@@ -104,7 +118,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* - ❗注意 Kotlin → Jvm 后的 object 类中的方法并不是静态的
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isStatic get() = Modifier.isStatic(modifiers)
|
||||
val isStatic get() = Modifier.isStatic(modifiers).also { templates.add("<isStatic> ($it)") }
|
||||
|
||||
/**
|
||||
* [Class]、[Member] 类型是否包含 final
|
||||
@@ -118,7 +132,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* - ❗注意 Kotlin → Jvm 后没有 open 标识的 [Class]、[Member] 和没有任何关联的 [Class]、[Member] 都将为 final
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isFinal get() = Modifier.isFinal(modifiers)
|
||||
val isFinal get() = Modifier.isFinal(modifiers).also { templates.add("<isFinal> ($it)") }
|
||||
|
||||
/**
|
||||
* [Class]、[Member] 类型是否包含 synchronized
|
||||
@@ -130,7 +144,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isSynchronized get() = Modifier.isSynchronized(modifiers)
|
||||
val isSynchronized get() = Modifier.isSynchronized(modifiers).also { templates.add("<isSynchronized> ($it)") }
|
||||
|
||||
/**
|
||||
* [Field] 类型是否包含 volatile
|
||||
@@ -142,7 +156,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isVolatile get() = Modifier.isVolatile(modifiers)
|
||||
val isVolatile get() = Modifier.isVolatile(modifiers).also { templates.add("<isVolatile> ($it)") }
|
||||
|
||||
/**
|
||||
* [Field] 类型是否包含 transient
|
||||
@@ -154,7 +168,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isTransient get() = Modifier.isTransient(modifiers)
|
||||
val isTransient get() = Modifier.isTransient(modifiers).also { templates.add("<isTransient> ($it)") }
|
||||
|
||||
/**
|
||||
* [Method] 类型是否包含 native
|
||||
@@ -168,7 +182,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isNative get() = Modifier.isNative(modifiers)
|
||||
val isNative get() = Modifier.isNative(modifiers).also { templates.add("<isNative> ($it)") }
|
||||
|
||||
/**
|
||||
* [Class] 类型是否包含 interface
|
||||
@@ -180,7 +194,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isInterface get() = Modifier.isInterface(modifiers)
|
||||
val isInterface get() = Modifier.isInterface(modifiers).also { templates.add("<isInterface> ($it)") }
|
||||
|
||||
/**
|
||||
* [Class]、[Member] 类型是否包含 abstract
|
||||
@@ -194,7 +208,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isAbstract get() = Modifier.isAbstract(modifiers)
|
||||
val isAbstract get() = Modifier.isAbstract(modifiers).also { templates.add("<isAbstract> ($it)") }
|
||||
|
||||
/**
|
||||
* [Class]、[Member] 类型是否包含 strictfp
|
||||
@@ -206,7 +220,7 @@ class ModifierRules private constructor(private val instance: Any) {
|
||||
* ^^^
|
||||
* @return [Boolean]
|
||||
*/
|
||||
val isStrict get() = Modifier.isStrict(modifiers)
|
||||
val isStrict get() = Modifier.isStrict(modifiers).also { templates.add("<isStrict> ($it)") }
|
||||
|
||||
/**
|
||||
* 获取当前对象的类型描述符
|
||||
|
@@ -45,6 +45,14 @@ internal class ConstructorRulesData internal constructor(
|
||||
var paramCountConditions: CountConditions? = null
|
||||
) : MemberRulesData() {
|
||||
|
||||
override val templates
|
||||
get() = arrayOf(
|
||||
paramCount.takeIf { it >= 0 }?.let { "paramCount:[$it]" } ?: "",
|
||||
paramCountRange.takeIf { it.isEmpty().not() }?.let { "paramCountRange:[$it]" } ?: "",
|
||||
paramCountConditions?.let { "paramCountConditions:[existed]" } ?: "",
|
||||
paramTypes?.typeOfString()?.let { "paramTypes:[$it]" } ?: "", *super.templates
|
||||
)
|
||||
|
||||
override val objectName get() = "Constructor"
|
||||
|
||||
override val isInitialize
|
||||
|
@@ -43,6 +43,13 @@ internal class FieldRulesData internal constructor(
|
||||
var type: Any? = null
|
||||
) : MemberRulesData() {
|
||||
|
||||
override val templates
|
||||
get() = arrayOf(
|
||||
name.takeIf { it.isNotBlank() }?.let { "name:[$it]" } ?: "",
|
||||
nameConditions?.let { "nameConditions:$it" } ?: "",
|
||||
type?.let { "type:[$it]" } ?: "", *super.templates
|
||||
)
|
||||
|
||||
override val objectName get() = "Field"
|
||||
|
||||
override val isInitialize get() = super.isInitializeOfSuper || name.isNotBlank() || nameConditions != null || type != null
|
||||
|
@@ -28,6 +28,7 @@
|
||||
package com.highcapable.yukihookapi.hook.core.finder.members.data
|
||||
|
||||
import com.highcapable.yukihookapi.hook.core.finder.base.data.BaseRulesData
|
||||
import com.highcapable.yukihookapi.hook.core.finder.base.rules.ModifierRules
|
||||
import com.highcapable.yukihookapi.hook.core.finder.type.factory.CountConditions
|
||||
import java.lang.reflect.Member
|
||||
|
||||
@@ -46,6 +47,13 @@ internal open class MemberRulesData internal constructor(
|
||||
var matchCountConditions: CountConditions? = null
|
||||
) : BaseRulesData() {
|
||||
|
||||
override val templates
|
||||
get() = arrayOf(
|
||||
modifiers?.let { "modifiers:${ModifierRules.templates(uniqueValue)}" } ?: "",
|
||||
orderIndex?.let { it.takeIf { it.second }?.let { e -> "orderIndex:[${e.first}]" } ?: "orderIndex:[last]" } ?: "",
|
||||
matchIndex?.let { it.takeIf { it.second }?.let { e -> "matchIndex:[${e.first}]" } ?: "matchIndex:[last]" } ?: ""
|
||||
)
|
||||
|
||||
override val objectName get() = "Member"
|
||||
|
||||
/**
|
||||
|
@@ -52,6 +52,17 @@ internal class MethodRulesData internal constructor(
|
||||
var returnType: Any? = null
|
||||
) : MemberRulesData() {
|
||||
|
||||
override val templates
|
||||
get() = arrayOf(
|
||||
name.takeIf { it.isNotBlank() }?.let { "name:[$it]" } ?: "",
|
||||
nameConditions?.let { "nameConditions:$it" } ?: "",
|
||||
paramCount.takeIf { it >= 0 }?.let { "paramCount:[$it]" } ?: "",
|
||||
paramCountRange.takeIf { it.isEmpty().not() }?.let { "paramCountRange:[$it]" } ?: "",
|
||||
paramCountConditions?.let { "paramCountConditions:[existed]" } ?: "",
|
||||
paramTypes?.typeOfString()?.let { "paramTypes:[$it]" } ?: "",
|
||||
returnType?.let { "returnType:[$it]" } ?: "", *super.templates
|
||||
)
|
||||
|
||||
override val objectName get() = "Method"
|
||||
|
||||
override val isInitialize
|
||||
|
@@ -371,38 +371,9 @@ internal object ReflectionTool {
|
||||
* @throws IllegalStateException 如果 [BaseRulesData] 的类型错误
|
||||
*/
|
||||
private fun BaseRulesData.throwNotFoundError(instanceSet: Any?): Nothing = when (this) {
|
||||
is FieldRulesData -> throw createException(
|
||||
instanceSet, name = "Field",
|
||||
name.takeIf { it.isNotBlank() }?.let { "name:[$it]" } ?: "",
|
||||
nameConditions?.let { "nameConditions:$it" } ?: "",
|
||||
type?.let { "type:[$it]" } ?: "",
|
||||
modifiers?.let { "modifiers:[existed]" } ?: "",
|
||||
orderIndex?.let { it.takeIf { it.second }?.let { e -> "orderIndex:[${e.first}]" } ?: "orderIndex:[last]" } ?: "",
|
||||
matchIndex?.let { it.takeIf { it.second }?.let { e -> "matchIndex:[${e.first}]" } ?: "matchIndex:[last]" } ?: ""
|
||||
)
|
||||
is MethodRulesData -> throw createException(
|
||||
instanceSet, name = "Method",
|
||||
name.takeIf { it.isNotBlank() }?.let { "name:[$it]" } ?: "",
|
||||
nameConditions?.let { "nameConditions:$it" } ?: "",
|
||||
paramCount.takeIf { it >= 0 }?.let { "paramCount:[$it]" } ?: "",
|
||||
paramCountRange.takeIf { it.isEmpty().not() }?.let { "paramCountRange:[$it]" } ?: "",
|
||||
paramCountConditions?.let { "paramCountConditions:[existed]" } ?: "",
|
||||
paramTypes?.typeOfString()?.let { "paramTypes:[$it]" } ?: "",
|
||||
returnType?.let { "returnType:[$it]" } ?: "",
|
||||
modifiers?.let { "modifiers:[existed]" } ?: "",
|
||||
orderIndex?.let { it.takeIf { it.second }?.let { e -> "orderIndex:[${e.first}]" } ?: "orderIndex:[last]" } ?: "",
|
||||
matchIndex?.let { it.takeIf { it.second }?.let { e -> "matchIndex:[${e.first}]" } ?: "matchIndex:[last]" } ?: ""
|
||||
)
|
||||
is ConstructorRulesData -> throw createException(
|
||||
instanceSet, name = "Constructor",
|
||||
paramCount.takeIf { it >= 0 }?.let { "paramCount:[$it]" } ?: "",
|
||||
paramCountRange.takeIf { it.isEmpty().not() }?.let { "paramCountRange:[$it]" } ?: "",
|
||||
paramCountConditions?.let { "paramCountConditions:[existed]" } ?: "",
|
||||
paramTypes?.typeOfString()?.let { "paramTypes:[$it]" } ?: "",
|
||||
modifiers?.let { "modifiers:[existed]" } ?: "",
|
||||
orderIndex?.let { it.takeIf { it.second }?.let { e -> "orderIndex:[${e.first}]" } ?: "orderIndex:[last]" } ?: "",
|
||||
matchIndex?.let { it.takeIf { it.second }?.let { e -> "matchIndex:[${e.first}]" } ?: "matchIndex:[last]" } ?: ""
|
||||
)
|
||||
is FieldRulesData -> throw createException(instanceSet, objectName, *templates)
|
||||
is MethodRulesData -> throw createException(instanceSet, objectName, *templates)
|
||||
is ConstructorRulesData -> throw createException(instanceSet, objectName, *templates)
|
||||
else -> error("Type [$this] not allowed")
|
||||
}
|
||||
|
||||
@@ -483,21 +454,6 @@ internal object ReflectionTool {
|
||||
yLoggerW(msg = "Failed to get the declared Constructors in [$this] because got an exception\n$it")
|
||||
}.getOrNull()
|
||||
|
||||
/**
|
||||
* 获取参数数组文本化内容
|
||||
* @return [String]
|
||||
*/
|
||||
private fun Array<out Class<*>>?.typeOfString() =
|
||||
StringBuilder("(").also { sb ->
|
||||
var isFirst = true
|
||||
if (this == null || isEmpty()) return "()"
|
||||
forEach {
|
||||
if (isFirst) isFirst = false else sb.append(",")
|
||||
sb.append(it.canonicalName)
|
||||
}
|
||||
sb.append(")")
|
||||
}.toString()
|
||||
|
||||
/**
|
||||
* 判断两个方法、构造方法类型数组是否相等
|
||||
*
|
||||
|
@@ -140,14 +140,14 @@ inline fun Class<*>.hasConstructor(initiate: ConstructorConditions = { emptyPara
|
||||
* @param conditions 条件方法体
|
||||
* @return [Boolean] 是否存在
|
||||
*/
|
||||
inline fun Member.hasModifiers(conditions: ModifierConditions) = conditions(ModifierRules.with(this))
|
||||
inline fun Member.hasModifiers(conditions: ModifierConditions) = conditions(ModifierRules.with(instance = this))
|
||||
|
||||
/**
|
||||
* 查询 [Class] 中匹配的描述符
|
||||
* @param conditions 条件方法体
|
||||
* @return [Boolean] 是否存在
|
||||
*/
|
||||
inline fun Class<*>.hasModifiers(conditions: ModifierConditions) = conditions(ModifierRules.with(this))
|
||||
inline fun Class<*>.hasModifiers(conditions: ModifierConditions) = conditions(ModifierRules.with(instance = this))
|
||||
|
||||
/**
|
||||
* 查找并得到变量
|
||||
|
Reference in New Issue
Block a user