Example usage for org.springframework.util MethodInvoker getPreparedMethod

List of usage examples for org.springframework.util MethodInvoker getPreparedMethod

Introduction

In this page you can find the example usage for org.springframework.util MethodInvoker getPreparedMethod.

Prototype

public Method getPreparedMethod() throws IllegalStateException 

Source Link

Document

Return the prepared Method object that will be invoked.

Usage

From source file:org.kuali.rice.krad.uif.lifecycle.finalize.InvokeFinalizerTask.java

/**
 * Invokes the finalize method for the component (if configured) and sets the render output for
 * the component to the returned method string (if method is not a void type)
 *//* w ww  .  jav  a2  s .c  om*/
@Override
protected void performLifecycleTask() {
    Component component = (Component) getElementState().getElement();
    String finalizeMethodToCall = component.getFinalizeMethodToCall();
    MethodInvoker finalizeMethodInvoker = component.getFinalizeMethodInvoker();

    if (StringUtils.isBlank(finalizeMethodToCall) && (finalizeMethodInvoker == null)) {
        return;
    }

    if (finalizeMethodInvoker == null) {
        finalizeMethodInvoker = new MethodInvoker();
    }

    // if method not set on invoker, use finalizeMethodToCall, note staticMethod could be set(don't know since
    // there is not a getter), if so it will override the target method in prepare
    if (StringUtils.isBlank(finalizeMethodInvoker.getTargetMethod())) {
        finalizeMethodInvoker.setTargetMethod(finalizeMethodToCall);
    }

    // if target class or object not set, use view helper service
    if ((finalizeMethodInvoker.getTargetClass() == null) && (finalizeMethodInvoker.getTargetObject() == null)) {
        finalizeMethodInvoker.setTargetObject(ViewLifecycle.getHelper());
    }

    // setup arguments for method
    List<Object> additionalArguments = component.getFinalizeMethodAdditionalArguments();
    if (additionalArguments == null) {
        additionalArguments = new ArrayList<Object>();
    }

    Object[] arguments = new Object[2 + additionalArguments.size()];
    arguments[0] = component;
    arguments[1] = ViewLifecycle.getModel();

    int argumentIndex = 1;
    for (Object argument : additionalArguments) {
        argumentIndex++;
        arguments[argumentIndex] = argument;
    }
    finalizeMethodInvoker.setArguments(arguments);

    // invoke finalize method
    try {
        LOG.debug("Invoking finalize method: " + finalizeMethodInvoker.getTargetMethod() + " for component: "
                + component.getId());
        finalizeMethodInvoker.prepare();

        Class<?> methodReturnType = finalizeMethodInvoker.getPreparedMethod().getReturnType();
        if (StringUtils.equals("void", methodReturnType.getName())) {
            finalizeMethodInvoker.invoke();
        } else {
            String renderOutput = (String) finalizeMethodInvoker.invoke();

            component.setSelfRendered(true);
            component.setRenderedHtmlOutput(renderOutput);
        }
    } catch (Exception e) {
        LOG.error("Error invoking finalize method for component: " + component.getId(), e);
        throw new RuntimeException("Error invoking finalize method for component: " + component.getId(), e);
    }
}