Here you can find the source of invokeMethodAndGetRecursively(Object object, String methodName, Object... args)
Parameter | Description |
---|---|
object | target object. |
methodName | name of the method. |
args | arguments. |
Parameter | Description |
---|---|
Exception | an exception |
public static Object invokeMethodAndGetRecursively(Object object, String methodName, Object... args) throws Exception
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Method; public class Main { /**/*from ww w . j a v a 2s. c o m*/ * Invokes and returns result of method with given name and arguments of target object's class or one of it's superclasses. * @param object target object. * @param methodName name of the method. * @param args arguments. * @return result of method invokation. * @throws Exception */ public static Object invokeMethodAndGetRecursively(Object object, String methodName, Object... args) throws Exception { Class clazz = object.getClass(); boolean ended = false; do { for (Method m : clazz.getDeclaredMethods()) if (m.getName().equalsIgnoreCase(methodName)) { ended = true; m.setAccessible(true); try { return m.invoke(object, args); } finally { m.setAccessible(false); } } clazz = clazz.getSuperclass(); } while (clazz != Object.class && !ended); throw new NullPointerException("There is no method with given name"); } }