Skip to content

Commit d1b02e3

Browse files
committed
Add callStaticMethod to Reflection and JavaObject, update Reflect interface
1 parent db13265 commit d1b02e3

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

plugin/src/main/kotlin/dev/mmrl/module/Reflection.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,36 @@ class Reflection(wxOptions: WXOptions) : WXUInterface(wxOptions) {
8484
}
8585
}
8686

87+
@JavascriptInterface
88+
fun callStaticMethod(classId: String, methodName: String, argsJson: String?): String? {
89+
return try {
90+
val clazz = ReflectStore.getObject<Class<*>>(classId) ?: return null
91+
val rawArgs = parseJsonArgs(argsJson)
92+
val staticMethods = clazz.methods.filter {
93+
it.name == methodName && java.lang.reflect.Modifier.isStatic(it.modifiers)
94+
}
95+
96+
if (staticMethods.isEmpty()) {
97+
console.error("No static method named '$methodName' found on class $classId")
98+
return null
99+
}
100+
101+
val signature = findMatchingCallable(staticMethods, rawArgs) ?: run {
102+
console.error("No static method overload for '$methodName' matches provided args on class $classId")
103+
return null
104+
}
105+
106+
val result = signature.callable.invoke(null, *signature.coercedArgs)
107+
toJsRepresentation(result, modId)
108+
} catch (e: InvocationTargetException) {
109+
console.error(e.targetException ?: e)
110+
null
111+
} catch (e: Throwable) {
112+
console.error(e)
113+
null
114+
}
115+
}
116+
87117
@JavascriptInterface
88118
fun getField(objectId: String, fieldName: String): String? {
89119
return try {

ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wxu",
3-
"version": "0.0.64",
3+
"version": "0.0.65",
44
"type": "module",
55
"description": "WXU TypeScript Package",
66
"source": "src/index.ts",

ts/src/classes/JavaObject.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ export class JavaObject {
136136
return this.reflect.callMethod(objId, method, jsonArgs);
137137
}
138138

139+
public static callStaticMethod(
140+
objId: string,
141+
method: string,
142+
args: Array<string | boolean | number> | null
143+
): string | null {
144+
const jsonArgs = args == null ? "[]" : JSON.stringify(args);
145+
return this.reflect.callStaticMethod(objId, method, jsonArgs);
146+
}
147+
139148
public static getField(objId: string, field: string): string | null {
140149
return this.reflect.getField(objId, field);
141150
}

ts/src/types/Reflect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export interface Reflect {
66
methodName: string,
77
argsJson: string | null
88
): string | null;
9+
callStaticMethod(
10+
objectId: string,
11+
methodName: string,
12+
argsJson: string | null
13+
): string | null;
914
getField(objectId: string, fieldName: string): string | null;
1015
setField(objectId: string, fieldName: string, value: string): boolean;
1116
createProxy(interfaceName: string, methodsMapJson: string): string | null;

0 commit comments

Comments
 (0)