6.7.0 - Alpha21 - 修复 util.java.array 无法正常使用 Boolean 等类型参数的问题

This commit is contained in:
SuperMonster003
2026-02-23 20:05:21 +08:00
parent 9757e3690b
commit fd910d52b8
3 changed files with 57 additions and 44 deletions

View File

@@ -1,7 +1,7 @@
{
"$data": {
"v6.7.0": {
"released_date": "2026/02/15",
"released_date": "2026/02/23",
"feature": [
"插件中心功能, 支持插件的安装/卸载/更新等操作 (入口: 主页抽屉按钮/主页标签页)",
"版本历史功能, 支持查看/恢复可编辑文件的历史版本 (入口: 主页抽屉按钮/文件管理器菜单/代码编辑器菜单)",
@@ -60,6 +60,7 @@
"timers.keepAlive 方法 timeout 参数功能无效的问题",
"floaty.window/rawWindow 方法无法接受字符串参数的问题 _[`issue #467`](http://issues.autojs6.com/467)_",
"util.class[Name]/getClass[Name] 可能返回错误结果的问题",
"util.java.array 无法正常使用 Boolean 等类型参数的问题",
"threads.disposable() 返回的对象在存取数据时可能被意外装箱的问题 _[`issue #435`](http://issues.autojs6.com/435)_",
"console.build 方法多次调用时, 日志浮动窗口样式选项未能正常重置的问题",
"console.build 方法的 (title/content)BackgroundColor 选项导致透明度或着色选项被覆盖的问题 _[`issue #458`](http://issues.autojs6.com/458)_",

View File

@@ -2,19 +2,23 @@ package org.autojs.autojs.runtime.api.augment.util
import org.autojs.autojs.annotation.RhinoFunctionBody
import org.autojs.autojs.annotation.RhinoSingletonFunctionInterface
import org.autojs.autojs.rhino.ArgumentGuards.Companion.component1
import org.autojs.autojs.rhino.ArgumentGuards.Companion.component2
import org.autojs.autojs.rhino.extension.AnyExtensions.isJsNullish
import org.autojs.autojs.rhino.extension.AnyExtensions.jsBrief
import org.autojs.autojs.rhino.extension.IterableExtensions.toNativeArray
import org.autojs.autojs.rhino.extension.ScriptableExtensions.prop
import org.autojs.autojs.runtime.api.augment.Augmentable
import org.autojs.autojs.runtime.exception.WrappedIllegalArgumentException
import org.autojs.autojs.util.RhinoUtils
import org.autojs.autojs.util.RhinoUtils.newNativeArray
import org.autojs.autojs.util.RhinoUtils.newNativeObject
import org.mozilla.javascript.Context
import org.mozilla.javascript.NativeArray
import org.mozilla.javascript.NativeJavaClass
import org.mozilla.javascript.NativeObject
import org.mozilla.javascript.ScriptableObject
import java.lang.reflect.Array.newInstance
import org.mozilla.javascript.Wrapper
@Suppress("unused")
object Java : Augmentable() {
@@ -31,9 +35,20 @@ object Java : Augmentable() {
@RhinoSingletonFunctionInterface
fun instanceof(args: Array<out Any?>): Any = ensureArgumentsLength(args, 2) {
val (obj, clazz) = it
obj ?: return@ensureArgumentsLength false
clazz ?: return@ensureArgumentsLength false
Class.forName(Context.toString(clazz)).isAssignableFrom(obj.javaClass)
obj ?: throw WrappedIllegalArgumentException("Argument \"obj\" for util.java.instanceof must not be null")
val resolvedClass = when (clazz) {
null -> return@ensureArgumentsLength false
is Class<*> -> clazz
is NativeJavaClass -> clazz.classObject
is Wrapper -> when (val unwrapped = clazz.unwrap()) {
is Class<*> -> unwrapped
is NativeJavaClass -> unwrapped.classObject
else -> unwrapped.resolveClass()
}
else -> clazz.resolveClass()
}
resolvedClass.isAssignableFrom(obj.javaClass)
}
@JvmStatic
@@ -44,33 +59,15 @@ object Java : Augmentable() {
val dims = it.drop(1).map { arg -> Context.toNumber(arg).toInt() }
// @Hint by SuperMonster003 on Jun 3, 2024.
// ! Reflection may cause some performance overhead,
// ! and potential exceptions will become harder to trace.
// ! zh-CN:
// ! 反射会带来一定的额外性能开销, 潜在的异常也难以追踪.
// !
// # val method = this::class.members.find { mem ->
// # mem is KFunction<*> && mem.name == "newInstance" && mem.parameters.size == dims.size + 1
// # } as? KFunction<*>
// # method?.call(clazz, *dims.toIntArray().toTypedArray()) ?: throw IllegalArgumentException("Too many arguments for util.java")
when (dims.size) {
+0 -> newInstance(clazz, 0)
+1 -> newInstance(clazz, dims[0])
+2 -> newInstance(clazz, dims[0], dims[1])
+3 -> newInstance(clazz, dims[0], dims[1], dims[2])
+4 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3])
+5 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4])
+6 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5])
+7 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6])
+8 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7])
+9 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8])
10 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8], dims[9])
11 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8], dims[9], dims[10])
12 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8], dims[9], dims[10], dims[11])
13 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8], dims[9], dims[10], dims[11], dims[12])
else -> throw WrappedIllegalArgumentException("Too many arguments for util.java")
if (dims.isEmpty()) {
// Keep behavior consistent with Rhino JavaScript implementation: calling array(type) throws.
// zh-CN: 保持与 Rhino JavaScript 实现一致: 调用 array(type) 时抛出异常.
throw WrappedIllegalArgumentException("Missing array dimensions for util.java.array")
}
// Use java.lang.reflect.Array.newInstance(Class, int[]) to support arbitrary dimensions.
// zh-CN: 使用 java.lang.reflect.Array.newInstance(Class, int[]) 以支持任意维度数量.
java.lang.reflect.Array.newInstance(clazz, *dims.toIntArray())
}
@JvmStatic
@@ -96,10 +93,12 @@ object Java : Augmentable() {
if (o !is NativeObject) throw WrappedIllegalArgumentException("Argument \"o\" ${o.jsBrief()} for util.objectToMap must be a JavaScript Object")
hashMapOf<String, Any?>().also { map ->
for (key in ScriptableObject.getPropertyIds(o)) {
if (RhinoUtils.js_object_hasOwnProperty(o, key.toString())) {
map[key.toString()] = o.prop(key.toString())
}
}
}
}
@JvmStatic
@RhinoSingletonFunctionInterface
@@ -116,6 +115,14 @@ object Java : Augmentable() {
}
private fun typeToClass(type: Any?) = when (type) {
is Class<*> -> type
is NativeJavaClass -> type.classObject
is Wrapper -> when (val unwrapped = type.unwrap()) {
is Class<*> -> unwrapped
is NativeJavaClass -> unwrapped.classObject
else -> unwrapped.resolveClass()
}
is String -> when (type) {
"string" -> String::class.java
"int" -> Integer.TYPE
"long" -> java.lang.Long.TYPE
@@ -123,7 +130,12 @@ object Java : Augmentable() {
"char" -> Character.TYPE
"byte" -> java.lang.Byte.TYPE
"float" -> java.lang.Float.TYPE
else -> Class.forName(Context.toString(type))
"short" -> java.lang.Short.TYPE
"boolean" -> java.lang.Boolean.TYPE
else -> type.resolveClass()
}
else -> type.resolveClass()
}
private fun Any?.resolveClass(): Class<*> = Class.forName(Context.toString(this))
}

View File

@@ -1,5 +1,5 @@
#Sun Feb 15 16:19:31 CST 2026
BUILD_TIME=1771143571726
#Mon Feb 23 16:51:52 CST 2026
BUILD_TIME=1771836712381
COMPILE_SDK_VERSION=36
IMAGE_QUANT_CMAKE_VERSION=3.22.1
IMAGE_QUANT_NDK_VERSION=26.1.10909125
@@ -27,6 +27,6 @@ RAPID_OCR_OPENCV_MOBILE_LABEL_VERSION=13
RAPID_OCR_OPENCV_MOBILE_VERSION=4.5.3
TARGET_SDK_VERSION=36
TARGET_SDK_VERSION_INRT=29
VERSION_BUILD=3746
VERSION_NAME=6.7.0 Alpha20
VERSION_BUILD=3750
VERSION_NAME=6.7.0 Alpha21
VSCODE_EXT_REQUIRED_VERSION=1.0.13