Here you can find the source of invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args)
Parameter | Description |
---|---|
handle | The handle to invoke it on |
methodName | The name of the method |
parameterClasses | The parameter types |
args | The arguments |
@SuppressWarnings("unused") @Nullable public static Object invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args)
//package com.java2s; //License from project: Open Source License import javax.annotation.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Optional; public class Main { /**/*from ww w .j av a 2s. c om*/ * Invokes the method * * @param handle The handle to invoke it on * @param methodName The name of the method * @param parameterClasses The parameter types * @param args The arguments * * @return The resulting object or null if an error occurred / the method didn't return a thing */ @SuppressWarnings("unused") @Nullable public static Object invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args) { return invokeMethod(handle.getClass(), handle, methodName, parameterClasses, args); } /** * Invokes the method * * @param clazz The class to invoke it from * @param handle The handle to invoke it on * @param methodName The name of the method * @param parameterClasses The parameter types * @param args The arguments * * @return The resulting object or null if an error occurred / the method didn't return a thing */ @Nullable public static Object invokeMethod(Class<?> clazz, Object handle, String methodName, Class[] parameterClasses, Object... args) { Optional<Method> methodOptional = getMethod(clazz, methodName, parameterClasses); if (!methodOptional.isPresent()) { return null; } Method method = methodOptional.get(); try { return method.invoke(handle, args); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; } /** * Invokes the method * * @param method The method to invoke * @param handle The handle to invoke on * @param args The arguments * * @return The resulting object or null if an error occurred / the method didn't return a thing */ public static Object invokeMethod(Method method, Object handle, Object... args) { try { return method.invoke(handle, args); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; } /** * Invokes the method * * @param rethrowMethodErrors If true, any exceptions by the method invoked will be propagated up * @param method The method to invoke * @param handle The handle to invoke on * @param args The arguments * * @return The resulting object or null if an error occurred / the method didn't return a thing * * @throws InvocationTargetException if an exception is thrown by the method */ public static Object invokeMethod(boolean rethrowMethodErrors, Method method, Object handle, Object... args) throws InvocationTargetException { try { return method.invoke(handle, args); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { if (rethrowMethodErrors) { throw e; } else { e.printStackTrace(); } } return null; } /** * @param clazz The class to get the method for * @param name The name of the method * @param params The parameters of the method * * @return The method, if any */ public static Optional<Method> getMethod(Class<?> clazz, String name, Class<?>... params) { try { return Optional.of(clazz.getMethod(name, params)); } catch (NoSuchMethodException ignored) { } try { return Optional.of(clazz.getDeclaredMethod(name, params)); } catch (NoSuchMethodException ignored) { } return Optional.empty(); } }