Android examples for java.lang.reflect:Method Invoke
try to Invoke a method on an Object
//package com.java2s; import android.support.v4.util.SimpleArrayMap; import android.util.Log; import java.lang.reflect.Method; public class Main { private final static String TAG = "DrawableReflectiveUtils"; private static SimpleArrayMap<String, Method> sCachedMethods = new SimpleArrayMap<>(); @SuppressWarnings("unchecked") public static <T> T tryInvoke(Object target, String methodName, Class<?>[] argTypes, Object... args) { try {/*from w w w . j a va 2s . c o m*/ Method method = sCachedMethods.get(methodName); if (method != null) { return (T) method.invoke(target, args); } method = target.getClass().getDeclaredMethod(methodName, argTypes); sCachedMethods.put(methodName, method); return (T) method.invoke(target, args); } catch (Exception pokemon) { Log.e(TAG, "Unable to invoke " + methodName + " on " + target, pokemon); } return null; } }