예제 #1
0
  /**
   * Invokes a method.
   *
   * @param o the object of the method that is to be invoked
   * @param methodName name of the method that is to be invoked
   * @param params all of the methods parameters. If params consists of class objects the parameter
   *     values from the gui will be used (TOTO isn't there a cleaner way?)
   * @return the return value of the method
   */
  private Object invoke(Object o, String methodName, Object... params)
      throws InvocationTargetException {

    ArrayList<Class> paramTypes = new ArrayList<Class>();

    for (Object p : params) {
      Class c = p.getClass();
      paramTypes.add(c);
    }

    MethodDescription methodDescription =
        this.getMethodDescription(o, methodName, paramTypes.toArray(new Class[] {}));

    if (methodDescription != null) {
      this.invoke(methodDescription);

      return methodDescription.getReturnValue();
    }

    return null;
  }
예제 #2
0
  /**
   * Invokes the method specified by a method description.
   *
   * @param methodDescription the method description of the method that is to be invoked.
   */
  protected void invoke(MethodDescription methodDescription) throws InvocationTargetException {

    VParamUtil.throwIfNull(methodDescription);

    Object o = getObject(methodDescription.getObjectID());

    ObjectDescription oDesc = getObjectDescription(o);

    if (o == null) {
      throw new IllegalStateException("Object must not be null!");
    }

    if (methodDescription.getMethodType() == MethodType.REFERENCE) {

      Object[] parameters = methodDescription.getParameters();

      // error handling
      if (parameters.length > 1) {
        throw new IllegalArgumentException(
            "More than one parameter " + "for reference methods is not allowed!");
      }

      if (parameters.length > 0 && parameters[0] != null) {
        methodDescription.setReturnValue(parameters[0]);
      } else {
        methodDescription.setReturnValue(o);
      }

    } else if (o instanceof ProxyObject) {
      ProxyObject proxy = (ProxyObject) o;
      methodDescription.setReturnValue(proxy.invoke(methodDescription));
      if (!methodDescription
          .getReturnType()
          .equals(methodDescription.getReturnValue().getClass())) {
        System.err.println(">> ProxyObject: wrong type of return value!");
        generateErrorMessage(">> ProxyObject:" + " wrong type of return value!", methodDescription);
      }
    } else if (methodDescription.getMethodType() == MethodType.DEFAULT
        || methodDescription.getMethodType() == MethodType.CUSTOM_REFERENCE) {
      Method m = null;

      try {
        m =
            o.getClass()
                .getMethod(
                    methodDescription.getMethodName(), methodDescription.getParameterTypes());

        // access this method even if language visibility forbids
        // invocation of this method
        m.setAccessible(true);

        methodDescription.setReturnValue(m.invoke(o, methodDescription.getParameters()));

      } catch (NoSuchMethodException ex) {
        Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex);
        generateErrorMessage(
            methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription);
      } catch (IllegalAccessException ex) {
        Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex);
        generateErrorMessage(
            methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription);
      } catch (IllegalArgumentException ex) {
        System.err.println(">> ObjectInspector:" + " invoked method with wrong arguments!");

        String yourParamStr = "";
        String expectedParamStr = "";

        if (methodDescription.getParameters() != null) {

          for (int i = 0; i < methodDescription.getParameters().length; i++) {
            Object parameter = methodDescription.getParameters()[i];
            if (parameter != null) {
              yourParamStr += parameter.getClass().getName() + " ";
            } else {
              yourParamStr += "null ";
            }
          }
        } else {
          yourParamStr = "no parameters given!";
        }

        for (int i = 0; i < m.getParameterTypes().length; i++) {
          expectedParamStr += m.getParameterTypes()[i].getName() + " ";
        }

        System.err.println("   your parameters:     " + yourParamStr);
        System.err.println("   expected parameters: " + expectedParamStr);

        generateErrorMessage(
            methodDescription.getMethodName() + "(): method invoked with wrong arguments!",
            methodDescription);

      } catch (InvocationTargetException ex) {
        Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex);
        generateErrorMessage(methodDescription, ex.getCause());
        throw ex;

      } catch (SecurityException ex) {
        Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex);
        generateErrorMessage(
            methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription);
      }
    } // end else if (o instanceof ProxyObject)
  }