Example usage for org.apache.commons.lang3.reflect MethodUtils invokeExactMethod

List of usage examples for org.apache.commons.lang3.reflect MethodUtils invokeExactMethod

Introduction

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

Prototype

public static Object invokeExactMethod(final Object object, final String methodName)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 

Source Link

Document

Invokes a method whose parameter types match exactly the object types.

This uses reflection to invoke the method obtained from a call to #getAccessibleMethod (Class,String,Class[])}.

Usage

From source file:com.azaptree.services.tests.support.AzaptreeAbstractTestNGSpringContextTests.java

/**
 * Ensures that the application context is closed after the test class is done
 *///  w ww.  j  av a  2  s . c o  m
@Override
@AfterClass(alwaysRun = true)
protected void springTestContextAfterTestClass() throws Exception {
    super.springTestContextAfterTestClass();

    try {
        MethodUtils.invokeExactMethod(applicationContext, "close");
        log.info("closed the application context");
    } catch (final Exception e) {
        log.error("Failed to close application context", e);
    }
}

From source file:org.dvare.ruleengine.parser.RuleParser.java

public RuleStructure parseRule(Object rule, int size) {

    try {/*from w  ww.j av a 2 s . c  o  m*/
        Object name = MethodUtils.invokeExactMethod(rule, "getName");

        Object priorityObject = MethodUtils.invokeExactMethod(rule, "priority");

        Integer priority = 0;
        if (priorityObject != null) {
            priority = (Integer) priorityObject;
        }

        String ruleId = null;
        if (name != null) {
            ruleId = (String) name + size;
        } else {
            ruleId = "" + size;
        }

        RuleStructure ruleStructure = new RuleStructure();
        ruleStructure.ruleId = ruleId;
        ruleStructure.rule = rule;
        ruleStructure.priority = priority;

        Method before = MethodUtils.getAccessibleMethod(rule.getClass(), "before");
        MethodStructure beforeStructure = new MethodStructure();
        beforeStructure.order = 0;
        beforeStructure.method = before;
        ruleStructure.beforeMethods.add(beforeStructure);

        if (rule instanceof BasicRule) {
            Method condition = MethodUtils.getAccessibleMethod(rule.getClass(), "condition");
            ConditionStructure conditionStructure = new ConditionStructure();
            conditionStructure.order = 0;
            conditionStructure.conditionType = ConditionType.CODE;
            conditionStructure.condition = condition;
            ruleStructure.conditions.add(conditionStructure);
        } else if (rule instanceof TextualRule) {
            Method condition = MethodUtils.getAccessibleMethod(rule.getClass(), "condition",
                    new Class[] { TextualRuleEngine.class });
            ConditionStructure conditionStructure = new ConditionStructure();
            conditionStructure.order = 0;
            conditionStructure.conditionType = ConditionType.TEXT;
            conditionStructure.condition = condition;
            ruleStructure.conditions.add(conditionStructure);
        }

        Method after = MethodUtils.getAccessibleMethod(rule.getClass(), "after");
        MethodStructure afterStructure = new MethodStructure();
        afterStructure.order = 0;
        afterStructure.method = after;
        ruleStructure.afterMethods.add(afterStructure);

        Method success = MethodUtils.getAccessibleMethod(rule.getClass(), "success");
        MethodStructure successStructure = new MethodStructure();
        successStructure.order = 0;
        successStructure.method = success;
        ruleStructure.afterMethods.add(successStructure);

        Method fail = MethodUtils.getAccessibleMethod(rule.getClass(), "fail");
        MethodStructure failStructure = new MethodStructure();
        failStructure.order = 0;
        failStructure.method = fail;
        ruleStructure.afterMethods.add(failStructure);

        return ruleStructure;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.meveo.admin.action.admin.CacheBean.java

/**
 * Extract values of cached object to show in a list. In case of list of items, show only the first 10 items, in case of mapped items - only first 2 entries.
 * //  www. ja va 2 s.  c o  m
 * @param cachedObject Item to convert to string
 * @return A string representation of an item. Preferred way is code (id) or id or a value. For lists, separate items by a comma, for maps: key:[items..]
 */
@SuppressWarnings("rawtypes")
public String getShortRepresentationOfCachedValue(Object item, boolean returnToStringForSimpleObjects) {

    if (item instanceof List) {
        StringBuilder builder = new StringBuilder();
        List listObject = (List) item;
        for (int i = 0; i < 10 && i < listObject.size(); i++) {
            builder.append(builder.length() == 0 ? "" : ", ");
            Object listItem = listObject.get(i);
            builder.append(getShortRepresentationOfCachedValue(listItem, false));
        }

        if (listObject.size() > 10) {
            builder.append(", ...");
        }

        return builder.toString();

    } else if (item instanceof Map) {
        StringBuilder builder = new StringBuilder();
        Map mapObject = (Map) item;
        int i = 0;
        for (Object mapEntry : mapObject.entrySet()) {
            builder.append(builder.length() == 0 ? "" : ", ");
            Object key = ((Entry) mapEntry).getKey();
            Object value = ((Entry) mapEntry).getValue();
            if (i > 2) {
                break;
            }
            builder.append(String.format("%s: [%s]", key, getShortRepresentationOfCachedValue(value, false)));
            i++;
        }
        if (mapObject.size() > 2) {
            builder.append(", ...");
        }
        return builder.toString();

    } else if (returnToStringForSimpleObjects) {
        return item.toString();

    } else if (item instanceof BusinessEntity) {
        return String.format("%s (%s)", ((BusinessEntity) item).getCode(), ((BusinessEntity) item).getId());

    } else if (item instanceof IEntity) {
        return ((IEntity) item).getId().toString();

    } else if (item instanceof Long) {
        return item.toString();

    } else {

        Object code = null;
        try {
            code = MethodUtils.invokeExactMethod(item, "getCode");
        } catch (Exception e) {
            // Method does not exist - so just ignore
        }
        Object id = null;
        try {
            id = MethodUtils.invokeExactMethod(item, "getId");
        } catch (Exception e) {
            // Method does not exist - so just ignore
        }

        if (code != null && id != null) {
            return String.format("%s (%s)", code, id);
        } else if (code != null) {
            return code.toString();
        } else if (id != null) {
            return id.toString();
        } else {
            return item.toString();
        }
    }

}