blob: 2025b712a72033dd4a732629936da0e3abb2e25e [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"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07007#include "gtest/gtest.h"
8
9namespace art {
10
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011class ClassLinkerTest : public RuntimeTest {
12 protected:
13 void AssertNonExistantClass(const StringPiece& descriptor) {
14 EXPECT_TRUE(class_linker_.get()->FindSystemClass(descriptor) == NULL);
15 }
16
17 void AssertPrimitiveClass(const StringPiece& descriptor) {
18 Class* primitive = class_linker_.get()->FindSystemClass(descriptor);
19 ASSERT_TRUE(primitive != NULL);
20 ASSERT_TRUE(primitive->GetClass() != NULL);
21 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
22 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
23 ASSERT_EQ(descriptor, primitive->GetDescriptor());
24 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
25 EXPECT_FALSE(primitive->HasSuperClass());
26 EXPECT_TRUE(primitive->GetComponentType() == NULL);
27 EXPECT_TRUE(primitive->GetStatus() == Class::kStatusInitialized);
28 EXPECT_FALSE(primitive->IsErroneous());
29 EXPECT_TRUE(primitive->IsVerified());
30 EXPECT_TRUE(primitive->IsLinked());
31 EXPECT_FALSE(primitive->IsArray());
32 EXPECT_EQ(0, primitive->array_rank_);
33 EXPECT_FALSE(primitive->IsInterface());
34 EXPECT_TRUE(primitive->IsPublic());
35 EXPECT_TRUE(primitive->IsFinal());
36 EXPECT_TRUE(primitive->IsPrimitive());
37 EXPECT_EQ(0U, primitive->NumDirectMethods());
38 EXPECT_EQ(0U, primitive->NumVirtualMethods());
39 EXPECT_EQ(0U, primitive->NumInstanceFields());
40 EXPECT_EQ(0U, primitive->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070041 EXPECT_EQ(0U, primitive->NumInterfaces());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042 }
43
44 void AssertArrayClass(const StringPiece& array_descriptor,
45 int32_t array_rank,
46 const StringPiece& component_type) {
47 Class* array = class_linker_.get()->FindSystemClass(array_descriptor);
48 ASSERT_TRUE(array != NULL);
49 ASSERT_TRUE(array->GetClass() != NULL);
50 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
51 EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
52 ASSERT_EQ(array_descriptor, array->GetDescriptor());
53 EXPECT_TRUE(array->GetSuperClass() != NULL);
54 EXPECT_EQ(class_linker_.get()->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
55 EXPECT_TRUE(array->HasSuperClass());
56 ASSERT_TRUE(array->GetComponentType() != NULL);
57 ASSERT_TRUE(array->GetComponentType()->GetDescriptor() != NULL);
58 EXPECT_EQ(component_type, array->GetComponentType()->GetDescriptor());
59 EXPECT_TRUE(array->GetStatus() == Class::kStatusInitialized);
60 EXPECT_FALSE(array->IsErroneous());
61 EXPECT_TRUE(array->IsVerified());
62 EXPECT_TRUE(array->IsLinked());
63 EXPECT_TRUE(array->IsArray());
64 EXPECT_EQ(array_rank, array->array_rank_);
65 EXPECT_FALSE(array->IsInterface());
66 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
67 EXPECT_TRUE(array->IsFinal());
68 EXPECT_FALSE(array->IsPrimitive());
69 EXPECT_EQ(0U, array->NumDirectMethods());
70 EXPECT_EQ(0U, array->NumVirtualMethods());
71 EXPECT_EQ(0U, array->NumInstanceFields());
72 EXPECT_EQ(0U, array->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070073 EXPECT_EQ(2U, array->NumInterfaces());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070074 }
75};
Brian Carlstroma331b3c2011-07-18 17:47:56 -070076
77TEST_F(ClassLinkerTest, FindClassNonexistent) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078 Class* result1 = class_linker_.get()->FindSystemClass("NoSuchClass;");
Brian Carlstroma331b3c2011-07-18 17:47:56 -070079 EXPECT_TRUE(result1 == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070080 Class* result2 = class_linker_.get()->FindSystemClass("LNoSuchClass;");
Brian Carlstroma331b3c2011-07-18 17:47:56 -070081 EXPECT_TRUE(result2 == NULL);
Brian Carlstrom934486c2011-07-12 23:42:50 -070082}
83
Brian Carlstroma331b3c2011-07-18 17:47:56 -070084TEST_F(ClassLinkerTest, FindClassNested) {
Brian Carlstromf3eb61f2011-07-23 20:22:26 -070085 scoped_ptr<DexFile> nested_dex(OpenDexFileBase64(kNestedDex));
86 class_linker_.get()->RegisterDexFile(nested_dex.get());
Brian Carlstrom934486c2011-07-12 23:42:50 -070087
Brian Carlstromf3eb61f2011-07-23 20:22:26 -070088 Class* outer = class_linker_.get()->FindClass("LNested;", NULL, nested_dex.get());
Brian Carlstroma331b3c2011-07-18 17:47:56 -070089 ASSERT_TRUE(outer != NULL);
90 EXPECT_EQ(0U, outer->NumVirtualMethods());
91 EXPECT_EQ(1U, outer->NumDirectMethods());
Brian Carlstrom934486c2011-07-12 23:42:50 -070092
Brian Carlstromf3eb61f2011-07-23 20:22:26 -070093 Class* inner = class_linker_.get()->FindClass("LNested$Inner;", NULL, nested_dex.get());
Brian Carlstroma331b3c2011-07-18 17:47:56 -070094 ASSERT_TRUE(inner != NULL);
95 EXPECT_EQ(0U, inner->NumVirtualMethods());
96 EXPECT_EQ(1U, inner->NumDirectMethods());
Brian Carlstrom934486c2011-07-12 23:42:50 -070097}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070098
Brian Carlstroma331b3c2011-07-18 17:47:56 -070099TEST_F(ClassLinkerTest, FindClass) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700100 ClassLinker* linker = class_linker_.get();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700101
102 StringPiece expected = "BCDFIJSZV";
103 for (int ch = 0; ch < 255; ch++) {
104 char* s = reinterpret_cast<char*>(&ch);
105 StringPiece descriptor(s, 1);
106 if (expected.find(ch) == StringPiece::npos) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700107 AssertNonExistantClass(descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700108 } else {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700109 AssertPrimitiveClass(descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700110 }
111 }
Elliott Hughesa5780da2011-07-17 11:39:39 -0700112
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700113 Class* JavaLangObject = linker->FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom934486c2011-07-12 23:42:50 -0700114 ASSERT_TRUE(JavaLangObject != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700115 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
116 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700117 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700118 ASSERT_TRUE(JavaLangObject->GetDescriptor() == "Ljava/lang/Object;");
119 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
120 EXPECT_FALSE(JavaLangObject->HasSuperClass());
121 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
122 EXPECT_FALSE(JavaLangObject->IsErroneous());
123 EXPECT_FALSE(JavaLangObject->IsVerified());
124 EXPECT_TRUE(JavaLangObject->IsLinked());
125 EXPECT_FALSE(JavaLangObject->IsArray());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700126 EXPECT_EQ(0, JavaLangObject->array_rank_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700127 EXPECT_FALSE(JavaLangObject->IsInterface());
128 EXPECT_TRUE(JavaLangObject->IsPublic());
129 EXPECT_FALSE(JavaLangObject->IsFinal());
130 EXPECT_FALSE(JavaLangObject->IsPrimitive());
Carl Shapirof10edca2011-07-14 23:16:21 -0700131 EXPECT_EQ(1U, JavaLangObject->NumDirectMethods());
132 EXPECT_EQ(0U, JavaLangObject->NumVirtualMethods());
133 EXPECT_EQ(0U, JavaLangObject->NumInstanceFields());
134 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700135 EXPECT_EQ(0U, JavaLangObject->NumInterfaces());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700136
Brian Carlstrom934486c2011-07-12 23:42:50 -0700137
Brian Carlstromf615a612011-07-23 12:50:34 -0700138 scoped_ptr<DexFile> dex(OpenDexFileBase64(kMyClassDex));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700139 linker->RegisterDexFile(dex.get());
140 EXPECT_TRUE(linker->FindSystemClass("LMyClass;") == NULL);
141 Class* MyClass = linker->FindClass("LMyClass;", NULL, dex.get());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700142 ASSERT_TRUE(MyClass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700143 ASSERT_TRUE(MyClass->GetClass() != NULL);
144 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700145 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700146 ASSERT_TRUE(MyClass->GetDescriptor() == "LMyClass;");
147 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
148 EXPECT_TRUE(MyClass->HasSuperClass());
149 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
150 EXPECT_TRUE(MyClass->GetStatus() == Class::kStatusResolved);
151 EXPECT_FALSE(MyClass->IsErroneous());
152 EXPECT_FALSE(MyClass->IsVerified());
153 EXPECT_TRUE(MyClass->IsLinked());
154 EXPECT_FALSE(MyClass->IsArray());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700155 EXPECT_EQ(0, JavaLangObject->array_rank_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700156 EXPECT_FALSE(MyClass->IsInterface());
157 EXPECT_FALSE(MyClass->IsPublic());
158 EXPECT_FALSE(MyClass->IsFinal());
159 EXPECT_FALSE(MyClass->IsPrimitive());
Carl Shapirof10edca2011-07-14 23:16:21 -0700160 EXPECT_EQ(1U, MyClass->NumDirectMethods());
161 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
162 EXPECT_EQ(0U, MyClass->NumInstanceFields());
163 EXPECT_EQ(0U, MyClass->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700164 EXPECT_EQ(0U, MyClass->NumInterfaces());
Brian Carlstroma0808032011-07-18 00:39:23 -0700165
166 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
167
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700168 // created by class_linker
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700169 AssertArrayClass("[C", 1, "C");
170 AssertArrayClass("[Ljava/lang/Object;", 1, "Ljava/lang/Object;");
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700171 // synthesized on the fly
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700172 AssertArrayClass("[[C", 2, "C");
173 AssertArrayClass("[[[LMyClass;", 3, "LMyClass;");
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700174 // or not available at all
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700175 AssertNonExistantClass("[[[[LNonExistantClass;");
176}
177
178TEST_F(ClassLinkerTest, ProtoCompare) {
179 ClassLinker* linker = class_linker_.get();
180
Brian Carlstromf615a612011-07-23 12:50:34 -0700181 scoped_ptr<DexFile> proto_dex_file(OpenDexFileBase64(kProtoCompareDex));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700182 linker->RegisterDexFile(proto_dex_file.get());
183
184 Class* klass = linker->FindClass("LProtoCompare;", NULL, proto_dex_file.get());
185 ASSERT_TRUE(klass != NULL);
186
187 ASSERT_EQ(4U, klass->NumVirtualMethods());
188
189 Method* m1 = klass->GetVirtualMethod(0);
190 ASSERT_EQ("m1", m1->GetName());
191
192 Method* m2 = klass->GetVirtualMethod(1);
193 ASSERT_EQ("m2", m2->GetName());
194
195 Method* m3 = klass->GetVirtualMethod(2);
196 ASSERT_EQ("m3", m3->GetName());
197
198 Method* m4 = klass->GetVirtualMethod(3);
199 ASSERT_EQ("m4", m4->GetName());
200
201 EXPECT_TRUE(linker->HasSameReturnType(m1, m2));
202 EXPECT_TRUE(linker->HasSameReturnType(m2, m1));
203
204 EXPECT_TRUE(linker->HasSameReturnType(m1, m2));
205 EXPECT_TRUE(linker->HasSameReturnType(m2, m1));
206
207 EXPECT_FALSE(linker->HasSameReturnType(m1, m4));
208 EXPECT_FALSE(linker->HasSameReturnType(m4, m1));
209
210 EXPECT_TRUE(linker->HasSameArgumentTypes(m1, m2));
211 EXPECT_TRUE(linker->HasSameArgumentTypes(m2, m1));
212
213 EXPECT_FALSE(linker->HasSameArgumentTypes(m1, m3));
214 EXPECT_FALSE(linker->HasSameArgumentTypes(m3, m1));
215
216 EXPECT_FALSE(linker->HasSameArgumentTypes(m1, m4));
217 EXPECT_FALSE(linker->HasSameArgumentTypes(m4, m1));
218
219 EXPECT_TRUE(linker->HasSamePrototype(m1, m2));
220 EXPECT_TRUE(linker->HasSamePrototype(m2, m1));
221
222 EXPECT_FALSE(linker->HasSamePrototype(m1, m3));
223 EXPECT_FALSE(linker->HasSamePrototype(m3, m1));
224
225 EXPECT_FALSE(linker->HasSamePrototype(m3, m4));
226 EXPECT_FALSE(linker->HasSamePrototype(m4, m3));
227
228 EXPECT_FALSE(linker->HasSameName(m1, m2));
229 EXPECT_FALSE(linker->HasSameNameAndPrototype(m1, m2));
230}
231
232TEST_F(ClassLinkerTest, ProtoCompare2) {
233 ClassLinker* linker = class_linker_.get();
234
Brian Carlstromf615a612011-07-23 12:50:34 -0700235 scoped_ptr<DexFile> proto1_dex_file(OpenDexFileBase64(kProtoCompareDex));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700236 linker->RegisterDexFile(proto1_dex_file.get());
Brian Carlstromf615a612011-07-23 12:50:34 -0700237 scoped_ptr<DexFile> proto2_dex_file(OpenDexFileBase64(kProtoCompare2Dex));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700238 linker->RegisterDexFile(proto2_dex_file.get());
239
240 Class* klass1 = linker->FindClass("LProtoCompare;", NULL, proto1_dex_file.get());
241 ASSERT_TRUE(klass1 != NULL);
242 Class* klass2 = linker->FindClass("LProtoCompare2;", NULL, proto2_dex_file.get());
243 ASSERT_TRUE(klass2 != NULL);
244
245 Method* m1_1 = klass1->GetVirtualMethod(0);
246 ASSERT_EQ("m1", m1_1->GetName());
247 Method* m2_1 = klass1->GetVirtualMethod(1);
248 ASSERT_EQ("m2", m2_1->GetName());
249 Method* m3_1 = klass1->GetVirtualMethod(2);
250 ASSERT_EQ("m3", m3_1->GetName());
251 Method* m4_1 = klass1->GetVirtualMethod(3);
252 ASSERT_EQ("m4", m4_1->GetName());
253
254 Method* m1_2 = klass2->GetVirtualMethod(0);
255 ASSERT_EQ("m1", m1_2->GetName());
256 Method* m2_2 = klass2->GetVirtualMethod(1);
257 ASSERT_EQ("m2", m2_2->GetName());
258 Method* m3_2 = klass2->GetVirtualMethod(2);
259 ASSERT_EQ("m3", m3_2->GetName());
260 Method* m4_2 = klass2->GetVirtualMethod(3);
261 ASSERT_EQ("m4", m4_2->GetName());
262
263 EXPECT_TRUE(linker->HasSameNameAndPrototype(m1_1, m1_2));
264 EXPECT_TRUE(linker->HasSameNameAndPrototype(m1_2, m1_1));
265
266 EXPECT_TRUE(linker->HasSameNameAndPrototype(m2_1, m2_2));
267 EXPECT_TRUE(linker->HasSameNameAndPrototype(m2_2, m2_1));
268
269 EXPECT_TRUE(linker->HasSameNameAndPrototype(m3_1, m3_2));
270 EXPECT_TRUE(linker->HasSameNameAndPrototype(m3_2, m3_1));
271
272 EXPECT_TRUE(linker->HasSameNameAndPrototype(m4_1, m4_2));
273 EXPECT_TRUE(linker->HasSameNameAndPrototype(m4_2, m4_1));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700274}
275
276} // namespace art