随笔杂记

   :: 首页 :: 新随笔 ::  ::  :: 管理 ::
使用:
(Boolean) invokeMethod(LockPatternUtils.class, mLockutils,
                    "savedPasswordExists", new Class[] {int.class}, new Object[] {UserHandle.myUserId()});

(Boolean) invokeMethod(LockPatternUtils.class, mLockutils, "checkPattern",
                    new Class[] {List.classint.class}, new Object[] {null, UserHandle.myUserId()});

方法实现:

    public static Method getMethod(Class<?> cls, String methodName, Class<?> parameterTypes) {
        try {
            return cls.getDeclaredMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "getMethod() Exception: ", e);
            try {
                return cls.getMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException ex) {
                Log.e(TAG, "getMethod() Exception: ", ex);
                return null;
            }
        }
    }

    public static Object invokeStaticMethod(Class<?> cls, String methodName) {
        return invokeMethod(cls, null, methodName, nullnull);
    }

    public static Object invokeStaticMethod(Class<?> cls, String methodName, Class<?>[] parasTypes,
            Object[] parasObjs) {
        return invokeMethod(cls, null, methodName, parasTypes, parasObjs);
    }

    public static Object invokeMethod(Class<?> cls, Object obj, String methodName) {
        return invokeMethod(cls, obj, methodName, nullnull);
    }

    public static Object invokeMethod(Class<?> cls, Object obj, String methodName, Class<?>[] parasTypes,
            Object[] parasObjs) {
        Method method = getMethod(cls, methodName, parasTypes);
        try {
            if (method != null) {
                method.setAccessible(true);
                return method.invoke(obj, parasObjs);
            }
        } catch (Exception e) {
            Log.e(TAG, "invokeStaticMethod() Exception: ", e);
        }
        return null;
    }
    
posted on 2015-12-18 09:42 天宇恒星 阅读(220) 评论(0)  编辑  收藏 所属分类: Java