一段动态获得和执行方法的代码
2004-10-12 05:37
375 查看
/** * Override to run the test and assert its state. * @exception Throwable if any exception is thrown */ protected void runTest() throws Throwable { assertNotNull(fName); Method runMethod= null; try { // use getMethod to get all public inherited // methods. getDeclaredMethods returns all // methods of this class but excludes the // inherited ones. runMethod= getClass().getMethod(fName, null); } catch (NoSuchMethodException e) { fail("Method /""+fName+"/" not found"); } if (!Modifier.isPublic(runMethod.getModifiers())) { fail("Method /""+fName+"/" should be public"); } try { runMethod.invoke(this, new Class[0]); } catch (InvocationTargetException e) { e.fillInStackTrace(); throw e.getTargetException(); } catch (IllegalAccessException e) { e.fillInStackTrace(); throw e; } }
在看到这段代码之前,根本就没有想到Class会有getMethod这个方法,更没有想到会这么用,而且用得如此之妙。
相关文章推荐
- 动态执行javascript代码的几种方法介绍
- Unity动态载入可执行代码的方法
- Java实现等待所有子线程结束后再执行一段代码的方法
- java设置一段代码执行的超时时间的简单方法 间接实现获取Connection超时问题
- 测试一段程序代码执行时间的方法
- delphi如何获得一段代码执行消耗的时间
- C# 反射获得类名,私有方法,动态执行方法,返回多个值
- java设置一段代码执行的超时时间的简单方法 间接实现获取Connection超时问题
- JQuery动态执行javascript代码的方法
- 整理一下Java动态编译Java代码,并在加载到内存中然后执行类中方法的api的介绍
- 测试一段程序代码执行时间的方法
- 获得spring的指定目标对象,执行指定方法(JDK动态代理,cglib动态代理,Dubbo-Javassist代理)
- java中动态执行一段代码
- PHP实现动态执行代码的方法
- mach_absolute_time 快速容易的方法来检测一段ios 代码执行的效率函数
- PHP实现动态执行代码的方法
- C#代码在后台动态执行前台脚本的几种方法
- 关于js动态插入script 标签后页面JS代码执行失败的解决方法
- 动态跟踪Java代码的执行状况工具--BTrace
- MFC怎样测试一段代码执行所用时间?