2020-06-22 02:44:17 +00:00
|
|
|
/// <reference types="@javatypes/jdk" />
|
2020-06-02 10:01:56 +00:00
|
|
|
|
|
|
|
const JavaClass = Java.type('java.lang.Class')
|
2019-09-27 10:40:35 +00:00
|
|
|
const JavaObject = Java.type('java.lang.Object')
|
2020-06-02 10:01:56 +00:00
|
|
|
const NoSuchFieldException = Java.type('java.lang.NoSuchFieldException')
|
2020-10-13 01:24:58 +00:00
|
|
|
const fieldCache = new Map<string, java.lang.reflect.Field>()
|
|
|
|
const methodCache = new Map<string, java.lang.reflect.Method>()
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-10-13 01:24:58 +00:00
|
|
|
/**
|
|
|
|
* 反射工具类
|
|
|
|
* Created by MiaoWoo on 2017/2/9 0009.
|
|
|
|
*/
|
2019-09-07 04:23:15 +00:00
|
|
|
class Reflect {
|
2020-06-02 10:01:56 +00:00
|
|
|
private obj: java.lang.Object
|
2020-06-22 02:44:17 +00:00
|
|
|
private class: java.lang.Class<any>
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
constructor(obj: any) {
|
2019-11-04 12:19:50 +00:00
|
|
|
// if (obj === undefined || obj === null) { throw Error(`reflect object can't be ${obj}!`) }
|
2019-09-07 04:23:15 +00:00
|
|
|
if (obj instanceof JavaClass) {
|
2020-06-02 10:01:56 +00:00
|
|
|
this.obj = null
|
|
|
|
this.class = obj
|
2019-09-07 04:23:15 +00:00
|
|
|
} else {
|
2020-06-02 10:01:56 +00:00
|
|
|
this.obj = obj
|
2019-09-21 07:06:17 +00:00
|
|
|
if (obj !== null && obj !== undefined && obj.class) {
|
2020-06-02 10:01:56 +00:00
|
|
|
this.class = obj.class
|
2019-09-21 07:06:17 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 02:44:17 +00:00
|
|
|
method(name: string, ...args: java.lang.Class<any>[]) {
|
|
|
|
return declaredMethod(this.class, name, ...args)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
methods() {
|
2020-06-02 10:01:56 +00:00
|
|
|
return Java.from(declaredMethods(this.class))
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:24:58 +00:00
|
|
|
field(nameOrIndex: string | java.lang.String | number, declared = false): java.lang.reflect.Field {
|
|
|
|
if (nameOrIndex == undefined || nameOrIndex == null) throw new Error(`reflect field name can't be ${nameOrIndex} from ${this.class.getName()}!`)
|
|
|
|
let key = this.class.getName() + ':' + nameOrIndex + ':' + declared
|
|
|
|
if (fieldCache.has(key)) {
|
|
|
|
return fieldCache.get(key)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2020-10-13 01:24:58 +00:00
|
|
|
let field = null
|
|
|
|
if (typeof nameOrIndex == "number") {
|
|
|
|
field = this.fields(declared)[nameOrIndex]
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
// Try getting a public field
|
|
|
|
field = this.class.getField(nameOrIndex)
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
2020-10-13 01:24:58 +00:00
|
|
|
// Try again, getting a non-public field
|
|
|
|
field = declaredField(this.class, nameOrIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!field) throw new Error(`can't reflect field ${typeof nameOrIndex == "number" ? 'index' : 'name'} ${nameOrIndex} from ${this.class.getName()}!`)
|
2022-07-06 13:57:36 +00:00
|
|
|
fieldCache.set(key, field)
|
2020-10-13 01:24:58 +00:00
|
|
|
return accessible(field)
|
2020-06-02 10:01:56 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-06-02 10:01:56 +00:00
|
|
|
fields(declared = false): java.lang.reflect.Field[] {
|
|
|
|
return Java.from(declared ? this.class.getDeclaredFields() : this.class.getFields())
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 07:06:17 +00:00
|
|
|
values(declared = false) {
|
2020-10-13 01:24:58 +00:00
|
|
|
return this.fields(declared).reduce((cache, field) => { return cache[field.getName()] = this.get(field.getName()).get() }, {}) as any
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 10:01:56 +00:00
|
|
|
call(...args: any[]): Reflect {
|
|
|
|
let params = args.slice(1)
|
|
|
|
let method = accessible(declaredMethod(this.class, args[0], ...types(params)))
|
|
|
|
let result = method.invoke(this.get(), params)
|
|
|
|
return result && on(result)
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-06-02 10:01:56 +00:00
|
|
|
get(): any
|
|
|
|
get(index: number, declared?: boolean): Reflect
|
|
|
|
get(prop: string): Reflect
|
|
|
|
get(param?: string | number, declared: boolean = true): Reflect | any {
|
2020-10-13 01:24:58 +00:00
|
|
|
if (arguments.length === 0) return this.obj
|
|
|
|
return on(this.field(param, declared).get(this.obj))
|
2020-06-02 10:01:56 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-10-13 01:24:58 +00:00
|
|
|
set(param: string | number, value: any, declared: boolean = true): Reflect {
|
|
|
|
this.field(param, declared).set(this.obj, value)
|
2020-06-02 10:01:56 +00:00
|
|
|
return this
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2019-09-21 07:06:17 +00:00
|
|
|
create(...args): Reflect {
|
2020-06-02 10:01:56 +00:00
|
|
|
return on(declaredConstructor(this.class, args).newInstance(args))
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of types for an array of objects
|
|
|
|
*/
|
2020-06-02 10:01:56 +00:00
|
|
|
function types(values: any[], def?: any) {
|
2019-09-07 04:23:15 +00:00
|
|
|
if (values === null) {
|
2020-06-02 10:01:56 +00:00
|
|
|
return []
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2020-06-22 02:44:17 +00:00
|
|
|
let result: java.lang.Class<any>[] = []
|
2020-06-02 10:01:56 +00:00
|
|
|
values.forEach(t => result.push((t || def) ? JavaObject.class : t instanceof JavaClass ? t : t.class))
|
|
|
|
return result
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 10:01:56 +00:00
|
|
|
function accessible<T extends java.lang.reflect.AccessibleObject>(accessible: T): T {
|
2020-07-11 07:08:02 +00:00
|
|
|
if (!accessible?.isAccessible()) {
|
2020-06-02 10:01:56 +00:00
|
|
|
accessible.setAccessible(true)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2020-06-02 10:01:56 +00:00
|
|
|
return accessible
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function declaredConstructor(clazz, param) {
|
2020-06-02 10:01:56 +00:00
|
|
|
let constructor
|
2019-09-07 04:23:15 +00:00
|
|
|
try {
|
2020-06-02 10:01:56 +00:00
|
|
|
constructor = clazz.getDeclaredConstructor(types(param))
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
2019-09-07 04:23:15 +00:00
|
|
|
try {
|
2020-06-02 10:01:56 +00:00
|
|
|
constructor = clazz.getDeclaredConstructor(types(param, true))
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
2020-06-02 10:01:56 +00:00
|
|
|
constructor = clazz.getDeclaredConstructors()[0]
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 10:01:56 +00:00
|
|
|
return accessible(constructor)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 02:44:17 +00:00
|
|
|
function declaredField(clazz: java.lang.Class<any>, name: string | java.lang.String) {
|
2019-11-04 12:19:50 +00:00
|
|
|
if (!clazz) { throw Error(`target class can't be ${clazz}!`) }
|
2020-06-02 10:01:56 +00:00
|
|
|
let target = clazz
|
|
|
|
let field = null
|
2019-09-07 04:23:15 +00:00
|
|
|
// noinspection JSUnresolvedVariable
|
2019-11-04 12:19:50 +00:00
|
|
|
while (target !== JavaObject.class) {
|
2022-06-19 16:48:00 +00:00
|
|
|
console.debug(`reflect field ${name} from ${target.getName()}`)
|
2019-09-07 04:23:15 +00:00
|
|
|
try {
|
2020-06-02 10:01:56 +00:00
|
|
|
field = target.getDeclaredField(name)
|
|
|
|
if (field !== null) { break }
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (error: any) {
|
2020-06-02 10:01:56 +00:00
|
|
|
if (target === undefined) { break }
|
|
|
|
target = target.getSuperclass()
|
2022-06-19 16:48:00 +00:00
|
|
|
console.debug(`切换到超类: ${target.getName()}`)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (field === null) {
|
2020-06-02 10:01:56 +00:00
|
|
|
throw new NoSuchFieldException(name + " is not found in " + clazz.getName())
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2020-06-02 10:01:56 +00:00
|
|
|
return field
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:24:58 +00:00
|
|
|
function declaredMethod(clazz: java.lang.Class<any>, nameOrIndex: string | number, ...clazzs: java.lang.Class<any>[]): java.lang.reflect.Method {
|
|
|
|
let key = clazz.getName() + '.' + nameOrIndex + ':' + (clazzs || []).map(c => c.getName()).join(':')
|
|
|
|
if (methodCache.has(key)) { return methodCache.get(key) }
|
2022-06-19 16:48:00 +00:00
|
|
|
let target = clazz
|
2020-10-13 01:24:58 +00:00
|
|
|
if (typeof nameOrIndex === "number") {
|
|
|
|
methodCache.set(key, declaredMethods(clazz)[nameOrIndex])
|
|
|
|
} else {
|
2022-06-19 16:48:00 +00:00
|
|
|
while (target !== JavaObject.class && !methodCache.has(key)) {
|
2019-09-21 07:06:17 +00:00
|
|
|
try {
|
2022-06-19 16:48:00 +00:00
|
|
|
console.debug(`reflect method ${typeof nameOrIndex == "number" ? 'index' : 'name'} ${nameOrIndex} from ${target.getName()}`)
|
|
|
|
try {
|
|
|
|
methodCache.set(key, target.getMethod(nameOrIndex, clazzs as any))
|
|
|
|
} catch (ex: any) {
|
|
|
|
try {
|
|
|
|
methodCache.set(key, target.getDeclaredMethod(nameOrIndex, clazzs as any))
|
|
|
|
} catch (ex: any) {
|
|
|
|
for (const m of Java.from(declaredMethods(target))) {
|
|
|
|
if (m.getName() == nameOrIndex) {
|
|
|
|
methodCache.set(key, m)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error(`method ${typeof nameOrIndex == "number" ? 'index' : 'name'} ${nameOrIndex} not found.`)
|
2019-09-21 07:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-19 16:48:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (target === undefined) { break }
|
|
|
|
target = target.getSuperclass()
|
|
|
|
console.debug(`切换到超类: ${target.getName()}`)
|
2019-09-21 07:06:17 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-13 01:24:58 +00:00
|
|
|
if (!methodCache.has(key)) throw new Error(`can't reflect method ${typeof nameOrIndex == "number" ? 'index' : 'name'} ${nameOrIndex} from ${clazz.getName()}!`)
|
|
|
|
return methodCache.get(key)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 02:44:17 +00:00
|
|
|
function declaredMethods(clazz: java.lang.Class<any>) {
|
|
|
|
return clazz.getDeclaredMethods()
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapToObject(javaObj) {
|
2020-10-13 01:24:58 +00:00
|
|
|
if (!Java.isJavaObject(javaObj)) { throw new TypeError(`参数 ${javaObj} 不是一个Java对象!`) }
|
|
|
|
let target = Proxy.newProxy(javaObj, {
|
|
|
|
apply: (target, name, args) => { return args ? javaObj[name](args) : javaObj[name]() }
|
|
|
|
})
|
2020-06-02 10:01:56 +00:00
|
|
|
return target
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function on(obj) {
|
2019-09-21 07:06:17 +00:00
|
|
|
// if (!obj || !obj.class) { throw new TypeError(`参数 ${obj} 不是一个Java对象!`) }
|
2020-06-02 10:01:56 +00:00
|
|
|
return new Reflect(obj)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export = {
|
|
|
|
on,
|
|
|
|
accessible,
|
|
|
|
declaredMethods,
|
|
|
|
mapToObject
|
2020-04-01 03:08:15 +00:00
|
|
|
}
|