From e9219a51f30af35df4b515f2af54680002918cb3 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Tue, 25 Apr 2023 05:03:35 +0800 Subject: [PATCH] Modify remove Members cache function, optimizing performance of finder --- .../yukireflection/YukiReflection.md | 20 +-- .../yukireflection/YukiReflection.md | 20 +-- .../yukireflection/YukiReflection.kt | 20 +-- .../finder/base/data/BaseRulesData.kt | 7 +- .../finder/classes/data/ClassRulesData.kt | 4 +- .../members/data/ConstructorRulesData.kt | 4 +- .../finder/members/data/FieldRulesData.kt | 4 +- .../finder/members/data/MemberRulesData.kt | 4 +- .../finder/members/data/MethodRulesData.kt | 4 +- .../finder/store/ReflectsCacheStore.kt | 165 ------------------ .../finder/tools/ReflectionTool.kt | 128 ++++++++------ 11 files changed, 89 insertions(+), 291 deletions(-) delete mode 100644 yukireflection/src/main/java/com/highcapable/yukireflection/finder/store/ReflectsCacheStore.kt diff --git a/docs-source/src/en/api/public/com/highcapable/yukireflection/YukiReflection.md b/docs-source/src/en/api/public/com/highcapable/yukireflection/YukiReflection.md index 5a4e818..01bc425 100644 --- a/docs-source/src/en/api/public/com/highcapable/yukireflection/YukiReflection.md +++ b/docs-source/src/en/api/public/com/highcapable/yukireflection/YukiReflection.md @@ -122,29 +122,15 @@ var isAllowPrintingLogs: Boolean ::: -### isEnableMemberCache - field - -```kotlin:no-line-numbers -var isEnableMemberCache: Boolean -``` +

isEnableMemberCache - field

**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 - method diff --git a/docs-source/src/zh-cn/api/public/com/highcapable/yukireflection/YukiReflection.md b/docs-source/src/zh-cn/api/public/com/highcapable/yukireflection/YukiReflection.md index d7f9171..5d16c57 100644 --- a/docs-source/src/zh-cn/api/public/com/highcapable/yukireflection/YukiReflection.md +++ b/docs-source/src/zh-cn/api/public/com/highcapable/yukireflection/YukiReflection.md @@ -114,29 +114,15 @@ var isAllowPrintingLogs: Boolean ::: -### isEnableMemberCache - field - -```kotlin:no-line-numbers -var isEnableMemberCache: Boolean -``` +

isEnableMemberCache - field

**变更记录** `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 - method diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/YukiReflection.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/YukiReflection.kt index fe352e6..88f7184 100644 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/YukiReflection.kt +++ b/yukireflection/src/main/java/com/highcapable/yukireflection/YukiReflection.kt @@ -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 } /** diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/base/data/BaseRulesData.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/base/data/BaseRulesData.kt index a71e459..a33e0e7 100644 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/base/data/BaseRulesData.kt +++ b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/base/data/BaseRulesData.kt @@ -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]" } \ No newline at end of file diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/classes/data/ClassRulesData.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/classes/data/ClassRulesData.kt index 5ca1ba9..76c147f 100644 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/classes/data/ClassRulesData.kt +++ b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/classes/data/ClassRulesData.kt @@ -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() } \ No newline at end of file diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/ConstructorRulesData.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/ConstructorRulesData.kt index b51ee58..7447c29 100644 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/ConstructorRulesData.kt +++ b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/ConstructorRulesData.kt @@ -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() } \ No newline at end of file diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/FieldRulesData.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/FieldRulesData.kt index 8474c66..3353e5d 100644 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/FieldRulesData.kt +++ b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/FieldRulesData.kt @@ -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() } \ No newline at end of file diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/MemberRulesData.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/MemberRulesData.kt index 6391690..0272675 100644 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/MemberRulesData.kt +++ b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/MemberRulesData.kt @@ -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() } \ No newline at end of file diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/MethodRulesData.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/MethodRulesData.kt index df5c263..2235d4f 100644 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/MethodRulesData.kt +++ b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/members/data/MethodRulesData.kt @@ -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() } \ No newline at end of file diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/store/ReflectsCacheStore.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/store/ReflectsCacheStore.kt deleted file mode 100644 index 8db4007..0000000 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/store/ReflectsCacheStore.kt +++ /dev/null @@ -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>() - - /** 缓存的 [Class] 对象 */ - private val classData = ArrayMap?>() - - /** 缓存的 [Class] 数组 */ - private val classesData = ArrayMap>>() - - /** 缓存的 [Method] 数组 */ - private val methodsData = ArrayMap>() - - /** 缓存的 [Constructor] 数组 */ - private val constructorsData = ArrayMap>>() - - /** 缓存的 [Field] 数组 */ - private val fieldsData = ArrayMap>() - - /** - * 查找缓存中的 [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) { - 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>) { - classesData[hashCode] = instance - } - - /** - * 写入 [Method] 数组到缓存 - * @param hashCode 标识符 - * @param instances 实例数组 - */ - internal fun putMethods(hashCode: Int, instances: HashSet) { - if (YukiReflection.Configs.isEnableMemberCache.not()) return - methodsData[hashCode] = instances - } - - /** - * 写入 [Constructor] 数组到缓存 - * @param hashCode 标识符 - * @param instances 实例数组 - */ - internal fun putConstructors(hashCode: Int, instances: HashSet>) { - if (YukiReflection.Configs.isEnableMemberCache.not()) return - constructorsData[hashCode] = instances - } - - /** - * 写入 [Field] 数组到缓存 - * @param hashCode 标识符 - * @param instances 实例数组 - */ - internal fun putFields(hashCode: Int, instances: HashSet) { - if (YukiReflection.Configs.isEnableMemberCache.not()) return - fieldsData[hashCode] = instances - } -} \ No newline at end of file diff --git a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/tools/ReflectionTool.kt b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/tools/ReflectionTool.kt index 3c8ca82..a921eb8 100644 --- a/yukireflection/src/main/java/com/highcapable/yukireflection/finder/tools/ReflectionTool.kt +++ b/yukireflection/src/main/java/com/highcapable/yukireflection/finder/tools/ReflectionTool.kt @@ -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().classLoader ?: error("Operating system not supported") + + /** + * 内存缓存实例实现 + */ + private object MemoryCache { + + /** 缓存的 [Class] 列表数组 */ + val dexClassListData = ArrayMap>() + + /** 缓存的 [Class] 对象数组 */ + val classData = ArrayMap?>() + } /** * 写出当前 [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()?.flatMap { element -> element.current(ignored = true).field { name = "dexFile" }.current(ignored = true) ?.method { name = "entries" }?.invoke>()?.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>().also { classes -> + hashSetOf>().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().also { fields -> + hashSetOf().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().also { methods -> + hashSetOf().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>().also { constructors -> + hashSetOf>().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()