blob: eb11469341c4913e8508f99fb7fe95667a757b5f [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 Rogers23435d02012-09-24 11:23:12 -0700118#if VERIFY_OBJECT_ENABLED
119void Object::CheckFieldAssignment(MemberOffset field_offset, const Object* new_value) {
120 const Class* c = GetClass();
121 if (Runtime::Current()->GetClassLinker() == NULL ||
122 !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() ||
123 !c->IsResolved()) {
124 return;
125 }
126 for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
127 ObjectArray<Field>* fields = cur->GetIFields();
128 if (fields != NULL) {
129 size_t num_ref_ifields = cur->NumReferenceInstanceFields();
130 for (size_t i = 0; i < num_ref_ifields; ++i) {
131 Field* field = fields->Get(i);
132 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
133 FieldHelper fh(field);
134 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
135 return;
136 }
137 }
138 }
139 }
140 if (c->IsArrayClass()) {
141 // Bounds and assign-ability done in the array setter.
142 return;
143 }
144 if (IsClass()) {
145 ObjectArray<Field>* fields = AsClass()->GetSFields();
146 if (fields != NULL) {
147 size_t num_ref_sfields = AsClass()->NumReferenceStaticFields();
148 for (size_t i = 0; i < num_ref_sfields; ++i) {
149 Field* field = fields->Get(i);
150 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
151 FieldHelper fh(field);
152 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
153 return;
154 }
155 }
156 }
157 }
158 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
159 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
160}
161#endif
162
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700163// TODO: get global references for these
164Class* Field::java_lang_reflect_Field_ = NULL;
165
166void Field::SetClass(Class* java_lang_reflect_Field) {
167 CHECK(java_lang_reflect_Field_ == NULL);
168 CHECK(java_lang_reflect_Field != NULL);
169 java_lang_reflect_Field_ = java_lang_reflect_Field;
170}
171
172void Field::ResetClass() {
173 CHECK(java_lang_reflect_Field_ != NULL);
174 java_lang_reflect_Field_ = NULL;
175}
176
Ian Rogers0571d352011-11-03 19:51:38 -0700177void Field::SetOffset(MemberOffset num_bytes) {
178 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800179#if 0 // TODO enable later in boot and under !NDEBUG
180 FieldHelper fh(this);
181 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700182 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
183 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
184 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700186 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
187}
188
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700190 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191 if (IsStatic()) {
192 object = declaring_class_;
193 }
194 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700195}
196
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700197void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700198 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199 if (IsStatic()) {
200 object = declaring_class_;
201 }
202 object->SetField32(GetOffset(), new_value, IsVolatile());
203}
204
205uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700206 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 if (IsStatic()) {
208 object = declaring_class_;
209 }
210 return object->GetField64(GetOffset(), IsVolatile());
211}
212
213void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700214 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 if (IsStatic()) {
216 object = declaring_class_;
217 }
218 object->SetField64(GetOffset(), new_value, IsVolatile());
219}
220
221Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700222 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 if (IsStatic()) {
224 object = declaring_class_;
225 }
226 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
227}
228
229void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700230 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 if (IsStatic()) {
232 object = declaring_class_;
233 }
234 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
235}
236
237bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800238 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
239 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 return Get32(object);
241}
242
243void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
245 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700246 Set32(object, z);
247}
248
249int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800250 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
251 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700252 return Get32(object);
253}
254
255void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800256 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
257 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700258 Set32(object, b);
259}
260
261uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800262 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
263 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700264 return Get32(object);
265}
266
267void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
269 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700270 Set32(object, c);
271}
272
Ian Rogers466bb252011-10-14 03:29:56 -0700273int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
275 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700276 return Get32(object);
277}
278
Ian Rogers466bb252011-10-14 03:29:56 -0700279void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
281 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700282 Set32(object, s);
283}
284
285int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800286 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
287 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700288 return Get32(object);
289}
290
291void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
293 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 Set32(object, i);
295}
296
297int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
299 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 return Get64(object);
301}
302
303void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
305 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 Set64(object, j);
307}
308
Elliott Hughes1d878f32012-04-11 15:17:54 -0700309union Bits {
310 jdouble d;
311 jfloat f;
312 jint i;
313 jlong j;
314};
315
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800317 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
318 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700319 Bits bits;
320 bits.i = Get32(object);
321 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322}
323
324void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800325 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
326 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700327 Bits bits;
328 bits.f = f;
329 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330}
331
332double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800333 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
334 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700335 Bits bits;
336 bits.j = Get64(object);
337 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338}
339
340void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800341 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
342 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700343 Bits bits;
344 bits.d = d;
345 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346}
347
348Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800349 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
350 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351 return GetObj(object);
352}
353
354void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800355 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
356 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 SetObj(object, l);
358}
359
360// TODO: get global references for these
Mathieu Chartier66f19252012-09-18 08:57:04 -0700361Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
362Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363
Mathieu Chartier66f19252012-09-18 08:57:04 -0700364InvokeType AbstractMethod::GetInvokeType() const {
Ian Rogers08f753d2012-08-24 14:35:25 -0700365 // TODO: kSuper?
366 if (GetDeclaringClass()->IsInterface()) {
367 return kInterface;
368 } else if (IsStatic()) {
369 return kStatic;
370 } else if (IsDirect()) {
371 return kDirect;
372 } else {
373 return kVirtual;
374 }
375}
376
Mathieu Chartier66f19252012-09-18 08:57:04 -0700377void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
Elliott Hughes80609252011-09-23 17:24:51 -0700378 CHECK(java_lang_reflect_Constructor_ == NULL);
379 CHECK(java_lang_reflect_Constructor != NULL);
380 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
381
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 CHECK(java_lang_reflect_Method_ == NULL);
383 CHECK(java_lang_reflect_Method != NULL);
384 java_lang_reflect_Method_ = java_lang_reflect_Method;
385}
386
Mathieu Chartier66f19252012-09-18 08:57:04 -0700387void AbstractMethod::ResetClasses() {
Elliott Hughes80609252011-09-23 17:24:51 -0700388 CHECK(java_lang_reflect_Constructor_ != NULL);
389 java_lang_reflect_Constructor_ = NULL;
390
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700391 CHECK(java_lang_reflect_Method_ != NULL);
392 java_lang_reflect_Method_ = NULL;
393}
394
Mathieu Chartier66f19252012-09-18 08:57:04 -0700395ObjectArray<String>* AbstractMethod::GetDexCacheStrings() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 return GetFieldObject<ObjectArray<String>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700397 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398}
399
Mathieu Chartier66f19252012-09-18 08:57:04 -0700400void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
401 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700402 new_dex_cache_strings, false);
403}
404
Mathieu Chartier66f19252012-09-18 08:57:04 -0700405ObjectArray<AbstractMethod>* AbstractMethod::GetDexCacheResolvedMethods() const {
406 return GetFieldObject<ObjectArray<AbstractMethod>*>(
407 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800408}
409
Mathieu Chartier66f19252012-09-18 08:57:04 -0700410void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
411 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
Ian Rogers19846512012-02-24 11:42:47 -0800412 new_dex_cache_methods, false);
413}
414
Mathieu Chartier66f19252012-09-18 08:57:04 -0700415ObjectArray<Class>* AbstractMethod::GetDexCacheResolvedTypes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700416 return GetFieldObject<ObjectArray<Class>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700417 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700418}
419
Mathieu Chartier66f19252012-09-18 08:57:04 -0700420void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
421 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 new_dex_cache_classes, false);
423}
424
Mathieu Chartier66f19252012-09-18 08:57:04 -0700425ObjectArray<StaticStorageBase>* AbstractMethod::GetDexCacheInitializedStaticStorage() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700427 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428 false);
429}
430
Mathieu Chartier66f19252012-09-18 08:57:04 -0700431void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
432 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700433 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700434}
435
Mathieu Chartier66f19252012-09-18 08:57:04 -0700436size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 CHECK_LE(1, shorty.length());
438 uint32_t num_registers = 0;
439 for (int i = 1; i < shorty.length(); ++i) {
440 char ch = shorty[i];
441 if (ch == 'D' || ch == 'J') {
442 num_registers += 2;
443 } else {
444 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700445 }
446 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700447 return num_registers;
448}
449
Mathieu Chartier66f19252012-09-18 08:57:04 -0700450bool AbstractMethod::IsProxyMethod() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800451 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700452}
453
Mathieu Chartier66f19252012-09-18 08:57:04 -0700454AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
Ian Rogers466bb252011-10-14 03:29:56 -0700455 if (IsStatic()) {
456 return NULL;
457 }
458 Class* declaring_class = GetDeclaringClass();
459 Class* super_class = declaring_class->GetSuperClass();
460 uint16_t method_index = GetMethodIndex();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700461 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
462 AbstractMethod* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800463 // Did this method override a super class method? If so load the result from the super class'
464 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700465 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
466 result = super_class_vtable->Get(method_index);
467 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800468 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800469 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800470 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
471 CHECK_EQ(result,
472 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800473 } else {
474 MethodHelper mh(this);
475 MethodHelper interface_mh;
476 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
477 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
478 InterfaceEntry* entry = iftable->Get(i);
479 Class* interface = entry->GetInterface();
480 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700481 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers16f93672012-02-14 12:29:06 -0800482 interface_mh.ChangeMethod(interface_method);
483 if (mh.HasSameNameAndSignature(&interface_mh)) {
484 result = interface_method;
485 break;
486 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800487 }
488 }
Ian Rogers466bb252011-10-14 03:29:56 -0700489 }
490 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800491#ifndef NDEBUG
492 MethodHelper result_mh(result);
493 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
494#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700495 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700496}
497
Mathieu Chartier66f19252012-09-18 08:57:04 -0700498static const void* GetOatCode(const AbstractMethod* m)
499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800500 Runtime* runtime = Runtime::Current();
501 const void* code = m->GetCode();
502 // Peel off any method tracing trampoline.
503 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
504 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
505 }
506 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800507 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800508 code = runtime->GetClassLinker()->GetOatCodeFor(m);
509 }
510 return code;
511}
512
Mathieu Chartier66f19252012-09-18 08:57:04 -0700513uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700514 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
515}
516
Mathieu Chartier66f19252012-09-18 08:57:04 -0700517uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700518#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700519 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700520 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700522 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700523 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700524 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800525 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700526 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700527 if (mapping_table[i] == sought_offset) {
528 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700529 }
530 }
buzbee8320f382012-09-11 16:29:42 -0700531 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
532 << " in " << PrettyMethod(this);
533 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700534#else
535 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
536 return static_cast<uint32_t>(pc);
537#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700538}
539
Mathieu Chartier66f19252012-09-18 08:57:04 -0700540uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700541 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700542 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700543 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700544 return 0; // Special no mapping/pc == 0 case
545 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700546 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700547 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700548 uint32_t map_offset = mapping_table[i];
549 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700550 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800551 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700552 }
553 }
554 LOG(FATAL) << "Looking up Dex PC not contained in method";
555 return 0;
556}
557
Mathieu Chartier66f19252012-09-18 08:57:04 -0700558uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800559 MethodHelper mh(this);
560 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700561 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700562 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
563 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700564 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700565 if (iter_type_idx == DexFile::kDexNoIndex16) {
566 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700567 }
568 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800569 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700570 if (iter_exception_type == NULL) {
571 // The verifier should take care of resolving all exception classes early
572 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800573 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700574 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700575 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700576 }
577 }
578 // Handler not found
579 return DexFile::kDexNoIndex;
580}
581
Mathieu Chartier66f19252012-09-18 08:57:04 -0700582void AbstractMethod::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700583 if (kIsDebugBuild) {
584 self->AssertThreadSuspensionIsAllowable();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700585 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700586 CHECK_EQ(kRunnable, self->GetState());
587 }
TDYa12785321912012-04-01 15:24:56 -0700588
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700589 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700590 ManagedStack fragment;
591 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700592
593 // Call the invoke stub associated with the method.
594 // Pass everything as arguments.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700595 AbstractMethod::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700596
597 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700598
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500599 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700600 bool log = false;
601 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800602 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
603 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700604 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700605 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700606 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800607 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
608 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700609 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700610 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800611 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
612 PrettyMethod(this).c_str(), GetCode(), stub,
613 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700614 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700615 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700616 }
617 }
618
619 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700620 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700621}
622
Mathieu Chartier66f19252012-09-18 08:57:04 -0700623bool AbstractMethod::IsRegistered() const {
624 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800625 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800626 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700627 return native_method != jni_stub;
628}
629
Mathieu Chartier66f19252012-09-18 08:57:04 -0700630void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800631 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700632 CHECK(IsNative()) << PrettyMethod(this);
633 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700634#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700635 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700636 native_method, false);
637#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800638 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700639 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800640 native_method, false);
641 } else {
642 // We've been asked to associate this method with the given native method but are working
643 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
644 // the native method to runtime support and store the target somewhere runtime support will
645 // find it.
646#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700647 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800648 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
649#else
650 UNIMPLEMENTED(FATAL);
651#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700652 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800653 reinterpret_cast<const uint8_t*>(native_method), false);
654 }
TDYa12726467572012-04-17 20:51:22 -0700655#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700656}
657
Mathieu Chartier66f19252012-09-18 08:57:04 -0700658void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700659 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700660 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800661 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700662}
663
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700664void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700665 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
666 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700667 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700668 if (new_status > kStatusResolved) {
669 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
670 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800671 if (new_status == kStatusError) {
672 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
673
674 // stash current exception
675 Thread* self = Thread::Current();
676 SirtRef<Throwable> exception(self->GetException());
677 CHECK(exception.get() != NULL);
678
679 // clear exception to call FindSystemClass
680 self->ClearException();
681 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
682 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
683 CHECK(!self->IsExceptionPending());
684
685 // only verification errors, not initialization problems, should set a verify error.
686 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
687 Class* exception_class = exception->GetClass();
688 if (!eiie_class->IsAssignableFrom(exception_class)) {
689 SetVerifyErrorClass(exception_class);
690 }
691
692 // restore exception
693 self->SetException(exception.get());
694 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700695 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700696}
697
698DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700699 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700700}
701
702void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700703 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700704}
705
Brian Carlstrom1f870082011-08-23 16:02:11 -0700706Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700707 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700708 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500709 // TODO: decide whether we want this check. It currently fails during bootstrap.
710 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700711 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800712 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700713}
714
Ian Rogers0571d352011-11-03 19:51:38 -0700715void Class::SetClassSize(size_t new_class_size) {
716 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
717 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
718}
719
Ian Rogersd418eda2012-01-30 12:14:28 -0800720// Return the class' name. The exact format is bizarre, but it's the specified behavior for
721// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
722// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
723// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
724String* Class::ComputeName() {
725 String* name = GetName();
726 if (name != NULL) {
727 return name;
728 }
729 std::string descriptor(ClassHelper(this).GetDescriptor());
730 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
731 // The descriptor indicates that this is the class for
732 // a primitive type; special-case the return value.
733 const char* c_name = NULL;
734 switch (descriptor[0]) {
735 case 'Z': c_name = "boolean"; break;
736 case 'B': c_name = "byte"; break;
737 case 'C': c_name = "char"; break;
738 case 'S': c_name = "short"; break;
739 case 'I': c_name = "int"; break;
740 case 'J': c_name = "long"; break;
741 case 'F': c_name = "float"; break;
742 case 'D': c_name = "double"; break;
743 case 'V': c_name = "void"; break;
744 default:
745 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
746 }
747 name = String::AllocFromModifiedUtf8(c_name);
748 } else {
749 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
750 // components.
751 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
752 descriptor.erase(0, 1);
753 descriptor.erase(descriptor.size() - 1);
754 }
755 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
756 name = String::AllocFromModifiedUtf8(descriptor.c_str());
757 }
758 SetName(name);
759 return name;
760}
761
Elliott Hughes4681c802011-09-25 18:04:37 -0700762void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700763 if ((flags & kDumpClassFullDetail) == 0) {
764 os << PrettyClass(this);
765 if ((flags & kDumpClassClassLoader) != 0) {
766 os << ' ' << GetClassLoader();
767 }
768 if ((flags & kDumpClassInitialized) != 0) {
769 os << ' ' << GetStatus();
770 }
Elliott Hughese0918552011-10-28 17:18:29 -0700771 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700772 return;
773 }
774
775 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800776 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700777 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800778 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700779 os << " objectSize=" << SizeOf() << " "
780 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
781 os << StringPrintf(" access=0x%04x.%04x\n",
782 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
783 if (super != NULL) {
784 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
785 }
786 if (IsArrayClass()) {
787 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
788 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700789 if (kh.NumDirectInterfaces() > 0) {
790 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
791 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
792 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700793 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800794 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700795 }
796 }
797 os << " vtable (" << NumVirtualMethods() << " entries, "
798 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
799 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800800 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700801 }
802 os << " direct methods (" << NumDirectMethods() << " entries):\n";
803 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800804 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700805 }
806 if (NumStaticFields() > 0) {
807 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700808 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700809 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800810 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700811 }
812 } else {
813 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700814 }
815 }
816 if (NumInstanceFields() > 0) {
817 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700818 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700819 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800820 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700821 }
822 } else {
823 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700824 }
825 }
826}
827
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700828void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
829 if (new_reference_offsets != CLASS_WALK_SUPER) {
830 // Sanity check that the number of bits set in the reference offset bitmap
831 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800832 size_t count = 0;
833 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
834 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700835 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800836 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700837 }
838 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
839 new_reference_offsets, false);
840}
841
842void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
843 if (new_reference_offsets != CLASS_WALK_SUPER) {
844 // Sanity check that the number of bits set in the reference offset bitmap
845 // agrees with the number of references
846 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
847 NumReferenceStaticFieldsDuringLinking());
848 }
849 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
850 new_reference_offsets, false);
851}
852
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700853bool Class::Implements(const Class* klass) const {
854 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700855 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700856 // All interfaces implemented directly and by our superclass, and
857 // recursively all super-interfaces of those interfaces, are listed
858 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700859 int32_t iftable_count = GetIfTableCount();
860 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
861 for (int32_t i = 0; i < iftable_count; i++) {
862 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700863 return true;
864 }
865 }
866 return false;
867}
868
Elliott Hughese84278b2012-03-22 10:06:53 -0700869// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700870// are array classes.
871//
872// Consider an array class, e.g. Y[][], where Y is a subclass of X.
873// Y[][] = Y[][] --> true (identity)
874// X[][] = Y[][] --> true (element superclass)
875// Y = Y[][] --> false
876// Y[] = Y[][] --> false
877// Object = Y[][] --> true (everything is an object)
878// Object[] = Y[][] --> true
879// Object[][] = Y[][] --> true
880// Object[][][] = Y[][] --> false (too many []s)
881// Serializable = Y[][] --> true (all arrays are Serializable)
882// Serializable[] = Y[][] --> true
883// Serializable[][] = Y[][] --> false (unless Y is Serializable)
884//
885// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700886// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700887//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700888bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700889 DCHECK(IsArrayClass()) << PrettyClass(this);
890 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700891 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700892}
893
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700894bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700895 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
896 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700897 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700898 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700899 // src's super should be java_lang_Object, since it is an array.
900 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700901 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
902 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700903 return this == java_lang_Object;
904 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700905 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700906}
907
908bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700909 DCHECK(!IsInterface()) << PrettyClass(this);
910 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700911 const Class* current = this;
912 do {
913 if (current == klass) {
914 return true;
915 }
916 current = current->GetSuperClass();
917 } while (current != NULL);
918 return false;
919}
920
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800921bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700922 size_t i = 0;
923 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
924 ++i;
925 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700926 if (descriptor1.find('/', i) != StringPiece::npos ||
927 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700928 return false;
929 } else {
930 return true;
931 }
932}
933
934bool Class::IsInSamePackage(const Class* that) const {
935 const Class* klass1 = this;
936 const Class* klass2 = that;
937 if (klass1 == klass2) {
938 return true;
939 }
940 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700941 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700942 return false;
943 }
944 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700945 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700946 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700947 }
jeffhao4a801a42011-09-23 13:53:40 -0700948 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700950 }
951 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800952 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800953 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800954 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800955 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800956 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700957}
958
Elliott Hughesdbb40792011-11-18 17:05:22 -0800959bool Class::IsClassClass() const {
960 Class* java_lang_Class = GetClass()->GetClass();
961 return this == java_lang_Class;
962}
963
964bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800965 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800966}
967
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800968bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700969 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800970}
971
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800972ClassLoader* Class::GetClassLoader() const {
973 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700974}
975
Ian Rogers365c1022012-06-22 15:05:28 -0700976void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700978}
979
Mathieu Chartier66f19252012-09-18 08:57:04 -0700980AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700981 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700982 DCHECK(declaring_class != NULL) << PrettyClass(this);
983 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700984 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700985 int32_t iftable_count = GetIfTableCount();
986 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
987 for (int32_t i = 0; i < iftable_count; i++) {
988 InterfaceEntry* interface_entry = iftable->Get(i);
989 if (interface_entry->GetInterface() == declaring_class) {
990 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700991 }
992 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700993 return NULL;
994}
995
Mathieu Chartier66f19252012-09-18 08:57:04 -0700996AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700997 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700998 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700999 if (method != NULL) {
1000 return method;
1001 }
1002
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001003 int32_t iftable_count = GetIfTableCount();
1004 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1005 for (int32_t i = 0; i < iftable_count; i++) {
1006 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001007 if (method != NULL) {
1008 return method;
1009 }
1010 }
1011 return NULL;
1012}
1013
Mathieu Chartier66f19252012-09-18 08:57:04 -07001014AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001015 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001016 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001017 if (method != NULL) {
1018 return method;
1019 }
1020
1021 int32_t iftable_count = GetIfTableCount();
1022 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1023 for (int32_t i = 0; i < iftable_count; i++) {
1024 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
1025 if (method != NULL) {
1026 return method;
1027 }
1028 }
1029 return NULL;
1030}
1031
1032
Mathieu Chartier66f19252012-09-18 08:57:04 -07001033AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001034 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001035 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001036 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001037 mh.ChangeMethod(method);
1038 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001039 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001040 }
1041 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001042 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001043}
1044
Mathieu Chartier66f19252012-09-18 08:57:04 -07001045AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001046 if (GetDexCache() == dex_cache) {
1047 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001048 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001049 if (method->GetDexMethodIndex() == dex_method_idx) {
1050 return method;
1051 }
1052 }
1053 }
1054 return NULL;
1055}
1056
Mathieu Chartier66f19252012-09-18 08:57:04 -07001057AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001058 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001059 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001060 if (method != NULL) {
1061 return method;
1062 }
1063 }
1064 return NULL;
1065}
1066
Mathieu Chartier66f19252012-09-18 08:57:04 -07001067AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001068 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001069 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001070 if (method != NULL) {
1071 return method;
1072 }
1073 }
1074 return NULL;
1075}
1076
Mathieu Chartier66f19252012-09-18 08:57:04 -07001077AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001078 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001079 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001080 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001081 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001082 mh.ChangeMethod(method);
1083 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001084 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001085 }
1086 }
1087 return NULL;
1088}
1089
Mathieu Chartier66f19252012-09-18 08:57:04 -07001090AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001091 if (GetDexCache() == dex_cache) {
1092 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001093 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001094 if (method->GetDexMethodIndex() == dex_method_idx) {
1095 return method;
1096 }
1097 }
1098 }
1099 return NULL;
1100}
1101
Mathieu Chartier66f19252012-09-18 08:57:04 -07001102AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001103 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001104 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001105 if (method != NULL) {
1106 return method;
1107 }
1108 }
1109 return NULL;
1110}
1111
Mathieu Chartier66f19252012-09-18 08:57:04 -07001112AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001113 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001114 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001115 if (method != NULL) {
1116 return method;
1117 }
1118 }
1119 return NULL;
1120}
1121
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001122Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 // Is the field in this class?
1124 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001125 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1127 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001128 fh.ChangeField(f);
1129 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001130 return f;
1131 }
1132 }
1133 return NULL;
1134}
1135
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001136Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1137 if (GetDexCache() == dex_cache) {
1138 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1139 Field* f = GetInstanceField(i);
1140 if (f->GetDexFieldIndex() == dex_field_idx) {
1141 return f;
1142 }
1143 }
1144 }
1145 return NULL;
1146}
1147
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001148Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 // Is the field in this class, or any of its superclasses?
1150 // Interfaces are not relevant because they can't contain instance fields.
1151 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001152 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 if (f != NULL) {
1154 return f;
1155 }
1156 }
1157 return NULL;
1158}
1159
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001160Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1161 // Is the field in this class, or any of its superclasses?
1162 // Interfaces are not relevant because they can't contain instance fields.
1163 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1164 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1165 if (f != NULL) {
1166 return f;
1167 }
1168 }
1169 return NULL;
1170}
1171
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001172Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1173 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001174 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001175 for (size_t i = 0; i < NumStaticFields(); ++i) {
1176 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001177 fh.ChangeField(f);
1178 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001179 return f;
1180 }
1181 }
1182 return NULL;
1183}
1184
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001185Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1186 if (dex_cache == GetDexCache()) {
1187 for (size_t i = 0; i < NumStaticFields(); ++i) {
1188 Field* f = GetStaticField(i);
1189 if (f->GetDexFieldIndex() == dex_field_idx) {
1190 return f;
1191 }
1192 }
1193 }
1194 return NULL;
1195}
1196
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001197Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1198 // Is the field in this class (or its interfaces), or any of its
1199 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001200 ClassHelper kh;
1201 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001202 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001203 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001204 if (f != NULL) {
1205 return f;
1206 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001207 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001208 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001209 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1210 Class* interface = kh.GetDirectInterface(i);
1211 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001212 if (f != NULL) {
1213 return f;
1214 }
1215 }
1216 }
1217 return NULL;
1218}
1219
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001220Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1221 ClassHelper kh;
1222 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1223 // Is the field in this class?
1224 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1225 if (f != NULL) {
1226 return f;
1227 }
1228 // Is this field in any of this class' interfaces?
1229 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001230 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1231 Class* interface = kh.GetDirectInterface(i);
1232 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001233 if (f != NULL) {
1234 return f;
1235 }
1236 }
1237 }
1238 return NULL;
1239}
1240
Ian Rogersb067ac22011-12-13 18:05:09 -08001241Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1242 // Find a field using the JLS field resolution order
1243 ClassHelper kh;
1244 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1245 // Is the field in this class?
1246 Field* f = k->FindDeclaredInstanceField(name, type);
1247 if (f != NULL) {
1248 return f;
1249 }
1250 f = k->FindDeclaredStaticField(name, type);
1251 if (f != NULL) {
1252 return f;
1253 }
1254 // Is this field in any of this class' interfaces?
1255 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001256 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1257 Class* interface = kh.GetDirectInterface(i);
1258 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001259 if (f != NULL) {
1260 return f;
1261 }
1262 }
1263 }
1264 return NULL;
1265}
1266
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001267Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001268 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001269 DCHECK_GE(component_count, 0);
1270 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001271
Ian Rogersa15e67d2012-02-28 13:51:55 -08001272 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001273 size_t data_size = component_count * component_size;
1274 size_t size = header_size + data_size;
1275
1276 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1277 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1278 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1279 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001280 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001281 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001282 return NULL;
1283 }
1284
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001285 Heap* heap = Runtime::Current()->GetHeap();
1286 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001287 if (array != NULL) {
1288 DCHECK(array->IsArrayInstance());
1289 array->SetLength(component_count);
1290 }
1291 return array;
1292}
1293
1294Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001295 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001296 return Alloc(array_class, component_count, array_class->GetComponentSize());
1297}
1298
Elliott Hughes80609252011-09-23 17:24:51 -07001299bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001300 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001301 "length=%i; index=%i", length_, index);
1302 return false;
1303}
1304
1305bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001306 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001307 "Can't store an element of type %s into an array of type %s",
1308 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1309 return false;
1310}
1311
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001312template<typename T>
1313PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001314 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001315 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1316 return down_cast<PrimitiveArray<T>*>(raw_array);
1317}
1318
1319template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1320
1321// Explicitly instantiate all the primitive array types.
1322template class PrimitiveArray<uint8_t>; // BooleanArray
1323template class PrimitiveArray<int8_t>; // ByteArray
1324template class PrimitiveArray<uint16_t>; // CharArray
1325template class PrimitiveArray<double>; // DoubleArray
1326template class PrimitiveArray<float>; // FloatArray
1327template class PrimitiveArray<int32_t>; // IntArray
1328template class PrimitiveArray<int64_t>; // LongArray
1329template class PrimitiveArray<int16_t>; // ShortArray
1330
Ian Rogers466bb252011-10-14 03:29:56 -07001331// Explicitly instantiate Class[][]
1332template class ObjectArray<ObjectArray<Class> >;
1333
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001334// TODO: get global references for these
1335Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001336
Brian Carlstroma663ea52011-08-19 23:33:41 -07001337void String::SetClass(Class* java_lang_String) {
1338 CHECK(java_lang_String_ == NULL);
1339 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001340 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001341}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001342
Brian Carlstroma663ea52011-08-19 23:33:41 -07001343void String::ResetClass() {
1344 CHECK(java_lang_String_ != NULL);
1345 java_lang_String_ = NULL;
1346}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001347
Brian Carlstromc74255f2011-09-11 22:47:39 -07001348String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001349 return Runtime::Current()->GetInternTable()->InternWeak(this);
1350}
1351
Brian Carlstrom395520e2011-09-25 19:35:00 -07001352int32_t String::GetHashCode() {
1353 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1354 if (result == 0) {
1355 ComputeHashCode();
1356 }
1357 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1358 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1359 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001360 return result;
1361}
1362
1363int32_t String::GetLength() const {
1364 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1365 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1366 return result;
1367}
1368
1369uint16_t String::CharAt(int32_t index) const {
1370 // TODO: do we need this? Equals is the only caller, and could
1371 // bounds check itself.
1372 if (index < 0 || index >= count_) {
1373 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001374 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001375 "length=%i; index=%i", count_, index);
1376 return 0;
1377 }
1378 return GetCharArray()->Get(index + GetOffset());
1379}
1380
1381String* String::AllocFromUtf16(int32_t utf16_length,
1382 const uint16_t* utf16_data_in,
1383 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001384 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001385 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001386 if (string == NULL) {
1387 return NULL;
1388 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001389 // TODO: use 16-bit wide memset variant
1390 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001391 if (array == NULL) {
1392 return NULL;
1393 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001394 for (int i = 0; i < utf16_length; i++) {
1395 array->Set(i, utf16_data_in[i]);
1396 }
1397 if (hash_code != 0) {
1398 string->SetHashCode(hash_code);
1399 } else {
1400 string->ComputeHashCode();
1401 }
1402 return string;
1403}
1404
1405String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001406 if (utf == NULL) {
1407 return NULL;
1408 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001409 size_t char_count = CountModifiedUtf8Chars(utf);
1410 return AllocFromModifiedUtf8(char_count, utf);
1411}
1412
1413String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1414 const char* utf8_data_in) {
1415 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001416 if (string == NULL) {
1417 return NULL;
1418 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001419 uint16_t* utf16_data_out =
1420 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1421 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1422 string->ComputeHashCode();
1423 return string;
1424}
1425
1426String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001427 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1428 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001429 return NULL;
1430 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001431 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001432}
1433
1434String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001435 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001436 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001437 if (string == NULL) {
1438 return NULL;
1439 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001440 string->SetArray(array);
1441 string->SetCount(array->GetLength());
1442 return string;
1443}
1444
1445bool String::Equals(const String* that) const {
1446 if (this == that) {
1447 // Quick reference equality test
1448 return true;
1449 } else if (that == NULL) {
1450 // Null isn't an instanceof anything
1451 return false;
1452 } else if (this->GetLength() != that->GetLength()) {
1453 // Quick length inequality test
1454 return false;
1455 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001456 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001457 // hash code was already equal
1458 for (int32_t i = 0; i < that->GetLength(); ++i) {
1459 if (this->CharAt(i) != that->CharAt(i)) {
1460 return false;
1461 }
1462 }
1463 return true;
1464 }
1465}
1466
Elliott Hughes5d78d392011-12-13 16:53:05 -08001467bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001468 if (this->GetLength() != that_length) {
1469 return false;
1470 } else {
1471 for (int32_t i = 0; i < that_length; ++i) {
1472 if (this->CharAt(i) != that_chars[that_offset + i]) {
1473 return false;
1474 }
1475 }
1476 return true;
1477 }
1478}
1479
1480bool String::Equals(const char* modified_utf8) const {
1481 for (int32_t i = 0; i < GetLength(); ++i) {
1482 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1483 if (ch == '\0' || ch != CharAt(i)) {
1484 return false;
1485 }
1486 }
1487 return *modified_utf8 == '\0';
1488}
1489
1490bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001491 if (modified_utf8.size() != GetLength()) {
1492 return false;
1493 }
1494 const char* p = modified_utf8.data();
1495 for (int32_t i = 0; i < GetLength(); ++i) {
1496 uint16_t ch = GetUtf16FromUtf8(&p);
1497 if (ch != CharAt(i)) {
1498 return false;
1499 }
1500 }
1501 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001502}
1503
1504// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1505std::string String::ToModifiedUtf8() const {
1506 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001507 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001508 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001509 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1510 return result;
1511}
1512
Ian Rogers1c5eb702012-02-01 09:18:34 -08001513void Throwable::SetCause(Throwable* cause) {
1514 CHECK(cause != NULL);
1515 CHECK(cause != this);
1516 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1517 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1518}
1519
Ian Rogers466bb252011-10-14 03:29:56 -07001520bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001521 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001522 return false;
1523 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001524 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001525}
1526
Ian Rogers9074b992011-10-26 17:41:55 -07001527std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001528 std::string result(PrettyTypeOf(this));
1529 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001530 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001531 if (msg != NULL) {
1532 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001533 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001534 result += "\n";
1535 Object* stack_state = GetStackState();
1536 // check stack state isn't missing or corrupt
1537 if (stack_state != NULL && stack_state->IsObjectArray()) {
1538 // Decode the internal stack trace into the depth and method trace
1539 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1540 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001541 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1542 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001543 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001544 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001545 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001546 uint32_t dex_pc = pc_trace->Get(i);
1547 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001548 const char* source_file = mh.GetDeclaringClassSourceFile();
1549 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1550 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001551 }
Ian Rogers9074b992011-10-26 17:41:55 -07001552 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001553 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001554 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001555 result += "Caused by: ";
1556 result += cause->Dump();
1557 }
Ian Rogers9074b992011-10-26 17:41:55 -07001558 return result;
1559}
1560
Ian Rogers5167c972012-02-03 10:41:20 -08001561
1562Class* Throwable::java_lang_Throwable_ = NULL;
1563
1564void Throwable::SetClass(Class* java_lang_Throwable) {
1565 CHECK(java_lang_Throwable_ == NULL);
1566 CHECK(java_lang_Throwable != NULL);
1567 java_lang_Throwable_ = java_lang_Throwable;
1568}
1569
1570void Throwable::ResetClass() {
1571 CHECK(java_lang_Throwable_ != NULL);
1572 java_lang_Throwable_ = NULL;
1573}
1574
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001575Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1576
1577void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1578 CHECK(java_lang_StackTraceElement_ == NULL);
1579 CHECK(java_lang_StackTraceElement != NULL);
1580 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1581}
1582
1583void StackTraceElement::ResetClass() {
1584 CHECK(java_lang_StackTraceElement_ != NULL);
1585 java_lang_StackTraceElement_ = NULL;
1586}
1587
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001588StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1589 String* method_name,
1590 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001591 int32_t line_number) {
1592 StackTraceElement* trace =
1593 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1594 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1595 const_cast<String*>(declaring_class), false);
1596 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1597 const_cast<String*>(method_name), false);
1598 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1599 const_cast<String*>(file_name), false);
1600 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1601 line_number, false);
1602 return trace;
1603}
1604
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001605} // namespace art