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 types of the parameters |
parameters | The parameters of the method |
Exception
Parameter | Description |
---|
SecurityException | In case of problem |
NoSuchMethodException | In case of problem |
IllegalArgumentException | In case of problem |
IllegalAccessException | In case of problem |
InvocationTargetException | In case of problem |
Declaration
public static Object invokeMethod(Object object, String methodName,
Class<?>[] parameterTypes, Object[] parameters)
throws SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException
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.j ava2s . 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 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
- invokeMethod(Object obj, String methodName, Object... args)
- invokeMethod(Object obj, String methodname, Object... args)
- invokeMethod(Object obj, String methodName, Object... params)
- invokeMethod(Object obj, String methodName, Object[] params, Class[] paramTypes)
- invokeMethod(Object object, Method method, Object... args)
- invokeMethod(Object object, String methodName, Class>[] signature, Object[] parameters)
- invokeMethod(Object object, String methodName, List> argList)
- invokeMethod(Object object, String methodName, Object arg, Class argType)
- invokeMethod(Object object, String methodName, Object... parameters)