@@ -10,31 +10,58 @@ import java.lang.reflect.Method
1010object Utils {
1111
1212 enum class MethodTypes { OBJECT , STATIC }
13- class Empty { companion object { val instance = Empty () } }
1413
1514 /* *
1615 * 直接对指定对象 new 来获得 instance
1716 */
18- fun findClass (script : Script , target : String ): Class <* > {
17+ fun findClass (script : Script , target : String ): Class <* >? {
1918 return try {
2019 Class .forName(target)
2120 } catch (e: ClassNotFoundException ) {
2221 e.printStackTrace()
2322 FastScript .main.sendConsoleMessage(" &7[&2Fast&aScript&7] &cERROR &8| &c脚本 &4${script.name} 没有找到类 ${target} , 依赖于该类的方法将不会起作用!" )
24- Empty .instance.javaClass
23+ null
2524 }
2625 }
2726
27+ /* *
28+ * 对指定动态对象执行某个方法并返回结果
29+ */
2830 fun getObjectMethodResults (script : Script , clazz : Class <* >, methodName : String , args : Array <Any ?> = arrayOf()): Any? {
31+ return getObjectMethodResults(script, clazz, arrayOf(), methodName, args)
32+ }
33+
34+ /* *
35+ * 对指定动态对象执行某个方法并返回结果
36+ */
37+ fun getObjectMethodResults (script : Script , clazz : Class <* >, initArgs : Array <Any ?>, methodName : String , args : Array <Any ?> = arrayOf()): Any? {
2938 val method = clazz.getMethod(methodName)
30- return getMethodResults(script, method, clazz.newInstance(), args)
39+ val constructor = clazz.getDeclaredConstructor()
40+ constructor .isAccessible = true
41+ return getMethodResults(script, method, constructor .newInstance(initArgs), args)
3142 }
3243
44+ /* *
45+ * 对指定动态对象执行某个方法并返回结果
46+ * 给定指定对象
47+ */
48+ fun getObjectMethodResults (script : Script , objects : Any? , methodName : String , args : Array <Any ?> = arrayOf()): Any? {
49+ if (objects == null ) return null
50+ val method = objects.javaClass.getMethod(methodName)
51+ return getMethodResults(script, method, objects, args)
52+ }
53+
54+ /* *
55+ * 对指定静态对象执行某个方法并返回结果
56+ */
3357 fun getStaticMethodResults (script : Script , clazz : Class <* >, methodName : String , args : Array <Any ?> = arrayOf()): Any? {
3458 val method = clazz.getMethod(methodName)
3559 return getMethodResults(script, method, null , args)
3660 }
3761
62+ /* *
63+ * 对指定对象执行某个方法并返回结果
64+ */
3865 private fun getMethodResults (script : Script , method : Method , obj : Any? , args : Array <Any ?>): Any? {
3966 try {
4067 return method.invoke(obj, args)
0 commit comments