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

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

Description

invoke Method

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Gerold Klinger and sourceheads Information Technology GmbH.
 * All rights reserved.//w w w . ja v a  2 s  .co  m
 * 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.
 *
 * Contributors:
 *     ...
 ******************************************************************************/

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

public class Main {
    public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] paramTypes,
            final Object[] parameters) throws NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        final Method method = getDeclaredMethod(object, methodName, paramTypes);
        method.setAccessible(true);
        return method.invoke(object, parameters);
    }

    public static Method getDeclaredMethod(final Object object, final String name, final Class<?>... paramTypes)
            throws NoSuchMethodException {
        Method method = null;
        Class<?> clazz = object.getClass();

        // TODO: this is a hack, implement better solution
        do {
            try {
                method = clazz.getDeclaredMethod(name, paramTypes);
            } catch (Exception ignored) {
            }
        } while (method == null && (clazz = clazz.getSuperclass()) != null);

        if (method == null) {
            throw new NoSuchMethodException(
                    object.getClass() + "." + name + '(' + Arrays.toString(paramTypes) + ')');
        }

        return method;
    }
}

Related

  1. invokeMethod(final Method method, final Object object, final Object[] args)
  2. invokeMethod(final Object instance, final String methodName, final Class[] parTypes, final Object[] parameters)
  3. invokeMethod(final Object obj, final Method method, final Object... args)
  4. invokeMethod(final Object obj, final String methodName, final boolean throwException)
  5. invokeMethod(final Object object, final String methodName, final Class[] parameterTypes, final Object[] parameters)
  6. invokeMethod(final Object p, final String methodName)
  7. invokeMethod(java.lang.Object toObj, String tcMethodName, Class toResultClass)
  8. invokeMethod(Method inMethod, Object inObject, Object[] inArgs)
  9. invokeMethod(Method m, Object instance, Object[] args)