Java tutorial
//package com.java2s; import android.support.annotation.NonNull; import java.lang.reflect.Method; public class Main { public static <T> T callMethod(@NonNull Object obj, @NonNull String name, Object... args) throws Exception { for (Class cls = obj.getClass(); // cls != null && cls != Object.class; // cls = cls.getSuperclass()) { for (Method m : cls.getDeclaredMethods()) { if (m.getName().equals(name)) { m.setAccessible(true); //noinspection unchecked return (T) m.invoke(obj, args); } } } throw new RuntimeException("no method matching " + name); } }