Here you can find the source of invoke(boolean makeAccessible, T object, Class super T> methodClass, String methodName, Class>[] paramTypes, Object... params)
Parameter | Description |
---|---|
makeAccessible | if true, uses AccessibleObject#setAccessible(boolean) to make the method accessible, even when it's protected. As Java documentation says, this will possibly trigger exceptions (wrapped by a RuntimeException ). |
object | the object for which the method is invoked |
methodClass | the class where the method is declared (might be an objec't superclass and you need to identify the declaring class). |
methodName | the method name |
paramTypes | the types of parameters the method accepts (it's like in Class#getDeclaredMethod(String,Class) . |
params | the actual parameters for the invocation. |
@SuppressWarnings("unchecked") public static <T, RT> RT invoke(boolean makeAccessible, T object, Class<? super T> methodClass, String methodName, Class<?>[] paramTypes, Object... params)
//package com.java2s; /*/* w ww . ja va 2s .com*/ * __________ * CREDITS * __________ * * Team page: http://isatab.sf.net/ * - Marco Brandizi (software engineer: ISAvalidator, ISAconverter, BII data management utility, BII model) * - Eamonn Maguire (software engineer: ISAcreator, ISAcreator configurator, ISAvalidator, ISAconverter, BII data management utility, BII web) * - Nataliya Sklyar (software engineer: BII web application, BII model, BII data management utility) * - Philippe Rocca-Serra (technical coordinator: user requirements and standards compliance for ISA software, ISA-tab format specification, BII model, ISAcreator wizard, ontology) * - Susanna-Assunta Sansone (coordinator: ISA infrastructure design, standards compliance, ISA-tab format specification, BII model, funds raising) * * Contributors: * - Manon Delahaye (ISA team trainee: BII web services) * - Richard Evans (ISA team trainee: rISAtab) * * * ______________________ * Contacts and Feedback: * ______________________ * * Project overview: http://isatab.sourceforge.net/ * * To follow general discussion: isatab-devel@list.sourceforge.net * To contact the developers: isatools@googlegroups.com * * To report bugs: http://sourceforge.net/tracker/?group_id=215183&atid=1032649 * To request enhancements: http://sourceforge.net/tracker/?group_id=215183&atid=1032652 * * * __________ * License: * __________ * * This work is licenced under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License. To view a copy of this licence, visit http://creativecommons.org/licenses/by-sa/2.0/uk/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA. * * __________ * Sponsors * __________ * This work has been funded mainly by the EU Carcinogenomics (http://www.carcinogenomics.eu) [PL 037712] and in part by the * EU NuGO [NoE 503630](http://www.nugo.org/everyone) projects and in part by EMBL-EBI. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /** * Invokes a method by means of reflection. This is a shortcut for the classes and methods existing in the standard * reflection package. * * @param makeAccessible if true, uses {@link AccessibleObject#setAccessible(boolean)} to make the method accessible, even * when it's protected. As Java documentation says, this will possibly trigger exceptions (wrapped by a {@link RuntimeException}). * * @param object the object for which the method is invoked * @param methodClass the class where the method is declared (might be an objec't superclass and you need to identify the * declaring class). * @param methodName the method name * @param paramTypes the types of parameters the method accepts (it's like in {@link Class#getDeclaredMethod(String, Class...)}. * @param params the actual parameters for the invocation. * * @return whatever the method returns, or null if it's void. */ @SuppressWarnings("unchecked") public static <T, RT> RT invoke(boolean makeAccessible, T object, Class<? super T> methodClass, String methodName, Class<?>[] paramTypes, Object... params) { try { if (object == null) throw new NullPointerException("Cannot invoke the method '" + methodName + "' over a null object"); Method setPropM = methodClass.getDeclaredMethod(methodName, paramTypes); if (makeAccessible) setPropM.setAccessible(true); return (RT) setPropM.invoke(object, params); } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new RuntimeException(String.format("Internal Error while invoking '%s'.'%s': %s", object.getClass().getSimpleName(), methodName, ex.getMessage()), ex); } } /** * Default class is object.getClass(). {@link NullPointerException} if object is null. */ @SuppressWarnings("unchecked") public static <RT> RT invoke(boolean makeAccessible, Object object, String methodName, Class<?>[] paramTypes, Object... params) { if (object == null) throw new NullPointerException("Cannot invoke the method '" + methodName + "' over a null object"); return invoke(makeAccessible, object, (Class<Object>) object.getClass(), methodName, paramTypes, params); } /** * Defaults to makeAccessible = true. */ @SuppressWarnings("unchecked") public static <T, RT> RT invoke(Object object, Class<? super T> methodClass, String methodName, Class<?>[] paramTypes, Object... params) { return invoke(true, object, (Class<Object>) methodClass, methodName, paramTypes, params); } /** * Defaults to methodClass = object.getClass() and makeAccessible = true. {@link NullPointerException} if object is null. */ @SuppressWarnings("unchecked") public static <T, RT> RT invoke(Object object, String methodName, Class<?>[] paramTypes, Object... params) { return invoke(true, object, (Class<Object>) object.getClass(), methodName, paramTypes, params); } }