1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name, jobjectArray interfaces, jobject loader, jobjectArray methods, jobjectArray throws) { Thread* self = soa.Self();
//...
StackHandleScope<12> hs(self); // 初始化一个temp_kcass MutableHandle<mirror::Class> temp_klass(hs.NewHandle( AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class)))); if (temp_klass == nullptr) { CHECK(self->IsExceptionPending()); // OOME. return nullptr; } // 给设置一堆标记位 // ...
// Proxies have 1 direct method, the constructor const size_t num_direct_methods = 1;
// List of the actual virtual methods this class will have. std::vector<ArtMethod*> proxied_methods; // 这里会有一堆逻辑进行一下过滤,把private、final、static方法给过滤掉 // 只留下virtual_methods,可以进行代理 // ... const size_t num_virtual_methods = proxied_methods.size(); // 处理throw methods // ... temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods);
// Create the single direct method. CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_));
// Create virtual method using specified prototypes. // TODO These should really use the iterators. for (size_t i = 0; i < num_virtual_methods; ++i) { auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); auto* prototype = proxied_methods[i]; // 重点在这里,这里面会进行方法代理,见2.1.5 CreateProxyMethod(temp_klass, prototype, virtual_method); DCHECK(virtual_method->GetDeclaringClass() != nullptr); DCHECK(prototype->GetDeclaringClass() != nullptr); }
// 把父类设置为java.lang.reflect.Proxy temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this)); // Now effectively in the loaded state. mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self); self->AssertNoPendingException();
// At this point the class is loaded. Publish a ClassLoad event. // Note: this may be a temporary class. It is a listener's responsibility to handle this. Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass);
MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); { // Must hold lock on object when resolved. ObjectLock<mirror::Class> resolution_lock(self, temp_klass); // Link the fields and virtual methods, creating vtable and iftables. // The new class will replace the old one in the class table. Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); // 这里进行link,创建vtable和iftables if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) { if (!temp_klass->IsErroneous()) { mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self); } return nullptr; } }
Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass);
VisiblyInitializedCallback* callback = nullptr; { // Lock on klass is released. Lock new class object. ObjectLock<mirror::Class> initialization_lock(self, klass); // Conservatively go through the ClassStatus::kInitialized state. callback = MarkClassInitialized(self, klass); } if (callback != nullptr) { callback->MakeVisible(self); } return klass.Get(); }
|