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

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

Introduction

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

Prototype

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

Source Link

Document

Invoke a method whose parameter types match exactly the parameter types given.

This uses reflection to invoke the method obtained from a call to #getAccessibleMethod .

Usage

From source file:core.sample.pool.WorkerThread.java

/**
 * execute/*from   ww  w.j  av  a 2s  .co  m*/
 */
private void execute() {
    try {
        Class cls = getClass(this.getClassName());
        Object obj = cls.newInstance();
        this.result = MethodUtils.invokeExactMethod(obj, this.getMethodName(), this.getMethodParams(),
                this.getParmTypes());
        log.debug(" #### Execution Result = " + result + " for : " + this);
    } catch (ClassNotFoundException e) {
        log.error("ClassNotFoundException - " + e);
    } catch (NoSuchMethodException e) {
        log.error("NoSuchMethodException - " + e);
    } catch (IllegalAccessException e) {
        log.error("IllegalAccessException - " + e);
    } catch (InvocationTargetException e) {
        log.error("InvocationTargetException - " + e);
    } catch (InstantiationException e) {
        log.error("InstantiationException - " + e);
    }
}

From source file:no.abmu.organisationregister.service.hibernate3.VanillaModelH3Test.java

private void testManyToOne(Class targetClass, Class relationToClass, String propertyToTest, int numOfSaved)
        throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
        SQLException {// ww  w  . j a  v  a  2  s .c  o m

    List list = session.createQuery("from " + targetClass.getName()).list();

    assertTrue(list.size() == numOfSaved);
    for (int i = 0; i < list.size(); i++) {
        Object target = list.get(i);
        Object relation = relationToClass.newInstance();
        DomainTestHelper.populateBean(relation);
        session.save(relation);
        BeanUtils.setProperty(target, propertyToTest, relation);
        Serializable id = session.save(target);
        Object loaded = session.load(targetClass, id);

        Object loadedRelation = PropertyUtils.getProperty(loaded, propertyToTest);

        Boolean result = (Boolean) MethodUtils.invokeExactMethod(loadedRelation, "equals",
                new Object[] { relation }, new Class[] { Object.class });

        assertTrue("Not correct for target class " + targetClass.getName() + " when loading realtion class "
                + relationToClass + " using property " + propertyToTest, result.booleanValue());
    }
}

From source file:no.abmu.organisationregister.service.hibernate3.VanillaModelH3Test.java

private void testObject(Object obj)
        throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, SQLException {
    storeObject(obj);//from w  w  w .j av a 2  s  .c  o m
    session.flush();
    session.clear();

    //        session.save(obj);

    List list = session.createQuery("from " + obj.getClass().getName()).list();
    assertTrue("Should have size 1 not " + list.size(), 1 == list.size());

    Boolean result = (Boolean) MethodUtils.invokeExactMethod(obj, "equals", new Object[] { list.get(0) },
            new Class[] { Object.class });
    assertTrue(result.booleanValue());

}

From source file:no.abmu.user.service.hibernate3.UserH3Test.java

private void testObject(Object obj) throws Exception {
    UserService userService = (UserService) applicationContext.getBean("userService");

    List list = Arrays.asList(userService.find("from " + obj.getClass().getName()));
    assertTrue("size should be 1 not " + list.size(), 1 == list.size());

    Boolean result = (Boolean) MethodUtils.invokeExactMethod(obj, "equals", new Object[] { list.get(0) },
            new Class[] { Object.class });
    assertTrue("not equals: \n" + obj.toString() + " \n!=\n" + list.get(0).toString(), result.booleanValue());

}

From source file:org.opencms.configuration.CmsSetNextRule.java

/**
 * Process the end of this element.<p>
 * /*w w  w . j  a v  a 2  s.com*/
 * @param namespace the namespace URI of the matching element, or an empty string if the parser is not namespace 
 *                  aware or the element has no namespace
 * @param name the local name if the parser is namespace aware, or just the element name otherwise
 * @throws Exception if something goes wrong
 */
@Override
public void end(String namespace, String name) throws Exception {

    // Determine the target object for the method call: the parent object
    Object parent = digester.peek(1);
    Object child = digester.peek(0);

    // Retrieve or construct the parameter values array
    Object[] parameters = null;
    if (m_paramCount > 0) {
        parameters = (Object[]) digester.popParams();
        if (LOG.isTraceEnabled()) {
            for (int i = 0, size = parameters.length; i < size; i++) {
                LOG.trace("[SetNextRuleWithParams](" + i + ")" + parameters[i]);
            }
        }

        // In the case where the target method takes a single parameter
        // and that parameter does not exist (the CallParamRule never
        // executed or the CallParamRule was intended to set the parameter
        // from an attribute but the attribute wasn't present etc) then
        // skip the method call.
        //
        // This is useful when a class has a "default" value that should
        // only be overridden if data is present in the XML. I don't
        // know why this should only apply to methods taking *one*
        // parameter, but it always has been so we can't change it now.
        if ((m_paramCount == 1) && (parameters[0] == null)) {
            return;
        }

    } else if (m_paramTypes.length != 0) {
        // Having paramCount == 0 and paramTypes.length == 1 indicates
        // that we have the special case where the target method has one
        // parameter being the body text of the current element.

        // There is no body text included in the source XML file,
        // so skip the method call
        if (m_bodyText == null) {
            return;
        }

        parameters = new Object[1];
        parameters[0] = m_bodyText;
        if (m_paramTypes.length == 0) {
            m_paramTypes = new Class[1];
            m_paramTypes[0] = String.class;
        }

    } else {
        // When paramCount is zero and paramTypes.length is zero it
        // means that we truly are calling a method with no parameters.
        // Nothing special needs to be done here.
        parameters = new Object[0];
    }

    // Construct the parameter values array we will need
    // We only do the conversion if the param value is a String and
    // the specified paramType is not String. 
    Object[] paramValues = new Object[m_paramTypes.length];

    Class<?> propertyClass = child.getClass();
    for (int i = 0; i < m_paramTypes.length; i++) {
        if (m_paramTypes[i] == propertyClass) {
            // implant the original child to set if Class matches: 
            paramValues[i] = child;
        } else if ((parameters[i] == null)
                || ((parameters[i] instanceof String) && !String.class.isAssignableFrom(m_paramTypes[i]))) {
            // convert nulls and convert stringy parameters 
            // for non-stringy param types
            if (parameters[i] == null) {
                paramValues[i] = null;
            } else {
                paramValues[i] = ConvertUtils.convert((String) parameters[i], m_paramTypes[i]);
            }

        } else {
            paramValues[i] = parameters[i];
        }
    }

    if (parent == null) {
        StringBuffer sb = new StringBuffer();
        sb.append("[SetNextRuleWithParams]{");
        sb.append(digester.getMatch());
        sb.append("} Call target is null (");
        sb.append("targetOffset=");
        sb.append(m_targetOffset);
        sb.append(",stackdepth=");
        sb.append(digester.getCount());
        sb.append(")");
        throw new org.xml.sax.SAXException(sb.toString());
    }

    // Invoke the required method on the top object
    if (LOG.isDebugEnabled()) {
        StringBuffer sb = new StringBuffer("[SetNextRuleWithParams]{");
        sb.append(digester.getMatch());
        sb.append("} Call ");
        sb.append(parent.getClass().getName());
        sb.append(".");
        sb.append(m_methodName);
        sb.append("(");
        for (int i = 0; i < paramValues.length; i++) {
            if (i > 0) {
                sb.append(",");
            }
            if (paramValues[i] == null) {
                sb.append("null");
            } else {
                sb.append(paramValues[i].toString());
            }
            sb.append("/");
            if (m_paramTypes[i] == null) {
                sb.append("null");
            } else {
                sb.append(m_paramTypes[i].getName());
            }
        }
        sb.append(")");
        LOG.debug(sb.toString());
    }

    Object result = null;
    if (m_useExactMatch) {
        // invoke using exact match
        result = MethodUtils.invokeExactMethod(parent, m_methodName, paramValues, m_paramTypes);

    } else {
        // invoke using fuzzier match
        result = MethodUtils.invokeMethod(parent, m_methodName, paramValues, m_paramTypes);
    }

    processMethodCallResult(result);
}

From source file:org.red5.server.pooling.Worker.java

/** {@inheritDoc} */
/*//www . ja  v  a 2  s.c o  m
 * (non-Javadoc)
 * 
 * @see java.lang.Runnable#run()
 */
public void run() {
    try {
        Class<?> cls = getClass(this.getClassName());
        Object obj = cls.newInstance();
        this.result = MethodUtils.invokeExactMethod(obj, this.getMethodName(), this.getMethodParams(),
                this.getParamTypes());
        if (log.isDebugEnabled()) {
            log.debug(" #### Execution Result = " + result + " for : " + this);
        }
    } catch (ClassNotFoundException e) {
        log.error("ClassNotFoundException - " + e);
    } catch (NoSuchMethodException e) {
        log.error("NoSuchMethodException - " + e);
    } catch (IllegalAccessException e) {
        log.error("IllegalAccessException - " + e);
    } catch (InvocationTargetException e) {
        log.error("InvocationTargetException - " + e);
    } catch (InstantiationException e) {
        log.error("InstantiationException - " + e);
    }
}

From source file:org.red5.server.pooling.WorkerThread.java

/**
 * execute/*  w w  w. ja v a  2  s. co m*/
 */
private void execute() {
    try {
        Class cls = getClass(this.getClassName());
        Object obj = cls.newInstance();
        this.result = MethodUtils.invokeExactMethod(obj, this.getMethodName(), this.getMethodParams(),
                this.getParmTypes());
        if (log.isDebugEnabled()) {
            log.debug(" #### Execution Result = " + result + " for : " + this);
        }
    } catch (ClassNotFoundException e) {
        log.error("ClassNotFoundException - " + e);
    } catch (NoSuchMethodException e) {
        log.error("NoSuchMethodException - " + e);
    } catch (IllegalAccessException e) {
        log.error("IllegalAccessException - " + e);
    } catch (InvocationTargetException e) {
        log.error("InvocationTargetException - " + e);
    } catch (InstantiationException e) {
        log.error("InstantiationException - " + e);
    }
}