Java Method Call invoke(Object object, String function, String parameter)

Here you can find the source of invoke(Object object, String function, String parameter)

Description

Invoke function of object by String.

License

Apache License

Parameter

Parameter Description
object object invoked
function function name
parameter "String:str,long:200"

Exception

Parameter Description
InvocationTargetException an exception
IllegalAccessException an exception
NoSuchMethodException an exception
IllegalArgumentException an exception
SecurityException an exception

Return

result of invoked method

Declaration

public static Object invoke(Object object, String function, String parameter) throws SecurityException,
        IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
/*/* w  w w. j  a  v a 2s  .  co m*/
 * Copyright (C) 2011 Baidu.com Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    private static Class<?> mType;
    private static Object mValue;

    /**
     * invoke object's method including private method
     * 
     * @param owner
     *            : target object
     * @param classLevel
     *            : 0 means itself, 1 means it's father, and so on...
     * @param methodName
     *            : name of the target method
     * @param parameterTypes
     *            : types of the target method's parameters
     * @param parameters
     *            : parameters of the target method
     * @return result of invoked method
     * 
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    public static Object invoke(Object owner, String targetClass, String methodName, Class<?>[] parameterTypes,
            Object[] parameters) throws SecurityException, NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        Class<?> ownerclass = getTargetclass(owner, targetClass);
        Method method = ownerclass.getDeclaredMethod(methodName, parameterTypes);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        Object result = method.invoke(owner, parameters);
        return result;
    }

    /**
     * Invoke function of object by String.
     * 
     * @param object
     *            object invoked
     * @param function
     *            function name
     * @param parameter
     *            "String:str,long:200"
     * @return result of invoked method
     * 
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws SecurityException
     */
    public static Object invoke(Object object, String function, String parameter) throws SecurityException,
            IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        System.out.println(function + "(" + (null == parameter ? "" : parameter) + ")");
        Class<?>[] types = null;
        Object[] values = null;

        // get parameter
        if (null == parameter) {
            types = new Class[] {};
            values = new Object[] {};
        } else {
            String[] parameters = parameter.split(",");
            types = new Class[parameters.length];
            values = new Object[parameters.length];
            for (int i = 0; i < parameters.length; i++) {
                mType = null;
                mValue = null;
                getParameters(parameters[i]);
                types[i] = mType;
                values[i] = mValue;
            }
        }

        return invoke(object, null, function, types, values);
    }

    private static Class<?> getTargetclass(Object owner, String targetClass) {
        try {
            return null == targetClass ? owner.getClass() : Class.forName(targetClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void getParameters(String parameterString) {
        String type = parameterString.substring(0, parameterString.indexOf(":"));
        String value = parameterString.substring(parameterString.indexOf(":") + 1, parameterString.length());

        if ("String".equalsIgnoreCase(type)) {
            mType = String.class;
            mValue = String.valueOf(value);
        } else if ("int".equalsIgnoreCase(type)) {
            mType = int.class;
            mValue = Integer.valueOf(value).intValue();
        } else if ("boolean".equalsIgnoreCase(type)) {
            mType = boolean.class;
            mValue = Boolean.valueOf(value).booleanValue();
        } else if ("float".equalsIgnoreCase(type)) {
            mType = float.class;
            mValue = Float.valueOf(value).floatValue();
        } else if ("double".equalsIgnoreCase(type)) {
            mType = double.class;
            mValue = Double.valueOf(value).doubleValue();
        } else if ("long".equalsIgnoreCase(type)) {
            mType = long.class;
            mValue = Long.valueOf(value).longValue();
        }
    }
}

Related

  1. invoke(Object obj, String methodName, Object[] params)
  2. invoke(Object obj, String name, Object... args)
  3. invoke(Object object, Method method)
  4. invoke(Object object, Object[] args, String methodName)
  5. invoke(Object object, String function, Object... params)
  6. invoke(Object object, String method, Object... args)
  7. invoke(Object object, String methodName)
  8. invoke(Object object, String methodName, Class[] argTypes, Object... args)
  9. invoke(Object object, String methodName, Class returnType, Object... parameters)