首页
社区
课程
招聘
[求助]在Android P ART源码中,ClassLinker::DefineClass调用了Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine函数。。。。
发表于: 2018-11-28 22:16 3063

[求助]在Android P ART源码中,ClassLinker::DefineClass调用了Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine函数。。。。

2018-11-28 22:16
3063
在Android P ART源码中,ClassLinker::DefineClass调用了Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine函数
ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self,
                                               const char* descriptor,
                                               size_t hash,
                                               Handle<mirror::ClassLoader> class_loader,
                                               const DexFile& dex_file,
                                               const DexFile::ClassDef& dex_class_def) {
  StackHandleScope<3> hs(self);
  auto klass = hs.NewHandle<mirror::Class>(nullptr);

.......
  // Get the real dex file. This will return the input if there aren't any callbacks or they do
  // nothing.
  DexFile const* new_dex_file = nullptr;   //
  DexFile::ClassDef const* new_class_def = nullptr   //
  // TODO We should ideally figure out some way to move this after we get a lock on the klass so it
  // will only be called once.
  /*ClassPreDefine会根据dex_file、dex_class_def生成新的new_dex_file,new_class_def
   */
  Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor,
                                                            klass,
                                                            class_loader,
                                                            dex_file,
                                                            dex_class_def,
                                                            &new_dex_file,
                                                            &new_class_def);
  .......
}
RuntimeCallbacks中对应的是
void RuntimeCallbacks::ClassPreDefine(const char* descriptor,
                                      Handle<mirror::Class> temp_class,
                                      Handle<mirror::ClassLoader> loader,
                                      const DexFile& initial_dex_file,
                                      const DexFile::ClassDef& initial_class_def,
                                      /*out*/DexFile const** final_dex_file,
                                      /*out*/DexFile::ClassDef const** final_class_def) {
  DexFile const* current_dex_file = &initial_dex_file;
  DexFile::ClassDef const* current_class_def = &initial_class_def;
  for (ClassLoadCallback* cb : class_callbacks_) {
    DexFile const* new_dex_file = nullptr;
    DexFile::ClassDef const* new_class_def = nullptr;
	//ClassLoadCallback为 ClassCallback或DbgClassLoadCallback,
    cb->ClassPreDefine(descriptor,
                       temp_class,
                       loader,
                       *current_dex_file,
                       *current_class_def,
                       &new_dex_file,
                       &new_class_def);
    if ((new_dex_file != nullptr && new_dex_file != current_dex_file) ||
        (new_class_def != nullptr && new_class_def != current_class_def)) {
      DCHECK(new_dex_file != nullptr && new_class_def != nullptr);
      current_dex_file = new_dex_file;
      current_class_def = new_class_def;
    }
  }
  *final_dex_file = current_dex_file;
  *final_class_def = current_class_def;
}
而ClassCallback的ClassPreDefine方法中

struct ClassCallback : public art::ClassLoadCallback {
  void ClassPreDefine(const char* descriptor,
                      art::Handle<art::mirror::Class> klass,
                      art::Handle<art::mirror::ClassLoader> class_loader,
                      const art::DexFile& initial_dex_file,
                      const art::DexFile::ClassDef& initial_class_def ATTRIBUTE_UNUSED,
                      /*out*/art::DexFile const** final_dex_file,
                      /*out*/art::DexFile::ClassDef const** final_class_def)
      OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
 ....................
*final_class_def = &dex_file->GetClassDef(0);//返回GetClassDef(0)???????????????
      *final_dex_file = dex_file.release();//返回DexFile
.....................
}
}
final_class_def = &dex_file->GetClassDef(0)返回的永远是第1个ClassDef,这样的话不是与最初传入的initial_class_def类型完全不一样了吗?
ClassLinker::DefineClass 后面还怎么利用new_class_def来加载类?除非class_callbacks_为null,但是在ti_class.cc是将ClassCallback 对象加入 class_callbacks_中。哪位坛友能帮忙指点一下?万分感谢~~


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2018-11-29 11:44 被倔强石头编辑 ,原因:
收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//