Example usage for org.apache.commons.beanutils MethodUtils invokeMethod

List of usage examples for org.apache.commons.beanutils MethodUtils invokeMethod

Introduction

In this page you can find the example usage for org.apache.commons.beanutils MethodUtils invokeMethod.

Prototype

public static Object invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 

Source Link

Document

Invoke a named method whose parameter type matches the object type.

The behaviour of this method is less deterministic than #invokeExactMethod(Object object,String methodName,Object[] args,Class[] parameterTypes) .

Usage

From source file:com.clican.pluto.dataprocess.dpl.function.impl.BaseParamSingleRowFunction.java

public Object calculate(Map<String, Object> row) throws CalculationException, PrefixAndSuffixException {
    Object[] arg = new Object[this.pasList.size()];
    Class<?>[] classType = new Class<?>[this.pasList.size()];
    for (int i = 0; i < pasList.size(); i++) {
        arg[i] = pasList.get(i).getValue(row);
        if (arg[i] != null) {
            classType[i] = arg[i].getClass();
        } else {/*from w ww. ja v  a 2  s . c om*/
            classType[i] = Object.class;
        }
    }
    try {
        Object result = MethodUtils.invokeMethod(this, CALCULATE_METHOD_NAME, arg, classType);
        return result;
    } catch (Exception e) {
        throw new CalculationException(e);
    }
}

From source file:com.qcadoo.view.internal.hooks.HookDefinitionImpl.java

private Object call(final Object[] params, final Class<?>[] paramClasses) {
    try {//from   w w w  .j  a  v  a 2 s . c  o  m
        return MethodUtils.invokeMethod(bean, methodName, params, paramClasses);
    } catch (NoSuchMethodException e) {
        LOG.warn("custom validation method is not exist", e);
    } catch (IllegalAccessException e) {
        LOG.warn("problem while calling custom validation method", e);
    } catch (InvocationTargetException e) {
        LOG.warn("problem while calling custom validation method", e);
    } catch (ClassCastException e) {
        LOG.warn("custom validation method has returned not boolean type", e);
    }
    return null;
}

From source file:jp.terasoluna.fw.service.thin.AbstractBLogicMapper.java

/**
 * rWlX?WbN?JavaBean?A//from w  ww. j a v  a2s . co  m
 * WebwIuWFNgi[lJavaBean}bsO?B
 * blogic-ioblogic-params}bsO?B
 * 
 * @param request
 * @param response
 * @param io
 * @return rWlX?WbN?JavaBean
 */
protected Object setParams(HttpServletRequest request, HttpServletResponse response, BLogicIO io) {

    // Ce?[^?[??
    Iterator it = io.getBLogicParams().iterator();

    // lJavaBean??
    Object bean = null;
    // blogic-ioblogic-params???CX^X??
    if (it.hasNext()) {
        try {
            bean = ClassUtil.create(io.getInputBeanName());
        } catch (ClassLoadException e) {
            log.error("bean creation failure.");
            throw new SystemException(e, ERROR_BEAN_CREATE);
        }
    } else {
        return null;
    }

    // ListSBLogicPropertyCX^X?A???s
    while (it.hasNext()) {

        // BLogicPropertyCX^X
        BLogicProperty property = (BLogicProperty) it.next();
        // v?peB
        String propertyName = property.getProperty();

        // rWlX?WbNgpv?peB
        String blogicPropertyName = property.getBLogicProperty();

        // blogicPropertyName????ApropertyName
        if (blogicPropertyName == null) {
            blogicPropertyName = propertyName;
        }

        // li[?s?AJavaBeani[
        if (!"".equals(property.getSource()) && (property.getSource() != null)) {
            // Nl??\bh?z
            Object[] methodParams = new Object[] { propertyName, request, response };
            Class[] parameterTypes = new Class[] { String.class, HttpServletRequest.class,
                    HttpServletResponse.class };
            Object value = null;
            try {
                // l???s
                value = MethodUtils.invokeMethod(this,
                        "getValueFrom" + StringUtil.capitalizeInitial(property.getSource()), methodParams,
                        parameterTypes);
            } catch (NoSuchMethodException e) {
                log.error("no such method.");
                throw new SystemException(new BLogicMapperException(e), ERROR_GETVALUE,
                        new String[] { propertyName });
            } catch (IllegalAccessException e) {
                log.error("illegal access to the method.");
                throw new SystemException(new BLogicMapperException(e), ERROR_GETVALUE,
                        new String[] { propertyName });
            } catch (InvocationTargetException e) {
                log.error("exception is thrown out by invokeMethod.");
                throw new SystemException(new BLogicMapperException(e), ERROR_GETVALUE,
                        new String[] { propertyName });
            }

            try {
                // l??\bh?s
                BeanUtil.setBeanProperty(bean, blogicPropertyName, value);
            } catch (PropertyAccessException e) {
                log.error("setBeanProperty failure.");
                throw new SystemException(new BLogicMapperException(e), ERROR_SETPROPERTY,
                        new String[] { blogicPropertyName });
            }

        } else {
            // i[wOX??[
            log.error("source is illegal.");
            throw new SystemException(new BLogicMapperException(), ERROR_SOURCE);
        }
    }
    return bean;
}

From source file:jp.terasoluna.fw.service.thin.AbstractBLogicMapper.java

/**
 * blogic-ioblogic-resultMapping?s?B//from   w  ww. j  av a 2 s . c  o  m
 * 
 * @param request
 * @param response
 * @param io
 * @param bean
 */
protected void getResults(HttpServletRequest request, HttpServletResponse response, BLogicIO io, Object bean) {

    // Ce?[^?[??
    Iterator it = io.getBLogicResults().iterator();

    // blogic-result??Abeani[??`FbN
    if (!it.hasNext()) {
        if (bean != null) {
            if (log.isDebugEnabled()) {
                log.debug("The bean should be null.");
            }
            log.error("bean is not null.");
            throw new SystemException(new BLogicMapperException(), ERROR_BEAN_NOTNULL);
        }
    }

    // ListSBLogicPropertyNXCX^X?A???s
    while (it.hasNext()) {

        // BLogicPropertyCX^X
        BLogicProperty property = (BLogicProperty) it.next();

        // v?peB
        String propertyName = property.getProperty();

        // rWlX?WbNgpv?peB
        String blogicPropertyName = property.getBLogicProperty();

        // blogicPropertyName????ApropertyName
        if (blogicPropertyName == null) {
            blogicPropertyName = propertyName;
        }

        if (!"".equals(property.getDest()) && (property.getDest() != null)) {

            Object value = null;
            try {
                // ?ol?\bh?s
                value = BeanUtil.getBeanProperty(bean, blogicPropertyName);
            } catch (PropertyAccessException e) {
                log.error("getBeanProperty failure.");
                throw new SystemException(new BLogicMapperException(e), ERROR_GETPROPERTY,
                        new String[] { blogicPropertyName });
            }

            // i[??s?A?oli[
            Object[] methodParams = new Object[] { value, propertyName, request, response };
            Class[] parameterTypes = new Class[] { Object.class, String.class, HttpServletRequest.class,
                    HttpServletResponse.class };
            try {
                MethodUtils.invokeMethod(this,
                        "setValueTo" + StringUtil.capitalizeInitial(property.getSource()), methodParams,
                        parameterTypes);
            } catch (NoSuchMethodException e) {
                log.error("no such method.");
                throw new SystemException(new BLogicMapperException(e), ERROR_SETVALUE,
                        new String[] { propertyName });
            } catch (IllegalAccessException e) {
                log.error("illegal access to the method.");
                throw new SystemException(new BLogicMapperException(e), ERROR_SETVALUE,
                        new String[] { propertyName });
            } catch (InvocationTargetException e) {
                log.error("exception is thrown out by invokeMethod.");
                throw new SystemException(new BLogicMapperException(e), ERROR_SETVALUE,
                        new String[] { propertyName });
            }
        } else {
            // i[?w?AOX??[
            log.error("dest is illegal.");
            throw new SystemException(new BLogicMapperException(), ERROR_DEST);
        }
    }
}

From source file:org.apache.commons.digester3.examples.api.documentmarkup.SetTextSegmentRule.java

/**
 * Process the end of this element./* w w w.j av  a2s. co m*/
 */
public void textSegment(String text) throws Exception {
    Object target = getDigester().peek(0);

    // Call the specified method
    Class<?> paramTypes[] = new Class[] { String.class };
    MethodUtils.invokeMethod(target, methodName, new Object[] { text }, paramTypes);
}

From source file:org.biokoframework.http.scenario.ScenarioRunner.java

public void test(String baseUrl) throws Exception {
    System.out.println("=== SCENARIO COLLECTOR: " + fScenario.scenarioName());
    for (Entry<String, ScenarioStep> eachScenario : fScenario.scenarioSteps()) {
        System.out.println("\t------ SCENARIO: " + eachScenario.getKey() + " --------");

        // Polymorphic visitor?
        try {//from ww w .j  a  v  a2s .c  o  m
            MethodUtils.invokeMethod(this, "test", new Object[] { baseUrl, eachScenario.getValue(), },
                    new Class[] { String.class, eachScenario.getValue().getClass() });
        } catch (NoSuchMethodException exception) {
            fail("[EASY MAN] '" + fScenario.scenarioName() + "' not testable, probably wrong class");
        } catch (InvocationTargetException exception) {
            if (exception.getCause() != null) {
                if (exception.getCause() instanceof Exception) {
                    throw (Exception) exception.getCause();
                } else if (exception.getCause() instanceof Error) {
                    throw (Error) exception.getCause();
                }
            } else {
                throw exception;
            }
        }
        System.out.println("\t------ END -------");
    }
    System.out.println("=== END ===");
}

From source file:org.broadleafcommerce.core.catalog.domain.SkuImpl.java

@Override
@Deprecated/*from w w w  . j  a  va 2s .  c  o  m*/
public List<ProductOptionValue> getProductOptionValues() {
    //Changing this API to Set is ill-advised (especially in a patch release). The tendrils are widespread. Instead
    //we just migrate the call from the List to the internal Set representation. This is in response
    //to https://github.com/BroadleafCommerce/BroadleafCommerce/issues/917.
    return (List<ProductOptionValue>) Proxy.newProxyInstance(getClass().getClassLoader(),
            new Class<?>[] { List.class }, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    return MethodUtils.invokeMethod(getProductOptionValuesCollection(), method.getName(), args,
                            method.getParameterTypes());
                }
            });
}

From source file:org.fireflow.engine.invocation.impl.AbsServiceInvoker.java

public boolean invoke(WorkflowSession session, ActivityInstance activityInstance, ServiceBinding serviceBinding,
        ResourceBinding resourceBinding, Object theActivity) throws ServiceInvocationException {
    try {/*from  ww  w .j  a  va  2s  .  c  om*/
        WorkflowSessionLocalImpl sessionLocalImpl = (WorkflowSessionLocalImpl) session;
        RuntimeContext runtimeContext = sessionLocalImpl.getRuntimeContext();

        ProcessInstance processInstance = sessionLocalImpl.getCurrentProcessInstance();

        ProcessUtil processUtil = runtimeContext.getEngineModule(ProcessUtil.class,
                activityInstance.getProcessType());
        ServiceDef svc = processUtil.getServiceDef(activityInstance, theActivity,
                serviceBinding.getServiceId());
        if (svc == null) {
            ServiceInvocationException ex = new ServiceInvocationException(
                    "Id" + serviceBinding.getServiceId() + "?");
            ex.setErrorCode(ServiceInvocationException.SERVICE_DEF_NOT_FOUND);
            ex.setActivityInstance(activityInstance);
            throw ex;
        }

        // 1?Service Object
        Object serviceObject = this.getServiceObject(runtimeContext, session, activityInstance, serviceBinding,
                svc, theActivity);

        if (serviceObject == null) {
            ServiceInvocationException ex = new ServiceInvocationException(
                    "? service objectService[name=" + svc.getName() + ",displayName="
                            + svc.getDisplayName() + "]");
            ex.setErrorCode(ServiceInvocationException.SERVICE_OBJECT_NOT_FOUND);
            ex.setActivityInstance(activityInstance);
            throw new ServiceInvocationException();
        }

        // 2??Method
        String methodName = this.getOperationName(runtimeContext, session, activityInstance, serviceBinding);

        if (methodName == null || methodName.trim().equals("")) {
            ServiceInvocationException ex = new ServiceInvocationException("???"
                    + serviceBinding.getOperationName() + "Service[name=" + svc.getName()
                    + ",displayName=" + svc.getDisplayName() + "]");
            ex.setErrorCode(ServiceInvocationException.OPERATION_NOT_FOUND);
            ex.setActivityInstance(activityInstance);
            throw new ServiceInvocationException();
        }

        // 3???
        Object[] params = resolveInputParams(runtimeContext, session, processInstance, activityInstance,
                serviceBinding, svc);

        // 4??
        Class[] parameterTypes = this.getParameterTypes(serviceObject.getClass(), methodName, params);

        // 5?
        Object result = MethodUtils.invokeMethod(serviceObject, methodName, params, parameterTypes);

        // 6??
        assignOutputToVariable(runtimeContext, session, processInstance, activityInstance, serviceBinding,
                result);

        return true;
    } catch (ServiceInvocationException e) {
        if (e.getActivityInstance() == null) {
            e.setActivityInstance(activityInstance);
        }
        throw e;
    } catch (ScriptException e) {
        ServiceInvocationException ex = new ServiceInvocationException(e);
        ex.setErrorCode(findRootCause(e).getClass().getName());
        ex.setActivityInstance(activityInstance);
        throw ex;
    } catch (SecurityException e) {
        ServiceInvocationException ex = new ServiceInvocationException(e);
        ex.setErrorCode(findRootCause(e).getClass().getName());
        ex.setActivityInstance(activityInstance);
        throw ex;
    } catch (NoSuchMethodException e) {
        ServiceInvocationException ex = new ServiceInvocationException(e);
        ex.setErrorCode(findRootCause(e).getClass().getName());
        ex.setActivityInstance(activityInstance);
        throw ex;
    } catch (IllegalArgumentException e) {
        ServiceInvocationException ex = new ServiceInvocationException(e);
        ex.setErrorCode(findRootCause(e).getClass().getName());
        ex.setActivityInstance(activityInstance);
        throw ex;
    } catch (IllegalAccessException e) {
        ServiceInvocationException ex = new ServiceInvocationException(e);
        ex.setErrorCode(findRootCause(e).getClass().getName());
        ex.setActivityInstance(activityInstance);
        throw ex;
    } catch (InvocationTargetException e) {
        ServiceInvocationException ex = new ServiceInvocationException(e);
        ex.setErrorCode(findRootCause(e).getClass().getName());
        ex.setActivityInstance(activityInstance);
        throw ex;
    } catch (Exception e) {
        ServiceInvocationException ex = new ServiceInvocationException(e);
        ex.setErrorCode(findRootCause(e).getClass().getName());
        ex.setActivityInstance(activityInstance);
        throw ex;
    }

}

From source file:org.jspresso.framework.model.component.basic.AbstractComponentInvocationHandler.java

/**
 * Invokes a service method on the component.
 *
 * @param proxy/*from w  w  w. j  av a 2s  . c  om*/
 *     the component to invoke the service on.
 * @param method
 *     the method implemented by the component.
 * @param args
 *     the arguments of the method implemented by the component.
 * @return the value returned by the method execution if any.
 *
 * @throws NoSuchMethodException
 *     if no mean could be found to service the method.
 */
@SuppressWarnings("unchecked")
protected Object invokeServiceMethod(Object proxy, Method method, Object... args) throws NoSuchMethodException {
    IComponentService service = componentDescriptor.getServiceDelegate(method);
    if (service != null) {
        try {
            if (service instanceof AbstractComponentServiceDelegate<?>) {
                Method refinedMethod = service.getClass().getMethod(method.getName(),
                        method.getParameterTypes());
                if (refinedMethod != null) {
                    return ((AbstractComponentServiceDelegate<Object>) service).executeWith(proxy,
                            refinedMethod, args);
                }
            }
            int signatureSize = method.getParameterTypes().length + 1;
            Class<?>[] parameterTypes = new Class<?>[signatureSize];
            Object[] parameters = new Object[signatureSize];

            parameterTypes[0] = componentDescriptor.getComponentContract();
            parameters[0] = proxy;

            for (int i = 1; i < signatureSize; i++) {
                parameterTypes[i] = method.getParameterTypes()[i - 1];
                parameters[i] = args[i - 1];
            }
            return MethodUtils.invokeMethod(service, method.getName(), parameters, parameterTypes);
        } catch (IllegalAccessException | NoSuchMethodException ex) {
            throw new ComponentException(ex);
        } catch (InvocationTargetException ex) {
            if (ex.getCause() instanceof RuntimeException) {
                throw (RuntimeException) ex.getCause();
            }
            throw new ComponentException(ex.getCause());
        }
    }
    throw new NoSuchMethodException(method.toString());
}

From source file:org.jspresso.framework.model.component.basic.AbstractComponentInvocationHandler.java

private Object invokeExtensionMethod(IComponentExtension<? extends IComponent> componentExtension,
        Method method, Object... args)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    return MethodUtils.invokeMethod(componentExtension, method.getName(), args, method.getParameterTypes());
}