Here you can find the source of invoke(Object methodHostInstance, String methodName, Class>[] parameterTypes, Object[] args)
Parameter | Description |
---|---|
methodHostInstance | a parameter |
methodName | a parameter |
parameterTypes | a parameter |
args | a parameter |
public static Object invoke(Object methodHostInstance, String methodName, Class<?>[] parameterTypes, Object[] args)
//package com.java2s; //License from project: Open Source License import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static Object invoke(Object methodHostInstance, String methodName, Object arg) { Class<?>[] parameterTypes = { arg.getClass() }; Object[] args = { arg };/*from w w w . j ava 2s . c o m*/ return invoke(methodHostInstance, methodName, parameterTypes, args); } public static Object invoke(Object methodHostInstance, String methodName, Class<?>[] parameterTypes, Object[] args) { try { Method method = methodHostInstance.getClass().getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); try { Object result = method.invoke(methodHostInstance, args); return result; } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } finally { method.setAccessible(false); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } }