Java Reflection Method Invoke invokeMethod(final Object instance, final String methodName, final Class[] parTypes, final Object[] parameters)

Here you can find the source of invokeMethod(final Object instance, final String methodName, final Class[] parTypes, final Object[] parameters)

Description

invoke Method

License

Open Source License

Declaration

public static Object invokeMethod(final Object instance, final String methodName, final Class<?>[] parTypes,
            final Object[] parameters) throws NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 SAP AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   ww w.  j  av  a  2 s .co  m*/
 *     SAP AG - initial API and implementation
 *******************************************************************************/

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

public class Main {
    public static Object invokeMethod(final Object instance, final String methodName, final Object[] parameters)
            throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        assert instance != null && methodName != null && parameters != null;

        return invokeMethod(instance, methodName, createParameterTypes(parameters), parameters);
    }

    public static Object invokeMethod(final Object instance, final String methodName, final Class<?>[] parTypes,
            final Object[] parameters) throws NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        assert instance != null && methodName != null && parTypes != null && parameters != null;

        final Method method = getMethod(instance.getClass(), methodName, parTypes, parameters);
        method.setAccessible(true);
        return method.invoke(instance, parameters);
    }

    private static Class<?>[] createParameterTypes(final Object[] parameters) {
        assert parameters != null;
        final List<Class<?>> types = new ArrayList<Class<?>>();

        for (final Object object : parameters) {
            types.add(object.getClass());
        }
        return types.toArray(new Class[types.size()]);
    }

    private static Method getMethod(final Class<? extends Object> clazz, final String methodName,
            final Class<?>[] parTypes, final Object[] parameters) throws NoSuchMethodException {
        assert clazz != null && methodName != null && parTypes != null && parameters != null;

        try {
            return clazz.getDeclaredMethod(methodName, parTypes);
        } catch (NoSuchMethodException e) {
            final Class<?> superClass = clazz.getSuperclass();
            if (superClass != null) {
                return getMethod(superClass, methodName, parTypes, parameters);
            }
            throw e;
        }
    }
}

Related

  1. invokeMethod(Class clz, String methodName, Object... params)
  2. invokeMethod(Class returnClass, String methodName, Object ivokeObject, Object... objects)
  3. invokeMethod(final Method method, final Object instance, final Object... args)
  4. invokeMethod(final Method method, final Object object)
  5. invokeMethod(final Method method, final Object object, final Object[] args)
  6. invokeMethod(final Object obj, final Method method, final Object... args)
  7. invokeMethod(final Object obj, final String methodName, final boolean throwException)
  8. invokeMethod(final Object object, final String methodName, final Class[] parameterTypes, final Object[] parameters)
  9. invokeMethod(final Object object, final String methodName, final Class[] paramTypes, final Object[] parameters)