2019-09-07 04:23:15 +00:00
|
|
|
/**
|
|
|
|
* 反射工具类
|
2019-11-04 12:19:50 +00:00
|
|
|
* Created by MiaoWoo on 2017/2/9 0009.
|
2019-09-07 04:23:15 +00:00
|
|
|
*/
|
2019-09-27 10:40:35 +00:00
|
|
|
const JavaClass = Java.type('java.lang.Class');
|
|
|
|
const JavaObject = Java.type('java.lang.Object')
|
|
|
|
const NoSuchFieldException = Java.type('java.lang.NoSuchFieldException');
|
|
|
|
const methodCache = [];
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
class Reflect {
|
|
|
|
private obj: any;
|
|
|
|
private class: any
|
|
|
|
|
|
|
|
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) {
|
|
|
|
this.obj = null;
|
|
|
|
this.class = obj;
|
|
|
|
} else {
|
|
|
|
this.obj = obj;
|
2019-09-21 07:06:17 +00:00
|
|
|
if (obj !== null && obj !== undefined && obj.class) {
|
|
|
|
this.class = obj.class;
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
method(...args: any[]) {
|
|
|
|
return declaredMethod(this.class, args[0], types(args.slice(1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
methods() {
|
|
|
|
return Java.from(declaredMethods(this.class));
|
|
|
|
}
|
|
|
|
|
2019-09-21 07:06:17 +00:00
|
|
|
field(name): Reflect {
|
2019-09-07 04:23:15 +00:00
|
|
|
try {
|
|
|
|
// Try getting a public field
|
2019-09-27 10:40:35 +00:00
|
|
|
let field = this.class.field(name);
|
2019-09-07 04:23:15 +00:00
|
|
|
return on(field.get(this.obj));
|
|
|
|
} catch (ex) {
|
|
|
|
// Try again, getting a non-public field
|
|
|
|
return on(accessible(declaredField(this.class, name)).get(this.obj));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-21 07:06:17 +00:00
|
|
|
fields(declared = false) {
|
2019-09-07 04:23:15 +00:00
|
|
|
return Java.from(declared ? this.class.declaredFields : this.class.fields);
|
|
|
|
}
|
|
|
|
|
2019-09-21 07:06:17 +00:00
|
|
|
values(declared = false) {
|
2019-09-27 10:40:35 +00:00
|
|
|
let cache = {};
|
2019-09-21 07:06:17 +00:00
|
|
|
this.fields(declared).forEach(fed => cache[fed.name] = this.field(fed.name).get())
|
2019-09-07 04:23:15 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2019-09-21 07:06:17 +00:00
|
|
|
call(...args): Reflect {
|
2019-09-27 10:40:35 +00:00
|
|
|
let params = args.slice(1);
|
|
|
|
let method = accessible(declaredMethod(this.class, args[0], types(params)));
|
2019-09-21 07:06:17 +00:00
|
|
|
let result = method.invoke(this.get(), params);
|
|
|
|
return result && on(result);
|
2019-09-07 04:23:15 +00:00
|
|
|
};
|
|
|
|
|
2019-09-21 07:06:17 +00:00
|
|
|
get(...args): Reflect | any {
|
2019-09-07 04:23:15 +00:00
|
|
|
return args.length === 1 ? this.field(args[0]) : this.obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
// noinspection JSUnusedGlobalSymbols
|
2019-09-21 07:06:17 +00:00
|
|
|
set(name, value): Reflect {
|
2019-09-07 04:23:15 +00:00
|
|
|
accessible(declaredField(this.class, name)).set(this.obj, value);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2019-09-21 07:06:17 +00:00
|
|
|
create(...args): Reflect {
|
2019-09-07 04:23:15 +00:00
|
|
|
return on(declaredConstructor(this.class, args).newInstance(args));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of types for an array of objects
|
|
|
|
*/
|
|
|
|
function types(values, def?) {
|
|
|
|
if (values === null) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-09-27 10:40:35 +00:00
|
|
|
let result: any[] = [];
|
2019-09-07 04:23:15 +00:00
|
|
|
values.forEach(t => result.push((t || def) ? JavaObject.class : t instanceof JavaClass ? t : t.class));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function accessible(accessible) {
|
|
|
|
if (accessible === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!accessible.isAccessible()) {
|
|
|
|
accessible.setAccessible(true);
|
|
|
|
}
|
|
|
|
return accessible;
|
|
|
|
}
|
|
|
|
|
|
|
|
function declaredConstructor(clazz, param) {
|
2019-09-27 10:40:35 +00:00
|
|
|
let constructor;
|
2019-09-07 04:23:15 +00:00
|
|
|
try {
|
|
|
|
constructor = clazz.getDeclaredConstructor(types(param));
|
|
|
|
} catch (ex) {
|
|
|
|
try {
|
|
|
|
constructor = clazz.getDeclaredConstructor(types(param, true));
|
|
|
|
} catch (ex) {
|
|
|
|
constructor = clazz.getDeclaredConstructors()[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accessible(constructor);
|
|
|
|
}
|
|
|
|
|
|
|
|
function declaredField(clazz, name) {
|
2019-11-04 12:19:50 +00:00
|
|
|
if (!clazz) { throw Error(`target class can't be ${clazz}!`) }
|
|
|
|
let target = clazz;
|
2019-09-27 10:40:35 +00:00
|
|
|
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) {
|
2019-09-07 04:23:15 +00:00
|
|
|
try {
|
2019-11-04 12:19:50 +00:00
|
|
|
field = target.getDeclaredField(name);
|
|
|
|
if (field !== null) { break; }
|
2019-09-07 04:23:15 +00:00
|
|
|
} catch (e) {
|
2019-11-04 12:19:50 +00:00
|
|
|
if (target === undefined) { break; }
|
|
|
|
target = target.getSuperclass();
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (field === null) {
|
|
|
|
throw new NoSuchFieldException(name + " is not found in " + clazz.name);
|
|
|
|
}
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
|
|
|
function declaredMethod(clazz, name, clazzs) {
|
2019-09-27 10:40:35 +00:00
|
|
|
let key = clazz.name + '.' + name + ':' + (clazzs || []).join(':');
|
2019-09-07 04:23:15 +00:00
|
|
|
if (!methodCache[key]) {
|
|
|
|
try {
|
|
|
|
methodCache[key] = clazz.getMethod(name, clazzs);
|
|
|
|
} catch (ex) {
|
2019-09-21 07:06:17 +00:00
|
|
|
try {
|
|
|
|
methodCache[key] = clazz.getDeclaredMethod(name, clazzs);
|
|
|
|
} catch (ex) {
|
|
|
|
for (const m of Java.from(declaredMethods(clazz))) {
|
|
|
|
if (m.name == name) {
|
|
|
|
methodCache[key] = m;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return methodCache[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
function declaredMethods(clazz) {
|
|
|
|
return clazz.declaredMethods;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:40:35 +00:00
|
|
|
let classMethodsCache: any[] = [];
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
function mapToObject(javaObj) {
|
|
|
|
if (!javaObj || !javaObj.class) { throw new TypeError(`参数 ${javaObj} 不是一个Java对象!`) }
|
2019-09-27 10:40:35 +00:00
|
|
|
let target = {};
|
2019-09-07 04:23:15 +00:00
|
|
|
getJavaObjectMethods(javaObj).forEach(t => mapMethod(target, javaObj, t));
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJavaObjectMethods(javaObj) {
|
2019-09-27 10:40:35 +00:00
|
|
|
let className = javaObj.class.name;
|
2019-09-07 04:23:15 +00:00
|
|
|
if (!classMethodsCache[className]) {
|
2019-09-27 10:40:35 +00:00
|
|
|
let names: any[] = [];
|
|
|
|
let methods = javaObj.class.methods;
|
|
|
|
for (let i in methods) {
|
2019-09-07 04:23:15 +00:00
|
|
|
names.push(methods[i].name);
|
|
|
|
}
|
|
|
|
classMethodsCache[className] = names;
|
|
|
|
}
|
|
|
|
return classMethodsCache[className];
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapMethod(target, source, name) {
|
|
|
|
target[name] = function __SimpleDynamicMethod__(...args) {
|
|
|
|
if (args.length > 0) {
|
|
|
|
return source[name](args);
|
|
|
|
} else {
|
|
|
|
return source[name]();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function on(obj) {
|
2019-09-21 07:06:17 +00:00
|
|
|
// if (!obj || !obj.class) { throw new TypeError(`参数 ${obj} 不是一个Java对象!`) }
|
2019-09-07 04:23:15 +00:00
|
|
|
return new Reflect(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
export = {
|
|
|
|
on,
|
|
|
|
accessible,
|
|
|
|
declaredMethods,
|
|
|
|
mapToObject
|
|
|
|
};
|