blob: 5dfd0075a2c6d108b8ed9a50ae32e0d16ec82dd5 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro894d0fa2011-06-30 14:48:49 -070016
Elliott Hughes90a33692011-08-30 13:27:07 -070017#include "object.h"
18
19#include <stdint.h>
20#include <stdio.h>
21
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "array-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "art_field-inl.h"
Ian Rogers1f539342012-10-03 21:09:42 -070024#include "asm_support.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "class-inl.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "dex_file.h"
Ian Rogers7655f292013-07-29 11:07:13 -070030#include "entrypoints/entrypoint_utils.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
32#include "gc/heap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "iftable-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "object-inl.h"
36#include "object_array-inl.h"
Ian Rogers1f539342012-10-03 21:09:42 -070037#include "sirt_ref.h"
38#include "UniquePtr.h"
Carl Shapiro894d0fa2011-06-30 14:48:49 -070039
40namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041namespace mirror {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070042
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080043class ObjectTest : public CommonRuntimeTest {
Brian Carlstrom0b138b22011-07-27 15:19:17 -070044 protected:
Brian Carlstrom0a02d122013-09-06 15:42:40 -070045 void AssertString(int32_t expected_utf16_length,
Brian Carlstrom0b138b22011-07-27 15:19:17 -070046 const char* utf8_in,
47 const char* utf16_expected_le,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 int32_t expected_hash)
Ian Rogersb726dcb2012-09-05 08:57:23 -070049 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom0a02d122013-09-06 15:42:40 -070050 UniquePtr<uint16_t[]> utf16_expected(new uint16_t[expected_utf16_length]);
51 for (int32_t i = 0; i < expected_utf16_length; i++) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -070052 uint16_t ch = (((utf16_expected_le[i*2 + 0] & 0xff) << 8) |
53 ((utf16_expected_le[i*2 + 1] & 0xff) << 0));
54 utf16_expected[i] = ch;
55 }
56
Ian Rogers50b35e22012-10-04 10:09:15 -070057 Thread* self = Thread::Current();
Brian Carlstrom0a02d122013-09-06 15:42:40 -070058 SirtRef<String> string(self, String::AllocFromModifiedUtf8(self, expected_utf16_length, utf8_in));
59 ASSERT_EQ(expected_utf16_length, string->GetLength());
Carl Shapiro8860c0e2011-08-04 17:36:16 -070060 ASSERT_TRUE(string->GetCharArray() != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -070061 ASSERT_TRUE(string->GetCharArray()->GetData() != NULL);
Brian Carlstrom0a02d122013-09-06 15:42:40 -070062 // strlen is necessary because the 1-character string "\x00\x00" is interpreted as ""
63 ASSERT_TRUE(string->Equals(utf8_in) || (expected_utf16_length == 1 && strlen(utf8_in) == 0));
64 ASSERT_TRUE(string->Equals(StringPiece(utf8_in)) || (expected_utf16_length == 1 && strlen(utf8_in) == 0));
65 for (int32_t i = 0; i < expected_utf16_length; i++) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -070066 EXPECT_EQ(utf16_expected[i], string->CharAt(i));
Brian Carlstrom0b138b22011-07-27 15:19:17 -070067 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070068 EXPECT_EQ(expected_hash, string->GetHashCode());
Brian Carlstrom0b138b22011-07-27 15:19:17 -070069 }
70};
Brian Carlstroma331b3c2011-07-18 17:47:56 -070071
buzbeefc9e6fa2012-03-23 15:14:29 -070072// Keep the assembly code in sync
73TEST_F(ObjectTest, AsmConstants) {
Ian Rogersa9a82542013-10-04 11:17:26 -070074 EXPECT_EQ(CLASS_OFFSET, Object::ClassOffset().Int32Value());
75 EXPECT_EQ(LOCK_WORD_OFFSET, Object::MonitorOffset().Int32Value());
Jeff Hao5d917302013-02-27 17:57:33 -080076
Ian Rogersa9a82542013-10-04 11:17:26 -070077 EXPECT_EQ(CLASS_COMPONENT_TYPE_OFFSET, Class::ComponentTypeOffset().Int32Value());
78
79 EXPECT_EQ(ARRAY_LENGTH_OFFSET, Array::LengthOffset().Int32Value());
Ian Rogersef7d42f2014-01-06 12:55:46 -080080 EXPECT_EQ(OBJECT_ARRAY_DATA_OFFSET, Array::DataOffset(sizeof(HeapReference<Object>)).Int32Value());
Ian Rogersa9a82542013-10-04 11:17:26 -070081
82 EXPECT_EQ(STRING_VALUE_OFFSET, String::ValueOffset().Int32Value());
83 EXPECT_EQ(STRING_COUNT_OFFSET, String::CountOffset().Int32Value());
84 EXPECT_EQ(STRING_OFFSET_OFFSET, String::OffsetOffset().Int32Value());
85 EXPECT_EQ(STRING_DATA_OFFSET, Array::DataOffset(sizeof(uint16_t)).Int32Value());
86
Jeff Hao88474b42013-10-23 16:24:40 -070087 EXPECT_EQ(METHOD_DEX_CACHE_METHODS_OFFSET, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
Ian Rogersef7d42f2014-01-06 12:55:46 -080088 EXPECT_EQ(METHOD_PORTABLE_CODE_OFFSET, ArtMethod::EntryPointFromPortableCompiledCodeOffset().Int32Value());
89 EXPECT_EQ(METHOD_QUICK_CODE_OFFSET, ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
buzbeefc9e6fa2012-03-23 15:14:29 -070090}
91
Brian Carlstroma331b3c2011-07-18 17:47:56 -070092TEST_F(ObjectTest, IsInSamePackage) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070093 // Matches
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080094 EXPECT_TRUE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/Class;"));
95 EXPECT_TRUE(Class::IsInSamePackage("LFoo;", "LBar;"));
Carl Shapiro894d0fa2011-06-30 14:48:49 -070096
97 // Mismatches
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080098 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/io/File;"));
99 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/reflect/Method;"));
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700100}
101
Elliott Hughes081be7f2011-09-18 16:50:26 -0700102TEST_F(ObjectTest, Clone) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700104 SirtRef<ObjectArray<Object> > a1(soa.Self(),
105 class_linker_->AllocObjectArray<Object>(soa.Self(), 256));
Elliott Hughes081be7f2011-09-18 16:50:26 -0700106 size_t s1 = a1->SizeOf();
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 Object* clone = a1->Clone(soa.Self());
Elliott Hughes081be7f2011-09-18 16:50:26 -0700108 EXPECT_EQ(s1, clone->SizeOf());
109 EXPECT_TRUE(clone->GetClass() == a1->GetClass());
110}
111
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700112TEST_F(ObjectTest, AllocObjectArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700114 SirtRef<ObjectArray<Object> > oa(soa.Self(),
115 class_linker_->AllocObjectArray<Object>(soa.Self(), 2));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700116 EXPECT_EQ(2, oa->GetLength());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700117 EXPECT_TRUE(oa->Get(0) == NULL);
118 EXPECT_TRUE(oa->Get(1) == NULL);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100119 oa->Set<false>(0, oa.get());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700120 EXPECT_TRUE(oa->Get(0) == oa.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700121 EXPECT_TRUE(oa->Get(1) == NULL);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100122 oa->Set<false>(1, oa.get());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700123 EXPECT_TRUE(oa->Get(0) == oa.get());
124 EXPECT_TRUE(oa->Get(1) == oa.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700125
Ian Rogers98379392014-02-24 16:53:16 -0800126 Class* aioobe = class_linker_->FindSystemClass(soa.Self(),
127 "Ljava/lang/ArrayIndexOutOfBoundsException;");
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700128
129 EXPECT_TRUE(oa->Get(-1) == NULL);
Ian Rogers1f539342012-10-03 21:09:42 -0700130 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800131 EXPECT_EQ(aioobe, soa.Self()->GetException(NULL)->GetClass());
Ian Rogers1f539342012-10-03 21:09:42 -0700132 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700133
134 EXPECT_TRUE(oa->Get(2) == NULL);
Ian Rogers1f539342012-10-03 21:09:42 -0700135 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800136 EXPECT_EQ(aioobe, soa.Self()->GetException(NULL)->GetClass());
Ian Rogers1f539342012-10-03 21:09:42 -0700137 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700138
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700139 ASSERT_TRUE(oa->GetClass() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140 ClassHelper oa_ch(oa->GetClass());
Ian Rogersd24e2642012-06-06 21:21:43 -0700141 ASSERT_EQ(2U, oa_ch.NumDirectInterfaces());
Ian Rogers98379392014-02-24 16:53:16 -0800142 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Cloneable;"),
143 oa_ch.GetDirectInterface(0));
144 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"),
145 oa_ch.GetDirectInterface(1));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700146}
147
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700148TEST_F(ObjectTest, AllocArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800150 Class* c = class_linker_->FindSystemClass(soa.Self(), "[I");
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800151 SirtRef<Array> a(soa.Self(), Array::Alloc<true>(soa.Self(), c, 1));
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700152 ASSERT_TRUE(c == a->GetClass());
153
Ian Rogers98379392014-02-24 16:53:16 -0800154 c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;");
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800155 a.reset(Array::Alloc<true>(soa.Self(), c, 1));
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700156 ASSERT_TRUE(c == a->GetClass());
157
Ian Rogers98379392014-02-24 16:53:16 -0800158 c = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;");
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800159 a.reset(Array::Alloc<true>(soa.Self(), c, 1));
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700160 ASSERT_TRUE(c == a->GetClass());
161}
162
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700163template<typename ArrayT>
164void TestPrimitiveArray(ClassLinker* cl) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700166 typedef typename ArrayT::ElementType T;
167
Ian Rogers50b35e22012-10-04 10:09:15 -0700168 ArrayT* a = ArrayT::Alloc(soa.Self(), 2);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700169 EXPECT_EQ(2, a->GetLength());
170 EXPECT_EQ(0, a->Get(0));
171 EXPECT_EQ(0, a->Get(1));
172 a->Set(0, T(123));
173 EXPECT_EQ(T(123), a->Get(0));
174 EXPECT_EQ(0, a->Get(1));
175 a->Set(1, T(321));
176 EXPECT_EQ(T(123), a->Get(0));
177 EXPECT_EQ(T(321), a->Get(1));
178
Ian Rogers98379392014-02-24 16:53:16 -0800179 Class* aioobe = cl->FindSystemClass(soa.Self(), "Ljava/lang/ArrayIndexOutOfBoundsException;");
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700180
181 EXPECT_EQ(0, a->Get(-1));
Ian Rogers1f539342012-10-03 21:09:42 -0700182 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800183 EXPECT_EQ(aioobe, soa.Self()->GetException(NULL)->GetClass());
Ian Rogers1f539342012-10-03 21:09:42 -0700184 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700185
186 EXPECT_EQ(0, a->Get(2));
Ian Rogers1f539342012-10-03 21:09:42 -0700187 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 EXPECT_EQ(aioobe, soa.Self()->GetException(NULL)->GetClass());
Ian Rogers1f539342012-10-03 21:09:42 -0700189 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700190}
191
192TEST_F(ObjectTest, PrimitiveArray_Boolean_Alloc) {
193 TestPrimitiveArray<BooleanArray>(class_linker_);
194}
195TEST_F(ObjectTest, PrimitiveArray_Byte_Alloc) {
196 TestPrimitiveArray<ByteArray>(class_linker_);
197}
198TEST_F(ObjectTest, PrimitiveArray_Char_Alloc) {
199 TestPrimitiveArray<CharArray>(class_linker_);
200}
201TEST_F(ObjectTest, PrimitiveArray_Double_Alloc) {
202 TestPrimitiveArray<DoubleArray>(class_linker_);
203}
204TEST_F(ObjectTest, PrimitiveArray_Float_Alloc) {
205 TestPrimitiveArray<FloatArray>(class_linker_);
206}
207TEST_F(ObjectTest, PrimitiveArray_Int_Alloc) {
208 TestPrimitiveArray<IntArray>(class_linker_);
209}
210TEST_F(ObjectTest, PrimitiveArray_Long_Alloc) {
211 TestPrimitiveArray<LongArray>(class_linker_);
212}
213TEST_F(ObjectTest, PrimitiveArray_Short_Alloc) {
214 TestPrimitiveArray<ShortArray>(class_linker_);
215}
216
Elliott Hughesb408de72011-10-04 14:35:05 -0700217TEST_F(ObjectTest, CheckAndAllocArrayFromCode) {
Ian Rogersb886da82011-09-23 16:27:54 -0700218 // pretend we are trying to call 'new char[3]' from String.toCharArray
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800220 Class* java_util_Arrays = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/Arrays;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700221 ArtMethod* sort = java_util_Arrays->FindDirectMethod("sort", "([I)V");
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800222 const DexFile::StringId* string_id = java_lang_dex_file_->FindStringId("[I");
223 ASSERT_TRUE(string_id != NULL);
224 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(
225 java_lang_dex_file_->GetIndexForStringId(*string_id));
226 ASSERT_TRUE(type_id != NULL);
227 uint32_t type_idx = java_lang_dex_file_->GetIndexForTypeId(*type_id);
Hiroshi Yamauchie4e23c02013-12-06 13:38:43 -0800228 Object* array = CheckAndAllocArrayFromCodeInstrumented(type_idx, sort, 3, Thread::Current(), false,
229 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700230 EXPECT_TRUE(array->IsArrayInstance());
231 EXPECT_EQ(3, array->AsArray()->GetLength());
232 EXPECT_TRUE(array->GetClass()->IsArrayClass());
233 EXPECT_TRUE(array->GetClass()->GetComponentType()->IsPrimitive());
234}
235
Ian Rogers64b6d142012-10-29 16:34:15 -0700236TEST_F(ObjectTest, CreateMultiArray) {
237 ScopedObjectAccess soa(Thread::Current());
238
Ian Rogers98379392014-02-24 16:53:16 -0800239 SirtRef<Class> c(soa.Self(), class_linker_->FindSystemClass(soa.Self(), "I"));
Ian Rogers64b6d142012-10-29 16:34:15 -0700240 SirtRef<IntArray> dims(soa.Self(), IntArray::Alloc(soa.Self(), 1));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100241 dims->Set<false>(0, 1);
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800242 Array* multi = Array::CreateMultiArray(soa.Self(), c, dims);
Ian Rogers98379392014-02-24 16:53:16 -0800243 EXPECT_TRUE(multi->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[I"));
Ian Rogers64b6d142012-10-29 16:34:15 -0700244 EXPECT_EQ(1, multi->GetLength());
245
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100246 dims->Set<false>(0, -1);
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800247 multi = Array::CreateMultiArray(soa.Self(), c, dims);
Ian Rogers64b6d142012-10-29 16:34:15 -0700248 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800249 EXPECT_EQ(PrettyDescriptor(soa.Self()->GetException(NULL)->GetClass()),
Ian Rogers64b6d142012-10-29 16:34:15 -0700250 "java.lang.NegativeArraySizeException");
251 soa.Self()->ClearException();
252
253 dims.reset(IntArray::Alloc(soa.Self(), 2));
254 for (int i = 1; i < 20; ++i) {
255 for (int j = 0; j < 20; ++j) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100256 dims->Set<false>(0, i);
257 dims->Set<false>(1, j);
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800258 multi = Array::CreateMultiArray(soa.Self(), c, dims);
Ian Rogers98379392014-02-24 16:53:16 -0800259 EXPECT_TRUE(multi->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[[I"));
Ian Rogers64b6d142012-10-29 16:34:15 -0700260 EXPECT_EQ(i, multi->GetLength());
261 for (int k = 0; k < i; ++k) {
262 Array* outer = multi->AsObjectArray<Array>()->Get(k);
Ian Rogers98379392014-02-24 16:53:16 -0800263 EXPECT_TRUE(outer->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[I"));
Ian Rogers64b6d142012-10-29 16:34:15 -0700264 EXPECT_EQ(j, outer->GetLength());
265 }
266 }
267 }
268}
269
Brian Carlstromb9edb842011-08-28 16:31:06 -0700270TEST_F(ObjectTest, StaticFieldFromCode) {
jeffhaoabcfde32011-09-29 15:05:18 -0700271 // pretend we are trying to access 'Static.s0' from StaticsFromCode.<clinit>
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 ScopedObjectAccess soa(Thread::Current());
273 jobject class_loader = LoadDex("StaticsFromCode");
274 const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(class_loader)[0];
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700275 CHECK(dex_file != NULL);
276
Mathieu Chartier590fee92013-09-13 13:46:47 -0700277 SirtRef<mirror::ClassLoader> loader(soa.Self(), soa.Decode<ClassLoader*>(class_loader));
Ian Rogers98379392014-02-24 16:53:16 -0800278 Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", loader);
Ian Rogers241b5de2013-10-09 17:58:57 -0700279 ArtMethod* clinit = klass->FindClassInitializer();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800280 const DexFile::StringId* klass_string_id = dex_file->FindStringId("LStaticsFromCode;");
281 ASSERT_TRUE(klass_string_id != NULL);
282 const DexFile::TypeId* klass_type_id = dex_file->FindTypeId(
283 dex_file->GetIndexForStringId(*klass_string_id));
284 ASSERT_TRUE(klass_type_id != NULL);
285
286 const DexFile::StringId* type_string_id = dex_file->FindStringId("Ljava/lang/Object;");
287 ASSERT_TRUE(type_string_id != NULL);
288 const DexFile::TypeId* type_type_id = dex_file->FindTypeId(
289 dex_file->GetIndexForStringId(*type_string_id));
290 ASSERT_TRUE(type_type_id != NULL);
291
292 const DexFile::StringId* name_str_id = dex_file->FindStringId("s0");
293 ASSERT_TRUE(name_str_id != NULL);
294
295 const DexFile::FieldId* field_id = dex_file->FindFieldId(
296 *klass_type_id, *name_str_id, *type_type_id);
297 ASSERT_TRUE(field_id != NULL);
298 uint32_t field_idx = dex_file->GetIndexForFieldId(*field_id);
299
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200300 ArtField* field = FindFieldFromCode<StaticObjectRead, true>(field_idx, clinit, Thread::Current(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800301 sizeof(HeapReference<Object>));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700302 Object* s0 = field->GetObj(klass);
Ian Rogers64b6d142012-10-29 16:34:15 -0700303 EXPECT_TRUE(s0 != NULL);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700304
Ian Rogers50b35e22012-10-04 10:09:15 -0700305 SirtRef<CharArray> char_array(soa.Self(), CharArray::Alloc(soa.Self(), 0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100306 field->SetObj<false>(field->GetDeclaringClass(), char_array.get());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700307 EXPECT_EQ(char_array.get(), field->GetObj(klass));
Brian Carlstromb9edb842011-08-28 16:31:06 -0700308
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100309 field->SetObj<false>(field->GetDeclaringClass(), NULL);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700310 EXPECT_EQ(NULL, field->GetObj(klass));
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700311
Brian Carlstromea46f952013-07-30 01:26:50 -0700312 // TODO: more exhaustive tests of all 6 cases of ArtField::*FromCode
Brian Carlstromb9edb842011-08-28 16:31:06 -0700313}
314
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700315TEST_F(ObjectTest, String) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700317 // Test the empty string.
318 AssertString(0, "", "", 0);
319
320 // Test one-byte characters.
321 AssertString(1, " ", "\x00\x20", 0x20);
322 AssertString(1, "", "\x00\x00", 0);
323 AssertString(1, "\x7f", "\x00\x7f", 0x7f);
324 AssertString(2, "hi", "\x00\x68\x00\x69", (31 * 0x68) + 0x69);
325
326 // Test two-byte characters.
327 AssertString(1, "\xc2\x80", "\x00\x80", 0x80);
328 AssertString(1, "\xd9\xa6", "\x06\x66", 0x0666);
329 AssertString(1, "\xdf\xbf", "\x07\xff", 0x07ff);
330 AssertString(3, "h\xd9\xa6i", "\x00\x68\x06\x66\x00\x69", (31 * ((31 * 0x68) + 0x0666)) + 0x69);
331
332 // Test three-byte characters.
333 AssertString(1, "\xe0\xa0\x80", "\x08\x00", 0x0800);
334 AssertString(1, "\xe1\x88\xb4", "\x12\x34", 0x1234);
335 AssertString(1, "\xef\xbf\xbf", "\xff\xff", 0xffff);
336 AssertString(3, "h\xe1\x88\xb4i", "\x00\x68\x12\x34\x00\x69", (31 * ((31 * 0x68) + 0x1234)) + 0x69);
337}
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400338
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700339TEST_F(ObjectTest, StringEqualsUtf8) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700340 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700341 SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "android"));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700342 EXPECT_TRUE(string->Equals("android"));
343 EXPECT_FALSE(string->Equals("Android"));
344 EXPECT_FALSE(string->Equals("ANDROID"));
345 EXPECT_FALSE(string->Equals(""));
346 EXPECT_FALSE(string->Equals("and"));
347 EXPECT_FALSE(string->Equals("androids"));
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700348
Ian Rogers50b35e22012-10-04 10:09:15 -0700349 SirtRef<String> empty(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), ""));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700350 EXPECT_TRUE(empty->Equals(""));
351 EXPECT_FALSE(empty->Equals("a"));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400352}
353
354TEST_F(ObjectTest, StringEquals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700356 SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "android"));
357 SirtRef<String> string_2(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "android"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700358 EXPECT_TRUE(string->Equals(string_2.get()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700359 EXPECT_FALSE(string->Equals("Android"));
360 EXPECT_FALSE(string->Equals("ANDROID"));
361 EXPECT_FALSE(string->Equals(""));
362 EXPECT_FALSE(string->Equals("and"));
363 EXPECT_FALSE(string->Equals("androids"));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400364
Ian Rogers50b35e22012-10-04 10:09:15 -0700365 SirtRef<String> empty(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), ""));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700366 EXPECT_TRUE(empty->Equals(""));
367 EXPECT_FALSE(empty->Equals("a"));
368}
369
Ian Rogers64b6d142012-10-29 16:34:15 -0700370TEST_F(ObjectTest, StringCompareTo) {
371 ScopedObjectAccess soa(Thread::Current());
372 SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "android"));
373 SirtRef<String> string_2(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "android"));
374 SirtRef<String> string_3(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "Android"));
375 SirtRef<String> string_4(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "and"));
376 SirtRef<String> string_5(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), ""));
377 EXPECT_EQ(0, string->CompareTo(string_2.get()));
378 EXPECT_LT(0, string->CompareTo(string_3.get()));
379 EXPECT_GT(0, string_3->CompareTo(string.get()));
380 EXPECT_LT(0, string->CompareTo(string_4.get()));
381 EXPECT_GT(0, string_4->CompareTo(string.get()));
382 EXPECT_LT(0, string->CompareTo(string_5.get()));
383 EXPECT_GT(0, string_5->CompareTo(string.get()));
384}
385
jeffhao0ce13152012-03-27 19:45:50 -0700386TEST_F(ObjectTest, StringLength) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700388 SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "android"));
jeffhao0ce13152012-03-27 19:45:50 -0700389 EXPECT_EQ(string->GetLength(), 7);
390 EXPECT_EQ(string->GetUtfLength(), 7);
391
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100392 string->SetOffset<false>(2);
393 string->SetCount<false>(5);
jeffhao0ce13152012-03-27 19:45:50 -0700394 EXPECT_TRUE(string->Equals("droid"));
395 EXPECT_EQ(string->GetLength(), 5);
396 EXPECT_EQ(string->GetUtfLength(), 5);
397}
398
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700399TEST_F(ObjectTest, DescriptorCompare) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700400 // Two classloaders conflicts in compile_time_class_paths_.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 ScopedObjectAccess soa(Thread::Current());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700402 ClassLinker* linker = class_linker_;
403
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 jobject jclass_loader_1 = LoadDex("ProtoCompare");
405 jobject jclass_loader_2 = LoadDex("ProtoCompare2");
Ian Rogers1f539342012-10-03 21:09:42 -0700406 SirtRef<ClassLoader> class_loader_1(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader_1));
407 SirtRef<ClassLoader> class_loader_2(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader_2));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700408
Ian Rogers98379392014-02-24 16:53:16 -0800409 Class* klass1 = linker->FindClass(soa.Self(), "LProtoCompare;", class_loader_1);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700410 ASSERT_TRUE(klass1 != NULL);
Ian Rogers98379392014-02-24 16:53:16 -0800411 Class* klass2 = linker->FindClass(soa.Self(), "LProtoCompare2;", class_loader_2);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700412 ASSERT_TRUE(klass2 != NULL);
413
Brian Carlstromea46f952013-07-30 01:26:50 -0700414 ArtMethod* m1_1 = klass1->GetVirtualMethod(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800415 MethodHelper mh(m1_1);
416 EXPECT_STREQ(mh.GetName(), "m1");
Brian Carlstromea46f952013-07-30 01:26:50 -0700417 ArtMethod* m2_1 = klass1->GetVirtualMethod(1);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 mh.ChangeMethod(m2_1);
419 EXPECT_STREQ(mh.GetName(), "m2");
Brian Carlstromea46f952013-07-30 01:26:50 -0700420 ArtMethod* m3_1 = klass1->GetVirtualMethod(2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800421 mh.ChangeMethod(m3_1);
422 EXPECT_STREQ(mh.GetName(), "m3");
Brian Carlstromea46f952013-07-30 01:26:50 -0700423 ArtMethod* m4_1 = klass1->GetVirtualMethod(3);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800424 mh.ChangeMethod(m4_1);
425 EXPECT_STREQ(mh.GetName(), "m4");
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700426
Brian Carlstromea46f952013-07-30 01:26:50 -0700427 ArtMethod* m1_2 = klass2->GetVirtualMethod(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800428 mh.ChangeMethod(m1_2);
429 EXPECT_STREQ(mh.GetName(), "m1");
Brian Carlstromea46f952013-07-30 01:26:50 -0700430 ArtMethod* m2_2 = klass2->GetVirtualMethod(1);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800431 mh.ChangeMethod(m2_2);
432 EXPECT_STREQ(mh.GetName(), "m2");
Brian Carlstromea46f952013-07-30 01:26:50 -0700433 ArtMethod* m3_2 = klass2->GetVirtualMethod(2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800434 mh.ChangeMethod(m3_2);
435 EXPECT_STREQ(mh.GetName(), "m3");
Brian Carlstromea46f952013-07-30 01:26:50 -0700436 ArtMethod* m4_2 = klass2->GetVirtualMethod(3);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 mh.ChangeMethod(m4_2);
438 EXPECT_STREQ(mh.GetName(), "m4");
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700439
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 mh.ChangeMethod(m1_1);
441 MethodHelper mh2(m1_2);
442 EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2));
443 EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700444
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800445 mh.ChangeMethod(m2_1);
446 mh2.ChangeMethod(m2_2);
447 EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2));
448 EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700449
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800450 mh.ChangeMethod(m3_1);
451 mh2.ChangeMethod(m3_2);
452 EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2));
453 EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700454
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800455 mh.ChangeMethod(m4_1);
456 mh2.ChangeMethod(m4_2);
457 EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2));
458 EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400459}
460
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700461TEST_F(ObjectTest, StringHashCode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700463 SirtRef<String> empty(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), ""));
464 SirtRef<String> A(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "A"));
465 SirtRef<String> ABC(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "ABC"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700466
467 EXPECT_EQ(0, empty->GetHashCode());
468 EXPECT_EQ(65, A->GetHashCode());
469 EXPECT_EQ(64578, ABC->GetHashCode());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700470}
471
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700472TEST_F(ObjectTest, InstanceOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700473 ScopedObjectAccess soa(Thread::Current());
474 jobject jclass_loader = LoadDex("XandY");
Ian Rogers1f539342012-10-03 21:09:42 -0700475 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700476
Ian Rogers98379392014-02-24 16:53:16 -0800477 Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader);
478 Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700479 ASSERT_TRUE(X != NULL);
480 ASSERT_TRUE(Y != NULL);
481
Ian Rogers50b35e22012-10-04 10:09:15 -0700482 SirtRef<Object> x(soa.Self(), X->AllocObject(soa.Self()));
483 SirtRef<Object> y(soa.Self(), Y->AllocObject(soa.Self()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700484 ASSERT_TRUE(x.get() != NULL);
485 ASSERT_TRUE(y.get() != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700486
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700487 EXPECT_TRUE(x->InstanceOf(X));
488 EXPECT_FALSE(x->InstanceOf(Y));
489 EXPECT_TRUE(y->InstanceOf(X));
490 EXPECT_TRUE(y->InstanceOf(Y));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700491
Ian Rogers98379392014-02-24 16:53:16 -0800492 Class* java_lang_Class = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Class;");
493 Class* Object_array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;");
Elliott Hughes92f14b22011-10-06 12:29:54 -0700494
495 EXPECT_FALSE(java_lang_Class->InstanceOf(Object_array_class));
496 EXPECT_TRUE(Object_array_class->InstanceOf(java_lang_Class));
497
498 // All array classes implement Cloneable and Serializable.
Ian Rogers50b35e22012-10-04 10:09:15 -0700499 Object* array = ObjectArray<Object>::Alloc(soa.Self(), Object_array_class, 1);
Ian Rogers98379392014-02-24 16:53:16 -0800500 Class* java_lang_Cloneable = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Cloneable;");
501 Class* java_io_Serializable = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;");
Elliott Hughes92f14b22011-10-06 12:29:54 -0700502 EXPECT_TRUE(array->InstanceOf(java_lang_Cloneable));
503 EXPECT_TRUE(array->InstanceOf(java_io_Serializable));
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700504}
505
506TEST_F(ObjectTest, IsAssignableFrom) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700507 ScopedObjectAccess soa(Thread::Current());
508 jobject jclass_loader = LoadDex("XandY");
Ian Rogers1f539342012-10-03 21:09:42 -0700509 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader));
Ian Rogers98379392014-02-24 16:53:16 -0800510 Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader);
511 Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700512
513 EXPECT_TRUE(X->IsAssignableFrom(X));
514 EXPECT_TRUE(X->IsAssignableFrom(Y));
515 EXPECT_FALSE(Y->IsAssignableFrom(X));
516 EXPECT_TRUE(Y->IsAssignableFrom(Y));
Ian Rogersd81871c2011-10-03 13:57:23 -0700517
518 // class final String implements CharSequence, ..
Ian Rogers98379392014-02-24 16:53:16 -0800519 Class* string = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
520 Class* charseq = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/CharSequence;");
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 // Can String be assigned to CharSequence without a cast?
522 EXPECT_TRUE(charseq->IsAssignableFrom(string));
523 // Can CharSequence be assigned to String without a cast?
524 EXPECT_FALSE(string->IsAssignableFrom(charseq));
525
526 // Primitive types are only assignable to themselves
527 const char* prims = "ZBCSIJFD";
528 Class* prim_types[strlen(prims)];
529 for (size_t i = 0; i < strlen(prims); i++) {
530 prim_types[i] = class_linker_->FindPrimitiveClass(prims[i]);
531 }
532 for (size_t i = 0; i < strlen(prims); i++) {
533 for (size_t j = 0; i < strlen(prims); i++) {
534 if (i == j) {
535 EXPECT_TRUE(prim_types[i]->IsAssignableFrom(prim_types[j]));
536 } else {
537 EXPECT_FALSE(prim_types[i]->IsAssignableFrom(prim_types[j]));
538 }
539 }
540 }
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700541}
542
543TEST_F(ObjectTest, IsAssignableFromArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700544 ScopedObjectAccess soa(Thread::Current());
545 jobject jclass_loader = LoadDex("XandY");
Ian Rogers1f539342012-10-03 21:09:42 -0700546 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader));
Ian Rogers98379392014-02-24 16:53:16 -0800547 Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader);
548 Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700549 ASSERT_TRUE(X != NULL);
550 ASSERT_TRUE(Y != NULL);
551
Ian Rogers98379392014-02-24 16:53:16 -0800552 Class* YA = class_linker_->FindClass(soa.Self(), "[LY;", class_loader);
553 Class* YAA = class_linker_->FindClass(soa.Self(), "[[LY;", class_loader);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700554 ASSERT_TRUE(YA != NULL);
555 ASSERT_TRUE(YAA != NULL);
556
Ian Rogers98379392014-02-24 16:53:16 -0800557 Class* XAA = class_linker_->FindClass(soa.Self(), "[[LX;", class_loader);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700558 ASSERT_TRUE(XAA != NULL);
559
Ian Rogers98379392014-02-24 16:53:16 -0800560 Class* O = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
561 Class* OA = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;");
562 Class* OAA = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;");
563 Class* OAAA = class_linker_->FindSystemClass(soa.Self(), "[[[Ljava/lang/Object;");
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700564 ASSERT_TRUE(O != NULL);
565 ASSERT_TRUE(OA != NULL);
566 ASSERT_TRUE(OAA != NULL);
567 ASSERT_TRUE(OAAA != NULL);
568
Ian Rogers98379392014-02-24 16:53:16 -0800569 Class* S = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;");
570 Class* SA = class_linker_->FindSystemClass(soa.Self(), "[Ljava/io/Serializable;");
571 Class* SAA = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/io/Serializable;");
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700572 ASSERT_TRUE(S != NULL);
573 ASSERT_TRUE(SA != NULL);
574 ASSERT_TRUE(SAA != NULL);
575
Ian Rogers98379392014-02-24 16:53:16 -0800576 Class* IA = class_linker_->FindSystemClass(soa.Self(), "[I");
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700577 ASSERT_TRUE(IA != NULL);
578
579 EXPECT_TRUE(YAA->IsAssignableFrom(YAA)); // identity
580 EXPECT_TRUE(XAA->IsAssignableFrom(YAA)); // element superclass
581 EXPECT_FALSE(YAA->IsAssignableFrom(XAA));
582 EXPECT_FALSE(Y->IsAssignableFrom(YAA));
583 EXPECT_FALSE(YA->IsAssignableFrom(YAA));
584 EXPECT_TRUE(O->IsAssignableFrom(YAA)); // everything is an Object
585 EXPECT_TRUE(OA->IsAssignableFrom(YAA));
586 EXPECT_TRUE(OAA->IsAssignableFrom(YAA));
587 EXPECT_TRUE(S->IsAssignableFrom(YAA)); // all arrays are Serializable
588 EXPECT_TRUE(SA->IsAssignableFrom(YAA));
589 EXPECT_FALSE(SAA->IsAssignableFrom(YAA)); // unless Y was Serializable
590
591 EXPECT_FALSE(IA->IsAssignableFrom(OA));
592 EXPECT_FALSE(OA->IsAssignableFrom(IA));
593 EXPECT_TRUE(O->IsAssignableFrom(IA));
594}
595
Elliott Hughescdf53122011-08-19 15:46:09 -0700596TEST_F(ObjectTest, FindInstanceField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700597 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700598 SirtRef<String> s(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "ABC"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700599 ASSERT_TRUE(s.get() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700600 Class* c = s->GetClass();
601 ASSERT_TRUE(c != NULL);
602
603 // Wrong type.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700604 EXPECT_TRUE(c->FindDeclaredInstanceField("count", "J") == NULL);
605 EXPECT_TRUE(c->FindInstanceField("count", "J") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700606
607 // Wrong name.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700608 EXPECT_TRUE(c->FindDeclaredInstanceField("Count", "I") == NULL);
609 EXPECT_TRUE(c->FindInstanceField("Count", "I") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700610
611 // Right name and type.
Brian Carlstromea46f952013-07-30 01:26:50 -0700612 ArtField* f1 = c->FindDeclaredInstanceField("count", "I");
613 ArtField* f2 = c->FindInstanceField("count", "I");
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 EXPECT_TRUE(f1 != NULL);
615 EXPECT_TRUE(f2 != NULL);
616 EXPECT_EQ(f1, f2);
617
618 // TODO: check that s.count == 3.
619
620 // Ensure that we handle superclass fields correctly...
Ian Rogers98379392014-02-24 16:53:16 -0800621 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/StringBuilder;");
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 ASSERT_TRUE(c != NULL);
623 // No StringBuilder.count...
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700624 EXPECT_TRUE(c->FindDeclaredInstanceField("count", "I") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 // ...but there is an AbstractStringBuilder.count.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700626 EXPECT_TRUE(c->FindInstanceField("count", "I") != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700627}
628
629TEST_F(ObjectTest, FindStaticField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700631 SirtRef<String> s(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), "ABC"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700632 ASSERT_TRUE(s.get() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 Class* c = s->GetClass();
634 ASSERT_TRUE(c != NULL);
635
636 // Wrong type.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700637 EXPECT_TRUE(c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "I") == NULL);
638 EXPECT_TRUE(c->FindStaticField("CASE_INSENSITIVE_ORDER", "I") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700639
640 // Wrong name.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700641 EXPECT_TRUE(c->FindDeclaredStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == NULL);
642 EXPECT_TRUE(c->FindStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700643
644 // Right name and type.
Brian Carlstromea46f952013-07-30 01:26:50 -0700645 ArtField* f1 = c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
646 ArtField* f2 = c->FindStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 EXPECT_TRUE(f1 != NULL);
648 EXPECT_TRUE(f2 != NULL);
649 EXPECT_EQ(f1, f2);
650
651 // TODO: test static fields via superclasses.
652 // TODO: test static fields via interfaces.
653 // TODO: test that interfaces trump superclasses.
654}
655
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800656} // namespace mirror
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700657} // namespace art