ARouter icon indicating copy to clipboard operation
ARouter copied to clipboard

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

Open YuYongzhi opened this issue 3 years ago • 1 comments

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!");
}

YuYongzhi avatar Mar 04 '21 07:03 YuYongzhi

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Mar 04 '21 07:03 CLAassistant