一段动态获得和执行方法的代码

博客涉及异常(exception)、类(class)、方法(methods)和空值(null)等信息技术相关内容,但具体细节因内容缺失暂不明确。这些元素在编程中较为常见,异常处理可保障程序稳定性,类和方法是面向对象编程基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/**
  * 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这个方法,更没有想到会这么用,而且用得如此之妙。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值