Пример #1
0
 private void throwExceptionWithTarget(String m, MBeanException e) {
   if (!ignoreErrors) {
     throw new GroovyRuntimeException(m + e, e.getTargetException());
   }
 }
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    final Class<?> methodClass = method.getDeclaringClass();

    if (methodClass.equals(NotificationBroadcaster.class)
        || methodClass.equals(NotificationEmitter.class))
      return invokeBroadcasterMethod(proxy, method, args);

    // local or not: equals, toString, hashCode
    if (shouldDoLocally(proxy, method)) return doLocally(proxy, method, args);

    try {
      if (isMXBean()) {
        MXBeanProxy p = findMXBeanProxy(methodClass);
        return p.invoke(connection, objectName, method, args);
      } else {
        final String methodName = method.getName();
        final Class<?>[] paramTypes = method.getParameterTypes();
        final Class<?> returnType = method.getReturnType();

        /* Inexplicably, InvocationHandler specifies that args is null
        when the method takes no arguments rather than a
        zero-length array.  */
        final int nargs = (args == null) ? 0 : args.length;

        if (methodName.startsWith("get")
            && methodName.length() > 3
            && nargs == 0
            && !returnType.equals(Void.TYPE)) {
          return connection.getAttribute(objectName, methodName.substring(3));
        }

        if (methodName.startsWith("is")
            && methodName.length() > 2
            && nargs == 0
            && (returnType.equals(Boolean.TYPE) || returnType.equals(Boolean.class))) {
          return connection.getAttribute(objectName, methodName.substring(2));
        }

        if (methodName.startsWith("set")
            && methodName.length() > 3
            && nargs == 1
            && returnType.equals(Void.TYPE)) {
          Attribute attr = new Attribute(methodName.substring(3), args[0]);
          connection.setAttribute(objectName, attr);
          return null;
        }

        final String[] signature = new String[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) signature[i] = paramTypes[i].getName();
        return connection.invoke(
            objectName, methodName,
            args, signature);
      }
    } catch (MBeanException e) {
      throw e.getTargetException();
    } catch (RuntimeMBeanException re) {
      throw re.getTargetException();
    } catch (RuntimeErrorException rre) {
      throw rre.getTargetError();
    }
    /* The invoke may fail because it can't get to the MBean, with
      one of the these exceptions declared by
      MBeanServerConnection.invoke:
      - RemoteException: can't talk to MBeanServer;
      - InstanceNotFoundException: objectName is not registered;
      - ReflectionException: objectName is registered but does not
        have the method being invoked.
      In all of these cases, the exception will be wrapped by the
      proxy mechanism in an UndeclaredThrowableException unless
      it happens to be declared in the "throws" clause of the
      method being invoked on the proxy.
    */
  }