Java examples for Reflection:Method
A helper method to invoke a method via reflection and wrap any exceptions as RuntimeException instances
//package com.java2s; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /**//w w w.j a v a2s .c o m * A helper method to invoke a method via reflection and wrap any exceptions * as {@link RuntimeException} instances * * @param method the method to invoke * @param instance the object instance (or null for static methods) * @param parameters the parameters to the method * @return the result of the method invocation */ public static Object invokeMethod(Method method, Object instance, Object... parameters) { if (method == null) { return null; } try { return method.invoke(instance, parameters); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getCause()); } } }