Java Reflection Method Invoke invokeMethod(String name, Object target)

Here you can find the source of invokeMethod(String name, Object target)

Description

invoke Method

License

Open Source License

Declaration

public static <T> T invokeMethod(String name, Object target) 

Method Source Code

//package com.java2s;
/*/*  w ww . j a va 2 s  . c  om*/
 * This file is part of alta-commons.
 *
 * Copyright (c) 2012, alta189 <http://github.com/alta189/alta-commons/>
 * alta-commons is licensed under the GNU Lesser General Public License.
 *
 * alta-commons is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * alta-commons is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static <T> T invokeMethod(String name, Object target) {
        Method method = findMethod(target.getClass(), name);
        if (method == null) {
            return null;
        }
        return invokeMethod(method, target);
    }

    @SuppressWarnings("unchecked")
    public static <T> T invokeMethod(Method method, Object target) {
        return invokeMethod(method, target, new Object[0]);
    }

    public static <T> T invokeMethod(String name, Object target, Object... args) {
        List<Class<?>> list = new ArrayList<Class<?>>();
        for (Object arg : args) {
            list.add(arg.getClass());
        }
        Method method = findMethod(target.getClass(), list.toArray(new Class<?>[0]), name);
        if (method == null) {
            return null;
        }
        return invokeMethod(method, target);
    }

    @SuppressWarnings("unchecked")
    public static <T> T invokeMethod(Method method, Object target, Object... args) {
        try {
            return (T) method.invoke(target, args);
        } catch (IllegalAccessException ignored) {
        } catch (InvocationTargetException ignored) {
        } catch (ClassCastException ignored) {
        }
        return null;
    }

    public static Method findMethod(Class<?> clazz, String name) {
        return findMethod(clazz, name, new Class[0]);
    }

    public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
        return findMethod(clazz, paramTypes, name);
    }

    public static Method findMethod(Class<?> clazz, Class<?>[] paramTypes, String name) {
        Class<?> search = clazz;
        while (search != null) {
            Method[] methods = (search.isInterface() ? search.getMethods() : search.getDeclaredMethods());
            for (Method method : methods) {
                if (name.equals(method.getName())
                        && (paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                    return method;
                }
            }
            search = search.getSuperclass();
        }
        return null;
    }
}

Related

  1. invokeMethod(Object target, String signature, Object... args)
  2. invokeMethod(Object target, String thisMethod, Object value)
  3. invokeMethod(Object theObject, String methodName, Object... parametersObject)
  4. invokeMethod(String className, String method, Class[] paramTypes, Object obj, Object[] args)
  5. invokeMethod(String methodName, Object gameCommand)
  6. invokeMethod2(Class cls, Object obj, String methodName, Object[] args)
  7. invokeMethodAndGet(Object object, String methodName, Object... args)
  8. invokeMethodAndGetRecursively(Object object, String methodName, Object... args)
  9. invokeMethodAndReturn(Class clazz, String method, Object object)