Java Method Call invoke(final Object component, final Method method, final Class type, final String value)

Here you can find the source of invoke(final Object component, final Method method, final Class type, final String value)

Description

Invoke a method.

License

Open Source License

Parameter

Parameter Description
component the component to invoke the method on
method the method to invoke
type the property type
value the property value

Exception

Parameter Description
ReflectiveOperationException if an error occurs

Declaration

private static void invoke(final Object component, final Method method, final Class<?> type, final String value)
        throws ReflectiveOperationException 

Method Source Code


//package com.java2s;
import java.lang.reflect.Method;

public class Main {
    /**/*from  www. ja v a  2 s. co  m*/
     * Invoke a method.
     *
     * @param component the component to invoke the method on
     * @param method the method to invoke
     * @param type the property type
     * @param value the property value
     * @throws ReflectiveOperationException if an error occurs
     */
    private static void invoke(final Object component, final Method method, final Class<?> type, final String value)
            throws ReflectiveOperationException {
        Object obj = valueOf(type, value);
        method.invoke(component, obj);
    }

    /**
     * Invoke a method.
     *
     * @param component the component to invoke the method on
     * @param method the method to invoke
     * @param keyType the key property type
     * @param valueType the value property type
     * @param key the property key
     * @param value the property value
     * @throws ReflectiveOperationException if an error occurs
     */
    private static void invoke(final Object component, final Method method, final Class<?> keyType,
            final Class<?> valueType, final String key, final String value) throws ReflectiveOperationException {
        Object keyObj = valueOf(keyType, key);
        Object valueObj = valueOf(valueType, value);
        method.invoke(component, keyObj, valueObj);
    }

    /**
     * Convert a string value to a boxed primitive type.
     *
     * @param type the boxed primitive type
     * @param value the string value
     * @return a boxed primitive type
     */
    public static Object valueOf(final Class<?> type, final String value) {
        if (type == String.class) {
            return value;
        }
        if (type == boolean.class) {
            return Boolean.valueOf(value);
        }
        if (type == int.class) {
            return Integer.valueOf(value);
        }
        if (type == long.class) {
            return Long.valueOf(value);
        }
        if (type == float.class) {
            return Float.valueOf(value);
        }
        if (type == double.class) {
            return Double.valueOf(value);
        }
        throw new IllegalArgumentException("Unsupported type " + type.getName());
    }
}

Related

  1. invoke(Class cls, String methodName, Object... parameter)
  2. invoke(Class returnType, Method m, Object obj, Object... args)
  3. invoke(E e, String methodName)
  4. invoke(final Method m, final Object obj, final Object... args)
  5. invoke(final Method method, final Object obj, final Object... args)
  6. invoke(final Object instance, final String method, final Object... parameters)
  7. invoke(final Object instance, final String methodName, final Class[] methodParams, final Object[] args)
  8. invoke(final Object object, final String methodName, final Class[] methodParamTypes, final Object[] methodParamValues)
  9. invoke(final Object target, final Method method, final Object... parameters)