Java Reflection Method Invoke invokeMethodWithoutException(Object object, String methodName, Class[] parameterTypes, Object[] parameters)

Here you can find the source of invokeMethodWithoutException(Object object, String methodName, Class[] parameterTypes, Object[] parameters)

Description

Invoke a method.

License

Open Source License

Parameter

Parameter Description
object The object on which we must invoke method
methodName The name of the method to invoke
parameterTypes The type of the parameters
parameters The parameters of the method

Return

true if the method is invoke without exception, false otherwise.

Declaration

public static boolean invokeMethodWithoutException(Object object,
        String methodName, Class<?>[] parameterTypes,
        Object[] parameters) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011, 2012 THALES GLOBAL SERVICES.
 * 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  w w  w  .  ja v a 2  s . c o  m
 *    Obeo - initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * Invoke a method.
     * 
     * @param object
     *            The object on which we must invoke method
     * @param methodName
     *            The name of the method to invoke
     * @param parameterTypes
     *            The type of the parameters
     * @param parameters
     *            The parameters of the method
     * @return true if the method is invoke without exception, false otherwise.
     */
    public static boolean invokeMethodWithoutException(Object object,
            String methodName, Class<?>[] parameterTypes,
            Object[] parameters) {
        try {
            invokeMethod(object, methodName, parameterTypes, parameters);
            return true;
        } catch (SecurityException e) {
            // Do nothing
        } catch (IllegalArgumentException e) {
            // Do nothing
        } catch (NoSuchMethodException e) {
            // Do nothing
        } catch (IllegalAccessException e) {
            // Do nothing
        } catch (InvocationTargetException e) {
            // Do nothing
        }
        return false;
    }

    /**
     * Invoke a method.
     * 
     * @param object
     *            The object on which we must invoke method
     * @param aClass
     *            The class which declares the method
     * @param methodName
     *            The name of the method to invoke
     * @param parameterTypes
     *            The type of the parameters
     * @param parameters
     *            The parameters of the method
     * @param setVisible
     *            true to set the method visible before calling.
     * @return true if the method is invoke without exception, false otherwise.
     */
    public static boolean invokeMethodWithoutException(Object object,
            Class aClass, String methodName, Class<?>[] parameterTypes,
            Object[] parameters, boolean setVisible) {
        try {
            invokeMethod(object, aClass, methodName, parameterTypes,
                    parameters, setVisible);
            return true;
        } catch (SecurityException e) {
            // Do nothing
        } catch (IllegalArgumentException e) {
            // Do nothing
        } catch (NoSuchMethodException e) {
            // Do nothing
        } catch (IllegalAccessException e) {
            // Do nothing
        } catch (InvocationTargetException e) {
            // Do nothing
        }
        return false;
    }

    /**
     * Invoke a method.
     * 
     * @param object
     *            The object on which we must invoke method
     * @param methodName
     *            The name of the method to invoke
     * @param parameterTypes
     *            The types of the parameters
     * @param parameters
     *            The parameters of the method
     * @throws SecurityException
     *             In case of problem
     * @throws NoSuchMethodException
     *             In case of problem
     * @throws IllegalArgumentException
     *             In case of problem
     * @throws IllegalAccessException
     *             In case of problem
     * @throws InvocationTargetException
     *             In case of problem
     */
    // CHECKSTYLE:OFF
    public static Object invokeMethod(Object object, String methodName,
            Class<?>[] parameterTypes, Object[] parameters)
            throws SecurityException, NoSuchMethodException,
            IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        // CHECKSTYLE:ON
        Method method = object.getClass().getMethod(methodName,
                parameterTypes);
        return method.invoke(object, parameters);
    }

    /**
     * Invoke a method.
     * 
     * @param object
     *            The object on which we must invoke method
     * @param aClass
     *            The class which declares the method
     * @param methodName
     *            The name of the method to invoke
     * @param parameterTypes
     *            The types of the parameters
     * @param parameters
     *            The parameters of the method
     * @param setVisible
     *            true to set the method visible before calling.
     * @throws SecurityException
     *             In case of problem
     * @throws NoSuchMethodException
     *             In case of problem
     * @throws IllegalArgumentException
     *             In case of problem
     * @throws IllegalAccessException
     *             In case of problem
     * @throws InvocationTargetException
     *             In case of problem
     */
    // CHECKSTYLE:OFF
    public static Object invokeMethod(Object object, Class aClass,
            String methodName, Class<?>[] parameterTypes,
            Object[] parameters, boolean setVisible)
            throws SecurityException, NoSuchMethodException,
            IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        // CHECKSTYLE:ON
        Method method = aClass
                .getDeclaredMethod(methodName, parameterTypes);
        if (setVisible) {
            method.setAccessible(true);
        }
        return method.invoke(object, parameters);
    }
}

Related

  1. invokeMethods(final Object target, final List methods)
  2. invokeMethodsWithAnnotation(final Class annotation, final Object instance, final Object... args)
  3. invokeMethodWithArray2(Object target, Method method, Object[] args)
  4. invokeMethodWithKnownParamTypes(Object object, String methodName, Class[] methodParamTypes, Object... args)
  5. invokeMethodWithNoArgs(Object o, Method method)