invoke Static Method - Android java.lang.reflect

Android examples for java.lang.reflect:Method Invoke

Description

invoke Static Method

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

import android.util.Log;

public class Main {
    private static final String TAG = "loader";

    public static Object invokeStaticMethod(Class cls, Class[] type,
            String methodname, Object[] args) {
        try {/* w  w  w.j  av  a 2  s  . co m*/
            Method method = cls.getDeclaredMethod(methodname, type);
            if (method == null)
                return null;
            method.setAccessible(true);
            return method.invoke(null, args);
        } catch (Exception e) {

            Log.e(TAG, "", e);
            return null;
        }
    }

    public static Object invokeStaticMethod(String clsName, Class[] type,
            String methodname, Object[] args) {
        Class clzz = null;
        try {
            clzz = Class.forName(clsName);
        } catch (Exception e) {
            clzz = null;
        }
        if (clzz == null)
            return null;
        return invokeStaticMethod(clzz, type, methodname, args);
    }
}

Related Tutorials