blob: 284f2217202c786385acf4dcd458a90ccb5a36dc [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 Rogers00f7d0e2012-07-19 15:28:27 -0700585 CHECK_EQ(kRunnable, self->GetState());
586 }
TDYa12785321912012-04-01 15:24:56 -0700587
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700589 ManagedStack fragment;
590 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700591
592 // Call the invoke stub associated with the method.
593 // Pass everything as arguments.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700594 AbstractMethod::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700595
596 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700597
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500598 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700599 bool log = false;
600 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800601 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
602 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700603 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700604 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700605 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800606 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
607 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700608 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700609 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800610 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
611 PrettyMethod(this).c_str(), GetCode(), stub,
612 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700613 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700614 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700615 }
616 }
617
618 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700619 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700620}
621
Mathieu Chartier66f19252012-09-18 08:57:04 -0700622bool AbstractMethod::IsRegistered() const {
623 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800624 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800625 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700626 return native_method != jni_stub;
627}
628
Mathieu Chartier66f19252012-09-18 08:57:04 -0700629void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800630 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700631 CHECK(IsNative()) << PrettyMethod(this);
632 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700633#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700634 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700635 native_method, false);
636#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800637 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700638 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800639 native_method, false);
640 } else {
641 // We've been asked to associate this method with the given native method but are working
642 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
643 // the native method to runtime support and store the target somewhere runtime support will
644 // find it.
645#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700646 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800647 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
648#else
649 UNIMPLEMENTED(FATAL);
650#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700651 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800652 reinterpret_cast<const uint8_t*>(native_method), false);
653 }
TDYa12726467572012-04-17 20:51:22 -0700654#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700655}
656
Mathieu Chartier66f19252012-09-18 08:57:04 -0700657void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700658 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700659 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800660 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700661}
662
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700663void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700664 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
665 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700666 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700667 if (new_status > kStatusResolved) {
668 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
669 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800670 if (new_status == kStatusError) {
671 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
672
673 // stash current exception
674 Thread* self = Thread::Current();
675 SirtRef<Throwable> exception(self->GetException());
676 CHECK(exception.get() != NULL);
677
678 // clear exception to call FindSystemClass
679 self->ClearException();
680 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
681 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
682 CHECK(!self->IsExceptionPending());
683
684 // only verification errors, not initialization problems, should set a verify error.
685 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
686 Class* exception_class = exception->GetClass();
687 if (!eiie_class->IsAssignableFrom(exception_class)) {
688 SetVerifyErrorClass(exception_class);
689 }
690
691 // restore exception
692 self->SetException(exception.get());
693 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700694 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700695}
696
697DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700698 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700699}
700
701void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700702 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700703}
704
Brian Carlstrom1f870082011-08-23 16:02:11 -0700705Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700706 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700707 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500708 // TODO: decide whether we want this check. It currently fails during bootstrap.
709 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700710 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800711 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700712}
713
Ian Rogers0571d352011-11-03 19:51:38 -0700714void Class::SetClassSize(size_t new_class_size) {
715 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
716 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
717}
718
Ian Rogersd418eda2012-01-30 12:14:28 -0800719// Return the class' name. The exact format is bizarre, but it's the specified behavior for
720// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
721// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
722// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
723String* Class::ComputeName() {
724 String* name = GetName();
725 if (name != NULL) {
726 return name;
727 }
728 std::string descriptor(ClassHelper(this).GetDescriptor());
729 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
730 // The descriptor indicates that this is the class for
731 // a primitive type; special-case the return value.
732 const char* c_name = NULL;
733 switch (descriptor[0]) {
734 case 'Z': c_name = "boolean"; break;
735 case 'B': c_name = "byte"; break;
736 case 'C': c_name = "char"; break;
737 case 'S': c_name = "short"; break;
738 case 'I': c_name = "int"; break;
739 case 'J': c_name = "long"; break;
740 case 'F': c_name = "float"; break;
741 case 'D': c_name = "double"; break;
742 case 'V': c_name = "void"; break;
743 default:
744 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
745 }
746 name = String::AllocFromModifiedUtf8(c_name);
747 } else {
748 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
749 // components.
750 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
751 descriptor.erase(0, 1);
752 descriptor.erase(descriptor.size() - 1);
753 }
754 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
755 name = String::AllocFromModifiedUtf8(descriptor.c_str());
756 }
757 SetName(name);
758 return name;
759}
760
Elliott Hughes4681c802011-09-25 18:04:37 -0700761void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700762 if ((flags & kDumpClassFullDetail) == 0) {
763 os << PrettyClass(this);
764 if ((flags & kDumpClassClassLoader) != 0) {
765 os << ' ' << GetClassLoader();
766 }
767 if ((flags & kDumpClassInitialized) != 0) {
768 os << ' ' << GetStatus();
769 }
Elliott Hughese0918552011-10-28 17:18:29 -0700770 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700771 return;
772 }
773
774 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800775 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700776 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800777 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700778 os << " objectSize=" << SizeOf() << " "
779 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
780 os << StringPrintf(" access=0x%04x.%04x\n",
781 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
782 if (super != NULL) {
783 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
784 }
785 if (IsArrayClass()) {
786 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
787 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700788 if (kh.NumDirectInterfaces() > 0) {
789 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
790 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
791 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700792 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800793 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700794 }
795 }
796 os << " vtable (" << NumVirtualMethods() << " entries, "
797 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
798 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800799 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700800 }
801 os << " direct methods (" << NumDirectMethods() << " entries):\n";
802 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800803 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700804 }
805 if (NumStaticFields() > 0) {
806 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700807 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700808 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800809 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700810 }
811 } else {
812 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700813 }
814 }
815 if (NumInstanceFields() > 0) {
816 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700817 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700818 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800819 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700820 }
821 } else {
822 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700823 }
824 }
825}
826
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700827void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
828 if (new_reference_offsets != CLASS_WALK_SUPER) {
829 // Sanity check that the number of bits set in the reference offset bitmap
830 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800831 size_t count = 0;
832 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
833 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700834 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800835 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700836 }
837 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
838 new_reference_offsets, false);
839}
840
841void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
842 if (new_reference_offsets != CLASS_WALK_SUPER) {
843 // Sanity check that the number of bits set in the reference offset bitmap
844 // agrees with the number of references
845 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
846 NumReferenceStaticFieldsDuringLinking());
847 }
848 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
849 new_reference_offsets, false);
850}
851
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700852bool Class::Implements(const Class* klass) const {
853 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700854 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700855 // All interfaces implemented directly and by our superclass, and
856 // recursively all super-interfaces of those interfaces, are listed
857 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700858 int32_t iftable_count = GetIfTableCount();
859 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
860 for (int32_t i = 0; i < iftable_count; i++) {
861 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700862 return true;
863 }
864 }
865 return false;
866}
867
Elliott Hughese84278b2012-03-22 10:06:53 -0700868// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700869// are array classes.
870//
871// Consider an array class, e.g. Y[][], where Y is a subclass of X.
872// Y[][] = Y[][] --> true (identity)
873// X[][] = Y[][] --> true (element superclass)
874// Y = Y[][] --> false
875// Y[] = Y[][] --> false
876// Object = Y[][] --> true (everything is an object)
877// Object[] = Y[][] --> true
878// Object[][] = Y[][] --> true
879// Object[][][] = Y[][] --> false (too many []s)
880// Serializable = Y[][] --> true (all arrays are Serializable)
881// Serializable[] = Y[][] --> true
882// Serializable[][] = Y[][] --> false (unless Y is Serializable)
883//
884// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700885// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700886//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700887bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700888 DCHECK(IsArrayClass()) << PrettyClass(this);
889 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700890 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700891}
892
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700893bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700894 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
895 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700896 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700897 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700898 // src's super should be java_lang_Object, since it is an array.
899 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700900 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
901 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700902 return this == java_lang_Object;
903 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700904 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700905}
906
907bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700908 DCHECK(!IsInterface()) << PrettyClass(this);
909 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700910 const Class* current = this;
911 do {
912 if (current == klass) {
913 return true;
914 }
915 current = current->GetSuperClass();
916 } while (current != NULL);
917 return false;
918}
919
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800920bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700921 size_t i = 0;
922 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
923 ++i;
924 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700925 if (descriptor1.find('/', i) != StringPiece::npos ||
926 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700927 return false;
928 } else {
929 return true;
930 }
931}
932
933bool Class::IsInSamePackage(const Class* that) const {
934 const Class* klass1 = this;
935 const Class* klass2 = that;
936 if (klass1 == klass2) {
937 return true;
938 }
939 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700940 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700941 return false;
942 }
943 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700944 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700945 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700946 }
jeffhao4a801a42011-09-23 13:53:40 -0700947 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700948 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700949 }
950 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800951 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800952 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800953 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800954 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800955 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700956}
957
Elliott Hughesdbb40792011-11-18 17:05:22 -0800958bool Class::IsClassClass() const {
959 Class* java_lang_Class = GetClass()->GetClass();
960 return this == java_lang_Class;
961}
962
963bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800964 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800965}
966
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800967bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700968 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800969}
970
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800971ClassLoader* Class::GetClassLoader() const {
972 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700973}
974
Ian Rogers365c1022012-06-22 15:05:28 -0700975void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700977}
978
Mathieu Chartier66f19252012-09-18 08:57:04 -0700979AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700980 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700981 DCHECK(declaring_class != NULL) << PrettyClass(this);
982 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700983 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700984 int32_t iftable_count = GetIfTableCount();
985 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
986 for (int32_t i = 0; i < iftable_count; i++) {
987 InterfaceEntry* interface_entry = iftable->Get(i);
988 if (interface_entry->GetInterface() == declaring_class) {
989 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700990 }
991 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700992 return NULL;
993}
994
Mathieu Chartier66f19252012-09-18 08:57:04 -0700995AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700996 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700997 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700998 if (method != NULL) {
999 return method;
1000 }
1001
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001002 int32_t iftable_count = GetIfTableCount();
1003 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1004 for (int32_t i = 0; i < iftable_count; i++) {
1005 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001006 if (method != NULL) {
1007 return method;
1008 }
1009 }
1010 return NULL;
1011}
1012
Mathieu Chartier66f19252012-09-18 08:57:04 -07001013AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001014 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001015 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001016 if (method != NULL) {
1017 return method;
1018 }
1019
1020 int32_t iftable_count = GetIfTableCount();
1021 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1022 for (int32_t i = 0; i < iftable_count; i++) {
1023 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
1024 if (method != NULL) {
1025 return method;
1026 }
1027 }
1028 return NULL;
1029}
1030
1031
Mathieu Chartier66f19252012-09-18 08:57:04 -07001032AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, 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 < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001035 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001036 mh.ChangeMethod(method);
1037 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001038 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001039 }
1040 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001041 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001042}
1043
Mathieu Chartier66f19252012-09-18 08:57:04 -07001044AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001045 if (GetDexCache() == dex_cache) {
1046 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001047 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001048 if (method->GetDexMethodIndex() == dex_method_idx) {
1049 return method;
1050 }
1051 }
1052 }
1053 return NULL;
1054}
1055
Mathieu Chartier66f19252012-09-18 08:57:04 -07001056AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001057 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001058 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001059 if (method != NULL) {
1060 return method;
1061 }
1062 }
1063 return NULL;
1064}
1065
Mathieu Chartier66f19252012-09-18 08:57:04 -07001066AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001067 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001068 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001069 if (method != NULL) {
1070 return method;
1071 }
1072 }
1073 return NULL;
1074}
1075
Mathieu Chartier66f19252012-09-18 08:57:04 -07001076AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001077 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001078 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001079 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001080 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001081 mh.ChangeMethod(method);
1082 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001083 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001084 }
1085 }
1086 return NULL;
1087}
1088
Mathieu Chartier66f19252012-09-18 08:57:04 -07001089AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001090 if (GetDexCache() == dex_cache) {
1091 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001092 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001093 if (method->GetDexMethodIndex() == dex_method_idx) {
1094 return method;
1095 }
1096 }
1097 }
1098 return NULL;
1099}
1100
Mathieu Chartier66f19252012-09-18 08:57:04 -07001101AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001102 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001103 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001104 if (method != NULL) {
1105 return method;
1106 }
1107 }
1108 return NULL;
1109}
1110
Mathieu Chartier66f19252012-09-18 08:57:04 -07001111AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001112 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001113 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001114 if (method != NULL) {
1115 return method;
1116 }
1117 }
1118 return NULL;
1119}
1120
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001121Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 // Is the field in this class?
1123 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001124 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1126 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001127 fh.ChangeField(f);
1128 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001129 return f;
1130 }
1131 }
1132 return NULL;
1133}
1134
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001135Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1136 if (GetDexCache() == dex_cache) {
1137 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1138 Field* f = GetInstanceField(i);
1139 if (f->GetDexFieldIndex() == dex_field_idx) {
1140 return f;
1141 }
1142 }
1143 }
1144 return NULL;
1145}
1146
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001147Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 // Is the field in this class, or any of its superclasses?
1149 // Interfaces are not relevant because they can't contain instance fields.
1150 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001151 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 if (f != NULL) {
1153 return f;
1154 }
1155 }
1156 return NULL;
1157}
1158
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001159Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1160 // Is the field in this class, or any of its superclasses?
1161 // Interfaces are not relevant because they can't contain instance fields.
1162 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1163 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1164 if (f != NULL) {
1165 return f;
1166 }
1167 }
1168 return NULL;
1169}
1170
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001171Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1172 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001173 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001174 for (size_t i = 0; i < NumStaticFields(); ++i) {
1175 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001176 fh.ChangeField(f);
1177 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001178 return f;
1179 }
1180 }
1181 return NULL;
1182}
1183
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001184Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1185 if (dex_cache == GetDexCache()) {
1186 for (size_t i = 0; i < NumStaticFields(); ++i) {
1187 Field* f = GetStaticField(i);
1188 if (f->GetDexFieldIndex() == dex_field_idx) {
1189 return f;
1190 }
1191 }
1192 }
1193 return NULL;
1194}
1195
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001196Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1197 // Is the field in this class (or its interfaces), or any of its
1198 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001199 ClassHelper kh;
1200 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001201 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001202 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001203 if (f != NULL) {
1204 return f;
1205 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001206 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001207 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001208 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1209 Class* interface = kh.GetDirectInterface(i);
1210 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001211 if (f != NULL) {
1212 return f;
1213 }
1214 }
1215 }
1216 return NULL;
1217}
1218
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001219Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1220 ClassHelper kh;
1221 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1222 // Is the field in this class?
1223 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1224 if (f != NULL) {
1225 return f;
1226 }
1227 // Is this field in any of this class' interfaces?
1228 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001229 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1230 Class* interface = kh.GetDirectInterface(i);
1231 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001232 if (f != NULL) {
1233 return f;
1234 }
1235 }
1236 }
1237 return NULL;
1238}
1239
Ian Rogersb067ac22011-12-13 18:05:09 -08001240Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1241 // Find a field using the JLS field resolution order
1242 ClassHelper kh;
1243 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1244 // Is the field in this class?
1245 Field* f = k->FindDeclaredInstanceField(name, type);
1246 if (f != NULL) {
1247 return f;
1248 }
1249 f = k->FindDeclaredStaticField(name, type);
1250 if (f != NULL) {
1251 return f;
1252 }
1253 // Is this field in any of this class' interfaces?
1254 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001255 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1256 Class* interface = kh.GetDirectInterface(i);
1257 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001258 if (f != NULL) {
1259 return f;
1260 }
1261 }
1262 }
1263 return NULL;
1264}
1265
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001266Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001267 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001268 DCHECK_GE(component_count, 0);
1269 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001270
Ian Rogersa15e67d2012-02-28 13:51:55 -08001271 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001272 size_t data_size = component_count * component_size;
1273 size_t size = header_size + data_size;
1274
1275 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1276 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1277 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1278 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001279 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001280 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001281 return NULL;
1282 }
1283
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001284 Heap* heap = Runtime::Current()->GetHeap();
1285 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001286 if (array != NULL) {
1287 DCHECK(array->IsArrayInstance());
1288 array->SetLength(component_count);
1289 }
1290 return array;
1291}
1292
1293Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001294 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001295 return Alloc(array_class, component_count, array_class->GetComponentSize());
1296}
1297
Elliott Hughes80609252011-09-23 17:24:51 -07001298bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001299 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001300 "length=%i; index=%i", length_, index);
1301 return false;
1302}
1303
1304bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001305 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001306 "Can't store an element of type %s into an array of type %s",
1307 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1308 return false;
1309}
1310
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001311template<typename T>
1312PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001313 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001314 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1315 return down_cast<PrimitiveArray<T>*>(raw_array);
1316}
1317
1318template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1319
1320// Explicitly instantiate all the primitive array types.
1321template class PrimitiveArray<uint8_t>; // BooleanArray
1322template class PrimitiveArray<int8_t>; // ByteArray
1323template class PrimitiveArray<uint16_t>; // CharArray
1324template class PrimitiveArray<double>; // DoubleArray
1325template class PrimitiveArray<float>; // FloatArray
1326template class PrimitiveArray<int32_t>; // IntArray
1327template class PrimitiveArray<int64_t>; // LongArray
1328template class PrimitiveArray<int16_t>; // ShortArray
1329
Ian Rogers466bb252011-10-14 03:29:56 -07001330// Explicitly instantiate Class[][]
1331template class ObjectArray<ObjectArray<Class> >;
1332
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001333// TODO: get global references for these
1334Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001335
Brian Carlstroma663ea52011-08-19 23:33:41 -07001336void String::SetClass(Class* java_lang_String) {
1337 CHECK(java_lang_String_ == NULL);
1338 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001339 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001340}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001341
Brian Carlstroma663ea52011-08-19 23:33:41 -07001342void String::ResetClass() {
1343 CHECK(java_lang_String_ != NULL);
1344 java_lang_String_ = NULL;
1345}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001346
Brian Carlstromc74255f2011-09-11 22:47:39 -07001347String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001348 return Runtime::Current()->GetInternTable()->InternWeak(this);
1349}
1350
Brian Carlstrom395520e2011-09-25 19:35:00 -07001351int32_t String::GetHashCode() {
1352 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1353 if (result == 0) {
1354 ComputeHashCode();
1355 }
1356 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1357 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1358 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001359 return result;
1360}
1361
1362int32_t String::GetLength() const {
1363 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1364 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1365 return result;
1366}
1367
1368uint16_t String::CharAt(int32_t index) const {
1369 // TODO: do we need this? Equals is the only caller, and could
1370 // bounds check itself.
1371 if (index < 0 || index >= count_) {
1372 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001373 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001374 "length=%i; index=%i", count_, index);
1375 return 0;
1376 }
1377 return GetCharArray()->Get(index + GetOffset());
1378}
1379
1380String* String::AllocFromUtf16(int32_t utf16_length,
1381 const uint16_t* utf16_data_in,
1382 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001383 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001384 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001385 if (string == NULL) {
1386 return NULL;
1387 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001388 // TODO: use 16-bit wide memset variant
1389 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001390 if (array == NULL) {
1391 return NULL;
1392 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001393 for (int i = 0; i < utf16_length; i++) {
1394 array->Set(i, utf16_data_in[i]);
1395 }
1396 if (hash_code != 0) {
1397 string->SetHashCode(hash_code);
1398 } else {
1399 string->ComputeHashCode();
1400 }
1401 return string;
1402}
1403
1404String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001405 if (utf == NULL) {
1406 return NULL;
1407 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001408 size_t char_count = CountModifiedUtf8Chars(utf);
1409 return AllocFromModifiedUtf8(char_count, utf);
1410}
1411
1412String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1413 const char* utf8_data_in) {
1414 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001415 if (string == NULL) {
1416 return NULL;
1417 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001418 uint16_t* utf16_data_out =
1419 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1420 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1421 string->ComputeHashCode();
1422 return string;
1423}
1424
1425String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001426 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1427 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001428 return NULL;
1429 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001430 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431}
1432
1433String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001434 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001435 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001436 if (string == NULL) {
1437 return NULL;
1438 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001439 string->SetArray(array);
1440 string->SetCount(array->GetLength());
1441 return string;
1442}
1443
1444bool String::Equals(const String* that) const {
1445 if (this == that) {
1446 // Quick reference equality test
1447 return true;
1448 } else if (that == NULL) {
1449 // Null isn't an instanceof anything
1450 return false;
1451 } else if (this->GetLength() != that->GetLength()) {
1452 // Quick length inequality test
1453 return false;
1454 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001455 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001456 // hash code was already equal
1457 for (int32_t i = 0; i < that->GetLength(); ++i) {
1458 if (this->CharAt(i) != that->CharAt(i)) {
1459 return false;
1460 }
1461 }
1462 return true;
1463 }
1464}
1465
Elliott Hughes5d78d392011-12-13 16:53:05 -08001466bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001467 if (this->GetLength() != that_length) {
1468 return false;
1469 } else {
1470 for (int32_t i = 0; i < that_length; ++i) {
1471 if (this->CharAt(i) != that_chars[that_offset + i]) {
1472 return false;
1473 }
1474 }
1475 return true;
1476 }
1477}
1478
1479bool String::Equals(const char* modified_utf8) const {
1480 for (int32_t i = 0; i < GetLength(); ++i) {
1481 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1482 if (ch == '\0' || ch != CharAt(i)) {
1483 return false;
1484 }
1485 }
1486 return *modified_utf8 == '\0';
1487}
1488
1489bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001490 if (modified_utf8.size() != GetLength()) {
1491 return false;
1492 }
1493 const char* p = modified_utf8.data();
1494 for (int32_t i = 0; i < GetLength(); ++i) {
1495 uint16_t ch = GetUtf16FromUtf8(&p);
1496 if (ch != CharAt(i)) {
1497 return false;
1498 }
1499 }
1500 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001501}
1502
1503// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1504std::string String::ToModifiedUtf8() const {
1505 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001506 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001507 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001508 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1509 return result;
1510}
1511
Ian Rogers1c5eb702012-02-01 09:18:34 -08001512void Throwable::SetCause(Throwable* cause) {
1513 CHECK(cause != NULL);
1514 CHECK(cause != this);
1515 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1516 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1517}
1518
Ian Rogers466bb252011-10-14 03:29:56 -07001519bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001520 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001521 return false;
1522 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001523 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001524}
1525
Ian Rogers9074b992011-10-26 17:41:55 -07001526std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001527 std::string result(PrettyTypeOf(this));
1528 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001529 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001530 if (msg != NULL) {
1531 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001532 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001533 result += "\n";
1534 Object* stack_state = GetStackState();
1535 // check stack state isn't missing or corrupt
1536 if (stack_state != NULL && stack_state->IsObjectArray()) {
1537 // Decode the internal stack trace into the depth and method trace
1538 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1539 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001540 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1541 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001542 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001543 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001544 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001545 uint32_t dex_pc = pc_trace->Get(i);
1546 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001547 const char* source_file = mh.GetDeclaringClassSourceFile();
1548 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1549 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001550 }
Ian Rogers9074b992011-10-26 17:41:55 -07001551 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001552 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001553 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001554 result += "Caused by: ";
1555 result += cause->Dump();
1556 }
Ian Rogers9074b992011-10-26 17:41:55 -07001557 return result;
1558}
1559
Ian Rogers5167c972012-02-03 10:41:20 -08001560
1561Class* Throwable::java_lang_Throwable_ = NULL;
1562
1563void Throwable::SetClass(Class* java_lang_Throwable) {
1564 CHECK(java_lang_Throwable_ == NULL);
1565 CHECK(java_lang_Throwable != NULL);
1566 java_lang_Throwable_ = java_lang_Throwable;
1567}
1568
1569void Throwable::ResetClass() {
1570 CHECK(java_lang_Throwable_ != NULL);
1571 java_lang_Throwable_ = NULL;
1572}
1573
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001574Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1575
1576void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1577 CHECK(java_lang_StackTraceElement_ == NULL);
1578 CHECK(java_lang_StackTraceElement != NULL);
1579 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1580}
1581
1582void StackTraceElement::ResetClass() {
1583 CHECK(java_lang_StackTraceElement_ != NULL);
1584 java_lang_StackTraceElement_ = NULL;
1585}
1586
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001587StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1588 String* method_name,
1589 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001590 int32_t line_number) {
1591 StackTraceElement* trace =
1592 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1593 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1594 const_cast<String*>(declaring_class), false);
1595 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1596 const_cast<String*>(method_name), false);
1597 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1598 const_cast<String*>(file_name), false);
1599 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1600 line_number, false);
1601 return trace;
1602}
1603
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001604} // namespace art