private Object invokeMethod(Object target, String methodName, Class[] params, Object[] args)
      throws MBeanException, ReflectionException {
    // First try on this instance, then on the target
    Object realTarget = null;
    Method method = null;
    try {
      realTarget = this;
      method = realTarget.getClass().getMethod(methodName, params);
    } catch (NoSuchMethodException x) {
      realTarget = target;
    }

    if (realTarget == null) {
      throw new MBeanException(new ServiceNotFoundException(
          "Could not find target"));
    }

    if (method == null) {
      try {
        method = realTarget.getClass().getMethod(methodName, params);
      } catch (NoSuchMethodException x) {
        throw new ReflectionException(x);
      }
    }

    try {
      Object value = method.invoke(realTarget, args);
      Logger logger = getLogger();
      if (logger.isEnabledFor(Logger.DEBUG)) {
        logger.debug("Method invocation returned value: " + value);
      }
      return value;
    } catch (IllegalAccessException x) {
      throw new ReflectionException(x);
    } catch (IllegalArgumentException x) {
      throw new MBeanException(x);
    } catch (InvocationTargetException x) {
      Throwable t = x.getTargetException();
      if (t instanceof Error) {
        throw new MBeanException(new RuntimeErrorException((Error) t));
      } else {
        throw new MBeanException((Exception) t);
      }
    }
  }
@author <a href="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
@version $Revision: 1.14 $
geode-core/src/main/java/org/apache/geode/admin/jmx/internal/MX4JModelMBean.java