Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    /**
     * 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());
    }
}