Heap allocate class supporting arrays for fields, methods, interfaces
Other supporting changes
- fix ref to reflect
- add shared array_interfaces_ array_iftable_
- fixed issue with loaded class overwriting special object_size_ cases
- added Class::interfaces_idx to avoid reuse of interfaces_ for non-Object data
Change-Id: Ibcbeee6ac001baefdbba9a194123b3c6fe488f1f
diff --git a/src/class_linker.cc b/src/class_linker.cc
index 04f703b..6ee3af3 100644
--- a/src/class_linker.cc
+++ b/src/class_linker.cc
@@ -29,11 +29,6 @@
void ClassLinker::Init(std::vector<DexFile*> boot_class_path) {
- // setup boot_class_path_ so that object_array_class_ can be properly initialized
- for (size_t i = 0; i != boot_class_path.size(); ++i) {
- AppendToBootClassPath(boot_class_path[i]);
- }
-
// Allocate and partially initialize the Class, Object, Field, Method classes.
// Initialization will be completed when the definitions are loaded.
java_lang_Class_ = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class)));
@@ -48,21 +43,13 @@
java_lang_Class_->super_class_ = java_lang_Object_;
- java_lang_ref_Field_ = AllocClass(NULL);
- CHECK(java_lang_ref_Field_ != NULL);
- java_lang_ref_Field_->descriptor_ = "Ljava/lang/ref/Field;";
+ java_lang_reflect_Field_ = AllocClass(NULL);
+ CHECK(java_lang_reflect_Field_ != NULL);
+ java_lang_reflect_Field_->descriptor_ = "Ljava/lang/reflect/Field;";
- java_lang_ref_Method_ = AllocClass(NULL);
- CHECK(java_lang_ref_Method_ != NULL);
- java_lang_ref_Method_->descriptor_ = "Ljava/lang/Method;";
-
- java_lang_Cloneable_ = AllocClass(NULL);
- CHECK(java_lang_Cloneable_ != NULL);
- java_lang_Cloneable_->descriptor_ = "Ljava/lang/Cloneable;";
-
- java_io_Serializable_ = AllocClass(NULL);
- CHECK(java_io_Serializable_ != NULL);
- java_io_Serializable_->descriptor_ = "Ljava/io/Serializable;";
+ java_lang_reflect_Method_ = AllocClass(NULL);
+ CHECK(java_lang_reflect_Method_ != NULL);
+ java_lang_reflect_Method_->descriptor_ = "Ljava/lang/reflect/Method;";
java_lang_String_ = AllocClass(NULL);
CHECK(java_lang_String_ != NULL);
@@ -79,11 +66,50 @@
primitive_boolean_ = CreatePrimitiveClass("Z");
primitive_void_ = CreatePrimitiveClass("V");
+ // object_array_class_ is needed to heap alloc DexCache instances
+ // created by AppendToBootClassPath below
+ object_array_class_ = AllocClass(NULL);
+ CHECK(object_array_class_ != NULL);
+
+ // setup boot_class_path_ so that the below array classes can be
+ // initialized using the normal FindSystemClass API
+ for (size_t i = 0; i != boot_class_path.size(); ++i) {
+ AppendToBootClassPath(boot_class_path[i]);
+ }
+
+ // A single, global copy of "interfaces" and "iftable" for reuse across array classes
+ java_lang_Cloneable_ = AllocClass(NULL);
+ CHECK(java_lang_Cloneable_ != NULL);
+ java_lang_Cloneable_->descriptor_ = "Ljava/lang/Cloneable;";
+
+ java_io_Serializable_ = AllocClass(NULL);
+ CHECK(java_io_Serializable_ != NULL);
+ java_io_Serializable_->descriptor_ = "Ljava/io/Serializable;";
+
+ array_interfaces_ = AllocObjectArray(2);
+ CHECK(array_interfaces_ != NULL);
+ array_interfaces_->Set(0, java_lang_Cloneable_);
+ array_interfaces_->Set(1, java_io_Serializable_);
+
+ // We assume that Cloneable/Serializable don't have superinterfaces --
+ // normally we'd have to crawl up and explicitly list all of the
+ // supers as well. These interfaces don't have any methods, so we
+ // don't have to worry about the ifviPool either.
+ array_iftable_ = new InterfaceEntry[2];
+ CHECK(array_iftable_ != NULL);
+ memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
+ array_iftable_[0].SetClass(down_cast<Class*>(array_interfaces_->Get(0)));
+ array_iftable_[1].SetClass(down_cast<Class*>(array_interfaces_->Get(1)));
+
char_array_class_ = FindSystemClass("[C");
CHECK(char_array_class_ != NULL);
+ class_array_class_ = FindSystemClass("[Ljava/lang/Class;");
+ CHECK(class_array_class_ != NULL);
+ field_array_class_ = FindSystemClass("[Ljava/lang/reflect/Field;");
+ CHECK(field_array_class_ != NULL);
+ method_array_class_ = FindSystemClass("[Ljava/lang/reflect/Method;");
+ CHECK(method_array_class_ != NULL);
- object_array_class_ = FindSystemClass("[Ljava/lang/Object;");
- CHECK(object_array_class_ != NULL);
}
DexCache* ClassLinker::AllocDexCache() {
@@ -97,17 +123,17 @@
}
StaticField* ClassLinker::AllocStaticField() {
- return down_cast<StaticField*>(Heap::AllocObject(java_lang_ref_Field_,
+ return down_cast<StaticField*>(Heap::AllocObject(java_lang_reflect_Field_,
sizeof(StaticField)));
}
InstanceField* ClassLinker::AllocInstanceField() {
- return down_cast<InstanceField*>(Heap::AllocObject(java_lang_ref_Field_,
+ return down_cast<InstanceField*>(Heap::AllocObject(java_lang_reflect_Field_,
sizeof(InstanceField)));
}
Method* ClassLinker::AllocMethod() {
- return down_cast<Method*>(Heap::AllocObject(java_lang_ref_Method_,
+ return down_cast<Method*>(Heap::AllocObject(java_lang_reflect_Method_,
sizeof(Method)));
}
@@ -143,6 +169,7 @@
const DexFile::ClassDef* dex_class_def = pair.second;
DexCache* dex_cache = FindDexCache(dex_file);
// Load the class from the dex file.
+ // TODO add fast path to avoid all these comparisons once special cases are found
if (descriptor == "Ljava/lang/Object;") {
klass = java_lang_Object_;
klass->dex_cache_ = dex_cache;
@@ -152,26 +179,31 @@
klass = java_lang_Class_;
klass->dex_cache_ = dex_cache;
klass->object_size_ = sizeof(Class);
- } else if (descriptor == "Ljava/lang/ref/Field;") {
- klass = java_lang_ref_Field_;
+ } else if (descriptor == "Ljava/lang/reflect/Field;") {
+ klass = java_lang_reflect_Field_;
klass->dex_cache_ = dex_cache;
klass->object_size_ = sizeof(Field);
- } else if (descriptor == "Ljava/lang/ref/Method;") {
- klass = java_lang_ref_Method_;
+ } else if (descriptor == "Ljava/lang/reflect/Method;") {
+ klass = java_lang_reflect_Method_;
klass->dex_cache_ = dex_cache;
klass->object_size_ = sizeof(Method);
- } else if (descriptor == "Ljava/lang/Cloneable;") {
- klass = java_lang_Cloneable_;
- klass->dex_cache_ = dex_cache;
- } else if (descriptor == "Ljava/io/Serializable;") {
- klass = java_io_Serializable_;
- klass->dex_cache_ = dex_cache;
} else if (descriptor == "Ljava/lang/String;") {
klass = java_lang_String_;
klass->dex_cache_ = dex_cache;
klass->object_size_ = sizeof(String);
+ } else if (descriptor == "Ljava/lang/Cloneable;") {
+ klass = java_lang_Cloneable_;
+ klass->dex_cache_ = dex_cache;
+ klass->object_size_ = 0;
+ } else if (descriptor == "Ljava/io/Serializable;") {
+ klass = java_io_Serializable_;
+ klass->dex_cache_ = dex_cache;
+ klass->object_size_ = 0;
} else {
klass = AllocClass(dex_cache);
+ // LinkInstanceFields only will update object_size_ if it is 0
+ // to avoid overwriting the special initialized cases above.
+ klass->object_size_ = 0;
}
LoadClass(*dex_file, *dex_class_def, klass);
// Check for a pending exception during load
@@ -245,10 +277,10 @@
klass->super_class_ = NULL;
klass->super_class_idx_ = dex_class_def.superclass_idx_;
- klass->num_static_fields_ = header.static_fields_size_;
- klass->num_instance_fields_ = header.instance_fields_size_;
- klass->num_direct_methods_ = header.direct_methods_size_;
- klass->num_virtual_methods_ = header.virtual_methods_size_;
+ size_t num_static_fields = header.static_fields_size_;
+ size_t num_instance_fields = header.instance_fields_size_;
+ size_t num_direct_methods = header.direct_methods_size_;
+ size_t num_virtual_methods = header.virtual_methods_size_;
klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
@@ -256,58 +288,61 @@
LoadInterfaces(dex_file, dex_class_def, klass);
// Load static fields.
- if (klass->NumStaticFields() != 0) {
- // TODO: allocate on the object heap.
- klass->sfields_ = new StaticField*[klass->NumStaticFields()]();
+ DCHECK(klass->sfields_ == NULL);
+ if (num_static_fields != 0) {
+ klass->sfields_ = AllocObjectArray(num_static_fields);
uint32_t last_idx = 0;
for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
DexFile::Field dex_field;
dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
StaticField* sfield = AllocStaticField();
- klass->sfields_[i] = sfield;
+ klass->SetStaticField(i, sfield);
LoadField(dex_file, dex_field, klass, sfield);
}
}
// Load instance fields.
- if (klass->NumInstanceFields() != 0) {
+ DCHECK(klass->ifields_ == NULL);
+ if (num_instance_fields != 0) {
// TODO: allocate on the object heap.
- klass->ifields_ = new InstanceField*[klass->NumInstanceFields()]();
+ klass->ifields_ = AllocObjectArray(num_instance_fields);
uint32_t last_idx = 0;
for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
DexFile::Field dex_field;
dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
InstanceField* ifield = AllocInstanceField();
- klass->ifields_[i] = ifield;
+ klass->SetInstanceField(i, ifield);
LoadField(dex_file, dex_field, klass, ifield);
}
}
// Load direct methods.
- if (klass->NumDirectMethods() != 0) {
+ DCHECK(klass->direct_methods_ == NULL);
+ if (num_direct_methods != 0) {
// TODO: append direct methods to class object
- klass->direct_methods_ = new Method*[klass->NumDirectMethods()]();
+ klass->direct_methods_ = AllocObjectArray(num_direct_methods);
uint32_t last_idx = 0;
for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
DexFile::Method dex_method;
dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Method* meth = AllocMethod();
- klass->direct_methods_[i] = meth;
+ klass->SetDirectMethod(i, meth);
LoadMethod(dex_file, dex_method, klass, meth);
// TODO: register maps
}
}
// Load virtual methods.
- if (klass->NumVirtualMethods() != 0) {
+ DCHECK(klass->virtual_methods_ == NULL);
+ if (num_virtual_methods != 0) {
// TODO: append virtual methods to class object
- klass->virtual_methods_ = new Method*[klass->NumVirtualMethods()]();
+ klass->virtual_methods_ = AllocObjectArray(num_virtual_methods);
uint32_t last_idx = 0;
for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
DexFile::Method dex_method;
dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Method* meth = AllocMethod();
- klass->virtual_methods_[i] = meth;
+ klass->SetVirtualMethod(i, meth);
LoadMethod(dex_file, dex_method, klass, meth);
// TODO: register maps
}
@@ -319,12 +354,13 @@
Class* klass) {
const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
if (list != NULL) {
- klass->interface_count_ = list->Size();
- // TODO: allocate the interfaces array on the object heap.
- klass->interfaces_ = new Class*[list->Size()]();
+ DCHECK(klass->interfaces_ == NULL);
+ klass->interfaces_ = AllocObjectArray(list->Size());
+ DCHECK(klass->interfaces_idx_ == NULL);
+ klass->interfaces_idx_ = new uint32_t[list->Size()];
for (size_t i = 0; i < list->Size(); ++i) {
const DexFile::TypeItem& type_item = list->GetTypeItem(i);
- klass->interfaces_[i] = reinterpret_cast<Class*>(type_item.type_idx_);
+ klass->interfaces_idx_[i] = type_item.type_idx_;
}
}
}
@@ -515,9 +551,16 @@
//
// Array classes are simple enough that we don't need to do a full
// link step.
- Class* new_class = AllocClass(NULL);
- if (new_class == NULL) {
- return NULL;
+
+ Class* new_class;
+ if (descriptor == "[Ljava/lang/Object;") {
+ CHECK(object_array_class_);
+ new_class = object_array_class_;
+ } else {
+ new_class = AllocClass(NULL);
+ if (new_class == NULL) {
+ return NULL;
+ }
}
new_class->descriptor_alloc_ = new std::string(descriptor.data(),
descriptor.size());
@@ -542,26 +585,15 @@
// so we need to make sure the class object is GC-valid while we're in
// there. Do this by clearing the interface list so the GC will just
// think that the entries are null.
- //
- // TODO?
- // We may want to create a single, global copy of "interfaces" and
- // "iftable" somewhere near the start and just point to those (and
- // remember not to free them for arrays).
- new_class->interface_count_ = 2;
- new_class->interfaces_ = new Class*[2];
- memset(new_class->interfaces_, 0, sizeof(Class*) * 2);
- new_class->interfaces_[0] = java_lang_Cloneable_;
- new_class->interfaces_[1] = java_io_Serializable_;
- // We assume that Cloneable/Serializable don't have superinterfaces --
- // normally we'd have to crawl up and explicitly list all of the
- // supers as well. These interfaces don't have any methods, so we
- // don't have to worry about the ifviPool either.
+
+ // Use the single, global copies of "interfaces" and "iftable"
+ // (remember not to free them for arrays).
+ DCHECK(array_interfaces_ != NULL);
+ new_class->interfaces_ = array_interfaces_;
new_class->iftable_count_ = 2;
- new_class->iftable_ = new InterfaceEntry[2];
- memset(new_class->iftable_, 0, sizeof(InterfaceEntry) * 2);
- new_class->iftable_[0].SetClass(new_class->interfaces_[0]);
- new_class->iftable_[1].SetClass(new_class->interfaces_[1]);
+ DCHECK(array_iftable_ != NULL);
+ new_class->iftable_ = array_iftable_;
// Inherit access flags from the component type. Arrays can't be
// used as a superclass or interface, so we want to add "final"
@@ -992,15 +1024,6 @@
}
bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
- scoped_array<uint32_t> interfaces_idx;
- // TODO: store interfaces_idx in the Class object
- // TODO: move this outside of link interfaces
- if (klass->interface_count_ > 0) {
- size_t length = klass->interface_count_ * sizeof(klass->interfaces_[0]);
- interfaces_idx.reset(new uint32_t[klass->interface_count_]);
- memcpy(interfaces_idx.get(), klass->interfaces_, length);
- memset(klass->interfaces_, 0xFF, length);
- }
// Mark the class as loaded.
klass->status_ = Class::kStatusLoaded;
if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
@@ -1011,16 +1034,16 @@
}
klass->super_class_ = super_class; // TODO: write barrier
}
- if (klass->interface_count_ > 0) {
- for (size_t i = 0; i < klass->interface_count_; ++i) {
- uint32_t idx = interfaces_idx[i];
- klass->interfaces_[i] = ResolveClass(klass, idx, dex_file);
- if (klass->interfaces_[i] == NULL) {
+ if (klass->NumInterfaces() > 0) {
+ for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
+ uint32_t idx = klass->interfaces_idx_[i];
+ klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
+ if (klass->GetInterface(i) == NULL) {
LG << "Failed to resolve interface";
return false;
}
// Verify
- if (!klass->CanAccess(klass->interfaces_[i])) {
+ if (!klass->CanAccess(klass->GetInterface(i))) {
LG << "Inaccessible interface";
return false;
}
@@ -1165,26 +1188,26 @@
} else {
super_ifcount = 0;
}
- size_t ifCount = super_ifcount;
- ifCount += klass->interface_count_;
- for (size_t i = 0; i < klass->interface_count_; i++) {
- ifCount += klass->interfaces_[i]->iftable_count_;
+ size_t ifcount = super_ifcount;
+ ifcount += klass->NumInterfaces();
+ for (size_t i = 0; i < klass->NumInterfaces(); i++) {
+ ifcount += klass->GetInterface(i)->iftable_count_;
}
- if (ifCount == 0) {
+ if (ifcount == 0) {
DCHECK(klass->iftable_count_ == 0);
DCHECK(klass->iftable_ == NULL);
return true;
}
- klass->iftable_ = new InterfaceEntry[ifCount * sizeof(InterfaceEntry)];
- memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifCount);
+ klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
+ memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
if (super_ifcount != 0) {
memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
sizeof(InterfaceEntry) * super_ifcount);
}
// Flatten the interface inheritance hierarchy.
size_t idx = super_ifcount;
- for (size_t i = 0; i < klass->interface_count_; i++) {
- Class* interf = klass->interfaces_[i];
+ for (size_t i = 0; i < klass->NumInterfaces(); i++) {
+ Class* interf = klass->GetInterface(i);
DCHECK(interf != NULL);
if (!interf->IsInterface()) {
LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
@@ -1195,12 +1218,12 @@
klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
}
}
- CHECK_EQ(idx, ifCount);
- klass->iftable_count_ = ifCount;
- if (klass->IsInterface() || super_ifcount == ifCount) {
+ CHECK_EQ(idx, ifcount);
+ klass->iftable_count_ = ifcount;
+ if (klass->IsInterface() || super_ifcount == ifcount) {
return true;
}
- for (size_t i = super_ifcount; i < ifCount; i++) {
+ for (size_t i = super_ifcount; i < ifcount; i++) {
pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
}
if (pool_size == 0) {
@@ -1209,7 +1232,7 @@
klass->ifvi_pool_count_ = pool_size;
klass->ifvi_pool_ = new uint32_t[pool_size];
std::vector<Method*> miranda_list;
- for (size_t i = super_ifcount; i < ifCount; ++i) {
+ for (size_t i = super_ifcount; i < ifcount; ++i) {
klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
Class* interface = klass->iftable_[i].GetClass();
pool_offset += interface->NumVirtualMethods(); // end here
@@ -1250,19 +1273,18 @@
}
}
if (miranda_count != 0) {
- int oldMethodCount = klass->NumVirtualMethods();
- int newMethodCount = oldMethodCount + miranda_count;
- Method** newVirtualMethods = new Method*[newMethodCount];
+ int old_method_count = klass->NumVirtualMethods();
+ int new_method_count = old_method_count + miranda_count;
+ ObjectArray* new_virtual_methods = AllocObjectArray(new_method_count);
if (klass->virtual_methods_ != NULL) {
- memcpy(newVirtualMethods,
- klass->virtual_methods_,
- klass->NumVirtualMethods() * sizeof(Method*));
+ ObjectArray::Copy(klass->virtual_methods_, 0,
+ new_virtual_methods, 0,
+ old_method_count);
}
- klass->virtual_methods_ = newVirtualMethods;
- klass->num_virtual_methods_ = newMethodCount;
+ klass->virtual_methods_ = new_virtual_methods;
CHECK(klass->vtable_ != NULL);
- int oldVtableCount = klass->vtable_count_;
+ int old_vtable_count = klass->vtable_count_;
klass->vtable_count_ += miranda_count;
for (int i = 0; i < miranda_count; i++) {
@@ -1270,9 +1292,9 @@
memcpy(meth, miranda_list[i], sizeof(Method));
meth->klass_ = klass;
meth->access_flags_ |= kAccMiranda;
- meth->method_index_ = 0xFFFF & (oldVtableCount + i);
- klass->virtual_methods_[oldMethodCount+i] = meth;
- klass->vtable_[oldVtableCount + i] = meth;
+ meth->method_index_ = 0xFFFF & (old_vtable_count + i);
+ klass->SetVirtualMethod(old_method_count + i, meth);
+ klass->vtable_[old_vtable_count + i] = meth;
}
}
return true;
@@ -1415,7 +1437,10 @@
}
#endif
- klass->object_size_ = field_offset;
+ if (klass->object_size_ == 0) {
+ // avoid overwriting object_size_ of special crafted classes such as java_lang_*_
+ klass->object_size_ = field_offset;
+ }
return true;
}
diff --git a/src/class_linker.h b/src/class_linker.h
index ee56595..622eaea 100644
--- a/src/class_linker.h
+++ b/src/class_linker.h
@@ -161,8 +161,8 @@
Class* java_lang_Class_;
Class* java_lang_Object_;
- Class* java_lang_ref_Field_;
- Class* java_lang_ref_Method_;
+ Class* java_lang_reflect_Field_;
+ Class* java_lang_reflect_Method_;
Class* java_lang_Cloneable_;
Class* java_io_Serializable_;
Class* java_lang_String_;
@@ -177,8 +177,14 @@
Class* primitive_long_;
Class* primitive_void_;
- Class* object_array_class_;
Class* char_array_class_;
+ Class* class_array_class_;
+ Class* object_array_class_;
+ Class* field_array_class_;
+ Class* method_array_class_;
+
+ ObjectArray* array_interfaces_;
+ InterfaceEntry* array_iftable_;
FRIEND_TEST(ClassLinkerTest, ProtoCompare);
FRIEND_TEST(ClassLinkerTest, ProtoCompare2);
diff --git a/src/class_linker_test.cc b/src/class_linker_test.cc
index 54b13fe..2025b71 100644
--- a/src/class_linker_test.cc
+++ b/src/class_linker_test.cc
@@ -38,7 +38,7 @@
EXPECT_EQ(0U, primitive->NumVirtualMethods());
EXPECT_EQ(0U, primitive->NumInstanceFields());
EXPECT_EQ(0U, primitive->NumStaticFields());
- EXPECT_EQ(0U, primitive->interface_count_);
+ EXPECT_EQ(0U, primitive->NumInterfaces());
}
void AssertArrayClass(const StringPiece& array_descriptor,
@@ -70,7 +70,7 @@
EXPECT_EQ(0U, array->NumVirtualMethods());
EXPECT_EQ(0U, array->NumInstanceFields());
EXPECT_EQ(0U, array->NumStaticFields());
- EXPECT_EQ(2U, array->interface_count_);
+ EXPECT_EQ(2U, array->NumInterfaces());
}
};
@@ -132,7 +132,7 @@
EXPECT_EQ(0U, JavaLangObject->NumVirtualMethods());
EXPECT_EQ(0U, JavaLangObject->NumInstanceFields());
EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
- EXPECT_EQ(0U, JavaLangObject->interface_count_);
+ EXPECT_EQ(0U, JavaLangObject->NumInterfaces());
scoped_ptr<DexFile> dex(OpenDexFileBase64(kMyClassDex));
@@ -161,7 +161,7 @@
EXPECT_EQ(0U, MyClass->NumVirtualMethods());
EXPECT_EQ(0U, MyClass->NumInstanceFields());
EXPECT_EQ(0U, MyClass->NumStaticFields());
- EXPECT_EQ(0U, MyClass->interface_count_);
+ EXPECT_EQ(0U, MyClass->NumInterfaces());
EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
diff --git a/src/common_test.h b/src/common_test.h
index 96834b1..e7b4f30 100644
--- a/src/common_test.h
+++ b/src/common_test.h
@@ -12,6 +12,15 @@
// package java.lang;
// public class Object {}
+
+// package java.lang;
+// public class Class {}
+//
+// package java.lang.reflect;
+// public class Field {}
+//
+// package java.lang.reflect;
+// public class Method {}
//
// package java.lang;
// public interface Cloneable {}
@@ -19,17 +28,23 @@
// package java.io;
// public interface Serializable {}
static const char kJavaLangDex[] =
- "ZGV4CjAzNQAdS5NB20pfz7Z3u9Jh2IVuB3Hxe6BzR9FAAgAAcAAAAHhWNBIAAAAAAAAAALgBAAAI"
- "AAAAcAAAAAQAAACQAAAAAQAAAKAAAAAAAAAAAAAAAAEAAACsAAAAAwAAALQAAAAsAQAAFAEAACgB"
- "AAAwAQAAQAEAAFgBAABvAQAAgwEAAJABAACjAQAAAgAAAAMAAAAEAAAABwAAAAcAAAADAAAAAAAA"
- "AAIAAAAAAAAAAgAAAAEAAAD/////AAAAAAUAAAAAAAAAqwEAAAAAAAAAAAAAAQYAAAIAAAAAAAAA"
- "BgAAAAAAAAAAAAAAAAAAAAEAAAABBgAAAgAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQABAAAAAACm"
- "AQAAAQAAAA4AAAAGPGluaXQ+AA5DbG9uZWFibGUuamF2YQAWTGphdmEvaW8vU2VyaWFsaXphYmxl"
- "OwAVTGphdmEvbGFuZy9DbG9uZWFibGU7ABJMamF2YS9sYW5nL09iamVjdDsAC09iamVjdC5qYXZh"
- "ABFTZXJpYWxpemFibGUuamF2YQABVgADAAcOAAAAAQAAgYAElAIAAAALAAAAAAAAAAEAAAAAAAAA"
- "AQAAAAgAAABwAAAAAgAAAAQAAACQAAAAAwAAAAEAAACgAAAABQAAAAEAAACsAAAABgAAAAMAAAC0"
- "AAAAASAAAAEAAAAUAQAAAiAAAAgAAAAoAQAAAyAAAAEAAACmAQAAACAAAAEAAACrAQAAABAAAAEA"
- "AAC4AQAA";
+ "ZGV4CjAzNQCgffHhLqornhe/ZtOPPH5jBex6xYfwloPAAwAAcAAAAHhWNBIAAAAAAAAAADgDAAAO"
+ "AAAAcAAAAAcAAACoAAAAAQAAAMQAAAAAAAAAAAAAAAQAAADQAAAABgAAAPAAAAAQAgAAsAEAAAwC"
+ "AAAUAgAAIAIAADACAAA8AgAAVAIAAGcCAAB+AgAAkgIAAK0CAADJAgAA1gIAAOMCAAD2AgAABAAA"
+ "AAUAAAAGAAAABwAAAAgAAAAJAAAADQAAAA0AAAAGAAAAAAAAAAEAAAAAAAAAAwAAAAAAAAAEAAAA"
+ "AAAAAAUAAAAAAAAAAwAAAAEAAAD/////AAAAAAsAAAAAAAAADQMAAAAAAAAAAAAAAQYAAAMAAAAA"
+ "AAAADAAAAAAAAAAAAAAAAAAAAAEAAAABAAAAAwAAAAAAAAABAAAAAAAAABcDAAAAAAAAAgAAAAEG"
+ "AAADAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAEAAAAAQAAAAMAAAAAAAAAAwAAAAAAAAAhAwAAAAAA"
+ "AAUAAAABAAAAAwAAAAAAAAAKAAAAAAAAACsDAAAAAAAAAQABAAAAAAD5AgAAAQAAAA4AAAABAAEA"
+ "AQAAAP4CAAAEAAAAcBABAAAADgABAAEAAQAAAAMDAAAEAAAAcBABAAAADgABAAEAAQAAAAgDAAAE"
+ "AAAAcBABAAAADgAGPGluaXQ+AApDbGFzcy5qYXZhAA5DbG9uZWFibGUuamF2YQAKRmllbGQuamF2"
+ "YQAWTGphdmEvaW8vU2VyaWFsaXphYmxlOwARTGphdmEvbGFuZy9DbGFzczsAFUxqYXZhL2xhbmcv"
+ "Q2xvbmVhYmxlOwASTGphdmEvbGFuZy9PYmplY3Q7ABlMamF2YS9sYW5nL3JlZmxlY3QvRmllbGQ7"
+ "ABpMamF2YS9sYW5nL3JlZmxlY3QvTWV0aG9kOwALTWV0aG9kLmphdmEAC09iamVjdC5qYXZhABFT"
+ "ZXJpYWxpemFibGUuamF2YQABVgADAAcOAAUABw4ABQAHDgAFAAcOAAAAAQABgYAEsAMAAAEAAIGA"
+ "BMQDAAABAAKBgATcAwAAAQADgYAE9AMAAAALAAAAAAAAAAEAAAAAAAAAAQAAAA4AAABwAAAAAgAA"
+ "AAcAAACoAAAAAwAAAAEAAADEAAAABQAAAAQAAADQAAAABgAAAAYAAADwAAAAASAAAAQAAACwAQAA"
+ "AiAAAA4AAAAMAgAAAyAAAAQAAAD5AgAAACAAAAQAAAANAwAAABAAAAEAAAA4AwAA";
// package java.lang;
// public class Object {}
diff --git a/src/object.h b/src/object.h
index 770e8bb..d0b2f13 100644
--- a/src/object.h
+++ b/src/object.h
@@ -561,6 +561,51 @@
const void* native_method_;
};
+class Array : public Object {
+ public:
+ uint32_t GetLength() const {
+ return length_;
+ }
+ void SetLength(uint32_t length) {
+ length_ = length;
+ }
+ const void* GetData() const {
+ return &elements_;
+ }
+ void* GetData() {
+ return &elements_;
+ }
+
+ private:
+ // The number of array elements.
+ uint32_t length_;
+ // Location of first element.
+ uint32_t elements_[0];
+ Array();
+};
+
+class ObjectArray : public Array {
+ public:
+ Object* Get(uint32_t i) const {
+ DCHECK_LT(i, GetLength());
+ Object* const * data = reinterpret_cast<Object* const *>(GetData());
+ return data[i];
+ }
+ void Set(uint32_t i, Object* object) {
+ DCHECK_LT(i, GetLength());
+ Object** data = reinterpret_cast<Object**>(GetData());
+ data[i] = object;
+ }
+ static void Copy(ObjectArray* src, int src_pos, ObjectArray* dst, int dst_pos, size_t length) {
+ for (size_t i = 0; i < length; i++) {
+ dst->Set(dst_pos + i, src->Get(src_pos + i));
+ }
+ }
+
+ private:
+ ObjectArray();
+};
+
// Class objects.
class Class : public Object {
public:
@@ -692,24 +737,32 @@
// Returns the number of static, private, and constructor methods.
size_t NumDirectMethods() const {
- return num_direct_methods_;
+ return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
}
Method* GetDirectMethod(uint32_t i) const {
- return direct_methods_[i];
+ return down_cast<Method*>(direct_methods_->Get(i));
+ }
+
+ void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
+ direct_methods_->Set(i, f);
}
// Returns the number of non-inherited virtual methods.
size_t NumVirtualMethods() const {
- return num_virtual_methods_;
+ return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
}
Method* GetVirtualMethod(uint32_t i) const {
- return virtual_methods_[i];
+ return down_cast<Method*>(virtual_methods_->Get(i));
+ }
+
+ void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
+ virtual_methods_->Set(i, f);
}
size_t NumInstanceFields() const {
- return num_instance_fields_;
+ return (ifields_ != NULL) ? ifields_->GetLength() : 0;
}
// Returns the number of instance fields containing reference types.
@@ -718,22 +771,23 @@
}
InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
- DCHECK_LT(i, num_instance_fields_);
- return ifields_[i];
+ return down_cast<InstanceField*>(ifields_->Get(i));
}
void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
- DCHECK_LT(i, num_instance_fields_);
- ifields_[i] = f;
+ ifields_->Set(i, f);
}
size_t NumStaticFields() const {
- return num_static_fields_;
+ return (sfields_ != NULL) ? sfields_->GetLength() : 0;
}
StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
- DCHECK_LT(i, num_static_fields_);
- return sfields_[i];
+ return down_cast<StaticField*>(sfields_->Get(i));
+ }
+
+ void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
+ sfields_->Set(i, f);
}
uint32_t GetReferenceOffsets() const {
@@ -749,12 +803,15 @@
Method* FindVirtualMethod(const StringPiece& name) const;
size_t NumInterfaces() const {
- return interface_count_;
+ return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
}
Class* GetInterface(uint32_t i) const {
- DCHECK_LT(i, interface_count_);
- return interfaces_[i];
+ return down_cast<Class*>(interfaces_->Get(i));
+ }
+
+ void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
+ interfaces_->Set(i, f);
}
Method* FindDirectMethodLocally(const StringPiece& name,
@@ -822,16 +879,14 @@
// InitiatingLoaderList initiating_loader_list_;
// array of interfaces this class implements directly
- size_t interface_count_;
- Class** interfaces_;
+ ObjectArray* interfaces_;
+ uint32_t* interfaces_idx_;
// static, private, and <init> methods
- size_t num_direct_methods_;
- Method** direct_methods_;
+ ObjectArray* direct_methods_;
// virtual methods defined in this class; invoked through vtable
- size_t num_virtual_methods_;
- Method** virtual_methods_;
+ ObjectArray* virtual_methods_;
// Virtual method table (vtable), for use by "invoke-virtual". The
// vtable from the superclass is copied in, and virtual methods from
@@ -869,13 +924,12 @@
// a superclass are listed in the superclass's Class.ifields.
//
// All instance fields that refer to objects are guaranteed to be at
- // the beginning of the field list. ifieldRefCount specifies the
- // number of reference fields.
- size_t num_instance_fields_;
+ // the beginning of the field list. num_reference_instance_fields_
+ // specifies the number of reference fields.
+ ObjectArray* ifields_;
// number of fields that are object refs
size_t num_reference_instance_fields_;
- InstanceField** ifields_;
// Bitmap of offsets of ifields.
uint32_t reference_offsets_;
@@ -884,8 +938,7 @@
const char* source_file_;
// Static fields
- size_t num_static_fields_;
- StaticField** sfields_;
+ ObjectArray* sfields_;
private:
Class();
@@ -899,50 +952,11 @@
DataObject();
};
-class Array : public Object {
- public:
- uint32_t GetLength() const {
- return length_;
- }
- void SetLength(uint32_t length) {
- length_ = length;
- }
- const void* GetData() const {
- return &elements_;
- }
- void* GetData() {
- return &elements_;
- }
-
- private:
- // The number of array elements.
- uint32_t length_;
- // Location of first element.
- uint32_t elements_[0];
- Array();
-};
-
class CharArray : public Array {
private:
CharArray();
};
-class ObjectArray : public Array {
- public:
- Object* Get(uint32_t i) const {
- DCHECK_LT(i, GetLength());
- Object* const * data = reinterpret_cast<Object* const *>(GetData());
- return data[i];
- }
- void Set(uint32_t i, Object* object) {
- DCHECK_LT(i, GetLength());
- Object** data = reinterpret_cast<Object**>(GetData());
- data[i] = object;
- }
- private:
- ObjectArray();
-};
-
class String : public Object {
public:
CharArray* array_;