Invoke method, Returns results if available, otherwise returns null. - Android java.lang.reflect

Android examples for java.lang.reflect:Method Invoke

Description

Invoke method, Returns results if available, otherwise returns null.

Demo Code


//package com.java2s;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.util.Log;

public class Main {
    /** Returns results if available, otherwise returns null. */
    public static Object invokeMethod(Method method, Object receiver,
            Object... args) {//from w w w.  j  av  a  2s  . c  o  m
        try {
            return method.invoke(receiver, args);
        } catch (IllegalArgumentException e) {
            Log.e("Safe invoke fail", "Invalid args", e);
        } catch (IllegalAccessException e) {
            Log.e("Safe invoke fail", "Invalid access", e);
        } catch (InvocationTargetException e) {
            Log.e("Safe invoke fail", "Invalid target", e);
        }

        return null;
    }
}

Related Tutorials