blob: e4b5f989df4d56dbca7137e503d4005b5cb96c50 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro3ee755d2011-06-28 12:11:04 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020
Ian Rogersdf20fe02011-07-20 20:34:16 -070021#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include <string>
24#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070028#include "dex_cache.h"
29#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070034#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Ian Rogers60db5ab2012-02-20 17:02:00 -080037#include "runtime_support.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070038#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070039#include "utils.h"
Elliott Hughesa4f94742012-05-29 16:28:38 -070040#include "well_known_classes.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070041
42namespace art {
43
Elliott Hughesdbb40792011-11-18 17:05:22 -080044String* Object::AsString() {
45 DCHECK(GetClass()->IsStringClass());
46 return down_cast<String*>(this);
47}
48
Elliott Hughes081be7f2011-09-18 16:50:26 -070049Object* Object::Clone() {
50 Class* c = GetClass();
51 DCHECK(!c->IsClassClass());
52
53 // Object::SizeOf gets the right size even if we're an array.
54 // Using c->AllocObject() here would be wrong.
55 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080056 Heap* heap = Runtime::Current()->GetHeap();
57 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070058 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070059 return NULL;
60 }
61
62 // Copy instance data. We assume memcpy copies by words.
63 // TODO: expose and use move32.
64 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070065 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070066 size_t offset = sizeof(Object);
67 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
68
Mathieu Chartier88c95be2012-09-11 14:06:41 -070069 // Perform write barriers on copied object references.
70 if (c->IsArrayClass()) {
71 if (!c->GetComponentType()->IsPrimitive()) {
72 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
73 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
74 }
75 } else {
76 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
77 size_t num_reference_fields = klass->NumReferenceInstanceFields();
78 for (size_t i = 0; i < num_reference_fields; ++i) {
79 Field* field = klass->GetInstanceField(i);
80 MemberOffset field_offset = field->GetOffset();
81 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
82 heap->WriteBarrierField(copy.get(), field_offset, ref);
83 }
84 }
85 }
86
Elliott Hughes20cde902011-10-04 17:37:27 -070087 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080088 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070089 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070090
Brian Carlstrom40381fb2011-10-19 14:13:40 -070091 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070092}
93
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070094uint32_t Object::GetThinLockId() {
95 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070096}
97
98void Object::MonitorEnter(Thread* thread) {
99 Monitor::MonitorEnter(thread, this);
100}
101
Ian Rogersff1ed472011-09-20 13:46:24 -0700102bool Object::MonitorExit(Thread* thread) {
103 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700104}
105
106void Object::Notify() {
107 Monitor::Notify(Thread::Current(), this);
108}
109
110void Object::NotifyAll() {
111 Monitor::NotifyAll(Thread::Current(), this);
112}
113
114void Object::Wait(int64_t ms, int32_t ns) {
115 Monitor::Wait(Thread::Current(), this, ms, ns, true);
116}
117
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700118// TODO: get global references for these
119Class* Field::java_lang_reflect_Field_ = NULL;
120
121void Field::SetClass(Class* java_lang_reflect_Field) {
122 CHECK(java_lang_reflect_Field_ == NULL);
123 CHECK(java_lang_reflect_Field != NULL);
124 java_lang_reflect_Field_ = java_lang_reflect_Field;
125}
126
127void Field::ResetClass() {
128 CHECK(java_lang_reflect_Field_ != NULL);
129 java_lang_reflect_Field_ = NULL;
130}
131
Ian Rogers0571d352011-11-03 19:51:38 -0700132void Field::SetOffset(MemberOffset num_bytes) {
133 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134#if 0 // TODO enable later in boot and under !NDEBUG
135 FieldHelper fh(this);
136 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700137 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
138 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
139 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700141 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
142}
143
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700144uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700146 if (IsStatic()) {
147 object = declaring_class_;
148 }
149 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700150}
151
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700152void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700153 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154 if (IsStatic()) {
155 object = declaring_class_;
156 }
157 object->SetField32(GetOffset(), new_value, IsVolatile());
158}
159
160uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700162 if (IsStatic()) {
163 object = declaring_class_;
164 }
165 return object->GetField64(GetOffset(), IsVolatile());
166}
167
168void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700169 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700170 if (IsStatic()) {
171 object = declaring_class_;
172 }
173 object->SetField64(GetOffset(), new_value, IsVolatile());
174}
175
176Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700177 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700178 if (IsStatic()) {
179 object = declaring_class_;
180 }
181 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
182}
183
184void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700185 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186 if (IsStatic()) {
187 object = declaring_class_;
188 }
189 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
190}
191
192bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
194 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 return Get32(object);
196}
197
198void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
200 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 Set32(object, z);
202}
203
204int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
206 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 return Get32(object);
208}
209
210void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
212 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 Set32(object, b);
214}
215
216uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
218 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 return Get32(object);
220}
221
222void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
224 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 Set32(object, c);
226}
227
Ian Rogers466bb252011-10-14 03:29:56 -0700228int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800229 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
230 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 return Get32(object);
232}
233
Ian Rogers466bb252011-10-14 03:29:56 -0700234void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800235 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
236 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 Set32(object, s);
238}
239
240int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800241 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
242 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 return Get32(object);
244}
245
246void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
248 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 Set32(object, i);
250}
251
252int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800253 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
254 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700255 return Get64(object);
256}
257
258void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
260 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 Set64(object, j);
262}
263
Elliott Hughes1d878f32012-04-11 15:17:54 -0700264union Bits {
265 jdouble d;
266 jfloat f;
267 jint i;
268 jlong j;
269};
270
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800272 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
273 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700274 Bits bits;
275 bits.i = Get32(object);
276 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277}
278
279void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
281 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700282 Bits bits;
283 bits.f = f;
284 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285}
286
287double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800288 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
289 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700290 Bits bits;
291 bits.j = Get64(object);
292 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700293}
294
295void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
297 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700298 Bits bits;
299 bits.d = d;
300 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301}
302
303Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
305 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 return GetObj(object);
307}
308
309void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800310 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
311 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 SetObj(object, l);
313}
314
315// TODO: get global references for these
Mathieu Chartier66f19252012-09-18 08:57:04 -0700316Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
317Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700318
Mathieu Chartier66f19252012-09-18 08:57:04 -0700319InvokeType AbstractMethod::GetInvokeType() const {
Ian Rogers08f753d2012-08-24 14:35:25 -0700320 // TODO: kSuper?
321 if (GetDeclaringClass()->IsInterface()) {
322 return kInterface;
323 } else if (IsStatic()) {
324 return kStatic;
325 } else if (IsDirect()) {
326 return kDirect;
327 } else {
328 return kVirtual;
329 }
330}
331
Mathieu Chartier66f19252012-09-18 08:57:04 -0700332void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
Elliott Hughes80609252011-09-23 17:24:51 -0700333 CHECK(java_lang_reflect_Constructor_ == NULL);
334 CHECK(java_lang_reflect_Constructor != NULL);
335 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
336
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 CHECK(java_lang_reflect_Method_ == NULL);
338 CHECK(java_lang_reflect_Method != NULL);
339 java_lang_reflect_Method_ = java_lang_reflect_Method;
340}
341
Mathieu Chartier66f19252012-09-18 08:57:04 -0700342void AbstractMethod::ResetClasses() {
Elliott Hughes80609252011-09-23 17:24:51 -0700343 CHECK(java_lang_reflect_Constructor_ != NULL);
344 java_lang_reflect_Constructor_ = NULL;
345
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346 CHECK(java_lang_reflect_Method_ != NULL);
347 java_lang_reflect_Method_ = NULL;
348}
349
Mathieu Chartier66f19252012-09-18 08:57:04 -0700350ObjectArray<String>* AbstractMethod::GetDexCacheStrings() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351 return GetFieldObject<ObjectArray<String>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700352 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353}
354
Mathieu Chartier66f19252012-09-18 08:57:04 -0700355void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
356 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 new_dex_cache_strings, false);
358}
359
Mathieu Chartier66f19252012-09-18 08:57:04 -0700360ObjectArray<AbstractMethod>* AbstractMethod::GetDexCacheResolvedMethods() const {
361 return GetFieldObject<ObjectArray<AbstractMethod>*>(
362 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800363}
364
Mathieu Chartier66f19252012-09-18 08:57:04 -0700365void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
366 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
Ian Rogers19846512012-02-24 11:42:47 -0800367 new_dex_cache_methods, false);
368}
369
Mathieu Chartier66f19252012-09-18 08:57:04 -0700370ObjectArray<Class>* AbstractMethod::GetDexCacheResolvedTypes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371 return GetFieldObject<ObjectArray<Class>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700372 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373}
374
Mathieu Chartier66f19252012-09-18 08:57:04 -0700375void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
376 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377 new_dex_cache_classes, false);
378}
379
Mathieu Chartier66f19252012-09-18 08:57:04 -0700380ObjectArray<StaticStorageBase>* AbstractMethod::GetDexCacheInitializedStaticStorage() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700382 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 false);
384}
385
Mathieu Chartier66f19252012-09-18 08:57:04 -0700386void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
387 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389}
390
Mathieu Chartier66f19252012-09-18 08:57:04 -0700391size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 CHECK_LE(1, shorty.length());
393 uint32_t num_registers = 0;
394 for (int i = 1; i < shorty.length(); ++i) {
395 char ch = shorty[i];
396 if (ch == 'D' || ch == 'J') {
397 num_registers += 2;
398 } else {
399 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700400 }
401 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700402 return num_registers;
403}
404
Mathieu Chartier66f19252012-09-18 08:57:04 -0700405bool AbstractMethod::IsProxyMethod() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800406 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700407}
408
Mathieu Chartier66f19252012-09-18 08:57:04 -0700409AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
Ian Rogers466bb252011-10-14 03:29:56 -0700410 if (IsStatic()) {
411 return NULL;
412 }
413 Class* declaring_class = GetDeclaringClass();
414 Class* super_class = declaring_class->GetSuperClass();
415 uint16_t method_index = GetMethodIndex();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700416 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
417 AbstractMethod* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 // Did this method override a super class method? If so load the result from the super class'
419 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700420 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
421 result = super_class_vtable->Get(method_index);
422 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800423 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800424 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800425 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
426 CHECK_EQ(result,
427 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800428 } else {
429 MethodHelper mh(this);
430 MethodHelper interface_mh;
431 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
432 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
433 InterfaceEntry* entry = iftable->Get(i);
434 Class* interface = entry->GetInterface();
435 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700436 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers16f93672012-02-14 12:29:06 -0800437 interface_mh.ChangeMethod(interface_method);
438 if (mh.HasSameNameAndSignature(&interface_mh)) {
439 result = interface_method;
440 break;
441 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 }
443 }
Ian Rogers466bb252011-10-14 03:29:56 -0700444 }
445 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446#ifndef NDEBUG
447 MethodHelper result_mh(result);
448 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
449#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700450 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700451}
452
Mathieu Chartier66f19252012-09-18 08:57:04 -0700453static const void* GetOatCode(const AbstractMethod* m)
454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800455 Runtime* runtime = Runtime::Current();
456 const void* code = m->GetCode();
457 // Peel off any method tracing trampoline.
458 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
459 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
460 }
461 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800462 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800463 code = runtime->GetClassLinker()->GetOatCodeFor(m);
464 }
465 return code;
466}
467
Mathieu Chartier66f19252012-09-18 08:57:04 -0700468uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700469 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
470}
471
Mathieu Chartier66f19252012-09-18 08:57:04 -0700472uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700473#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700474 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700475 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800476 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700477 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700478 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700479 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800480 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700481 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700482 if (mapping_table[i] == sought_offset) {
483 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700484 }
485 }
buzbee8320f382012-09-11 16:29:42 -0700486 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
487 << " in " << PrettyMethod(this);
488 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700489#else
490 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
491 return static_cast<uint32_t>(pc);
492#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700493}
494
Mathieu Chartier66f19252012-09-18 08:57:04 -0700495uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700496 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700497 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700498 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700499 return 0; // Special no mapping/pc == 0 case
500 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700501 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700502 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 uint32_t map_offset = mapping_table[i];
504 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700505 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800506 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700507 }
508 }
509 LOG(FATAL) << "Looking up Dex PC not contained in method";
510 return 0;
511}
512
Mathieu Chartier66f19252012-09-18 08:57:04 -0700513uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800514 MethodHelper mh(this);
515 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700516 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700517 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
518 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700519 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700520 if (iter_type_idx == DexFile::kDexNoIndex16) {
521 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700522 }
523 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800524 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700525 if (iter_exception_type == NULL) {
526 // The verifier should take care of resolving all exception classes early
527 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800528 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700529 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700530 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700531 }
532 }
533 // Handler not found
534 return DexFile::kDexNoIndex;
535}
536
Mathieu Chartier66f19252012-09-18 08:57:04 -0700537void AbstractMethod::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700538 if (kIsDebugBuild) {
539 self->AssertThreadSuspensionIsAllowable();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700540 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700541 CHECK_EQ(kRunnable, self->GetState());
542 }
TDYa12785321912012-04-01 15:24:56 -0700543
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700544 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700545 ManagedStack fragment;
546 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700547
548 // Call the invoke stub associated with the method.
549 // Pass everything as arguments.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700550 AbstractMethod::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700551
552 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700553
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500554 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700555 bool log = false;
556 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800557 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
558 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700559 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700560 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700561 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800562 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
563 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700564 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700565 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800566 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
567 PrettyMethod(this).c_str(), GetCode(), stub,
568 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700569 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700570 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700571 }
572 }
573
574 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700575 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700576}
577
Mathieu Chartier66f19252012-09-18 08:57:04 -0700578bool AbstractMethod::IsRegistered() const {
579 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800580 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800581 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700582 return native_method != jni_stub;
583}
584
Mathieu Chartier66f19252012-09-18 08:57:04 -0700585void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800586 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700587 CHECK(IsNative()) << PrettyMethod(this);
588 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700589#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700590 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700591 native_method, false);
592#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800593 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700594 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800595 native_method, false);
596 } else {
597 // We've been asked to associate this method with the given native method but are working
598 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
599 // the native method to runtime support and store the target somewhere runtime support will
600 // find it.
601#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700602 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800603 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
604#else
605 UNIMPLEMENTED(FATAL);
606#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700607 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800608 reinterpret_cast<const uint8_t*>(native_method), false);
609 }
TDYa12726467572012-04-17 20:51:22 -0700610#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700611}
612
Mathieu Chartier66f19252012-09-18 08:57:04 -0700613void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700614 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700615 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800616 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700617}
618
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700619void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700620 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
621 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700622 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700623 if (new_status > kStatusResolved) {
624 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
625 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800626 if (new_status == kStatusError) {
627 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
628
629 // stash current exception
630 Thread* self = Thread::Current();
631 SirtRef<Throwable> exception(self->GetException());
632 CHECK(exception.get() != NULL);
633
634 // clear exception to call FindSystemClass
635 self->ClearException();
636 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
637 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
638 CHECK(!self->IsExceptionPending());
639
640 // only verification errors, not initialization problems, should set a verify error.
641 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
642 Class* exception_class = exception->GetClass();
643 if (!eiie_class->IsAssignableFrom(exception_class)) {
644 SetVerifyErrorClass(exception_class);
645 }
646
647 // restore exception
648 self->SetException(exception.get());
649 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700650 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651}
652
653DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700654 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700655}
656
657void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700658 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700659}
660
Brian Carlstrom1f870082011-08-23 16:02:11 -0700661Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700662 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700663 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500664 // TODO: decide whether we want this check. It currently fails during bootstrap.
665 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700666 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800667 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700668}
669
Ian Rogers0571d352011-11-03 19:51:38 -0700670void Class::SetClassSize(size_t new_class_size) {
671 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
672 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
673}
674
Ian Rogersd418eda2012-01-30 12:14:28 -0800675// Return the class' name. The exact format is bizarre, but it's the specified behavior for
676// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
677// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
678// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
679String* Class::ComputeName() {
680 String* name = GetName();
681 if (name != NULL) {
682 return name;
683 }
684 std::string descriptor(ClassHelper(this).GetDescriptor());
685 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
686 // The descriptor indicates that this is the class for
687 // a primitive type; special-case the return value.
688 const char* c_name = NULL;
689 switch (descriptor[0]) {
690 case 'Z': c_name = "boolean"; break;
691 case 'B': c_name = "byte"; break;
692 case 'C': c_name = "char"; break;
693 case 'S': c_name = "short"; break;
694 case 'I': c_name = "int"; break;
695 case 'J': c_name = "long"; break;
696 case 'F': c_name = "float"; break;
697 case 'D': c_name = "double"; break;
698 case 'V': c_name = "void"; break;
699 default:
700 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
701 }
702 name = String::AllocFromModifiedUtf8(c_name);
703 } else {
704 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
705 // components.
706 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
707 descriptor.erase(0, 1);
708 descriptor.erase(descriptor.size() - 1);
709 }
710 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
711 name = String::AllocFromModifiedUtf8(descriptor.c_str());
712 }
713 SetName(name);
714 return name;
715}
716
Elliott Hughes4681c802011-09-25 18:04:37 -0700717void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700718 if ((flags & kDumpClassFullDetail) == 0) {
719 os << PrettyClass(this);
720 if ((flags & kDumpClassClassLoader) != 0) {
721 os << ' ' << GetClassLoader();
722 }
723 if ((flags & kDumpClassInitialized) != 0) {
724 os << ' ' << GetStatus();
725 }
Elliott Hughese0918552011-10-28 17:18:29 -0700726 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700727 return;
728 }
729
730 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800731 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700732 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800733 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700734 os << " objectSize=" << SizeOf() << " "
735 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
736 os << StringPrintf(" access=0x%04x.%04x\n",
737 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
738 if (super != NULL) {
739 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
740 }
741 if (IsArrayClass()) {
742 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
743 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700744 if (kh.NumDirectInterfaces() > 0) {
745 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
746 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
747 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700748 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800749 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700750 }
751 }
752 os << " vtable (" << NumVirtualMethods() << " entries, "
753 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
754 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800755 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700756 }
757 os << " direct methods (" << NumDirectMethods() << " entries):\n";
758 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800759 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700760 }
761 if (NumStaticFields() > 0) {
762 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700763 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700764 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800765 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700766 }
767 } else {
768 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700769 }
770 }
771 if (NumInstanceFields() > 0) {
772 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700773 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700774 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800775 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700776 }
777 } else {
778 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700779 }
780 }
781}
782
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700783void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
784 if (new_reference_offsets != CLASS_WALK_SUPER) {
785 // Sanity check that the number of bits set in the reference offset bitmap
786 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800787 size_t count = 0;
788 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
789 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700790 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800791 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700792 }
793 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
794 new_reference_offsets, false);
795}
796
797void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
798 if (new_reference_offsets != CLASS_WALK_SUPER) {
799 // Sanity check that the number of bits set in the reference offset bitmap
800 // agrees with the number of references
801 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
802 NumReferenceStaticFieldsDuringLinking());
803 }
804 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
805 new_reference_offsets, false);
806}
807
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700808bool Class::Implements(const Class* klass) const {
809 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700810 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700811 // All interfaces implemented directly and by our superclass, and
812 // recursively all super-interfaces of those interfaces, are listed
813 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700814 int32_t iftable_count = GetIfTableCount();
815 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
816 for (int32_t i = 0; i < iftable_count; i++) {
817 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700818 return true;
819 }
820 }
821 return false;
822}
823
Elliott Hughese84278b2012-03-22 10:06:53 -0700824// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700825// are array classes.
826//
827// Consider an array class, e.g. Y[][], where Y is a subclass of X.
828// Y[][] = Y[][] --> true (identity)
829// X[][] = Y[][] --> true (element superclass)
830// Y = Y[][] --> false
831// Y[] = Y[][] --> false
832// Object = Y[][] --> true (everything is an object)
833// Object[] = Y[][] --> true
834// Object[][] = Y[][] --> true
835// Object[][][] = Y[][] --> false (too many []s)
836// Serializable = Y[][] --> true (all arrays are Serializable)
837// Serializable[] = Y[][] --> true
838// Serializable[][] = Y[][] --> false (unless Y is Serializable)
839//
840// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700841// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700842//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700843bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700844 DCHECK(IsArrayClass()) << PrettyClass(this);
845 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700846 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700847}
848
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700849bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700850 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
851 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700852 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700853 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700854 // src's super should be java_lang_Object, since it is an array.
855 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700856 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
857 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700858 return this == java_lang_Object;
859 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700860 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700861}
862
863bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700864 DCHECK(!IsInterface()) << PrettyClass(this);
865 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700866 const Class* current = this;
867 do {
868 if (current == klass) {
869 return true;
870 }
871 current = current->GetSuperClass();
872 } while (current != NULL);
873 return false;
874}
875
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800876bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700877 size_t i = 0;
878 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
879 ++i;
880 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700881 if (descriptor1.find('/', i) != StringPiece::npos ||
882 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700883 return false;
884 } else {
885 return true;
886 }
887}
888
889bool Class::IsInSamePackage(const Class* that) const {
890 const Class* klass1 = this;
891 const Class* klass2 = that;
892 if (klass1 == klass2) {
893 return true;
894 }
895 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700896 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700897 return false;
898 }
899 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700900 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700901 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700902 }
jeffhao4a801a42011-09-23 13:53:40 -0700903 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700904 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700905 }
906 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800907 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800908 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800910 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800911 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700912}
913
Elliott Hughesdbb40792011-11-18 17:05:22 -0800914bool Class::IsClassClass() const {
915 Class* java_lang_Class = GetClass()->GetClass();
916 return this == java_lang_Class;
917}
918
919bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800920 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800921}
922
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800923bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700924 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800925}
926
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800927ClassLoader* Class::GetClassLoader() const {
928 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700929}
930
Ian Rogers365c1022012-06-22 15:05:28 -0700931void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700933}
934
Mathieu Chartier66f19252012-09-18 08:57:04 -0700935AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700936 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700937 DCHECK(declaring_class != NULL) << PrettyClass(this);
938 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700939 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700940 int32_t iftable_count = GetIfTableCount();
941 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
942 for (int32_t i = 0; i < iftable_count; i++) {
943 InterfaceEntry* interface_entry = iftable->Get(i);
944 if (interface_entry->GetInterface() == declaring_class) {
945 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700946 }
947 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700948 return NULL;
949}
950
Mathieu Chartier66f19252012-09-18 08:57:04 -0700951AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700952 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700953 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700954 if (method != NULL) {
955 return method;
956 }
957
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700958 int32_t iftable_count = GetIfTableCount();
959 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
960 for (int32_t i = 0; i < iftable_count; i++) {
961 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700962 if (method != NULL) {
963 return method;
964 }
965 }
966 return NULL;
967}
968
Mathieu Chartier66f19252012-09-18 08:57:04 -0700969AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800970 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700971 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800972 if (method != NULL) {
973 return method;
974 }
975
976 int32_t iftable_count = GetIfTableCount();
977 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
978 for (int32_t i = 0; i < iftable_count; i++) {
979 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
980 if (method != NULL) {
981 return method;
982 }
983 }
984 return NULL;
985}
986
987
Mathieu Chartier66f19252012-09-18 08:57:04 -0700988AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800989 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700990 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700991 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800992 mh.ChangeMethod(method);
993 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700994 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700995 }
996 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700997 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700998}
999
Mathieu Chartier66f19252012-09-18 08:57:04 -07001000AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001001 if (GetDexCache() == dex_cache) {
1002 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001003 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001004 if (method->GetDexMethodIndex() == dex_method_idx) {
1005 return method;
1006 }
1007 }
1008 }
1009 return NULL;
1010}
1011
Mathieu Chartier66f19252012-09-18 08:57:04 -07001012AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001013 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001014 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001015 if (method != NULL) {
1016 return method;
1017 }
1018 }
1019 return NULL;
1020}
1021
Mathieu Chartier66f19252012-09-18 08:57:04 -07001022AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001023 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001024 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001025 if (method != NULL) {
1026 return method;
1027 }
1028 }
1029 return NULL;
1030}
1031
Mathieu Chartier66f19252012-09-18 08:57:04 -07001032AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001033 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 < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001036 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001037 mh.ChangeMethod(method);
1038 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001039 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001040 }
1041 }
1042 return NULL;
1043}
1044
Mathieu Chartier66f19252012-09-18 08:57:04 -07001045AbstractMethod* Class::FindDeclaredVirtualMethod(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 < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001048 AbstractMethod* method = GetVirtualMethod(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::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001058 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001059 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001060 if (method != NULL) {
1061 return method;
1062 }
1063 }
1064 return NULL;
1065}
1066
Mathieu Chartier66f19252012-09-18 08:57:04 -07001067AbstractMethod* Class::FindVirtualMethod(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->FindDeclaredVirtualMethod(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
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001077Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 // Is the field in this class?
1079 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001080 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1082 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001083 fh.ChangeField(f);
1084 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001085 return f;
1086 }
1087 }
1088 return NULL;
1089}
1090
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001091Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1092 if (GetDexCache() == dex_cache) {
1093 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1094 Field* f = GetInstanceField(i);
1095 if (f->GetDexFieldIndex() == dex_field_idx) {
1096 return f;
1097 }
1098 }
1099 }
1100 return NULL;
1101}
1102
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001103Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 // Is the field in this class, or any of its superclasses?
1105 // Interfaces are not relevant because they can't contain instance fields.
1106 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001107 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 if (f != NULL) {
1109 return f;
1110 }
1111 }
1112 return NULL;
1113}
1114
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001115Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1116 // Is the field in this class, or any of its superclasses?
1117 // Interfaces are not relevant because they can't contain instance fields.
1118 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1119 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1120 if (f != NULL) {
1121 return f;
1122 }
1123 }
1124 return NULL;
1125}
1126
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001127Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1128 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001129 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001130 for (size_t i = 0; i < NumStaticFields(); ++i) {
1131 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001132 fh.ChangeField(f);
1133 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001134 return f;
1135 }
1136 }
1137 return NULL;
1138}
1139
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001140Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1141 if (dex_cache == GetDexCache()) {
1142 for (size_t i = 0; i < NumStaticFields(); ++i) {
1143 Field* f = GetStaticField(i);
1144 if (f->GetDexFieldIndex() == dex_field_idx) {
1145 return f;
1146 }
1147 }
1148 }
1149 return NULL;
1150}
1151
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001152Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1153 // Is the field in this class (or its interfaces), or any of its
1154 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001155 ClassHelper kh;
1156 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001157 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001158 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001159 if (f != NULL) {
1160 return f;
1161 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001162 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001163 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001164 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1165 Class* interface = kh.GetDirectInterface(i);
1166 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001167 if (f != NULL) {
1168 return f;
1169 }
1170 }
1171 }
1172 return NULL;
1173}
1174
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001175Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1176 ClassHelper kh;
1177 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1178 // Is the field in this class?
1179 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1180 if (f != NULL) {
1181 return f;
1182 }
1183 // Is this field in any of this class' interfaces?
1184 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001185 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1186 Class* interface = kh.GetDirectInterface(i);
1187 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001188 if (f != NULL) {
1189 return f;
1190 }
1191 }
1192 }
1193 return NULL;
1194}
1195
Ian Rogersb067ac22011-12-13 18:05:09 -08001196Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1197 // Find a field using the JLS field resolution order
1198 ClassHelper kh;
1199 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1200 // Is the field in this class?
1201 Field* f = k->FindDeclaredInstanceField(name, type);
1202 if (f != NULL) {
1203 return f;
1204 }
1205 f = k->FindDeclaredStaticField(name, type);
1206 if (f != NULL) {
1207 return f;
1208 }
1209 // Is this field in any of this class' interfaces?
1210 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001211 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1212 Class* interface = kh.GetDirectInterface(i);
1213 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001214 if (f != NULL) {
1215 return f;
1216 }
1217 }
1218 }
1219 return NULL;
1220}
1221
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001222Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001223 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001224 DCHECK_GE(component_count, 0);
1225 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001226
Ian Rogersa15e67d2012-02-28 13:51:55 -08001227 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001228 size_t data_size = component_count * component_size;
1229 size_t size = header_size + data_size;
1230
1231 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1232 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1233 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1234 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001235 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001236 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001237 return NULL;
1238 }
1239
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001240 Heap* heap = Runtime::Current()->GetHeap();
1241 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001242 if (array != NULL) {
1243 DCHECK(array->IsArrayInstance());
1244 array->SetLength(component_count);
1245 }
1246 return array;
1247}
1248
1249Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001250 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001251 return Alloc(array_class, component_count, array_class->GetComponentSize());
1252}
1253
Elliott Hughes80609252011-09-23 17:24:51 -07001254bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001255 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001256 "length=%i; index=%i", length_, index);
1257 return false;
1258}
1259
1260bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001261 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001262 "Can't store an element of type %s into an array of type %s",
1263 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1264 return false;
1265}
1266
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001267template<typename T>
1268PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001269 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001270 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1271 return down_cast<PrimitiveArray<T>*>(raw_array);
1272}
1273
1274template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1275
1276// Explicitly instantiate all the primitive array types.
1277template class PrimitiveArray<uint8_t>; // BooleanArray
1278template class PrimitiveArray<int8_t>; // ByteArray
1279template class PrimitiveArray<uint16_t>; // CharArray
1280template class PrimitiveArray<double>; // DoubleArray
1281template class PrimitiveArray<float>; // FloatArray
1282template class PrimitiveArray<int32_t>; // IntArray
1283template class PrimitiveArray<int64_t>; // LongArray
1284template class PrimitiveArray<int16_t>; // ShortArray
1285
Ian Rogers466bb252011-10-14 03:29:56 -07001286// Explicitly instantiate Class[][]
1287template class ObjectArray<ObjectArray<Class> >;
1288
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001289// TODO: get global references for these
1290Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001291
Brian Carlstroma663ea52011-08-19 23:33:41 -07001292void String::SetClass(Class* java_lang_String) {
1293 CHECK(java_lang_String_ == NULL);
1294 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001295 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001296}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001297
Brian Carlstroma663ea52011-08-19 23:33:41 -07001298void String::ResetClass() {
1299 CHECK(java_lang_String_ != NULL);
1300 java_lang_String_ = NULL;
1301}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001302
Brian Carlstromc74255f2011-09-11 22:47:39 -07001303String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001304 return Runtime::Current()->GetInternTable()->InternWeak(this);
1305}
1306
Brian Carlstrom395520e2011-09-25 19:35:00 -07001307int32_t String::GetHashCode() {
1308 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1309 if (result == 0) {
1310 ComputeHashCode();
1311 }
1312 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1313 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1314 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001315 return result;
1316}
1317
1318int32_t String::GetLength() const {
1319 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1320 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1321 return result;
1322}
1323
1324uint16_t String::CharAt(int32_t index) const {
1325 // TODO: do we need this? Equals is the only caller, and could
1326 // bounds check itself.
1327 if (index < 0 || index >= count_) {
1328 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001329 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001330 "length=%i; index=%i", count_, index);
1331 return 0;
1332 }
1333 return GetCharArray()->Get(index + GetOffset());
1334}
1335
1336String* String::AllocFromUtf16(int32_t utf16_length,
1337 const uint16_t* utf16_data_in,
1338 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001339 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001340 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001341 if (string == NULL) {
1342 return NULL;
1343 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001344 // TODO: use 16-bit wide memset variant
1345 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001346 if (array == NULL) {
1347 return NULL;
1348 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001349 for (int i = 0; i < utf16_length; i++) {
1350 array->Set(i, utf16_data_in[i]);
1351 }
1352 if (hash_code != 0) {
1353 string->SetHashCode(hash_code);
1354 } else {
1355 string->ComputeHashCode();
1356 }
1357 return string;
1358}
1359
1360String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001361 if (utf == NULL) {
1362 return NULL;
1363 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001364 size_t char_count = CountModifiedUtf8Chars(utf);
1365 return AllocFromModifiedUtf8(char_count, utf);
1366}
1367
1368String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1369 const char* utf8_data_in) {
1370 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001371 if (string == NULL) {
1372 return NULL;
1373 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001374 uint16_t* utf16_data_out =
1375 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1376 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1377 string->ComputeHashCode();
1378 return string;
1379}
1380
1381String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001382 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1383 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001384 return NULL;
1385 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001386 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001387}
1388
1389String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001390 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001391 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001392 if (string == NULL) {
1393 return NULL;
1394 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001395 string->SetArray(array);
1396 string->SetCount(array->GetLength());
1397 return string;
1398}
1399
1400bool String::Equals(const String* that) const {
1401 if (this == that) {
1402 // Quick reference equality test
1403 return true;
1404 } else if (that == NULL) {
1405 // Null isn't an instanceof anything
1406 return false;
1407 } else if (this->GetLength() != that->GetLength()) {
1408 // Quick length inequality test
1409 return false;
1410 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001411 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001412 // hash code was already equal
1413 for (int32_t i = 0; i < that->GetLength(); ++i) {
1414 if (this->CharAt(i) != that->CharAt(i)) {
1415 return false;
1416 }
1417 }
1418 return true;
1419 }
1420}
1421
Elliott Hughes5d78d392011-12-13 16:53:05 -08001422bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001423 if (this->GetLength() != that_length) {
1424 return false;
1425 } else {
1426 for (int32_t i = 0; i < that_length; ++i) {
1427 if (this->CharAt(i) != that_chars[that_offset + i]) {
1428 return false;
1429 }
1430 }
1431 return true;
1432 }
1433}
1434
1435bool String::Equals(const char* modified_utf8) const {
1436 for (int32_t i = 0; i < GetLength(); ++i) {
1437 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1438 if (ch == '\0' || ch != CharAt(i)) {
1439 return false;
1440 }
1441 }
1442 return *modified_utf8 == '\0';
1443}
1444
1445bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001446 if (modified_utf8.size() != GetLength()) {
1447 return false;
1448 }
1449 const char* p = modified_utf8.data();
1450 for (int32_t i = 0; i < GetLength(); ++i) {
1451 uint16_t ch = GetUtf16FromUtf8(&p);
1452 if (ch != CharAt(i)) {
1453 return false;
1454 }
1455 }
1456 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001457}
1458
1459// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1460std::string String::ToModifiedUtf8() const {
1461 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001462 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001463 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001464 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1465 return result;
1466}
1467
Ian Rogers1c5eb702012-02-01 09:18:34 -08001468void Throwable::SetCause(Throwable* cause) {
1469 CHECK(cause != NULL);
1470 CHECK(cause != this);
1471 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1472 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1473}
1474
Ian Rogers466bb252011-10-14 03:29:56 -07001475bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001476 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001477 return false;
1478 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001479 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001480}
1481
Ian Rogers9074b992011-10-26 17:41:55 -07001482std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001483 std::string result(PrettyTypeOf(this));
1484 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001485 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001486 if (msg != NULL) {
1487 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001488 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001489 result += "\n";
1490 Object* stack_state = GetStackState();
1491 // check stack state isn't missing or corrupt
1492 if (stack_state != NULL && stack_state->IsObjectArray()) {
1493 // Decode the internal stack trace into the depth and method trace
1494 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1495 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001496 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1497 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001498 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001499 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001500 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001501 uint32_t dex_pc = pc_trace->Get(i);
1502 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001503 const char* source_file = mh.GetDeclaringClassSourceFile();
1504 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1505 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001506 }
Ian Rogers9074b992011-10-26 17:41:55 -07001507 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001508 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001509 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001510 result += "Caused by: ";
1511 result += cause->Dump();
1512 }
Ian Rogers9074b992011-10-26 17:41:55 -07001513 return result;
1514}
1515
Ian Rogers5167c972012-02-03 10:41:20 -08001516
1517Class* Throwable::java_lang_Throwable_ = NULL;
1518
1519void Throwable::SetClass(Class* java_lang_Throwable) {
1520 CHECK(java_lang_Throwable_ == NULL);
1521 CHECK(java_lang_Throwable != NULL);
1522 java_lang_Throwable_ = java_lang_Throwable;
1523}
1524
1525void Throwable::ResetClass() {
1526 CHECK(java_lang_Throwable_ != NULL);
1527 java_lang_Throwable_ = NULL;
1528}
1529
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001530Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1531
1532void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1533 CHECK(java_lang_StackTraceElement_ == NULL);
1534 CHECK(java_lang_StackTraceElement != NULL);
1535 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1536}
1537
1538void StackTraceElement::ResetClass() {
1539 CHECK(java_lang_StackTraceElement_ != NULL);
1540 java_lang_StackTraceElement_ = NULL;
1541}
1542
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001543StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1544 String* method_name,
1545 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001546 int32_t line_number) {
1547 StackTraceElement* trace =
1548 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1549 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1550 const_cast<String*>(declaring_class), false);
1551 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1552 const_cast<String*>(method_name), false);
1553 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1554 const_cast<String*>(file_name), false);
1555 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1556 line_number, false);
1557 return trace;
1558}
1559
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001560} // namespace art