blob: f2043b13323a1c5b8aeb03bbabf3748d60fffe50 [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 Shapiro3ee755d2011-06-28 12:11:04 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020
Ian Rogersdf20fe02011-07-20 20:34:16 -070021#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include <string>
24#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070028#include "dex_cache.h"
29#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070034#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Ian Rogers60db5ab2012-02-20 17:02:00 -080037#include "runtime_support.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070038#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070039#include "utils.h"
Elliott Hughesa4f94742012-05-29 16:28:38 -070040#include "well_known_classes.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070041
42namespace art {
43
Elliott Hughesdbb40792011-11-18 17:05:22 -080044String* Object::AsString() {
45 DCHECK(GetClass()->IsStringClass());
46 return down_cast<String*>(this);
47}
48
Elliott Hughes081be7f2011-09-18 16:50:26 -070049Object* Object::Clone() {
50 Class* c = GetClass();
51 DCHECK(!c->IsClassClass());
52
53 // Object::SizeOf gets the right size even if we're an array.
54 // Using c->AllocObject() here would be wrong.
55 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080056 Heap* heap = Runtime::Current()->GetHeap();
57 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070058 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070059 return NULL;
60 }
61
62 // Copy instance data. We assume memcpy copies by words.
63 // TODO: expose and use move32.
64 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070065 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070066 size_t offset = sizeof(Object);
67 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
68
Mathieu Chartier88c95be2012-09-11 14:06:41 -070069 // Perform write barriers on copied object references.
70 if (c->IsArrayClass()) {
71 if (!c->GetComponentType()->IsPrimitive()) {
72 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
73 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
74 }
75 } else {
76 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
77 size_t num_reference_fields = klass->NumReferenceInstanceFields();
78 for (size_t i = 0; i < num_reference_fields; ++i) {
79 Field* field = klass->GetInstanceField(i);
80 MemberOffset field_offset = field->GetOffset();
81 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
82 heap->WriteBarrierField(copy.get(), field_offset, ref);
83 }
84 }
85 }
86
Elliott Hughes20cde902011-10-04 17:37:27 -070087 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080088 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070089 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070090
Brian Carlstrom40381fb2011-10-19 14:13:40 -070091 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070092}
93
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070094uint32_t Object::GetThinLockId() {
95 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070096}
97
98void Object::MonitorEnter(Thread* thread) {
99 Monitor::MonitorEnter(thread, this);
100}
101
Ian Rogersff1ed472011-09-20 13:46:24 -0700102bool Object::MonitorExit(Thread* thread) {
103 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700104}
105
106void Object::Notify() {
107 Monitor::Notify(Thread::Current(), this);
108}
109
110void Object::NotifyAll() {
111 Monitor::NotifyAll(Thread::Current(), this);
112}
113
114void Object::Wait(int64_t ms, int32_t ns) {
115 Monitor::Wait(Thread::Current(), this, ms, ns, true);
116}
117
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700118// TODO: get global references for these
119Class* Field::java_lang_reflect_Field_ = NULL;
120
121void Field::SetClass(Class* java_lang_reflect_Field) {
122 CHECK(java_lang_reflect_Field_ == NULL);
123 CHECK(java_lang_reflect_Field != NULL);
124 java_lang_reflect_Field_ = java_lang_reflect_Field;
125}
126
127void Field::ResetClass() {
128 CHECK(java_lang_reflect_Field_ != NULL);
129 java_lang_reflect_Field_ = NULL;
130}
131
Ian Rogers0571d352011-11-03 19:51:38 -0700132void Field::SetOffset(MemberOffset num_bytes) {
133 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134#if 0 // TODO enable later in boot and under !NDEBUG
135 FieldHelper fh(this);
136 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700137 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
138 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
139 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700141 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
142}
143
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700144uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700146 if (IsStatic()) {
147 object = declaring_class_;
148 }
149 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700150}
151
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700152void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700153 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154 if (IsStatic()) {
155 object = declaring_class_;
156 }
157 object->SetField32(GetOffset(), new_value, IsVolatile());
158}
159
160uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700162 if (IsStatic()) {
163 object = declaring_class_;
164 }
165 return object->GetField64(GetOffset(), IsVolatile());
166}
167
168void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700169 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700170 if (IsStatic()) {
171 object = declaring_class_;
172 }
173 object->SetField64(GetOffset(), new_value, IsVolatile());
174}
175
176Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700177 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700178 if (IsStatic()) {
179 object = declaring_class_;
180 }
181 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
182}
183
184void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700185 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186 if (IsStatic()) {
187 object = declaring_class_;
188 }
189 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
190}
191
192bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
194 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 return Get32(object);
196}
197
198void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
200 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 Set32(object, z);
202}
203
204int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
206 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 return Get32(object);
208}
209
210void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
212 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 Set32(object, b);
214}
215
216uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
218 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 return Get32(object);
220}
221
222void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
224 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 Set32(object, c);
226}
227
Ian Rogers466bb252011-10-14 03:29:56 -0700228int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800229 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
230 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 return Get32(object);
232}
233
Ian Rogers466bb252011-10-14 03:29:56 -0700234void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800235 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
236 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 Set32(object, s);
238}
239
240int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800241 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
242 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 return Get32(object);
244}
245
246void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
248 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 Set32(object, i);
250}
251
252int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800253 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
254 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700255 return Get64(object);
256}
257
258void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
260 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 Set64(object, j);
262}
263
Elliott Hughes1d878f32012-04-11 15:17:54 -0700264union Bits {
265 jdouble d;
266 jfloat f;
267 jint i;
268 jlong j;
269};
270
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800272 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
273 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700274 Bits bits;
275 bits.i = Get32(object);
276 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277}
278
279void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
281 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700282 Bits bits;
283 bits.f = f;
284 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285}
286
287double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800288 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
289 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700290 Bits bits;
291 bits.j = Get64(object);
292 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700293}
294
295void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
297 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700298 Bits bits;
299 bits.d = d;
300 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301}
302
303Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
305 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 return GetObj(object);
307}
308
309void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800310 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
311 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 SetObj(object, l);
313}
314
315// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700316Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700317Class* Method::java_lang_reflect_Method_ = NULL;
318
Ian Rogers08f753d2012-08-24 14:35:25 -0700319InvokeType Method::GetInvokeType() const {
320 // TODO: kSuper?
321 if (GetDeclaringClass()->IsInterface()) {
322 return kInterface;
323 } else if (IsStatic()) {
324 return kStatic;
325 } else if (IsDirect()) {
326 return kDirect;
327 } else {
328 return kVirtual;
329 }
330}
331
Elliott Hughes80609252011-09-23 17:24:51 -0700332void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
333 CHECK(java_lang_reflect_Constructor_ == NULL);
334 CHECK(java_lang_reflect_Constructor != NULL);
335 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
336
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 CHECK(java_lang_reflect_Method_ == NULL);
338 CHECK(java_lang_reflect_Method != NULL);
339 java_lang_reflect_Method_ = java_lang_reflect_Method;
340}
341
Elliott Hughes80609252011-09-23 17:24:51 -0700342void Method::ResetClasses() {
343 CHECK(java_lang_reflect_Constructor_ != NULL);
344 java_lang_reflect_Constructor_ = NULL;
345
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346 CHECK(java_lang_reflect_Method_ != NULL);
347 java_lang_reflect_Method_ = NULL;
348}
349
350ObjectArray<String>* Method::GetDexCacheStrings() const {
351 return GetFieldObject<ObjectArray<String>*>(
352 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
353}
354
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
356 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
357 new_dex_cache_strings, false);
358}
359
Ian Rogers19846512012-02-24 11:42:47 -0800360ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
361 return GetFieldObject<ObjectArray<Method>*>(
362 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
363}
364
365void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
366 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
367 new_dex_cache_methods, false);
368}
369
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700370ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
371 return GetFieldObject<ObjectArray<Class>*>(
372 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
373}
374
375void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
376 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
377 new_dex_cache_classes, false);
378}
379
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
381 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
382 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
383 false);
384}
385
386void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700387 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389}
390
391size_t Method::NumArgRegisters(const StringPiece& shorty) {
392 CHECK_LE(1, shorty.length());
393 uint32_t num_registers = 0;
394 for (int i = 1; i < shorty.length(); ++i) {
395 char ch = shorty[i];
396 if (ch == 'D' || ch == 'J') {
397 num_registers += 2;
398 } else {
399 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700400 }
401 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700402 return num_registers;
403}
404
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800405bool Method::IsProxyMethod() const {
406 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700407}
408
Ian Rogers466bb252011-10-14 03:29:56 -0700409Method* Method::FindOverriddenMethod() const {
410 if (IsStatic()) {
411 return NULL;
412 }
413 Class* declaring_class = GetDeclaringClass();
414 Class* super_class = declaring_class->GetSuperClass();
415 uint16_t method_index = GetMethodIndex();
416 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
417 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 // Did this method override a super class method? If so load the result from the super class'
419 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700420 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
421 result = super_class_vtable->Get(method_index);
422 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800423 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800424 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800425 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
426 CHECK_EQ(result,
427 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800428 } else {
429 MethodHelper mh(this);
430 MethodHelper interface_mh;
431 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
432 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
433 InterfaceEntry* entry = iftable->Get(i);
434 Class* interface = entry->GetInterface();
435 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
436 Method* interface_method = interface->GetVirtualMethod(j);
437 interface_mh.ChangeMethod(interface_method);
438 if (mh.HasSameNameAndSignature(&interface_mh)) {
439 result = interface_method;
440 break;
441 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 }
443 }
Ian Rogers466bb252011-10-14 03:29:56 -0700444 }
445 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446#ifndef NDEBUG
447 MethodHelper result_mh(result);
448 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
449#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700450 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700451}
452
Ian Rogers0c7abda2012-09-19 13:33:42 -0700453static const void* GetOatCode(const Method* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800454 Runtime* runtime = Runtime::Current();
455 const void* code = m->GetCode();
456 // Peel off any method tracing trampoline.
457 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
458 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
459 }
460 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800461 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800462 code = runtime->GetClassLinker()->GetOatCodeFor(m);
463 }
464 return code;
465}
466
Ian Rogers0c7abda2012-09-19 13:33:42 -0700467uintptr_t Method::NativePcOffset(const uintptr_t pc) const {
468 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
469}
470
471uint32_t Method::ToDexPc(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700472#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700473 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700474 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800475 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700476 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700477 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700478 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800479 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700480 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700481 if (mapping_table[i] == sought_offset) {
482 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700483 }
484 }
buzbee8320f382012-09-11 16:29:42 -0700485 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
486 << " in " << PrettyMethod(this);
487 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700488#else
489 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
490 return static_cast<uint32_t>(pc);
491#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700492}
493
Ian Rogers0c7abda2012-09-19 13:33:42 -0700494uintptr_t Method::ToNativePc(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700495 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700496 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700497 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700498 return 0; // Special no mapping/pc == 0 case
499 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700500 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700501 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 uint32_t map_offset = mapping_table[i];
503 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700504 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800505 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700506 }
507 }
508 LOG(FATAL) << "Looking up Dex PC not contained in method";
509 return 0;
510}
511
512uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800513 MethodHelper mh(this);
514 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700515 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700516 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
517 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700518 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700519 if (iter_type_idx == DexFile::kDexNoIndex16) {
520 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700521 }
522 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800523 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700524 if (iter_exception_type == NULL) {
525 // The verifier should take care of resolving all exception classes early
526 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700528 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700529 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700530 }
531 }
532 // Handler not found
533 return DexFile::kDexNoIndex;
534}
535
Elliott Hughes77405792012-03-15 15:22:12 -0700536void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 if (kIsDebugBuild) {
538 self->AssertThreadSuspensionIsAllowable();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700539 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700540 CHECK_EQ(kRunnable, self->GetState());
541 }
TDYa12785321912012-04-01 15:24:56 -0700542
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700544 ManagedStack fragment;
545 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700546
547 // Call the invoke stub associated with the method.
548 // Pass everything as arguments.
Ian Rogers1b09b092012-08-20 15:35:52 -0700549 Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700550
551 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700552
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500553 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700554 bool log = false;
555 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800556 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
557 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700558 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700559 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700560 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800561 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
562 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700563 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700564 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800565 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
566 PrettyMethod(this).c_str(), GetCode(), stub,
567 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700568 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700569 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700570 }
571 }
572
573 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700574 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700575}
576
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700577bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700578 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800579 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800580 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700581 return native_method != jni_stub;
582}
583
Ian Rogers60db5ab2012-02-20 17:02:00 -0800584void Method::RegisterNative(Thread* self, const void* native_method) {
585 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700586 CHECK(IsNative()) << PrettyMethod(this);
587 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700588#if defined(ART_USE_LLVM_COMPILER)
589 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
590 native_method, false);
591#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800592 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
593 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
594 native_method, false);
595 } else {
596 // We've been asked to associate this method with the given native method but are working
597 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
598 // the native method to runtime support and store the target somewhere runtime support will
599 // find it.
600#if defined(__arm__)
601 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
602 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
603#else
604 UNIMPLEMENTED(FATAL);
605#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -0700606 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800607 reinterpret_cast<const uint8_t*>(native_method), false);
608 }
TDYa12726467572012-04-17 20:51:22 -0700609#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700610}
611
Ian Rogers19846512012-02-24 11:42:47 -0800612void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700613 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700614 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800615 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700616}
617
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700618void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700619 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
620 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700621 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700622 if (new_status > kStatusResolved) {
623 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
624 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800625 if (new_status == kStatusError) {
626 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
627
628 // stash current exception
629 Thread* self = Thread::Current();
630 SirtRef<Throwable> exception(self->GetException());
631 CHECK(exception.get() != NULL);
632
633 // clear exception to call FindSystemClass
634 self->ClearException();
635 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
636 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
637 CHECK(!self->IsExceptionPending());
638
639 // only verification errors, not initialization problems, should set a verify error.
640 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
641 Class* exception_class = exception->GetClass();
642 if (!eiie_class->IsAssignableFrom(exception_class)) {
643 SetVerifyErrorClass(exception_class);
644 }
645
646 // restore exception
647 self->SetException(exception.get());
648 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700649 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700650}
651
652DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700653 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700654}
655
656void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700657 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700658}
659
Brian Carlstrom1f870082011-08-23 16:02:11 -0700660Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700661 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700662 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500663 // TODO: decide whether we want this check. It currently fails during bootstrap.
664 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700665 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800666 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700667}
668
Ian Rogers0571d352011-11-03 19:51:38 -0700669void Class::SetClassSize(size_t new_class_size) {
670 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
671 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
672}
673
Ian Rogersd418eda2012-01-30 12:14:28 -0800674// Return the class' name. The exact format is bizarre, but it's the specified behavior for
675// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
676// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
677// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
678String* Class::ComputeName() {
679 String* name = GetName();
680 if (name != NULL) {
681 return name;
682 }
683 std::string descriptor(ClassHelper(this).GetDescriptor());
684 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
685 // The descriptor indicates that this is the class for
686 // a primitive type; special-case the return value.
687 const char* c_name = NULL;
688 switch (descriptor[0]) {
689 case 'Z': c_name = "boolean"; break;
690 case 'B': c_name = "byte"; break;
691 case 'C': c_name = "char"; break;
692 case 'S': c_name = "short"; break;
693 case 'I': c_name = "int"; break;
694 case 'J': c_name = "long"; break;
695 case 'F': c_name = "float"; break;
696 case 'D': c_name = "double"; break;
697 case 'V': c_name = "void"; break;
698 default:
699 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
700 }
701 name = String::AllocFromModifiedUtf8(c_name);
702 } else {
703 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
704 // components.
705 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
706 descriptor.erase(0, 1);
707 descriptor.erase(descriptor.size() - 1);
708 }
709 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
710 name = String::AllocFromModifiedUtf8(descriptor.c_str());
711 }
712 SetName(name);
713 return name;
714}
715
Elliott Hughes4681c802011-09-25 18:04:37 -0700716void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700717 if ((flags & kDumpClassFullDetail) == 0) {
718 os << PrettyClass(this);
719 if ((flags & kDumpClassClassLoader) != 0) {
720 os << ' ' << GetClassLoader();
721 }
722 if ((flags & kDumpClassInitialized) != 0) {
723 os << ' ' << GetStatus();
724 }
Elliott Hughese0918552011-10-28 17:18:29 -0700725 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700726 return;
727 }
728
729 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800730 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700731 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800732 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700733 os << " objectSize=" << SizeOf() << " "
734 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
735 os << StringPrintf(" access=0x%04x.%04x\n",
736 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
737 if (super != NULL) {
738 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
739 }
740 if (IsArrayClass()) {
741 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
742 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700743 if (kh.NumDirectInterfaces() > 0) {
744 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
745 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
746 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700747 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800748 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700749 }
750 }
751 os << " vtable (" << NumVirtualMethods() << " entries, "
752 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
753 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800754 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700755 }
756 os << " direct methods (" << NumDirectMethods() << " entries):\n";
757 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800758 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700759 }
760 if (NumStaticFields() > 0) {
761 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700762 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700763 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800764 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700765 }
766 } else {
767 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700768 }
769 }
770 if (NumInstanceFields() > 0) {
771 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700772 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700773 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800774 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700775 }
776 } else {
777 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700778 }
779 }
780}
781
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700782void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
783 if (new_reference_offsets != CLASS_WALK_SUPER) {
784 // Sanity check that the number of bits set in the reference offset bitmap
785 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800786 size_t count = 0;
787 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
788 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700789 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800790 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700791 }
792 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
793 new_reference_offsets, false);
794}
795
796void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
797 if (new_reference_offsets != CLASS_WALK_SUPER) {
798 // Sanity check that the number of bits set in the reference offset bitmap
799 // agrees with the number of references
800 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
801 NumReferenceStaticFieldsDuringLinking());
802 }
803 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
804 new_reference_offsets, false);
805}
806
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700807bool Class::Implements(const Class* klass) const {
808 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700809 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700810 // All interfaces implemented directly and by our superclass, and
811 // recursively all super-interfaces of those interfaces, are listed
812 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700813 int32_t iftable_count = GetIfTableCount();
814 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
815 for (int32_t i = 0; i < iftable_count; i++) {
816 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700817 return true;
818 }
819 }
820 return false;
821}
822
Elliott Hughese84278b2012-03-22 10:06:53 -0700823// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700824// are array classes.
825//
826// Consider an array class, e.g. Y[][], where Y is a subclass of X.
827// Y[][] = Y[][] --> true (identity)
828// X[][] = Y[][] --> true (element superclass)
829// Y = Y[][] --> false
830// Y[] = Y[][] --> false
831// Object = Y[][] --> true (everything is an object)
832// Object[] = Y[][] --> true
833// Object[][] = Y[][] --> true
834// Object[][][] = Y[][] --> false (too many []s)
835// Serializable = Y[][] --> true (all arrays are Serializable)
836// Serializable[] = Y[][] --> true
837// Serializable[][] = Y[][] --> false (unless Y is Serializable)
838//
839// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700840// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700841//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700842bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700843 DCHECK(IsArrayClass()) << PrettyClass(this);
844 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700846}
847
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700848bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700849 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
850 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700851 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700852 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700853 // src's super should be java_lang_Object, since it is an array.
854 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700855 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
856 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700857 return this == java_lang_Object;
858 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700859 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700860}
861
862bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700863 DCHECK(!IsInterface()) << PrettyClass(this);
864 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700865 const Class* current = this;
866 do {
867 if (current == klass) {
868 return true;
869 }
870 current = current->GetSuperClass();
871 } while (current != NULL);
872 return false;
873}
874
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800875bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700876 size_t i = 0;
877 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
878 ++i;
879 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700880 if (descriptor1.find('/', i) != StringPiece::npos ||
881 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700882 return false;
883 } else {
884 return true;
885 }
886}
887
888bool Class::IsInSamePackage(const Class* that) const {
889 const Class* klass1 = this;
890 const Class* klass2 = that;
891 if (klass1 == klass2) {
892 return true;
893 }
894 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700895 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700896 return false;
897 }
898 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700899 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700900 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700901 }
jeffhao4a801a42011-09-23 13:53:40 -0700902 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700903 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700904 }
905 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800906 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800907 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800908 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800909 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800910 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700911}
912
Elliott Hughesdbb40792011-11-18 17:05:22 -0800913bool Class::IsClassClass() const {
914 Class* java_lang_Class = GetClass()->GetClass();
915 return this == java_lang_Class;
916}
917
918bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800919 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800920}
921
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800922bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700923 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800924}
925
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800926ClassLoader* Class::GetClassLoader() const {
927 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700928}
929
Ian Rogers365c1022012-06-22 15:05:28 -0700930void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700932}
933
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800934Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700935 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700936 DCHECK(declaring_class != NULL) << PrettyClass(this);
937 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700938 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700939 int32_t iftable_count = GetIfTableCount();
940 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
941 for (int32_t i = 0; i < iftable_count; i++) {
942 InterfaceEntry* interface_entry = iftable->Get(i);
943 if (interface_entry->GetInterface() == declaring_class) {
944 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700945 }
946 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700947 return NULL;
948}
949
Ian Rogers466bb252011-10-14 03:29:56 -0700950Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700951 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800952 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700953 if (method != NULL) {
954 return method;
955 }
956
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700957 int32_t iftable_count = GetIfTableCount();
958 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
959 for (int32_t i = 0; i < iftable_count; i++) {
960 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700961 if (method != NULL) {
962 return method;
963 }
964 }
965 return NULL;
966}
967
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800968Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
969 // Check the current class before checking the interfaces.
970 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
971 if (method != NULL) {
972 return method;
973 }
974
975 int32_t iftable_count = GetIfTableCount();
976 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
977 for (int32_t i = 0; i < iftable_count; i++) {
978 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
979 if (method != NULL) {
980 return method;
981 }
982 }
983 return NULL;
984}
985
986
987Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800988 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700989 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700990 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800991 mh.ChangeMethod(method);
992 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700993 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700994 }
995 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700996 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700997}
998
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800999Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1000 if (GetDexCache() == dex_cache) {
1001 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1002 Method* method = GetDirectMethod(i);
1003 if (method->GetDexMethodIndex() == dex_method_idx) {
1004 return method;
1005 }
1006 }
1007 }
1008 return NULL;
1009}
1010
1011Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1012 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001013 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001014 if (method != NULL) {
1015 return method;
1016 }
1017 }
1018 return NULL;
1019}
1020
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001021Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1022 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1023 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1024 if (method != NULL) {
1025 return method;
1026 }
1027 }
1028 return NULL;
1029}
1030
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001031Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001032 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001033 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001034 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001035 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001036 mh.ChangeMethod(method);
1037 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001038 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001039 }
1040 }
1041 return NULL;
1042}
1043
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001044Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1045 if (GetDexCache() == dex_cache) {
1046 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1047 Method* method = GetVirtualMethod(i);
1048 if (method->GetDexMethodIndex() == dex_method_idx) {
1049 return method;
1050 }
1051 }
1052 }
1053 return NULL;
1054}
1055
Ian Rogers466bb252011-10-14 03:29:56 -07001056Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1057 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1058 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1059 if (method != NULL) {
1060 return method;
1061 }
1062 }
1063 return NULL;
1064}
1065
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001066Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1067 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1068 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1069 if (method != NULL) {
1070 return method;
1071 }
1072 }
1073 return NULL;
1074}
1075
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001076Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 // Is the field in this class?
1078 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001079 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1081 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001082 fh.ChangeField(f);
1083 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001084 return f;
1085 }
1086 }
1087 return NULL;
1088}
1089
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001090Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1091 if (GetDexCache() == dex_cache) {
1092 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1093 Field* f = GetInstanceField(i);
1094 if (f->GetDexFieldIndex() == dex_field_idx) {
1095 return f;
1096 }
1097 }
1098 }
1099 return NULL;
1100}
1101
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001102Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 // Is the field in this class, or any of its superclasses?
1104 // Interfaces are not relevant because they can't contain instance fields.
1105 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001106 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 if (f != NULL) {
1108 return f;
1109 }
1110 }
1111 return NULL;
1112}
1113
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001114Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1115 // Is the field in this class, or any of its superclasses?
1116 // Interfaces are not relevant because they can't contain instance fields.
1117 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1118 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1119 if (f != NULL) {
1120 return f;
1121 }
1122 }
1123 return NULL;
1124}
1125
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001126Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1127 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001128 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001129 for (size_t i = 0; i < NumStaticFields(); ++i) {
1130 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001131 fh.ChangeField(f);
1132 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001133 return f;
1134 }
1135 }
1136 return NULL;
1137}
1138
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001139Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1140 if (dex_cache == GetDexCache()) {
1141 for (size_t i = 0; i < NumStaticFields(); ++i) {
1142 Field* f = GetStaticField(i);
1143 if (f->GetDexFieldIndex() == dex_field_idx) {
1144 return f;
1145 }
1146 }
1147 }
1148 return NULL;
1149}
1150
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001151Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1152 // Is the field in this class (or its interfaces), or any of its
1153 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001154 ClassHelper kh;
1155 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001156 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001157 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001158 if (f != NULL) {
1159 return f;
1160 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001161 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001162 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001163 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1164 Class* interface = kh.GetDirectInterface(i);
1165 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001166 if (f != NULL) {
1167 return f;
1168 }
1169 }
1170 }
1171 return NULL;
1172}
1173
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001174Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1175 ClassHelper kh;
1176 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1177 // Is the field in this class?
1178 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1179 if (f != NULL) {
1180 return f;
1181 }
1182 // Is this field in any of this class' interfaces?
1183 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001184 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1185 Class* interface = kh.GetDirectInterface(i);
1186 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001187 if (f != NULL) {
1188 return f;
1189 }
1190 }
1191 }
1192 return NULL;
1193}
1194
Ian Rogersb067ac22011-12-13 18:05:09 -08001195Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1196 // Find a field using the JLS field resolution order
1197 ClassHelper kh;
1198 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1199 // Is the field in this class?
1200 Field* f = k->FindDeclaredInstanceField(name, type);
1201 if (f != NULL) {
1202 return f;
1203 }
1204 f = k->FindDeclaredStaticField(name, type);
1205 if (f != NULL) {
1206 return f;
1207 }
1208 // Is this field in any of this class' interfaces?
1209 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001210 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1211 Class* interface = kh.GetDirectInterface(i);
1212 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001213 if (f != NULL) {
1214 return f;
1215 }
1216 }
1217 }
1218 return NULL;
1219}
1220
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001221Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001222 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001223 DCHECK_GE(component_count, 0);
1224 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001225
Ian Rogersa15e67d2012-02-28 13:51:55 -08001226 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001227 size_t data_size = component_count * component_size;
1228 size_t size = header_size + data_size;
1229
1230 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1231 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1232 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1233 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001234 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001235 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001236 return NULL;
1237 }
1238
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001239 Heap* heap = Runtime::Current()->GetHeap();
1240 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001241 if (array != NULL) {
1242 DCHECK(array->IsArrayInstance());
1243 array->SetLength(component_count);
1244 }
1245 return array;
1246}
1247
1248Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001250 return Alloc(array_class, component_count, array_class->GetComponentSize());
1251}
1252
Elliott Hughes80609252011-09-23 17:24:51 -07001253bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001254 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001255 "length=%i; index=%i", length_, index);
1256 return false;
1257}
1258
1259bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001260 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001261 "Can't store an element of type %s into an array of type %s",
1262 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1263 return false;
1264}
1265
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001266template<typename T>
1267PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001268 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001269 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1270 return down_cast<PrimitiveArray<T>*>(raw_array);
1271}
1272
1273template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1274
1275// Explicitly instantiate all the primitive array types.
1276template class PrimitiveArray<uint8_t>; // BooleanArray
1277template class PrimitiveArray<int8_t>; // ByteArray
1278template class PrimitiveArray<uint16_t>; // CharArray
1279template class PrimitiveArray<double>; // DoubleArray
1280template class PrimitiveArray<float>; // FloatArray
1281template class PrimitiveArray<int32_t>; // IntArray
1282template class PrimitiveArray<int64_t>; // LongArray
1283template class PrimitiveArray<int16_t>; // ShortArray
1284
Ian Rogers466bb252011-10-14 03:29:56 -07001285// Explicitly instantiate Class[][]
1286template class ObjectArray<ObjectArray<Class> >;
1287
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001288// TODO: get global references for these
1289Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001290
Brian Carlstroma663ea52011-08-19 23:33:41 -07001291void String::SetClass(Class* java_lang_String) {
1292 CHECK(java_lang_String_ == NULL);
1293 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001294 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001295}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001296
Brian Carlstroma663ea52011-08-19 23:33:41 -07001297void String::ResetClass() {
1298 CHECK(java_lang_String_ != NULL);
1299 java_lang_String_ = NULL;
1300}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001301
Brian Carlstromc74255f2011-09-11 22:47:39 -07001302String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001303 return Runtime::Current()->GetInternTable()->InternWeak(this);
1304}
1305
Brian Carlstrom395520e2011-09-25 19:35:00 -07001306int32_t String::GetHashCode() {
1307 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1308 if (result == 0) {
1309 ComputeHashCode();
1310 }
1311 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1312 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1313 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001314 return result;
1315}
1316
1317int32_t String::GetLength() const {
1318 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1319 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1320 return result;
1321}
1322
1323uint16_t String::CharAt(int32_t index) const {
1324 // TODO: do we need this? Equals is the only caller, and could
1325 // bounds check itself.
1326 if (index < 0 || index >= count_) {
1327 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001328 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001329 "length=%i; index=%i", count_, index);
1330 return 0;
1331 }
1332 return GetCharArray()->Get(index + GetOffset());
1333}
1334
1335String* String::AllocFromUtf16(int32_t utf16_length,
1336 const uint16_t* utf16_data_in,
1337 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001338 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001339 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001340 if (string == NULL) {
1341 return NULL;
1342 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001343 // TODO: use 16-bit wide memset variant
1344 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001345 if (array == NULL) {
1346 return NULL;
1347 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001348 for (int i = 0; i < utf16_length; i++) {
1349 array->Set(i, utf16_data_in[i]);
1350 }
1351 if (hash_code != 0) {
1352 string->SetHashCode(hash_code);
1353 } else {
1354 string->ComputeHashCode();
1355 }
1356 return string;
1357}
1358
1359String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001360 if (utf == NULL) {
1361 return NULL;
1362 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363 size_t char_count = CountModifiedUtf8Chars(utf);
1364 return AllocFromModifiedUtf8(char_count, utf);
1365}
1366
1367String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1368 const char* utf8_data_in) {
1369 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001370 if (string == NULL) {
1371 return NULL;
1372 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001373 uint16_t* utf16_data_out =
1374 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1375 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1376 string->ComputeHashCode();
1377 return string;
1378}
1379
1380String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001381 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1382 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001383 return NULL;
1384 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001385 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001386}
1387
1388String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001389 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001390 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001391 if (string == NULL) {
1392 return NULL;
1393 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001394 string->SetArray(array);
1395 string->SetCount(array->GetLength());
1396 return string;
1397}
1398
1399bool String::Equals(const String* that) const {
1400 if (this == that) {
1401 // Quick reference equality test
1402 return true;
1403 } else if (that == NULL) {
1404 // Null isn't an instanceof anything
1405 return false;
1406 } else if (this->GetLength() != that->GetLength()) {
1407 // Quick length inequality test
1408 return false;
1409 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001410 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001411 // hash code was already equal
1412 for (int32_t i = 0; i < that->GetLength(); ++i) {
1413 if (this->CharAt(i) != that->CharAt(i)) {
1414 return false;
1415 }
1416 }
1417 return true;
1418 }
1419}
1420
Elliott Hughes5d78d392011-12-13 16:53:05 -08001421bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001422 if (this->GetLength() != that_length) {
1423 return false;
1424 } else {
1425 for (int32_t i = 0; i < that_length; ++i) {
1426 if (this->CharAt(i) != that_chars[that_offset + i]) {
1427 return false;
1428 }
1429 }
1430 return true;
1431 }
1432}
1433
1434bool String::Equals(const char* modified_utf8) const {
1435 for (int32_t i = 0; i < GetLength(); ++i) {
1436 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1437 if (ch == '\0' || ch != CharAt(i)) {
1438 return false;
1439 }
1440 }
1441 return *modified_utf8 == '\0';
1442}
1443
1444bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001445 if (modified_utf8.size() != GetLength()) {
1446 return false;
1447 }
1448 const char* p = modified_utf8.data();
1449 for (int32_t i = 0; i < GetLength(); ++i) {
1450 uint16_t ch = GetUtf16FromUtf8(&p);
1451 if (ch != CharAt(i)) {
1452 return false;
1453 }
1454 }
1455 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001456}
1457
1458// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1459std::string String::ToModifiedUtf8() const {
1460 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001461 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001462 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001463 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1464 return result;
1465}
1466
Ian Rogers1c5eb702012-02-01 09:18:34 -08001467void Throwable::SetCause(Throwable* cause) {
1468 CHECK(cause != NULL);
1469 CHECK(cause != this);
1470 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1471 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1472}
1473
Ian Rogers466bb252011-10-14 03:29:56 -07001474bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001475 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001476 return false;
1477 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001478 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001479}
1480
Ian Rogers9074b992011-10-26 17:41:55 -07001481std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001482 std::string result(PrettyTypeOf(this));
1483 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001484 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001485 if (msg != NULL) {
1486 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001487 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001488 result += "\n";
1489 Object* stack_state = GetStackState();
1490 // check stack state isn't missing or corrupt
1491 if (stack_state != NULL && stack_state->IsObjectArray()) {
1492 // Decode the internal stack trace into the depth and method trace
1493 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1494 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001495 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1496 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001497 for (int32_t i = 0; i < depth; ++i) {
1498 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001499 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001500 uint32_t dex_pc = pc_trace->Get(i);
1501 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001502 const char* source_file = mh.GetDeclaringClassSourceFile();
1503 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1504 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001505 }
Ian Rogers9074b992011-10-26 17:41:55 -07001506 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001507 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001508 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001509 result += "Caused by: ";
1510 result += cause->Dump();
1511 }
Ian Rogers9074b992011-10-26 17:41:55 -07001512 return result;
1513}
1514
Ian Rogers5167c972012-02-03 10:41:20 -08001515
1516Class* Throwable::java_lang_Throwable_ = NULL;
1517
1518void Throwable::SetClass(Class* java_lang_Throwable) {
1519 CHECK(java_lang_Throwable_ == NULL);
1520 CHECK(java_lang_Throwable != NULL);
1521 java_lang_Throwable_ = java_lang_Throwable;
1522}
1523
1524void Throwable::ResetClass() {
1525 CHECK(java_lang_Throwable_ != NULL);
1526 java_lang_Throwable_ = NULL;
1527}
1528
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001529Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1530
1531void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1532 CHECK(java_lang_StackTraceElement_ == NULL);
1533 CHECK(java_lang_StackTraceElement != NULL);
1534 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1535}
1536
1537void StackTraceElement::ResetClass() {
1538 CHECK(java_lang_StackTraceElement_ != NULL);
1539 java_lang_StackTraceElement_ = NULL;
1540}
1541
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001542StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1543 String* method_name,
1544 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001545 int32_t line_number) {
1546 StackTraceElement* trace =
1547 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1548 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1549 const_cast<String*>(declaring_class), false);
1550 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1551 const_cast<String*>(method_name), false);
1552 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1553 const_cast<String*>(file_name), false);
1554 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1555 line_number, false);
1556 return trace;
1557}
1558
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001559} // namespace art