From 903f55db06b491cdc3cd3e5b8374bdf176efd399 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Fri, 30 Dec 2022 20:47:58 +0800 Subject: [PATCH] Fix Java Primitive Type Class not found problem in Android 5.0.2 (API 21) or latest --- .../hook/factory/ReflectionFactory.kt | 26 +++++++++---------- .../hook/type/java/VariableTypeFactory.kt | 16 ++++++------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt index 185a600e..6db11196 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt @@ -39,8 +39,7 @@ import com.highcapable.yukihookapi.hook.core.finder.members.FieldFinder import com.highcapable.yukihookapi.hook.core.finder.members.MethodFinder import com.highcapable.yukihookapi.hook.core.finder.tools.ReflectionTool import com.highcapable.yukihookapi.hook.core.finder.type.factory.* -import com.highcapable.yukihookapi.hook.type.java.AnyClass -import com.highcapable.yukihookapi.hook.type.java.UnitType +import com.highcapable.yukihookapi.hook.type.java.* import com.highcapable.yukihookapi.hook.xposed.bridge.status.YukiHookModuleStatus import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics import dalvik.system.BaseDexClassLoader @@ -180,17 +179,18 @@ infix fun Class<*>?.notImplements(other: Class<*>?) = implements(other).not() * - [java.lang.Byte] * @return [Class] */ -fun Class<*>.toJavaPrimitiveType() = - if (name != Unit.toString() && name != "java.lang.Void" && name != "void") - (name.replace("java.lang.Boolean", "boolean") - .replace("java.lang.Integer", "int") - .replace("java.lang.Float", "float") - .replace("java.lang.Double", "double") - .replace("java.lang.Long", "long") - .replace("java.lang.Short", "short") - .replace("java.lang.Character", "char") - .replace("java.lang.Byte", "byte")).toClass() - else UnitType +fun Class<*>.toJavaPrimitiveType() = when (this) { + classOf(), UnitClass, UnitType -> UnitType + BooleanClass, BooleanType -> BooleanType + IntClass, IntType -> IntType + FloatClass, FloatType -> FloatType + DoubleClass, DoubleType -> DoubleType + LongClass, LongType -> LongType + ShortClass, ShortType -> ShortType + CharClass, CharType -> CharType + ByteClass, ByteType -> ByteType + else -> this +} /** * 通过字符串类名转换为 [loader] 中的实体类 diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/type/java/VariableTypeFactory.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/type/java/VariableTypeFactory.kt index a00e7c1e..98eb1d61 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/type/java/VariableTypeFactory.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/type/java/VariableTypeFactory.kt @@ -84,7 +84,7 @@ val AnyType get() = AnyClass * 这是 Java 原始类型 (Primitive Type) - 它在字节码中的关键字为 "boolean" * @return [Class] */ -val BooleanType get() = "boolean".toClass() +val BooleanType get() = Boolean::class.javaPrimitiveType ?: "boolean".toClass() /** * 获得 [Char] 类型 @@ -92,7 +92,7 @@ val BooleanType get() = "boolean".toClass() * 这是 Java 原始类型 (Primitive Type) - 它在字节码中的关键字为 "char" * @return [Class] */ -val CharType get() = "char".toClass() +val CharType get() = Char::class.javaPrimitiveType ?: "char".toClass() /** * 获得 [Byte] 类型 @@ -100,7 +100,7 @@ val CharType get() = "char".toClass() * 这是 Java 原始类型 (Primitive Type) - 它在字节码中的关键字为 "byte" * @return [Class] */ -val ByteType get() = "byte".toClass() +val ByteType get() = Byte::class.javaPrimitiveType ?: "byte".toClass() /** * 获得 [Short] 类型 @@ -108,7 +108,7 @@ val ByteType get() = "byte".toClass() * 这是 Java 原始类型 (Primitive Type) - 它在字节码中的关键字为 "short" * @return [Class] */ -val ShortType get() = "short".toClass() +val ShortType get() = Short::class.javaPrimitiveType ?: "short".toClass() /** * 获得 [Int] 类型 @@ -116,7 +116,7 @@ val ShortType get() = "short".toClass() * 这是 Java 原始类型 (Primitive Type) - 它在字节码中的关键字为 "int" * @return [Class] */ -val IntType get() = "int".toClass() +val IntType get() = Int::class.javaPrimitiveType ?: "int".toClass() /** * 获得 [Float] 类型 @@ -124,7 +124,7 @@ val IntType get() = "int".toClass() * 这是 Java 原始类型 (Primitive Type) - 它在字节码中的关键字为 "float" * @return [Class] */ -val FloatType get() = "float".toClass() +val FloatType get() = Float::class.javaPrimitiveType ?: "float".toClass() /** * 获得 [Long] 类型 @@ -132,7 +132,7 @@ val FloatType get() = "float".toClass() * 这是 Java 原始类型 (Primitive Type) - 它在字节码中的关键字为 "long" * @return [Class] */ -val LongType get() = "long".toClass() +val LongType get() = Long::class.javaPrimitiveType ?: "long".toClass() /** * 获得 [Double] 类型 @@ -140,7 +140,7 @@ val LongType get() = "long".toClass() * 这是 Java 原始类型 (Primitive Type) - 它在字节码中的关键字为 "double" * @return [Class] */ -val DoubleType get() = "double".toClass() +val DoubleType get() = Double::class.javaPrimitiveType ?: "double".toClass() /** * 获得 [Unit] 类型