如何获取调用方法的参数列表?

时间:2014-07-08 13:50:04

标签: java reflection

我有一个名为xyz的类,其中包含10个具有不同参数的方法 现在我想使用Reflection

来调用运行时方法
Class docClass = Class.forName(this.getClass().getCanonicalName().toString());
Method method = getClass().getDeclaredMethod(requestObject.getMethod().toString());

它给了我异常,因为参数不是唯一的,方法7包含1个参数,2个参数(参数),1个参数3个。

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

getDeclaredMethod的方法Class将方法的名称作为第一个参数,将Class<?>的变量作为parameterTypes

由于它是一个变种,你不需要指定任何参数(正如你在问题文本中所做的那样)。

但是,不指定任何参数将意味着引用一个不带参数的方法。

您应该在requestObject个实例中添加一些,以便您可以将其表示为array Class<?>

由于您必须针对每个(我假设的)Class.forName表示进行String次调用,因此它会非常繁琐。

然而,您可以为该方法获得正确的重载。

答案 1 :(得分:0)

听起来你想要Object callMethod(Object receiver, String methodName, Object... parameters)。碰巧的是,我在blog here上写了一篇关于我的文章。我看起来像这样,

/**
 * <b>Rationale:</b> Provide a mechanism to generically call
 * a method.
 * 
 * @author Elliott Frisch
 */
public class MethodUtil {
  /**
   * Returns the size (or length) of an array.
   * 
   * @param obj
   *          The Array to find the size of .
   * @return The length of the array (if object is an
   *         array), or 0.
   */
  private static int safeSize(Object obj) {
    if (obj != null) {
      return Array.getLength(obj);
    }
    return 0;
  }

  /**
   * If it exists, invoke methodName on receiver - passing
   * parameters (if they exist) as arguments. If
   * receiver.methodName(parameters) returns, return the
   * returned value.
   * 
   * @param receiver
   *          The receiver to invoke.
   * @param methodName
   *          The name of the method to call.
   * @param parameters
   *          The arguments to pass to the method.
   * @return The value returned from invoking methodName on
   *         receiver.
   * @throws Exception
   *           Any Exception thrown by invoking the method
   *           with the passed parameters.
   */
  public static Object callMethod(Object receiver,
      String methodName, Object... parameters)
      throws Exception {
    if (receiver == null || methodName == null) {
      return null;
    }
    methodName = methodName.trim();
    if (methodName.length() == 0) {
      return null;
    }
    Class<?> cls = receiver.getClass();
    Method toInvoke = null;
    outer: for (Method method : cls.getMethods()) {
      if (!methodName.equals(method.getName())) {
        continue;
      }
      Class<?>[] mTypes = method.getParameterTypes();
      if (parameters == null && mTypes == null) {
        toInvoke = method;
        break;
      } else if (safeSize(mTypes) == 0 || safeSize(parameters) == 0) {
        continue;
      } else if (safeSize(mTypes) != safeSize(parameters)) {
        continue;
      }

      for (int i = 0; i < mTypes.length; ++i) {
        if (!mTypes[i].isAssignableFrom(parameters[i]
            .getClass())) {
          continue outer;
        }
      }
      toInvoke = method;
      break;
    }
    if (toInvoke != null) {
      try {
        return toInvoke.invoke(receiver, parameters);
      } catch (Exception t) {
        throw t;
      }
    }
    return null;
  }

  /**
   * A small sample program. Prints 100. Prints 200.
   * 
   * @param args
   */
  public static void main(String[] args) {
    try {
      BigDecimal a = new BigDecimal("99");
      a = (BigDecimal) callMethod(a, "add",
          new BigDecimal("1"));
      System.out.println(a);
      System.out.println(callMethod(a, "multiply",
          new BigDecimal("2")));
      System.out.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
相关问题