blob: 42a773ca0734b96cc78efe1b9c7960acb2169c2e [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "common_test.h"
4#include "class_linker.h"
5#include "dex_file.h"
6#include "heap.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -07007
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07008#include "gtest/gtest.h"
9
10namespace art {
11
Brian Carlstromf734cf52011-08-17 16:28:14 -070012class ClassLinkerTest : public CommonTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013 protected:
14 void AssertNonExistantClass(const StringPiece& descriptor) {
Carl Shapiro7a909592011-07-24 19:21:59 -070015 EXPECT_TRUE(class_linker_->FindSystemClass(descriptor) == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016 }
17
18 void AssertPrimitiveClass(const StringPiece& descriptor) {
Carl Shapiro7a909592011-07-24 19:21:59 -070019 Class* primitive = class_linker_->FindSystemClass(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020 ASSERT_TRUE(primitive != NULL);
21 ASSERT_TRUE(primitive->GetClass() != NULL);
22 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
23 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070024 ASSERT_TRUE(primitive->GetDescriptor()->Equals(descriptor));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
26 EXPECT_FALSE(primitive->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070027 EXPECT_TRUE(primitive->GetClassLoader() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028 EXPECT_TRUE(primitive->GetComponentType() == NULL);
29 EXPECT_TRUE(primitive->GetStatus() == Class::kStatusInitialized);
30 EXPECT_FALSE(primitive->IsErroneous());
31 EXPECT_TRUE(primitive->IsVerified());
32 EXPECT_TRUE(primitive->IsLinked());
33 EXPECT_FALSE(primitive->IsArray());
34 EXPECT_EQ(0, primitive->array_rank_);
35 EXPECT_FALSE(primitive->IsInterface());
36 EXPECT_TRUE(primitive->IsPublic());
37 EXPECT_TRUE(primitive->IsFinal());
38 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070039 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070040 EXPECT_EQ(0U, primitive->NumDirectMethods());
41 EXPECT_EQ(0U, primitive->NumVirtualMethods());
42 EXPECT_EQ(0U, primitive->NumInstanceFields());
43 EXPECT_EQ(0U, primitive->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070044 EXPECT_EQ(0U, primitive->NumInterfaces());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070045 }
46
47 void AssertArrayClass(const StringPiece& array_descriptor,
48 int32_t array_rank,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070049 const StringPiece& component_type,
50 ClassLoader* class_loader) {
51 Class* array = class_linker_->FindClass(array_descriptor, class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070052 ASSERT_TRUE(array != NULL);
53 ASSERT_TRUE(array->GetClass() != NULL);
54 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
55 EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070056 ASSERT_TRUE(array->GetDescriptor()->Equals(array_descriptor));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070057 EXPECT_TRUE(array->GetSuperClass() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -070058 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070059 EXPECT_TRUE(array->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070060 EXPECT_EQ(class_loader, array->GetClassLoader());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070061 ASSERT_TRUE(array->GetComponentType() != NULL);
62 ASSERT_TRUE(array->GetComponentType()->GetDescriptor() != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070063 EXPECT_TRUE(array->GetComponentType()->GetDescriptor()->Equals(component_type));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070064 EXPECT_TRUE(array->GetStatus() == Class::kStatusInitialized);
65 EXPECT_FALSE(array->IsErroneous());
66 EXPECT_TRUE(array->IsVerified());
67 EXPECT_TRUE(array->IsLinked());
68 EXPECT_TRUE(array->IsArray());
69 EXPECT_EQ(array_rank, array->array_rank_);
70 EXPECT_FALSE(array->IsInterface());
71 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
72 EXPECT_TRUE(array->IsFinal());
73 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070074 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070075 EXPECT_EQ(0U, array->NumDirectMethods());
76 EXPECT_EQ(0U, array->NumVirtualMethods());
77 EXPECT_EQ(0U, array->NumInstanceFields());
78 EXPECT_EQ(0U, array->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070079 EXPECT_EQ(2U, array->NumInterfaces());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070080 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -070081
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070082 void AssertDexFileClass(ClassLoader* class_loader, const char* descriptor) {
Brian Carlstromae3ac012011-07-27 01:30:28 -070083 ASSERT_TRUE(descriptor != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070084 Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstromae3ac012011-07-27 01:30:28 -070085 ASSERT_TRUE(klass != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070086 EXPECT_TRUE(klass->GetDescriptor()->Equals(descriptor));
87 if (klass->descriptor_->Equals(String::AllocFromModifiedUtf8("Ljava/lang/Object;"))) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070088 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -070089 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070090 EXPECT_TRUE(klass->HasSuperClass());
91 EXPECT_TRUE(klass->GetSuperClass() != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -070092 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070093 EXPECT_EQ(class_loader, klass->GetClassLoader());
Brian Carlstromae3ac012011-07-27 01:30:28 -070094 EXPECT_TRUE(klass->GetDexCache() != NULL);
95 EXPECT_TRUE(klass->GetComponentType() == NULL);
96 EXPECT_TRUE(klass->GetComponentType() == NULL);
97 EXPECT_EQ(Class::kStatusResolved, klass->GetStatus());
98 EXPECT_FALSE(klass->IsErroneous());
99 EXPECT_FALSE(klass->IsVerified());
100 EXPECT_TRUE(klass->IsLinked());
101 EXPECT_TRUE(klass->IsLoaded());
102 EXPECT_TRUE(klass->IsInSamePackage(klass));
103 EXPECT_TRUE(Class::IsInSamePackage(klass->GetDescriptor(), klass->GetDescriptor()));
104 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700105 EXPECT_TRUE(klass->IsAbstract());
106 if (klass->NumDirectMethods() == 1) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700107 EXPECT_TRUE(klass->GetDirectMethod(0)->GetName()->Equals("<clinit>"));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700108 } else {
109 EXPECT_EQ(0U, klass->NumDirectMethods());
110 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700111 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700112 if (!klass->IsSynthetic()) {
113 EXPECT_NE(0U, klass->NumDirectMethods());
114 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700115 }
116 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700117 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700118 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700119 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700120 }
121 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700122 EXPECT_FALSE(klass->IsAbstract());
123 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700124 }
125 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700126 EXPECT_FALSE(klass->IsFinal());
127 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700128 }
129
130 EXPECT_FALSE(klass->IsPrimitive());
131 EXPECT_TRUE(klass->CanAccess(klass));
132
133 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700134 Method* method = klass->GetDirectMethod(i);
135 EXPECT_TRUE(method != NULL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400136 EXPECT_EQ(klass, method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700137 }
138
139 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700140 Method* method = klass->GetVirtualMethod(i);
141 EXPECT_TRUE(method != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700142 }
143
144 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400145 Field* field = klass->GetInstanceField(i);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700146 EXPECT_TRUE(field != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700147 EXPECT_FALSE(field->IsStatic());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400148 EXPECT_EQ(klass, field->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700149 }
150
151 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400152 Field* field = klass->GetStaticField(i);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700153 EXPECT_TRUE(field != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700154 EXPECT_TRUE(field->IsStatic());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400155 EXPECT_EQ(klass, field->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700156 }
157
158 // Confirm that all instances fields are packed together at the start
159 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
160 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400161 Field* field = klass->GetInstanceField(i);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700162 ASSERT_TRUE(field != NULL);
163 ASSERT_TRUE(field->GetDescriptor() != NULL);
164 Class* field_type = class_linker_->FindClass(field->GetDescriptor(), class_loader);
165 ASSERT_TRUE(field_type != NULL);
166 EXPECT_FALSE(field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700167 }
168 for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400169 Field* field = klass->GetInstanceField(i);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700170 ASSERT_TRUE(field != NULL);
171 ASSERT_TRUE(field->GetDescriptor() != NULL);
172 Class* field_type = class_linker_->FindClass(field->GetDescriptor(), class_loader);
173 ASSERT_TRUE(field_type != NULL);
174 EXPECT_TRUE(field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700175 }
176
177 size_t total_num_reference_instance_fields = 0;
178 Class* k = klass;
179 while (k != NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700180 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
181 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700182 }
183 EXPECT_EQ(klass->GetReferenceOffsets() == 0,
184 total_num_reference_instance_fields == 0);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700185 }
186
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700187 static void TestRootVisitor(Object* root, void* arg) {
188 EXPECT_TRUE(root != NULL);
189 }
190
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700191 void AssertDexFile(const DexFile* dex, ClassLoader* class_loader) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700192 ASSERT_TRUE(dex != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700193 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
194 const DexFile::ClassDef class_def = dex->GetClassDef(i);
195 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700196 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700197 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700198 class_linker_->VisitRoots(TestRootVisitor, NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700199 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700200};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700201
202TEST_F(ClassLinkerTest, FindClassNonexistent) {
Carl Shapiro7a909592011-07-24 19:21:59 -0700203 Class* result1 = class_linker_->FindSystemClass("NoSuchClass;");
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700204 EXPECT_TRUE(result1 == NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700205 Class* result2 = class_linker_->FindSystemClass("LNoSuchClass;");
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700206 EXPECT_TRUE(result2 == NULL);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700207}
208
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700209TEST_F(ClassLinkerTest, FindClassNested) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700210 scoped_ptr<DexFile> dex(OpenDexFileBase64(kNestedDex));
211 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700212
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700213 Class* outer = class_linker_->FindClass("LNested;", class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700214 ASSERT_TRUE(outer != NULL);
215 EXPECT_EQ(0U, outer->NumVirtualMethods());
216 EXPECT_EQ(1U, outer->NumDirectMethods());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700217
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700218 Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700219 ASSERT_TRUE(inner != NULL);
220 EXPECT_EQ(0U, inner->NumVirtualMethods());
221 EXPECT_EQ(1U, inner->NumDirectMethods());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700222}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700224TEST_F(ClassLinkerTest, FindClass) {
Carl Shapiro7a909592011-07-24 19:21:59 -0700225 ClassLinker* linker = class_linker_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700226
227 StringPiece expected = "BCDFIJSZV";
228 for (int ch = 0; ch < 255; ch++) {
229 char* s = reinterpret_cast<char*>(&ch);
230 StringPiece descriptor(s, 1);
231 if (expected.find(ch) == StringPiece::npos) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700232 AssertNonExistantClass(descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700233 } else {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700234 AssertPrimitiveClass(descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700235 }
236 }
Elliott Hughesa5780da2011-07-17 11:39:39 -0700237
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700238 Class* JavaLangObject = linker->FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom934486c2011-07-12 23:42:50 -0700239 ASSERT_TRUE(JavaLangObject != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700240 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
241 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700242 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700243 ASSERT_TRUE(JavaLangObject->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700244 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
245 EXPECT_FALSE(JavaLangObject->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700246 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700247 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
248 EXPECT_FALSE(JavaLangObject->IsErroneous());
249 EXPECT_FALSE(JavaLangObject->IsVerified());
250 EXPECT_TRUE(JavaLangObject->IsLinked());
251 EXPECT_FALSE(JavaLangObject->IsArray());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700252 EXPECT_EQ(0, JavaLangObject->array_rank_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700253 EXPECT_FALSE(JavaLangObject->IsInterface());
254 EXPECT_TRUE(JavaLangObject->IsPublic());
255 EXPECT_FALSE(JavaLangObject->IsFinal());
256 EXPECT_FALSE(JavaLangObject->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700257 EXPECT_FALSE(JavaLangObject->IsSynthetic());
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700258 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
259 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
Carl Shapirof10edca2011-07-14 23:16:21 -0700260 EXPECT_EQ(0U, JavaLangObject->NumInstanceFields());
261 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700262 EXPECT_EQ(0U, JavaLangObject->NumInterfaces());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700263
Brian Carlstrom934486c2011-07-12 23:42:50 -0700264
Brian Carlstromf615a612011-07-23 12:50:34 -0700265 scoped_ptr<DexFile> dex(OpenDexFileBase64(kMyClassDex));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700266 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700267 EXPECT_TRUE(linker->FindSystemClass("LMyClass;") == NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 Class* MyClass = linker->FindClass("LMyClass;", class_loader);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700269 ASSERT_TRUE(MyClass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700270 ASSERT_TRUE(MyClass->GetClass() != NULL);
271 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700272 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700273 ASSERT_TRUE(MyClass->GetDescriptor()->Equals("LMyClass;"));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700274 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
275 EXPECT_TRUE(MyClass->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700276 EXPECT_EQ(class_loader, MyClass->GetClassLoader());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700277 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
278 EXPECT_TRUE(MyClass->GetStatus() == Class::kStatusResolved);
279 EXPECT_FALSE(MyClass->IsErroneous());
280 EXPECT_FALSE(MyClass->IsVerified());
281 EXPECT_TRUE(MyClass->IsLinked());
282 EXPECT_FALSE(MyClass->IsArray());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700283 EXPECT_EQ(0, JavaLangObject->array_rank_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700284 EXPECT_FALSE(MyClass->IsInterface());
285 EXPECT_FALSE(MyClass->IsPublic());
286 EXPECT_FALSE(MyClass->IsFinal());
287 EXPECT_FALSE(MyClass->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700288 EXPECT_FALSE(MyClass->IsSynthetic());
Carl Shapirof10edca2011-07-14 23:16:21 -0700289 EXPECT_EQ(1U, MyClass->NumDirectMethods());
290 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
291 EXPECT_EQ(0U, MyClass->NumInstanceFields());
292 EXPECT_EQ(0U, MyClass->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700293 EXPECT_EQ(0U, MyClass->NumInterfaces());
Brian Carlstroma0808032011-07-18 00:39:23 -0700294
295 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
296
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700297 // created by class_linker
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700298 AssertArrayClass("[C", 1, "C", NULL);
299 AssertArrayClass("[Ljava/lang/Object;", 1, "Ljava/lang/Object;", NULL);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700300 // synthesized on the fly
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700301 AssertArrayClass("[[C", 2, "C", NULL);
302 AssertArrayClass("[[[LMyClass;", 3, "LMyClass;", class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700303 // or not available at all
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700304 AssertNonExistantClass("[[[[LNonExistantClass;");
305}
306
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700307TEST_F(ClassLinkerTest, LibCore) {
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400308 scoped_ptr<DexFile> libcore_dex_file(GetLibCoreDex());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700309 EXPECT_TRUE(libcore_dex_file.get() != NULL);
310 AssertDexFile(libcore_dex_file.get(), NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700311}
312
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400313// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400314// reorder the fields in the C++ class. Managed class fields are ordered by
315// ClassLinker::LinkInstanceFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400316TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700317 Class* string = class_linker_->FindSystemClass( "Ljava/lang/String;");
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400318 ASSERT_EQ(4U, string->NumInstanceFields());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700319 EXPECT_TRUE(string->GetInstanceField(0)->GetName()->Equals("value"));
320 EXPECT_TRUE(string->GetInstanceField(1)->GetName()->Equals("hashCode"));
321 EXPECT_TRUE(string->GetInstanceField(2)->GetName()->Equals("offset"));
322 EXPECT_TRUE(string->GetInstanceField(3)->GetName()->Equals("count"));
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400323
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700324 Class* accessible_object = class_linker_->FindSystemClass("Ljava/lang/reflect/AccessibleObject;");
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400325 ASSERT_EQ(1U, accessible_object->NumInstanceFields());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700326 EXPECT_TRUE(accessible_object->GetInstanceField(0)->GetName()->Equals("flag"));
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400327
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700328 Class* field = class_linker_->FindSystemClass("Ljava/lang/reflect/Field;");
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400329 ASSERT_EQ(6U, field->NumInstanceFields());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700330 EXPECT_TRUE(field->GetInstanceField(0)->GetName()->Equals("declaringClass"));
331 EXPECT_TRUE(field->GetInstanceField(1)->GetName()->Equals("genericType"));
332 EXPECT_TRUE(field->GetInstanceField(2)->GetName()->Equals("type"));
333 EXPECT_TRUE(field->GetInstanceField(3)->GetName()->Equals("name"));
334 EXPECT_TRUE(field->GetInstanceField(4)->GetName()->Equals("slot"));
335 EXPECT_TRUE(field->GetInstanceField(5)->GetName()->Equals("genericTypesAreInitialized"));
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400336
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700337 Class* method = class_linker_->FindSystemClass("Ljava/lang/reflect/Method;");
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400338 ASSERT_EQ(11U, method->NumInstanceFields());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700339 EXPECT_TRUE(method->GetInstanceField( 0)->GetName()->Equals("declaringClass"));
340 EXPECT_TRUE(method->GetInstanceField( 1)->GetName()->Equals("exceptionTypes"));
341 EXPECT_TRUE(method->GetInstanceField( 2)->GetName()->Equals("formalTypeParameters"));
342 EXPECT_TRUE(method->GetInstanceField( 3)->GetName()->Equals("genericExceptionTypes"));
343 EXPECT_TRUE(method->GetInstanceField( 4)->GetName()->Equals("genericParameterTypes"));
344 EXPECT_TRUE(method->GetInstanceField( 5)->GetName()->Equals("genericReturnType"));
345 EXPECT_TRUE(method->GetInstanceField( 6)->GetName()->Equals("returnType"));
346 EXPECT_TRUE(method->GetInstanceField( 7)->GetName()->Equals("name"));
347 EXPECT_TRUE(method->GetInstanceField( 8)->GetName()->Equals("parameterTypes"));
348 EXPECT_TRUE(method->GetInstanceField( 9)->GetName()->Equals("genericTypesAreInitialized"));
349 EXPECT_TRUE(method->GetInstanceField(10)->GetName()->Equals("slot"));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700350
351 Class* class_loader = class_linker_->FindSystemClass("Ljava/lang/ClassLoader;");
352 ASSERT_EQ(2U, class_loader->NumInstanceFields());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700353 EXPECT_TRUE(class_loader->GetInstanceField(0)->GetName()->Equals("packages"));
354 EXPECT_TRUE(class_loader->GetInstanceField(1)->GetName()->Equals("parent"));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700355
356 Class* dex_base_class_loader = class_linker_->FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
357 ASSERT_EQ(2U, dex_base_class_loader->NumInstanceFields());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700358 EXPECT_TRUE(dex_base_class_loader->GetInstanceField(0)->GetName()->Equals("originalPath"));
359 EXPECT_TRUE(dex_base_class_loader->GetInstanceField(1)->GetName()->Equals("pathList"));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700360}
361
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400362// The first reference array element must be a multiple of 8 bytes from the
363// start of the object
364TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
365 Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
366 ObjectArray<String>* array = ObjectArray<String>::Alloc(array_class, 0);
367 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
368 uint32_t data_offset = reinterpret_cast<uint32_t>(array->GetData());
369 EXPECT_EQ(16U, data_offset - array_offset);
370}
371
372TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700373 LongArray* array = LongArray::Alloc(0);
374 EXPECT_EQ(class_linker_->FindSystemClass("[J"), array->GetClass());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400375 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
376 uint32_t data_offset = reinterpret_cast<uint32_t>(array->GetData());
377 EXPECT_EQ(16U, data_offset - array_offset);
378}
379
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700380TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
381 scoped_ptr<DexFile> dex_1(OpenDexFileBase64(kMyClassDex));
382 scoped_ptr<DexFile> dex_2(OpenDexFileBase64(kMyClassDex));
383 PathClassLoader* class_loader_1 = AllocPathClassLoader(dex_1.get());
384 PathClassLoader* class_loader_2 = AllocPathClassLoader(dex_2.get());
385 Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1);
386 Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2);
387 EXPECT_TRUE(MyClass_1 != NULL);
388 EXPECT_TRUE(MyClass_2 != NULL);
389 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400390}
391
Jesse Wilson7833bd22011-08-09 18:31:44 -0400392TEST_F(ClassLinkerTest, StaticFields) {
393 // TODO: uncomment expectations of initial values when InitializeClass works
394 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStatics));
395 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
396 Class* statics = class_linker_->FindClass("LStatics;", class_loader);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700397 // class_linker_->InitializeClass(statics); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400398
399 EXPECT_EQ(10U, statics->NumStaticFields());
400
Jesse Wilson35baaab2011-08-10 16:18:03 -0400401 Field* s0 = statics->GetStaticField(0);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700402 EXPECT_TRUE(s0->GetClass()->descriptor_->Equals("Ljava/lang/reflect/Field;"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400403 EXPECT_EQ('Z', s0->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700404// EXPECT_EQ(true, s0->GetBoolean()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400405 s0->SetBoolean(false);
406
Jesse Wilson35baaab2011-08-10 16:18:03 -0400407 Field* s1 = statics->GetStaticField(1);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400408 EXPECT_EQ('B', s1->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700409// EXPECT_EQ(5, s1->GetByte()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400410 s1->SetByte(6);
411
Jesse Wilson35baaab2011-08-10 16:18:03 -0400412 Field* s2 = statics->GetStaticField(2);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400413 EXPECT_EQ('C', s2->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700414// EXPECT_EQ('a', s2->GetChar()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400415 s2->SetChar('b');
416
Jesse Wilson35baaab2011-08-10 16:18:03 -0400417 Field* s3 = statics->GetStaticField(3);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400418 EXPECT_EQ('S', s3->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700419// EXPECT_EQ(65000, s3->GetShort()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400420 s3->SetShort(65001);
421
Jesse Wilson35baaab2011-08-10 16:18:03 -0400422 Field* s4 = statics->GetStaticField(4);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400423 EXPECT_EQ('I', s4->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700424// EXPECT_EQ(2000000000, s4->GetInt()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400425 s4->SetInt(2000000001);
426
Jesse Wilson35baaab2011-08-10 16:18:03 -0400427 Field* s5 = statics->GetStaticField(5);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400428 EXPECT_EQ('J', s5->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700429// EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400430 s5->SetLong(0x34567890abcdef12LL);
431
Jesse Wilson35baaab2011-08-10 16:18:03 -0400432 Field* s6 = statics->GetStaticField(6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400433 EXPECT_EQ('F', s6->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700434// EXPECT_EQ(0.5, s6->GetFloat()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400435 s6->SetFloat(0.75);
436
Jesse Wilson35baaab2011-08-10 16:18:03 -0400437 Field* s7 = statics->GetStaticField(7);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400438 EXPECT_EQ('D', s7->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700439// EXPECT_EQ(16777217, s7->GetDouble()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400440 s7->SetDouble(16777219);
441
Jesse Wilson35baaab2011-08-10 16:18:03 -0400442 Field* s8 = statics->GetStaticField(8);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400443 EXPECT_EQ('L', s8->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700444// EXPECT_TRUE(down_cast<String*>(s8->GetObject())->Equals("android")); // TODO: uncomment this
Elliott Hughesbfaadc82011-08-18 17:36:58 -0700445 s8->SetObject(String::AllocFromModifiedUtf8("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400446
Jesse Wilson35baaab2011-08-10 16:18:03 -0400447 Field* s9 = statics->GetStaticField(9);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400448 EXPECT_EQ('[', s9->GetType());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700449// EXPECT_EQ(NULL, s9->GetObject()); // TODO: uncomment this
Jesse Wilson7833bd22011-08-09 18:31:44 -0400450 s9->SetObject(NULL);
451
452 EXPECT_EQ(false, s0->GetBoolean());
453 EXPECT_EQ(6, s1->GetByte());
454 EXPECT_EQ('b', s2->GetChar());
455 EXPECT_EQ(65001, s3->GetShort());
456 EXPECT_EQ(2000000001, s4->GetInt());
457 EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong());
458 EXPECT_EQ(0.75, s6->GetFloat());
459 EXPECT_EQ(16777219, s7->GetDouble());
460 EXPECT_TRUE(down_cast<String*>(s8->GetObject())->Equals("robot"));
461}
462
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400463} // namespace art