From fd910d52b89a751b4679dc96d90af540d2c5370d Mon Sep 17 00:00:00 2001 From: SuperMonster003 Date: Mon, 23 Feb 2026 20:05:21 +0800 Subject: [PATCH] =?UTF-8?q?6.7.0=20-=20Alpha21=20-=20=E4=BF=AE=E5=A4=8D=20?= =?UTF-8?q?util.java.array=20=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20Boolean=20=E7=AD=89=E7=B1=BB=E5=9E=8B=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changelog/lang_zh-Hans.json | 3 +- .../autojs/runtime/api/augment/util/Java.kt | 90 +++++++++++-------- version.properties | 8 +- 3 files changed, 57 insertions(+), 44 deletions(-) diff --git a/.changelog/lang_zh-Hans.json b/.changelog/lang_zh-Hans.json index 4467e3ac..01e6b785 100644 --- a/.changelog/lang_zh-Hans.json +++ b/.changelog/lang_zh-Hans.json @@ -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)_", diff --git a/app/src/main/java/org/autojs/autojs/runtime/api/augment/util/Java.kt b/app/src/main/java/org/autojs/autojs/runtime/api/augment/util/Java.kt index e6ea6521..40f93a70 100644 --- a/app/src/main/java/org/autojs/autojs/runtime/api/augment/util/Java.kt +++ b/app/src/main/java/org/autojs/autojs/runtime/api/augment/util/Java.kt @@ -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): 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,7 +93,9 @@ object Java : Augmentable() { if (o !is NativeObject) throw WrappedIllegalArgumentException("Argument \"o\" ${o.jsBrief()} for util.objectToMap must be a JavaScript Object") hashMapOf().also { map -> for (key in ScriptableObject.getPropertyIds(o)) { - map[key.toString()] = o.prop(key.toString()) + if (RhinoUtils.js_object_hasOwnProperty(o, key.toString())) { + map[key.toString()] = o.prop(key.toString()) + } } } } @@ -116,14 +115,27 @@ object Java : Augmentable() { } private fun typeToClass(type: Any?) = when (type) { - "string" -> String::class.java - "int" -> Integer.TYPE - "long" -> java.lang.Long.TYPE - "double" -> java.lang.Double.TYPE - "char" -> Character.TYPE - "byte" -> java.lang.Byte.TYPE - "float" -> java.lang.Float.TYPE - else -> Class.forName(Context.toString(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 + "double" -> java.lang.Double.TYPE + "char" -> Character.TYPE + "byte" -> java.lang.Byte.TYPE + "float" -> java.lang.Float.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)) } diff --git a/version.properties b/version.properties index 88c1c881..692cdf36 100644 --- a/version.properties +++ b/version.properties @@ -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