Java FieldUtils类使用实例

管理 管理 编辑 删除

实例1: fixBaseContextImplOpsPackage

import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
private void fixBaseContextImplOpsPackage(Context context) throws IllegalAccessException {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 && context != null && !TextUtils.equals(context.getPackageName(), mHostContext.getPackageName())) {
        Context baseContext = context;
        Class clazz = baseContext.getClass();
        Field mOpPackageName = FieldUtils.getDeclaredField(clazz, "mOpPackageName", true);
        if (mOpPackageName != null) {
            Object valueObj = mOpPackageName.get(baseContext);
            if (valueObj instanceof String) {
                String opPackageName = ((String) valueObj);
                if (!TextUtils.equals(opPackageName, mHostContext.getPackageName())) {
                    mOpPackageName.set(baseContext, mHostContext.getPackageName());
                    Log.i(TAG, "fixBaseContextImplOpsPackage OK!Context=%s,", baseContext);
                }
            }
        }
    }
}
 

实例2: fixBaseContextImplContentResolverOpsPackage

import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
private void fixBaseContextImplContentResolverOpsPackage(Context context) throws IllegalAccessException {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 && context != null && !TextUtils.equals(context.getPackageName(), mHostContext.getPackageName())) {
        Context baseContext = context;
        Class clazz = baseContext.getClass();
        Field mContentResolver = FieldUtils.getDeclaredField(clazz, "mContentResolver", true);
        if (mContentResolver != null) {
            Object valueObj = mContentResolver.get(baseContext);
            if (valueObj instanceof ContentResolver) {
                ContentResolver contentResolver = ((ContentResolver) valueObj);
                Field mPackageName = FieldUtils.getDeclaredField(ContentResolver.class, "mPackageName", true);
                Object mPackageNameValueObj = mPackageName.get(contentResolver);
                if (mPackageNameValueObj != null && mPackageNameValueObj instanceof String) {
                    String packageName = ((String) mPackageNameValueObj);
                    if (!TextUtils.equals(packageName, mHostContext.getPackageName())) {
                        mPackageName.set(contentResolver, mHostContext.getPackageName());
                        Log.i(TAG, "fixBaseContextImplContentResolverOpsPackage OK!Context=%s,contentResolver=%s", baseContext, contentResolver);
                    }
                }

            }
        }
    }
}
 

实例3: setIntentClassLoader

import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
private void setIntentClassLoader(Intent intent, ClassLoader classLoader) {
    try {
        Bundle mExtras = (Bundle) FieldUtils.readField(intent, "mExtras");
        if (mExtras != null) {
            mExtras.setClassLoader(classLoader);
        } else {
            Bundle value = new Bundle();
            value.setClassLoader(classLoader);
            FieldUtils.writeField(intent, "mExtras", value);
        }
    } catch (Exception e) {
    } finally {
        intent.setExtrasClassLoader(classLoader);
    }
}
 

实例4: beforeInvoke

import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
@Override
protected boolean beforeInvoke(Object receiver, Method method, Object[] args) throws Throwable {
    //这里适配在android 5.0的机器上无法现实toast的问题。但是我也不知道还有那些机器需要这样做。
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
        int index = 1;
        if (args != null && args.length > index) {
            Object obj = args[index];
            View view = (View) FieldUtils.readField(obj, "mView");
            View nextView = (View) FieldUtils.readField(obj, "mNextView");
            if (nextView != null) {
                FieldUtils.writeField(nextView, "mContext", mHostContext);
            }
            if (view != null) {
                FieldUtils.writeField(view, "mContext", mHostContext);
            }
        }
    }
    return super.beforeInvoke(receiver, method, args);
}
 

实例5: onInstall

import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
@Override
protected void onInstall(ClassLoader classLoader) throws Throwable {

    Object target = ActivityThreadCompat.currentActivityThread();
    Class ActivityThreadClass = ActivityThreadCompat.activityThreadClass();

     /*替换ActivityThread.mInstrumentation,拦截组件调度消息*/
    Field mInstrumentationField = FieldUtils.getField(ActivityThreadClass, "mInstrumentation");
    Instrumentation mInstrumentation = (Instrumentation) FieldUtils.readField(mInstrumentationField, target);
    if (!PluginInstrumentation.class.isInstance(mInstrumentation)) {
        PluginInstrumentation pit = new PluginInstrumentation(mHostContext, mInstrumentation);
        pit.setEnable(isEnable());
        mPluginInstrumentations.add(pit);
        FieldUtils.writeField(mInstrumentationField, target, pit);
        Log.i(TAG, "Install Instrumentation Hook old=%s,new=%s", mInstrumentationField, pit);
    } else {
        Log.i(TAG, "Instrumentation has installed,skip");
    }
}


CRMEB-暮白寒窗雪 最后编辑于2023-03-17 15:34:25

回复({{post_count}})
回复从新到旧

{{item.user_info.nickname}} {{item.user_info.is_threads ? '(管理员)' : item.user_id == thread_uid ? '(作者)' : ''}}

{{item.user_name}} {{item.user_info.is_threads ? '(管理员)' : item.user_id == thread_uid ? '(作者)' : ''}}

{{item.user_info.title}}
{{item.created_at}}  {{item.user_info.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

{{itemc.user_info.is_threads ? '(管理员)' : itemc.user_id == thread_uid ? '(作者)' : ''}}

回复 {{itemc.comment_user_info.nickname}}

{{itemc.created_at}}   {{itemc.user_info.ip_address}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
查看更多
回复
添加回复 ({{post_count}})
53
{{like_count}}
{{collect_count}}

相关推荐

CRMEB-暮白寒窗雪 管理员
社区运营专员---高冷のBoy | 呆萌のGirl

回答

571

发布

766

经验

14195

内容精选
{{item.title}}
{{item.created_at}}
{{item.fic_view_count}}
换一批

快速安全登录

使用微信扫码登录
经验说明

1、发布新帖子,每发布一条帖子可获得20个经验,每天最多可获取200个经验;

2、发布新回复,每发布一条回复可获得10个经验,每天最多可获取200个经验;

3、发布的帖子被管理员设置为精华帖,每被加精一次可获得10个经验,无上限;

4、发布帖子被回复一条,加2个经验,无上限;

5、发布帖子被点赞一个或发布评论被点赞加5个经验,无上限;

6、论坛首页大神积分排行榜,统计90天以内获取的累积经验值进行排序。

7、技术社区经验和官方商城用户经验同步,累计经验值会提升商城会员等级,购买官方商城产品享受对应会员等级折扣

确定
{{item.label}} 首页头条 首页动态 加精 板块推荐 取 消 确 定
回复
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 开源下载 CRMEB官方论坛 帮助文档
返回顶部 返回顶部
CRMEB客服