Beispiel #1
0
  /**
   * Generates a new error message based on the excpetion message.
   *
   * @param message the rror message
   */
  public void generateErrorMessage(String message, MethodDescription mDesc) {

    ObjectDescription oDesc = getObjectDescription(getObject(mDesc.getObjectID()));
    String methodName = oDesc.getName() + "." + mDesc.getMethodName() + "()";

    System.err.println("Method \"" + methodName + "\" can't be invoked:" + message);
  }
Beispiel #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)
  }