6.7.0 - Alpha6 - 修复 util.class[Name]/getClass[Name] 可能返回错误结果的问题

This commit is contained in:
SuperMonster003
2025-09-28 22:54:25 +08:00
parent d51ba03014
commit 8de06609f5
4 changed files with 31 additions and 44 deletions

View File

@@ -158,6 +158,7 @@ import org.autojs.autojs.runtime.api.Threads as ApiThreads
import org.autojs.autojs.runtime.api.Timers as ApiTimers
import org.autojs.autojs.runtime.api.Toaster as ApiToaster
import org.autojs.autojs.runtime.api.UI as ApiUI
import org.autojs.autojs.runtime.api.Util as ApiUtil
import org.autojs.autojs.runtime.api.augment.autojs.Version as AutojsVersion
import org.autojs.autojs.runtime.api.augment.global.Legacy as GlobalLegacy
import org.autojs.autojs.runtime.api.augment.notice.Channel as NoticeChannel
@@ -241,6 +242,10 @@ class ScriptRuntime private constructor(builder: Builder) {
@ScriptVariable
val ui: ApiUI
@JvmField
@ScriptVariable
val util: ApiUtil
@JvmField
@ScriptVariable
val dialogs = ApiDialogs(this)
@@ -453,6 +458,7 @@ class ScriptRuntime private constructor(builder: Builder) {
floaty = ApiFloaty(uiHandler, this)
ui = ApiUI(mUiHandlerAppContext, this)
util = ApiUtil(mUiHandlerAppContext, this)
images = ApiImages(mUiHandlerAppContext, this)
device = ApiDevice(mUiHandlerAppContext)
media = ApiMedia(mUiHandlerAppContext, this)
@@ -711,7 +717,7 @@ class ScriptRuntime private constructor(builder: Builder) {
Global(this).assignWithGlobal(target, topLevelScope, GlobalClasses)
GlobalLegacy(this).assignWithGlobal(target, topLevelScope)
IsNullish.augmentWithGlobal(target, topLevelScope, false)
Util.augmentWithGlobal(target, topLevelScope, true).apply {
Util.augmentWithGlobal(target, topLevelScope, util, true).apply {
UtilJava.augmentWithGlobal(this, topLevelScope, false)
UtilVersion.augmentWithGlobal(this, topLevelScope, false)
UtilVersionCodes.augmentWithGlobal(this, topLevelScope, VersionCodesInfo.obj, false)

View File

@@ -0,0 +1,23 @@
package org.autojs.autojs.runtime.api
import org.autojs.autojs.extension.AnyExtensions.jsBrief
import org.autojs.autojs.runtime.ScriptRuntime
class Util(private val context: android.content.Context, private val scriptRuntime: ScriptRuntime) {
fun `class`(o: Any?) = getClassInternal(o, "class")
fun getClass(o: Any?) = getClassInternal(o, "getClass")
fun className(o: Any?) = getClassNameInternal(o, "className")
fun getClassName(o: Any?) = getClassNameInternal(o, "getClassName")
private fun getClassInternal(o: Any?, methodName: String): Class<out Any> {
require(o != null) { "Argument \"o\" ${o.jsBrief()} for util.$methodName must be non-null" }
return o as? Class<*> ?: o.javaClass
}
private fun getClassNameInternal(o: Any?, methodName: String): String = getClassInternal(o, methodName).name
}

View File

@@ -89,10 +89,6 @@ object Util : Augmentable() {
::deprecate.name,
::debuglog.name,
::log.name,
::`class`.name,
::getClass.name,
::className.name,
::getClassName.name,
::checkStringArgument.name,
::assureStringStartsWith.name,
::assureStringEndsWith.name,
@@ -458,40 +454,6 @@ object Util : Augmentable() {
AutoJs.instance.globalConsole.log("%s - %s", timestamp, formatRhino(*arguments))
}
@JvmStatic
@RhinoSingletonFunctionInterface
fun `class`(args: Array<out Any?>): Scriptable = ensureArgumentsOnlyOne(args) { o ->
require(o != null) { "Argument \"o\" ${o.jsBrief()} for util.class must be non-null" }
getClassInternal(o)
}
@JvmStatic
@RhinoSingletonFunctionInterface
fun getClass(args: Array<out Any?>): Scriptable = ensureArgumentsOnlyOne(args) { o ->
require(o != null) { "Argument \"o\" ${o.jsBrief()} for util.getClass must be non-null" }
getClassInternal(o)
}
@JvmStatic
@RhinoSingletonFunctionInterface
fun className(args: Array<out Any?>): String = ensureArgumentsOnlyOne(args) {
when (it) {
null -> throw WrappedIllegalArgumentException("Argument \"o\" ${it.jsBrief()} for util.className must be non-null")
is Class<*> -> it.name
else -> it.javaClass.name
}
}
@JvmStatic
@RhinoSingletonFunctionInterface
fun getClassName(args: Array<out Any?>): String = ensureArgumentsOnlyOne(args) {
when (it) {
null -> throw WrappedIllegalArgumentException("Argument \"o\" ${it.jsBrief()} for util.getClassName must be non-null")
is Class<*> -> it.name
else -> it.javaClass.name
}
}
@JvmStatic
@Deprecated("Deprecated in Java", ReplaceWith("checkStringArgument(args)"))
@RhinoSingletonFunctionInterface
@@ -768,11 +730,6 @@ object Util : Augmentable() {
coerceNumber(DisplayUtils.pxToSp(coerceFloatNumber(it)))
}
private fun getClassInternal(o: Any): Scriptable = when (o) {
is Class<*> -> o
else -> o.javaClass
}.let { cls -> withRhinoContext { cx -> cx.wrapFactory.wrapJavaClass(cx, ImporterTopLevel(cx), cls) } }
internal class RegularFunction(private val func: BaseFunction) : BaseFunction() {
override fun call(cx: Context, scope: Scriptable, thisObj: Scriptable?, args: Array<out Any?>): Any {