List of utility methods to do Reflection Method Invoke
Object | invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args) Invokes the method return invokeMethod(handle.getClass(), handle, methodName, parameterClasses, args);
|
Object | invokeMethod(Object handler, String strMethod, Class>[] cls, Object... params) invoke Method if (handler == null || strMethod == null) return null; Method method = null; try { if (cls == null) cls = new Class[0]; method = handler.getClass().getMethod(strMethod, cls); return method.invoke(handler, params); ... |
T | invokeMethod(Object instance, String methodName, Class Invoke a no-arg method using Reflection API. return invokeMethod(instance.getClass(), instance, methodName, NO_PARAMS, NO_ARGS, expectedReturnType);
|
Object | invokeMethod(Object o, String fieldName) invoke Method try { final Method m = o.getClass().getMethod(fieldName); return m.invoke(o); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException("Reflection error!", e); |
Object | invokeMethod(Object o, String methodName, Object... args) Invokes method on object. Method[] methods = o.getClass().getDeclaredMethods(); List<Method> reduce = new ArrayList<>(Arrays.asList(methods)); for (Iterator<Method> i = reduce.iterator(); i.hasNext();) { Method m = i.next(); if (!methodName.equals(m.getName())) { i.remove(); continue; Class<?>[] parameterTypes = m.getParameterTypes(); if (parameterTypes.length != args.length) { i.remove(); continue; if (reduce.isEmpty()) { throw new RuntimeException(String.format("TEST ISSUE Could not find method %s on %s with %d arguments.", methodName, o.getClass().getName(), args.length)); if (reduce.size() > 1) { throw new RuntimeException( String.format("TEST ISSUE Could not find unique method %s on %s. Found with %d matches.", methodName, o.getClass().getName(), reduce.size())); Method method = reduce.iterator().next(); method.setAccessible(true); try { return method.invoke(o, args); } catch (IllegalAccessException e) { throw e; } catch (InvocationTargetException e) { throw e.getTargetException(); |
Object | invokeMethod(Object o, String methodName, Object[] params) invoke Method Method methods[] = o.getClass().getMethods(); for (int i = 0; i < methods.length; ++i) { if (methodName.equals(methods[i].getName())) { try { methods[i].setAccessible(true); return methods[i].invoke(o, params); } catch (IllegalAccessException ex) { return null; ... |
Object | invokeMethod(Object obj, Class type, String name, Class[] parameterTypes, Object[] parameters) Invokes a method using reflection, pass out exception from invoked method. Method method = type.getDeclaredMethod(name, parameterTypes); try { if (method.isAccessible()) { return method.invoke(obj, parameters); } else { try { method.setAccessible(true); return method.invoke(obj, parameters); ... |
Object | invokeMethod(Object obj, Method method, Object... args) invoke Method Object result; method.setAccessible(true); try { result = method.invoke(obj, args); } catch (IllegalAccessException | InvocationTargetException e) { logger.error("invoke method failure", e); throw new RuntimeException(e); return result; |
Object | invokeMethod(Object obj, String mehtodName, Object... parameter) invoke Method Class c = obj.getClass(); Method method = null; Object result = null; if (parameter == null) { try { method = c.getMethod(mehtodName, new Class[] {}); result = method.invoke(obj, new Object[] {}); } catch (SecurityException e) { ... |
Object | invokeMethod(Object obj, String method) Invoke the method without parameters over the target object return invokeMethod(obj, method, new Object[0]); |