Modify remove Members cache function, optimizing performance of finder

This commit is contained in:
2023-04-25 05:03:35 +08:00
parent df6f698629
commit e9219a51f3
11 changed files with 89 additions and 291 deletions

View File

@@ -122,29 +122,15 @@ var isAllowPrintingLogs: Boolean
:::
### isEnableMemberCache <span class="symbol">- field</span>
```kotlin:no-line-numbers
var isEnableMemberCache: Boolean
```
<h3 class="deprecated">isEnableMemberCache - field</h3>
**Change Records**
`v1.0.0` `first`
**Function Illustrate**
`v1.0.2` `deprecated`
> 是否启用 `Member` 缓存功能
为防止 `Member` 复用过高造成的系统 GC 问题,此功能默认启用。
启用后会缓存已经找到的 `Method`、`Constructor`、`Field`。
缓存的 `Member` 都将处于 `ReflectsCacheStore` 的全局静态实例中。
推荐使用 `MethodFinder`、`ConstructorFinder`、`FieldFinder` 来获取 `Member`。
除非缓存的 `Member` 发生了混淆的问题,例如使用 R8 混淆后的 APP 的目标 `Member`,否则建议启用。
`Member` 的直接缓存功能已被移除,因为其存在内存溢出 (OOM) 问题
## configs <span class="symbol">- method</span>

View File

@@ -114,29 +114,15 @@ var isAllowPrintingLogs: Boolean
:::
### isEnableMemberCache <span class="symbol">- field</span>
```kotlin:no-line-numbers
var isEnableMemberCache: Boolean
```
<h3 class="deprecated">isEnableMemberCache - field</h3>
**变更记录**
`v1.0.0` `添加`
**功能描述**
`v1.0.2` `作废`
> 是否启用 `Member` 缓存功能
为防止 `Member` 复用过高造成的系统 GC 问题,此功能默认启用。
启用后会缓存已经找到的 `Method`、`Constructor`、`Field`。
缓存的 `Member` 都将处于 `ReflectsCacheStore` 的全局静态实例中。
推荐使用 `MethodFinder`、`ConstructorFinder`、`FieldFinder` 来获取 `Member`。
除非缓存的 `Member` 发生了混淆的问题,例如使用 R8 混淆后的 APP 的目标 `Member`,否则建议启用。
`Member` 的直接缓存功能已被移除,因为其存在内存溢出 (OOM) 问题
## configs <span class="symbol">- method</span>

View File

@@ -30,14 +30,7 @@
package com.highcapable.yukireflection
import com.highcapable.yukireflection.YukiReflection.Configs
import com.highcapable.yukireflection.finder.members.ConstructorFinder
import com.highcapable.yukireflection.finder.members.FieldFinder
import com.highcapable.yukireflection.finder.members.MethodFinder
import com.highcapable.yukireflection.finder.store.ReflectsCacheStore
import java.lang.reflect.Constructor
import java.lang.reflect.Field
import java.lang.reflect.Member
import java.lang.reflect.Method
/**
* [YukiReflection] 的装载调用类
@@ -85,17 +78,12 @@ object YukiReflection {
/**
* 是否启用 [Member] 缓存功能
*
* - 为防止 [Member] 复用过高造成的系统 GC 问题 - 此功能默认启用
* - ❗此方法及功能已被移除 - 在之后的版本中将直接被删除
*
* 启用后会缓存已经找到的 [Method]、[Constructor]、[Field]
*
* 缓存的 [Member] 都将处于 [ReflectsCacheStore] 的全局静态实例中
*
* 推荐使用 [MethodFinder]、[ConstructorFinder]、[FieldFinder] 来获取 [Member]
*
* 除非缓存的 [Member] 发生了混淆的问题 - 例如使用 R8 混淆后的 APP 的目标 [Member] - 否则建议启用
* - ❗[Member] 的直接缓存功能已被移除 - 因为其存在内存溢出 (OOM) 问题
*/
var isEnableMemberCache = true
@Deprecated(message = "此方法及功能已被移除,请删除此方法")
var isEnableMemberCache = false
}
/**

View File

@@ -140,10 +140,5 @@ internal abstract class BaseRulesData internal constructor(
*/
internal open val isInitialize get() = modifiers != null || orderIndex != null || matchIndex != null
/**
* 通过规则数据 [toString] 来得到一个 [Any.hashCode]
* @param other 额外的数据 - 可选
* @return [Int]
*/
internal open fun hashCode(other: Any? = null) = "[$other][$modifiers][$orderIndex][$matchIndex]".hashCode()
override fun toString() = "[$modifiers][$orderIndex][$matchIndex]"
}

View File

@@ -176,9 +176,7 @@ internal class ClassRulesData internal constructor(
isNoExtendsClass != null || isNoImplementsClass != null || extendsClass.isNotEmpty() || enclosingClass.isNotEmpty() ||
memberRules.isNotEmpty() || fieldRules.isNotEmpty() || methodRules.isNotEmpty() || constroctorRules.isNotEmpty()
override fun hashCode(other: Any?) = super.hashCode(other) + toString().hashCode()
override fun toString() = "[$fromPackages][$fullName][$simpleName][$singleName][$fullNameConditions][$simpleNameConditions]" +
"[$singleNameConditions][$modifiers][$isAnonymousClass][$isNoExtendsClass][$isNoImplementsClass][$extendsClass][$implementsClass]" +
"[$enclosingClass][$memberRules][$fieldRules][$methodRules][$constroctorRules]"
"[$enclosingClass][$memberRules][$fieldRules][$methodRules][$constroctorRules]" + super.toString()
}

View File

@@ -64,7 +64,5 @@ internal class ConstructorRulesData internal constructor(
get() = super.isInitializeOfSuper || paramTypes != null || paramTypesConditions != null || paramCount >= 0 ||
paramCountRange.isEmpty().not() || paramCountConditions != null
override fun hashCode(other: Any?) = super.hashCode(other) + toString().hashCode()
override fun toString() = "[$paramTypes][$paramTypesConditions][$paramCount][$paramCountRange]"
override fun toString() = "[$paramTypes][$paramTypesConditions][$paramCount][$paramCountRange]" + super.toString()
}

View File

@@ -60,7 +60,5 @@ internal class FieldRulesData internal constructor(
override val isInitialize
get() = super.isInitializeOfSuper || name.isNotBlank() || nameConditions != null || type != null || typeConditions != null
override fun hashCode(other: Any?) = super.hashCode(other) + toString().hashCode()
override fun toString() = "[$name][$nameConditions][$type][$typeConditions]"
override fun toString() = "[$name][$nameConditions][$type][$typeConditions]" + super.toString()
}

View File

@@ -71,7 +71,5 @@ internal open class MemberRulesData internal constructor(
override val isInitialize get() = isInitializeOfSuper || isInitializeOfMatch
override fun hashCode(other: Any?) = super.hashCode(other) + toString().hashCode()
override fun toString() = "[$isFindInSuper][$matchIndex][$matchCountRange]"
override fun toString() = "[$isFindInSuper][$matchIndex][$matchCountRange][$matchCountConditions]" + super.toString()
}

View File

@@ -79,8 +79,6 @@ internal class MethodRulesData internal constructor(
paramCount >= 0 || paramCountRange.isEmpty().not() || paramCountConditions != null ||
returnType != null || returnTypeConditions != null
override fun hashCode(other: Any?) = super.hashCode(other) + toString().hashCode()
override fun toString() = "[$name][$nameConditions][$paramTypes][$paramTypesConditions][$paramCount]" +
"[$paramCountRange][$returnType][$returnTypeConditions]"
"[$paramCountRange][$returnType][$returnTypeConditions]" + super.toString()
}

View File

@@ -1,165 +0,0 @@
/*
* YukiReflection - An efficient Reflection API for the Android platform built in Kotlin.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/fankes/YukiReflection
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is Created by fankes on 2022/3/29.
* This file is Modified by fankes on 2023/1/21.
*/
package com.highcapable.yukireflection.finder.store
import android.util.ArrayMap
import com.highcapable.yukireflection.YukiReflection
import java.lang.reflect.Constructor
import java.lang.reflect.Field
import java.lang.reflect.Member
import java.lang.reflect.Method
/**
* 这是一个全局静态的 [Class]、[Member] 缓存实例
*
* 为防止 [Class]、[Member] 复用过高造成的系统 GC 问题
*
* 查找后的 [Class] 自动进入缓存 - 不受任何控制
*
* 查找后的 [Member] 在 [YukiReflection.Configs.isEnableMemberCache] 启用后自动进入缓存
*/
internal object ReflectsCacheStore {
/** 缓存的 [Class] 列表 */
private val dexClassListData = ArrayMap<Int, List<String>>()
/** 缓存的 [Class] 对象 */
private val classData = ArrayMap<Int, Class<*>?>()
/** 缓存的 [Class] 数组 */
private val classesData = ArrayMap<Int, HashSet<Class<*>>>()
/** 缓存的 [Method] 数组 */
private val methodsData = ArrayMap<Int, HashSet<Method>>()
/** 缓存的 [Constructor] 数组 */
private val constructorsData = ArrayMap<Int, HashSet<Constructor<*>>>()
/** 缓存的 [Field] 数组 */
private val fieldsData = ArrayMap<Int, HashSet<Field>>()
/**
* 查找缓存中的 [Class] 列表
* @param hashCode 标识符
* @return [List]<[Class]>
*/
internal fun findDexClassList(hashCode: Int) = dexClassListData[hashCode]
/**
* 查找缓存中的 [Class]
* @param hashCode 标识符
* @return [Class] or null
*/
internal fun findClass(hashCode: Int) = classData[hashCode]
/**
* 查找缓存中的 [Class] 数组
* @param hashCode 标识符
* @return [HashSet]<[Class]> or null
*/
internal fun findClasses(hashCode: Int) = classesData[hashCode]
/**
* 查找缓存中的 [Method] 数组
* @param hashCode 标识符
* @return [HashSet]<[Method]>
*/
internal fun findMethods(hashCode: Int) = methodsData[hashCode]
/**
* 查找缓存中的 [Constructor] 数组
* @param hashCode 标识符
* @return [HashSet]<[Constructor]>
*/
internal fun findConstructors(hashCode: Int) = constructorsData[hashCode]
/**
* 查找缓存中的 [Field] 数组
* @param hashCode 标识符
* @return [HashSet]<[Field]>
*/
internal fun findFields(hashCode: Int) = fieldsData[hashCode]
/**
* 写入 [Class] 列表到缓存
* @param hashCode 标识符
* @param instance 实例
*/
internal fun putDexClassList(hashCode: Int, instance: List<String>) {
dexClassListData[hashCode] = instance
}
/**
* 写入 [Class] 到缓存
* @param hashCode 标识符
* @param instance 实例
*/
internal fun putClass(hashCode: Int, instance: Class<*>?) {
classData[hashCode] = instance
}
/**
* 写入 [Class] 数组到缓存
* @param hashCode 标识符
* @param instance 实例
*/
internal fun putClasses(hashCode: Int, instance: HashSet<Class<*>>) {
classesData[hashCode] = instance
}
/**
* 写入 [Method] 数组到缓存
* @param hashCode 标识符
* @param instances 实例数组
*/
internal fun putMethods(hashCode: Int, instances: HashSet<Method>) {
if (YukiReflection.Configs.isEnableMemberCache.not()) return
methodsData[hashCode] = instances
}
/**
* 写入 [Constructor] 数组到缓存
* @param hashCode 标识符
* @param instances 实例数组
*/
internal fun putConstructors(hashCode: Int, instances: HashSet<Constructor<*>>) {
if (YukiReflection.Configs.isEnableMemberCache.not()) return
constructorsData[hashCode] = instances
}
/**
* 写入 [Field] 数组到缓存
* @param hashCode 标识符
* @param instances 实例数组
*/
internal fun putFields(hashCode: Int, instances: HashSet<Field>) {
if (YukiReflection.Configs.isEnableMemberCache.not()) return
fieldsData[hashCode] = instances
}
}

View File

@@ -30,6 +30,8 @@
package com.highcapable.yukireflection.finder.tools
import android.util.ArrayMap
import com.highcapable.yukireflection.YukiReflection
import com.highcapable.yukireflection.factory.*
import com.highcapable.yukireflection.finder.base.data.BaseRulesData
import com.highcapable.yukireflection.finder.classes.data.ClassRulesData
@@ -37,7 +39,6 @@ import com.highcapable.yukireflection.finder.members.data.ConstructorRulesData
import com.highcapable.yukireflection.finder.members.data.FieldRulesData
import com.highcapable.yukireflection.finder.members.data.MemberRulesData
import com.highcapable.yukireflection.finder.members.data.MethodRulesData
import com.highcapable.yukireflection.finder.store.ReflectsCacheStore
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.type.defined.UndefinedType
import com.highcapable.yukireflection.type.defined.VagueType
@@ -63,8 +64,23 @@ internal object ReflectionTool {
/** 当前工具类的标签 */
private const val TAG = "YukiReflection#ReflectionTool"
/** 当前工具类的 [ClassLoader] */
private val reflectionClassLoader = javaClass.classLoader ?: error("Operating system not supported")
/**
* 当前工具类的 [ClassLoader]
* @return [ClassLoader]
*/
private val currentClassLoader get() = classOf<YukiReflection>().classLoader ?: error("Operating system not supported")
/**
* 内存缓存实例实现
*/
private object MemoryCache {
/** 缓存的 [Class] 列表数组 */
val dexClassListData = ArrayMap<String, List<String>>()
/** 缓存的 [Class] 对象数组 */
val classData = ArrayMap<String, Class<*>?>()
}
/**
* 写出当前 [ClassLoader] 下所有 [Class] 名称数组
@@ -72,7 +88,7 @@ internal object ReflectionTool {
* @return [List]<[String]>
* @throws IllegalStateException 如果 [loader] 不是 [BaseDexClassLoader]
*/
internal fun findDexClassList(loader: ClassLoader?) = ReflectsCacheStore.findDexClassList(loader.hashCode())
internal fun findDexClassList(loader: ClassLoader?) = MemoryCache.dexClassListData[loader.toString()]
?: DalvikBaseDexClassLoader.field { name = "pathList" }.ignored().get(loader.value().let {
while (it.value !is BaseDexClassLoader) {
if (it.value?.parent != null) it.value = it.value?.parent
@@ -81,7 +97,7 @@ internal object ReflectionTool {
}).current(ignored = true)?.field { name = "dexElements" }?.array<Any>()?.flatMap { element ->
element.current(ignored = true).field { name = "dexFile" }.current(ignored = true)
?.method { name = "entries" }?.invoke<Enumeration<String>>()?.toList().orEmpty()
}.orEmpty().also { if (it.isNotEmpty()) ReflectsCacheStore.putDexClassList(loader.hashCode(), it) }
}.orEmpty().also { if (it.isNotEmpty()) MemoryCache.dexClassListData[loader.toString()] = it }
/**
* 使用字符串类名查找 [Class] 是否存在
@@ -101,16 +117,16 @@ internal object ReflectionTool {
*/
@PublishedApi
internal fun findClassByName(name: String, loader: ClassLoader?, initialize: Boolean = false): Class<*> {
val hashCode = ("[$name][$loader]").hashCode()
val uniqueCode = "[$name][$loader]"
/**
* 获取 [Class.forName] 的 [Class] 对象
* @param name [Class] 完整名称
* @param initialize 是否初始化 [Class] 的静态方法块
* @param loader [Class] 所在的 [ClassLoader] - 默认为 [reflectionClassLoader]
* @param loader [Class] 所在的 [ClassLoader] - 默认为 [currentClassLoader]
* @return [Class]
*/
fun classForName(name: String, initialize: Boolean, loader: ClassLoader? = reflectionClassLoader) =
fun classForName(name: String, initialize: Boolean, loader: ClassLoader? = currentClassLoader) =
Class.forName(name, initialize, loader)
/**
@@ -118,9 +134,9 @@ internal object ReflectionTool {
* @return [Class] or null
*/
fun loadWithDefaultClassLoader() = if (initialize.not()) loader?.loadClass(name) else classForName(name, initialize, loader)
return ReflectsCacheStore.findClass(hashCode) ?: runCatching {
(loadWithDefaultClassLoader() ?: classForName(name, initialize)).also { ReflectsCacheStore.putClass(hashCode, it) }
}.getOrNull() ?: throw createException(loader ?: reflectionClassLoader, name = "Class", "name:[$name]")
return MemoryCache.classData[uniqueCode] ?: runCatching {
(loadWithDefaultClassLoader() ?: classForName(name, initialize)).also { MemoryCache.classData[uniqueCode] = it }
}.getOrNull() ?: throw createException(loader ?: currentClassLoader, name = "Class", "name:[$name]")
}
/**
@@ -132,7 +148,7 @@ internal object ReflectionTool {
* @throws NoClassDefFoundError 如果找不到 [Class]
*/
internal fun findClasses(loaderSet: ClassLoader?, rulesData: ClassRulesData) = rulesData.createResult {
ReflectsCacheStore.findClasses(hashCode(loaderSet)) ?: hashSetOf<Class<*>>().also { classes ->
hashSetOf<Class<*>>().also { classes ->
/**
* 开始查找作业
* @param instance 当前 [Class] 实例
@@ -191,7 +207,7 @@ internal object ReflectionTool {
value.modifiers?.also { runCatching { and(it(member.cast())) } }
}.finally { numberOfFound++ }
}.run { rule.matchCount(numberOfFound) { and(it && numberOfFound > 0) } }
else rule.matchCount(size) { and(it) }
else rule.matchCount(count()) { and(it) }
}
}
fieldRules.takeIf { it.isNotEmpty() }?.forEach { rule ->
@@ -206,7 +222,7 @@ internal object ReflectionTool {
value.typeConditions?.also { field.also { t -> runCatching { and(it(t.type(), t.type)) } } }
}.finally { numberOfFound++ }
}.run { rule.matchCount(numberOfFound) { and(it && numberOfFound > 0) } }
else rule.matchCount(size) { and(it) }
else rule.matchCount(count()) { and(it) }
}
}
methodRules.takeIf { it.isNotEmpty() }?.forEach { rule ->
@@ -229,7 +245,7 @@ internal object ReflectionTool {
value.nameConditions?.also { method.name.also { n -> runCatching { and(it(n.cast(), n)) } } }
}.finally { numberOfFound++ }
}.run { rule.matchCount(numberOfFound) { and(it && numberOfFound > 0) } }
else rule.matchCount(size) { and(it) }
else rule.matchCount(count()) { and(it) }
}
}
constroctorRules.takeIf { it.isNotEmpty() }?.forEach { rule ->
@@ -247,7 +263,7 @@ internal object ReflectionTool {
value.modifiers?.also { runCatching { and(it(constructor.cast())) } }
}.finally { numberOfFound++ }
}.run { rule.matchCount(numberOfFound) { and(it && numberOfFound > 0) } }
else rule.matchCount(size) { and(it) }
else rule.matchCount(count()) { and(it) }
}
}
}.finally { classes.add(instance) }
@@ -263,7 +279,7 @@ internal object ReflectionTool {
) startProcess(className.toClass(loaderSet))
}
}
}.takeIf { it.isNotEmpty() }?.also { ReflectsCacheStore.putClasses(hashCode(loaderSet), it) } ?: throwNotFoundError(loaderSet)
}.takeIf { it.isNotEmpty() } ?: throwNotFoundError(loaderSet)
}
/**
@@ -277,19 +293,19 @@ internal object ReflectionTool {
internal fun findFields(classSet: Class<*>?, rulesData: FieldRulesData) = rulesData.createResult {
if (type == UndefinedType) error("Field match type class is not found")
if (classSet == null) return@createResult hashSetOf()
ReflectsCacheStore.findFields(hashCode(classSet)) ?: hashSetOf<Field>().also { fields ->
hashSetOf<Field>().also { fields ->
classSet.existFields?.also { declares ->
var iType = -1
var iName = -1
var iModify = -1
var iNameCds = -1
var iTypeCds = -1
val iLType = type?.let(matchIndex) { e -> declares.filter { e == it.type }.lastIndex } ?: -1
val iLName = name.takeIf(matchIndex) { it.isNotBlank() }?.let { e -> declares.filter { e == it.name }.lastIndex } ?: -1
val iLModify = modifiers?.let(matchIndex) { e -> declares.filter { runOrFalse { e(it.cast()) } }.lastIndex } ?: -1
val iLType = type?.let(matchIndex) { e -> declares.findLastIndex { e == it.type } } ?: -1
val iLName = name.takeIf(matchIndex) { it.isNotBlank() }?.let { e -> declares.findLastIndex { e == it.name } } ?: -1
val iLModify = modifiers?.let(matchIndex) { e -> declares.findLastIndex { runOrFalse { e(it.cast()) } } } ?: -1
val iLNameCds = nameConditions
?.let(matchIndex) { e -> declares.filter { it.name.let { n -> runOrFalse { e(n.cast(), n) } } }.lastIndex } ?: -1
val iLTypeCds = typeConditions?.let(matchIndex) { e -> declares.filter { runOrFalse { e(it.type(), it.type) } }.lastIndex } ?: -1
?.let(matchIndex) { e -> declares.findLastIndex { it.name.let { n -> runOrFalse { e(n.cast(), n) } } } } ?: -1
val iLTypeCds = typeConditions?.let(matchIndex) { e -> declares.findLastIndex { runOrFalse { e(it.type(), it.type) } } } ?: -1
declares.forEachIndexed { index, instance ->
conditions {
type?.also {
@@ -322,11 +338,11 @@ internal object ReflectionTool {
hold && matchIndex.compare(iTypeCds, iLTypeCds)
})
}
orderIndex.compare(index, declares.lastIndex) { and(it) }
orderIndex.compare(index, declares.lastIndex()) { and(it) }
}.finally { fields.add(instance.apply { isAccessible = true }) }
}
}
}.takeIf { it.isNotEmpty() }?.also { ReflectsCacheStore.putFields(hashCode(classSet), it) } ?: findSuperOrThrow(classSet)
}.takeIf { it.isNotEmpty() } ?: findSuperOrThrow(classSet)
}
/**
@@ -342,7 +358,7 @@ internal object ReflectionTool {
if (classSet == null) return@createResult hashSetOf()
paramTypes?.takeIf { it.isNotEmpty() }
?.forEachIndexed { p, it -> if (it == UndefinedType) error("Method match paramType[$p] class is not found") }
ReflectsCacheStore.findMethods(hashCode(classSet)) ?: hashSetOf<Method>().also { methods ->
hashSetOf<Method>().also { methods ->
classSet.existMethods?.also { declares ->
var iReturnType = -1
var iReturnTypeCds = -1
@@ -354,23 +370,23 @@ internal object ReflectionTool {
var iName = -1
var iModify = -1
var iNameCds = -1
val iLReturnType = returnType?.let(matchIndex) { e -> declares.filter { e == it.returnType }.lastIndex } ?: -1
val iLReturnType = returnType?.let(matchIndex) { e -> declares.findLastIndex { e == it.returnType } } ?: -1
val iLReturnTypeCds = returnTypeConditions
?.let(matchIndex) { e -> declares.filter { runOrFalse { e(it.returnType(), it.returnType) } }.lastIndex } ?: -1
?.let(matchIndex) { e -> declares.findLastIndex { runOrFalse { e(it.returnType(), it.returnType) } } } ?: -1
val iLParamCount = paramCount.takeIf(matchIndex) { it >= 0 }
?.let { e -> declares.filter { e == it.parameterTypes.size }.lastIndex } ?: -1
?.let { e -> declares.findLastIndex { e == it.parameterTypes.size } } ?: -1
val iLParamCountRange = paramCountRange.takeIf(matchIndex) { it.isEmpty().not() }
?.let { e -> declares.filter { it.parameterTypes.size in e }.lastIndex } ?: -1
?.let { e -> declares.findLastIndex { it.parameterTypes.size in e } } ?: -1
val iLParamCountCds = paramCountConditions?.let(matchIndex) { e ->
declares.filter { it.parameterTypes.size.let { s -> runOrFalse { e(s.cast(), s) } } }.lastIndex
declares.findLastIndex { it.parameterTypes.size.let { s -> runOrFalse { e(s.cast(), s) } } }
} ?: -1
val iLParamTypes = paramTypes?.let(matchIndex) { e -> declares.filter { paramTypesEq(e, it.parameterTypes) }.lastIndex } ?: -1
val iLParamTypes = paramTypes?.let(matchIndex) { e -> declares.findLastIndex { paramTypesEq(e, it.parameterTypes) } } ?: -1
val iLParamTypesCds = paramTypesConditions
?.let(matchIndex) { e -> declares.filter { runOrFalse { e(it.paramTypes(), it.parameterTypes) } }.lastIndex } ?: -1
val iLName = name.takeIf(matchIndex) { it.isNotBlank() }?.let { e -> declares.filter { e == it.name }.lastIndex } ?: -1
val iLModify = modifiers?.let(matchIndex) { e -> declares.filter { runOrFalse { e(it.cast()) } }.lastIndex } ?: -1
?.let(matchIndex) { e -> declares.findLastIndex { runOrFalse { e(it.paramTypes(), it.parameterTypes) } } } ?: -1
val iLName = name.takeIf(matchIndex) { it.isNotBlank() }?.let { e -> declares.findLastIndex { e == it.name } } ?: -1
val iLModify = modifiers?.let(matchIndex) { e -> declares.findLastIndex { runOrFalse { e(it.cast()) } } } ?: -1
val iLNameCds = nameConditions
?.let(matchIndex) { e -> declares.filter { it.name.let { n -> runOrFalse { e(n.cast(), n) } } }.lastIndex } ?: -1
?.let(matchIndex) { e -> declares.findLastIndex { it.name.let { n -> runOrFalse { e(n.cast(), n) } } } } ?: -1
declares.forEachIndexed { index, instance ->
conditions {
name.takeIf { it.isNotBlank() }?.also {
@@ -433,11 +449,11 @@ internal object ReflectionTool {
hold && matchIndex.compare(iNameCds, iLNameCds)
})
}
orderIndex.compare(index, declares.lastIndex) { and(it) }
orderIndex.compare(index, declares.lastIndex()) { and(it) }
}.finally { methods.add(instance.apply { isAccessible = true }) }
}
}
}.takeIf { it.isNotEmpty() }?.also { ReflectsCacheStore.putMethods(hashCode(classSet), it) } ?: findSuperOrThrow(classSet)
}.takeIf { it.isNotEmpty() } ?: findSuperOrThrow(classSet)
}
/**
@@ -452,7 +468,7 @@ internal object ReflectionTool {
if (classSet == null) return@createResult hashSetOf()
paramTypes?.takeIf { it.isNotEmpty() }
?.forEachIndexed { p, it -> if (it == UndefinedType) error("Constructor match paramType[$p] class is not found") }
ReflectsCacheStore.findConstructors(hashCode(classSet)) ?: hashSetOf<Constructor<*>>().also { constructors ->
hashSetOf<Constructor<*>>().also { constructors ->
classSet.existConstructors?.also { declares ->
var iParamTypes = -1
var iParamTypesCds = -1
@@ -461,16 +477,16 @@ internal object ReflectionTool {
var iParamCountCds = -1
var iModify = -1
val iLParamCount = paramCount.takeIf(matchIndex) { it >= 0 }
?.let { e -> declares.filter { e == it.parameterTypes.size }.lastIndex } ?: -1
?.let { e -> declares.findLastIndex { e == it.parameterTypes.size } } ?: -1
val iLParamCountRange = paramCountRange.takeIf(matchIndex) { it.isEmpty().not() }
?.let { e -> declares.filter { it.parameterTypes.size in e }.lastIndex } ?: -1
?.let { e -> declares.findLastIndex { it.parameterTypes.size in e } } ?: -1
val iLParamCountCds = paramCountConditions?.let(matchIndex) { e ->
declares.filter { it.parameterTypes.size.let { s -> runOrFalse { e(s.cast(), s) } } }.lastIndex
declares.findLastIndex { it.parameterTypes.size.let { s -> runOrFalse { e(s.cast(), s) } } }
} ?: -1
val iLParamTypes = paramTypes?.let(matchIndex) { e -> declares.filter { paramTypesEq(e, it.parameterTypes) }.lastIndex } ?: -1
val iLParamTypes = paramTypes?.let(matchIndex) { e -> declares.findLastIndex { paramTypesEq(e, it.parameterTypes) } } ?: -1
val iLParamTypesCds = paramTypesConditions
?.let(matchIndex) { e -> declares.filter { runOrFalse { e(it.paramTypes(), it.parameterTypes) } }.lastIndex } ?: -1
val iLModify = modifiers?.let(matchIndex) { e -> declares.filter { runOrFalse { e(it.cast()) } }.lastIndex } ?: -1
?.let(matchIndex) { e -> declares.findLastIndex { runOrFalse { e(it.paramTypes(), it.parameterTypes) } } } ?: -1
val iLModify = modifiers?.let(matchIndex) { e -> declares.findLastIndex { runOrFalse { e(it.cast()) } } } ?: -1
declares.forEachIndexed { index, instance ->
conditions {
paramCount.takeIf { it >= 0 }?.also {
@@ -509,11 +525,11 @@ internal object ReflectionTool {
hold && matchIndex.compare(iModify, iLModify)
})
}
orderIndex.compare(index, declares.lastIndex) { and(it) }
orderIndex.compare(index, declares.lastIndex()) { and(it) }
}.finally { constructors.add(instance.apply { isAccessible = true }) }
}
}
}.takeIf { it.isNotEmpty() }?.also { ReflectsCacheStore.putConstructors(hashCode(classSet), it) } ?: findSuperOrThrow(classSet)
}.takeIf { it.isNotEmpty() } ?: findSuperOrThrow(classSet)
}
/**
@@ -549,6 +565,7 @@ internal object ReflectionTool {
is FieldRulesData -> isInitialize.not()
is MethodRulesData -> isInitialize.not()
is ConstructorRulesData -> isInitialize.not()
is ClassRulesData -> isInitialize.not()
else -> true
}.takeIf { it }?.also { error("You must set a condition when finding a $objectName") }
return result(this)
@@ -590,6 +607,7 @@ internal object ReflectionTool {
is FieldRulesData -> throw createException(instanceSet, objectName, *templates)
is MethodRulesData -> throw createException(instanceSet, objectName, *templates)
is ConstructorRulesData -> throw createException(instanceSet, objectName, *templates)
is ClassRulesData -> throw createException(instanceSet ?: currentClassLoader, objectName, *templates)
else -> error("Type [$this] not allowed")
}
@@ -630,7 +648,7 @@ internal object ReflectionTool {
/**
* 获取当前 [Class] 中存在的 [Member] 数组
* @return [Array]<[Member]>
* @return [Sequence]<[Member]> or null
*/
private val Class<*>.existMembers
get() = runCatching {
@@ -638,35 +656,35 @@ internal object ReflectionTool {
addAll(declaredFields.toList())
addAll(declaredMethods.toList())
addAll(declaredConstructors.toList())
}.toTypedArray()
}.asSequence()
}.onFailure {
yLoggerW(msg = "Failed to get the declared Members in [$this] because got an exception\n$it")
}.getOrNull()
/**
* 获取当前 [Class] 中存在的 [Field] 数组
* @return [Array]<[Field]>
* @return [Sequence]<[Field]> or null
*/
private val Class<*>.existFields
get() = runCatching { declaredFields }.onFailure {
get() = runCatching { declaredFields.asSequence() }.onFailure {
yLoggerW(msg = "Failed to get the declared Fields in [$this] because got an exception\n$it")
}.getOrNull()
/**
* 获取当前 [Class] 中存在的 [Method] 数组
* @return [Array]<[Method]>
* @return [Sequence]<[Method]> or null
*/
private val Class<*>.existMethods
get() = runCatching { declaredMethods }.onFailure {
get() = runCatching { declaredMethods.asSequence() }.onFailure {
yLoggerW(msg = "Failed to get the declared Methods in [$this] because got an exception\n$it")
}.getOrNull()
/**
* 获取当前 [Class] 中存在的 [Constructor] 数组
* @return [Array]<[Constructor]>
* @return [Sequence]<[Constructor]> or null
*/
private val Class<*>.existConstructors
get() = runCatching { declaredConstructors }.onFailure {
get() = runCatching { declaredConstructors.asSequence() }.onFailure {
yLoggerW(msg = "Failed to get the declared Constructors in [$this] because got an exception\n$it")
}.getOrNull()