Android examples for java.lang.reflect:Method Get
get Method from object by method name and parameter object type
import java.lang.reflect.Method; import android.content.Context; import android.util.Log; public class Main { private static final String TAG = ""; public static Method getMethod(Object object, String methodName, Object[] types) {/*from w w w . j a va 2s.co m*/ Method method = null; try { Class<?>[] typeClassArray = null; if (types != null && types.length > 0) { typeClassArray = new Class<?>[types.length]; for (int i = 0; i < types.length; i++) { typeClassArray[i] = (Class<?>) types[i]; } } else { typeClassArray = new Class<?>[] {}; } method = object.getClass().getMethod(methodName, typeClassArray); } catch (NoSuchMethodException e) { Log.w(TAG, "Method \"" + methodName + "\" not found for " + object.getClass().toString()); } catch (Exception e) { Log.e(TAG, e.getMessage()); } return method; } public static Class<?> getClass(Context context, String className) { Class<?> classDefinition = null; if (className.contains(context.getPackageName())) { classDefinition = getClass(className); } else { classDefinition = getClass(context.getPackageName() + className); } return classDefinition; } public static Class<?> getClass(String className) { Class<?> classDefinition = null; try { classDefinition = Class.forName(className); } catch (ClassNotFoundException e) { Log.w(TAG, "Class \"" + className + "\" not found"); } return classDefinition; } }