Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加 getIntent()、getArguments()为空时可能引起 NPE 的判断 #903

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

YuYongzhi
Copy link

@YuYongzhi YuYongzhi commented Mar 4, 2021

1、添加对 getIntent()getIntent().getExtras()getArguments()可能为null的情况进行判断。
最终生成的代码示例如下:
Activity:

android.os.Bundle bundle = substitute.getIntent().getExtras();

Fragment:

android.os.Bundle bundle = substitute.getArguments();
substitute.age = bundle == null ? substitute.age : bundle.getInt("age", substitute.age);
substitute.height = bundle == null ? substitute.height : bundle.getInt("height", substitute.height);
substitute.girl = bundle == null ? substitute.girl : bundle.getBoolean("boy", substitute.girl);
substitute.ch = bundle == null ? substitute.ch : bundle.getChar("ch", substitute.ch);
substitute.fl = bundle == null ? substitute.fl : bundle.getFloat("fl", substitute.fl);
substitute.dou = bundle == null ? substitute.dou : bundle.getDouble("dou", substitute.dou);
substitute.ser = bundle == null ? substitute.ser : (com.alibaba.android.arouter.demo.service.model.TestSerializable) bundle.getSerializable("ser");
substitute.pac = bundle == null ? substitute.pac : (com.alibaba.android.arouter.demo.service.model.TestParcelable) bundle.getParcelable("pac");

2、添加对 OBJECT 类型,使用serializationService.parseObject时,保留字段默认值的功能。

先把serializationService.parseObject的值赋值给"$T default_" + fieldName,再判断不为null时,再赋值给"substitute." + fieldName
生成的示例代码:

if (null != serializationService) {
    List<TestObj> default_objList = serializationService.parseObject(bundle.getString("objList"), new com.alibaba.android.arouter.facade.model.TypeWrapper<List<TestObj>>(){}.getType());
    if (null != default_objList) {
        substitute.objList = default_objList;
    }
} else {
    Log.e("ARouter::", "You want automatic inject the field 'objList' in class 'BlankFragment' , then you should implement 'SerializationService' to support object auto inject!");
}

…l, use default value for serializationService.parseObject
@CLAassistant
Copy link

CLAassistant commented Mar 4, 2021

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants