blob: 0cf2aa886bd1c82e81fe62411d1fc423905d8b5a [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
Bill Buzbeea5b30242012-09-28 07:19:44 -0700517// Find the lowest-address native safepoint pc for a given dex pc
Ian Rogers34065b52012-09-28 16:31:26 -0700518uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700519#if !defined(ART_USE_LLVM_COMPILER)
Bill Buzbeea5b30242012-09-28 07:19:44 -0700520 const uint32_t* mapping_table = GetPcToDexMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700521 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800522 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700523 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700524 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700525 size_t mapping_table_length = GetPcToDexMappingTableLength();
526 for (size_t i = 0; i < mapping_table_length; i += 2) {
527 if (mapping_table[i + 1] == dex_pc) {
528 return mapping_table[i] + reinterpret_cast<uintptr_t>(GetOatCode(this));
529 }
530 }
531 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
532 << " in " << PrettyMethod(this);
533 return 0;
534#else
535 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
536 return static_cast<uint32_t>(dex_pc);
537#endif
538}
539
540uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
541#if !defined(ART_USE_LLVM_COMPILER)
542 const uint32_t* mapping_table = GetPcToDexMappingTable();
543 if (mapping_table == NULL) {
544 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
545 return DexFile::kDexNoIndex; // Special no mapping case
546 }
547 size_t mapping_table_length = GetPcToDexMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800548 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700549 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700550 if (mapping_table[i] == sought_offset) {
551 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700552 }
553 }
buzbee8320f382012-09-11 16:29:42 -0700554 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
555 << " in " << PrettyMethod(this);
556 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700557#else
558 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
559 return static_cast<uint32_t>(pc);
560#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700561}
562
Mathieu Chartier66f19252012-09-18 08:57:04 -0700563uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700564 const uint32_t* mapping_table = GetDexToPcMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700565 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700566 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700567 return 0; // Special no mapping/pc == 0 case
568 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700569 size_t mapping_table_length = GetDexToPcMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700570 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700571 uint32_t map_offset = mapping_table[i];
572 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700573 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800574 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700575 }
576 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700577 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
578 << " in " << PrettyMethod(this);
Ian Rogersbdb03912011-09-14 00:55:44 -0700579 return 0;
580}
581
Mathieu Chartier66f19252012-09-18 08:57:04 -0700582uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800583 MethodHelper mh(this);
584 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700585 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700586 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
587 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700588 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700589 if (iter_type_idx == DexFile::kDexNoIndex16) {
590 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700591 }
592 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800593 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700594 if (iter_exception_type == NULL) {
595 // The verifier should take care of resolving all exception classes early
596 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800597 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700598 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700599 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700600 }
601 }
602 // Handler not found
603 return DexFile::kDexNoIndex;
604}
605
Mathieu Chartier66f19252012-09-18 08:57:04 -0700606void AbstractMethod::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 if (kIsDebugBuild) {
608 self->AssertThreadSuspensionIsAllowable();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609 CHECK_EQ(kRunnable, self->GetState());
610 }
TDYa12785321912012-04-01 15:24:56 -0700611
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700612 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700613 ManagedStack fragment;
614 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700615
616 // Call the invoke stub associated with the method.
617 // Pass everything as arguments.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700618 AbstractMethod::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700619
620 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700621
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500622 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700623 bool log = false;
624 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800625 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
626 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700627 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700628 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700629 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800630 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
631 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700632 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700633 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800634 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
635 PrettyMethod(this).c_str(), GetCode(), stub,
636 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700637 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700638 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700639 }
640 }
641
642 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700643 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700644}
645
Mathieu Chartier66f19252012-09-18 08:57:04 -0700646bool AbstractMethod::IsRegistered() const {
647 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800648 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800649 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700650 return native_method != jni_stub;
651}
652
Mathieu Chartier66f19252012-09-18 08:57:04 -0700653void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800654 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700655 CHECK(IsNative()) << PrettyMethod(this);
656 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700657#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700658 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700659 native_method, false);
660#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800661 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700662 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800663 native_method, false);
664 } else {
665 // We've been asked to associate this method with the given native method but are working
666 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
667 // the native method to runtime support and store the target somewhere runtime support will
668 // find it.
669#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700670 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800671 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
672#else
673 UNIMPLEMENTED(FATAL);
674#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700675 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800676 reinterpret_cast<const uint8_t*>(native_method), false);
677 }
TDYa12726467572012-04-17 20:51:22 -0700678#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700679}
680
Mathieu Chartier66f19252012-09-18 08:57:04 -0700681void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700682 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700683 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800684 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700685}
686
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700687void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700688 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
689 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700690 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700691 if (new_status > kStatusResolved) {
692 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
693 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800694 if (new_status == kStatusError) {
695 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
696
697 // stash current exception
698 Thread* self = Thread::Current();
699 SirtRef<Throwable> exception(self->GetException());
700 CHECK(exception.get() != NULL);
701
702 // clear exception to call FindSystemClass
703 self->ClearException();
704 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
705 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
706 CHECK(!self->IsExceptionPending());
707
708 // only verification errors, not initialization problems, should set a verify error.
709 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
710 Class* exception_class = exception->GetClass();
711 if (!eiie_class->IsAssignableFrom(exception_class)) {
712 SetVerifyErrorClass(exception_class);
713 }
714
715 // restore exception
716 self->SetException(exception.get());
717 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700718 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700719}
720
721DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700722 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700723}
724
725void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700726 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700727}
728
Brian Carlstrom1f870082011-08-23 16:02:11 -0700729Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700730 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700731 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500732 // TODO: decide whether we want this check. It currently fails during bootstrap.
733 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700734 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800735 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700736}
737
Ian Rogers0571d352011-11-03 19:51:38 -0700738void Class::SetClassSize(size_t new_class_size) {
739 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
740 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
741}
742
Ian Rogersd418eda2012-01-30 12:14:28 -0800743// Return the class' name. The exact format is bizarre, but it's the specified behavior for
744// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
745// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
746// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
747String* Class::ComputeName() {
748 String* name = GetName();
749 if (name != NULL) {
750 return name;
751 }
752 std::string descriptor(ClassHelper(this).GetDescriptor());
753 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
754 // The descriptor indicates that this is the class for
755 // a primitive type; special-case the return value.
756 const char* c_name = NULL;
757 switch (descriptor[0]) {
758 case 'Z': c_name = "boolean"; break;
759 case 'B': c_name = "byte"; break;
760 case 'C': c_name = "char"; break;
761 case 'S': c_name = "short"; break;
762 case 'I': c_name = "int"; break;
763 case 'J': c_name = "long"; break;
764 case 'F': c_name = "float"; break;
765 case 'D': c_name = "double"; break;
766 case 'V': c_name = "void"; break;
767 default:
768 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
769 }
770 name = String::AllocFromModifiedUtf8(c_name);
771 } else {
772 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
773 // components.
774 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
775 descriptor.erase(0, 1);
776 descriptor.erase(descriptor.size() - 1);
777 }
778 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
779 name = String::AllocFromModifiedUtf8(descriptor.c_str());
780 }
781 SetName(name);
782 return name;
783}
784
Elliott Hughes4681c802011-09-25 18:04:37 -0700785void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700786 if ((flags & kDumpClassFullDetail) == 0) {
787 os << PrettyClass(this);
788 if ((flags & kDumpClassClassLoader) != 0) {
789 os << ' ' << GetClassLoader();
790 }
791 if ((flags & kDumpClassInitialized) != 0) {
792 os << ' ' << GetStatus();
793 }
Elliott Hughese0918552011-10-28 17:18:29 -0700794 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700795 return;
796 }
797
798 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800799 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700800 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800801 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700802 os << " objectSize=" << SizeOf() << " "
803 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
804 os << StringPrintf(" access=0x%04x.%04x\n",
805 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
806 if (super != NULL) {
807 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
808 }
809 if (IsArrayClass()) {
810 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
811 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700812 if (kh.NumDirectInterfaces() > 0) {
813 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
814 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
815 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700816 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800817 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700818 }
819 }
820 os << " vtable (" << NumVirtualMethods() << " entries, "
821 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
822 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800823 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700824 }
825 os << " direct methods (" << NumDirectMethods() << " entries):\n";
826 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800827 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700828 }
829 if (NumStaticFields() > 0) {
830 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700831 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700832 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800833 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700834 }
835 } else {
836 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700837 }
838 }
839 if (NumInstanceFields() > 0) {
840 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700841 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700842 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800843 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700844 }
845 } else {
846 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700847 }
848 }
849}
850
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700851void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
852 if (new_reference_offsets != CLASS_WALK_SUPER) {
853 // Sanity check that the number of bits set in the reference offset bitmap
854 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800855 size_t count = 0;
856 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
857 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700858 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800859 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700860 }
861 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
862 new_reference_offsets, false);
863}
864
865void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
866 if (new_reference_offsets != CLASS_WALK_SUPER) {
867 // Sanity check that the number of bits set in the reference offset bitmap
868 // agrees with the number of references
869 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
870 NumReferenceStaticFieldsDuringLinking());
871 }
872 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
873 new_reference_offsets, false);
874}
875
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700876bool Class::Implements(const Class* klass) const {
877 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700878 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700879 // All interfaces implemented directly and by our superclass, and
880 // recursively all super-interfaces of those interfaces, are listed
881 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700882 int32_t iftable_count = GetIfTableCount();
883 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
884 for (int32_t i = 0; i < iftable_count; i++) {
885 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700886 return true;
887 }
888 }
889 return false;
890}
891
Elliott Hughese84278b2012-03-22 10:06:53 -0700892// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700893// are array classes.
894//
895// Consider an array class, e.g. Y[][], where Y is a subclass of X.
896// Y[][] = Y[][] --> true (identity)
897// X[][] = Y[][] --> true (element superclass)
898// Y = Y[][] --> false
899// Y[] = Y[][] --> false
900// Object = Y[][] --> true (everything is an object)
901// Object[] = Y[][] --> true
902// Object[][] = Y[][] --> true
903// Object[][][] = Y[][] --> false (too many []s)
904// Serializable = Y[][] --> true (all arrays are Serializable)
905// Serializable[] = Y[][] --> true
906// Serializable[][] = Y[][] --> false (unless Y is Serializable)
907//
908// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700909// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700910//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700911bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700912 DCHECK(IsArrayClass()) << PrettyClass(this);
913 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700914 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700915}
916
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700917bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700918 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
919 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700920 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700921 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700922 // src's super should be java_lang_Object, since it is an array.
923 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700924 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
925 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700926 return this == java_lang_Object;
927 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700928 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700929}
930
931bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700932 DCHECK(!IsInterface()) << PrettyClass(this);
933 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700934 const Class* current = this;
935 do {
936 if (current == klass) {
937 return true;
938 }
939 current = current->GetSuperClass();
940 } while (current != NULL);
941 return false;
942}
943
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800944bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700945 size_t i = 0;
946 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
947 ++i;
948 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700949 if (descriptor1.find('/', i) != StringPiece::npos ||
950 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700951 return false;
952 } else {
953 return true;
954 }
955}
956
957bool Class::IsInSamePackage(const Class* that) const {
958 const Class* klass1 = this;
959 const Class* klass2 = that;
960 if (klass1 == klass2) {
961 return true;
962 }
963 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700964 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700965 return false;
966 }
967 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700968 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700969 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700970 }
jeffhao4a801a42011-09-23 13:53:40 -0700971 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700972 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700973 }
974 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800975 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800976 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800977 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800978 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800979 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700980}
981
Elliott Hughesdbb40792011-11-18 17:05:22 -0800982bool Class::IsClassClass() const {
983 Class* java_lang_Class = GetClass()->GetClass();
984 return this == java_lang_Class;
985}
986
987bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800988 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800989}
990
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800991bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700992 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800993}
994
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800995ClassLoader* Class::GetClassLoader() const {
996 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700997}
998
Ian Rogers365c1022012-06-22 15:05:28 -0700999void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001001}
1002
Mathieu Chartier66f19252012-09-18 08:57:04 -07001003AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001004 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001005 DCHECK(declaring_class != NULL) << PrettyClass(this);
1006 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001007 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001008 int32_t iftable_count = GetIfTableCount();
1009 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1010 for (int32_t i = 0; i < iftable_count; i++) {
1011 InterfaceEntry* interface_entry = iftable->Get(i);
1012 if (interface_entry->GetInterface() == declaring_class) {
1013 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001014 }
1015 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001016 return NULL;
1017}
1018
Mathieu Chartier66f19252012-09-18 08:57:04 -07001019AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001020 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001021 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001022 if (method != NULL) {
1023 return method;
1024 }
1025
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001026 int32_t iftable_count = GetIfTableCount();
1027 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1028 for (int32_t i = 0; i < iftable_count; i++) {
1029 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001030 if (method != NULL) {
1031 return method;
1032 }
1033 }
1034 return NULL;
1035}
1036
Mathieu Chartier66f19252012-09-18 08:57:04 -07001037AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001038 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001039 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001040 if (method != NULL) {
1041 return method;
1042 }
1043
1044 int32_t iftable_count = GetIfTableCount();
1045 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1046 for (int32_t i = 0; i < iftable_count; i++) {
1047 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
1048 if (method != NULL) {
1049 return method;
1050 }
1051 }
1052 return NULL;
1053}
1054
1055
Mathieu Chartier66f19252012-09-18 08:57:04 -07001056AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001057 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001058 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001059 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001060 mh.ChangeMethod(method);
1061 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001062 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001063 }
1064 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001065 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001066}
1067
Mathieu Chartier66f19252012-09-18 08:57:04 -07001068AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001069 if (GetDexCache() == dex_cache) {
1070 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001071 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001072 if (method->GetDexMethodIndex() == dex_method_idx) {
1073 return method;
1074 }
1075 }
1076 }
1077 return NULL;
1078}
1079
Mathieu Chartier66f19252012-09-18 08:57:04 -07001080AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001081 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001082 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001083 if (method != NULL) {
1084 return method;
1085 }
1086 }
1087 return NULL;
1088}
1089
Mathieu Chartier66f19252012-09-18 08:57:04 -07001090AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001091 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001092 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001093 if (method != NULL) {
1094 return method;
1095 }
1096 }
1097 return NULL;
1098}
1099
Mathieu Chartier66f19252012-09-18 08:57:04 -07001100AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001101 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001102 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001103 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001104 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001105 mh.ChangeMethod(method);
1106 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001107 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001108 }
1109 }
1110 return NULL;
1111}
1112
Mathieu Chartier66f19252012-09-18 08:57:04 -07001113AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001114 if (GetDexCache() == dex_cache) {
1115 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001116 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001117 if (method->GetDexMethodIndex() == dex_method_idx) {
1118 return method;
1119 }
1120 }
1121 }
1122 return NULL;
1123}
1124
Mathieu Chartier66f19252012-09-18 08:57:04 -07001125AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001126 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001127 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001128 if (method != NULL) {
1129 return method;
1130 }
1131 }
1132 return NULL;
1133}
1134
Mathieu Chartier66f19252012-09-18 08:57:04 -07001135AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001136 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001137 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001138 if (method != NULL) {
1139 return method;
1140 }
1141 }
1142 return NULL;
1143}
1144
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001145Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 // Is the field in this class?
1147 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001148 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1150 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001151 fh.ChangeField(f);
1152 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001153 return f;
1154 }
1155 }
1156 return NULL;
1157}
1158
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001159Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1160 if (GetDexCache() == dex_cache) {
1161 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1162 Field* f = GetInstanceField(i);
1163 if (f->GetDexFieldIndex() == dex_field_idx) {
1164 return f;
1165 }
1166 }
1167 }
1168 return NULL;
1169}
1170
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001171Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 // Is the field in this class, or any of its superclasses?
1173 // Interfaces are not relevant because they can't contain instance fields.
1174 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001175 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 if (f != NULL) {
1177 return f;
1178 }
1179 }
1180 return NULL;
1181}
1182
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001183Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1184 // Is the field in this class, or any of its superclasses?
1185 // Interfaces are not relevant because they can't contain instance fields.
1186 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1187 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1188 if (f != NULL) {
1189 return f;
1190 }
1191 }
1192 return NULL;
1193}
1194
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001195Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1196 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001197 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001198 for (size_t i = 0; i < NumStaticFields(); ++i) {
1199 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001200 fh.ChangeField(f);
1201 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001202 return f;
1203 }
1204 }
1205 return NULL;
1206}
1207
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001208Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1209 if (dex_cache == GetDexCache()) {
1210 for (size_t i = 0; i < NumStaticFields(); ++i) {
1211 Field* f = GetStaticField(i);
1212 if (f->GetDexFieldIndex() == dex_field_idx) {
1213 return f;
1214 }
1215 }
1216 }
1217 return NULL;
1218}
1219
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001220Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1221 // Is the field in this class (or its interfaces), or any of its
1222 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001223 ClassHelper kh;
1224 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001225 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001226 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001227 if (f != NULL) {
1228 return f;
1229 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001230 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001231 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001232 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1233 Class* interface = kh.GetDirectInterface(i);
1234 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001235 if (f != NULL) {
1236 return f;
1237 }
1238 }
1239 }
1240 return NULL;
1241}
1242
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001243Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1244 ClassHelper kh;
1245 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1246 // Is the field in this class?
1247 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1248 if (f != NULL) {
1249 return f;
1250 }
1251 // Is this field in any of this class' interfaces?
1252 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001253 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1254 Class* interface = kh.GetDirectInterface(i);
1255 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001256 if (f != NULL) {
1257 return f;
1258 }
1259 }
1260 }
1261 return NULL;
1262}
1263
Ian Rogersb067ac22011-12-13 18:05:09 -08001264Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1265 // Find a field using the JLS field resolution order
1266 ClassHelper kh;
1267 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1268 // Is the field in this class?
1269 Field* f = k->FindDeclaredInstanceField(name, type);
1270 if (f != NULL) {
1271 return f;
1272 }
1273 f = k->FindDeclaredStaticField(name, type);
1274 if (f != NULL) {
1275 return f;
1276 }
1277 // Is this field in any of this class' interfaces?
1278 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001279 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1280 Class* interface = kh.GetDirectInterface(i);
1281 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001282 if (f != NULL) {
1283 return f;
1284 }
1285 }
1286 }
1287 return NULL;
1288}
1289
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001290Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001291 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001292 DCHECK_GE(component_count, 0);
1293 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001294
Ian Rogersa15e67d2012-02-28 13:51:55 -08001295 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001296 size_t data_size = component_count * component_size;
1297 size_t size = header_size + data_size;
1298
1299 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1300 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1301 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1302 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001303 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001304 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001305 return NULL;
1306 }
1307
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001308 Heap* heap = Runtime::Current()->GetHeap();
1309 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001310 if (array != NULL) {
1311 DCHECK(array->IsArrayInstance());
1312 array->SetLength(component_count);
1313 }
1314 return array;
1315}
1316
1317Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001319 return Alloc(array_class, component_count, array_class->GetComponentSize());
1320}
1321
Elliott Hughes80609252011-09-23 17:24:51 -07001322bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001323 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001324 "length=%i; index=%i", length_, index);
1325 return false;
1326}
1327
1328bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001329 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001330 "Can't store an element of type %s into an array of type %s",
1331 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1332 return false;
1333}
1334
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001335template<typename T>
1336PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001337 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001338 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1339 return down_cast<PrimitiveArray<T>*>(raw_array);
1340}
1341
1342template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1343
1344// Explicitly instantiate all the primitive array types.
1345template class PrimitiveArray<uint8_t>; // BooleanArray
1346template class PrimitiveArray<int8_t>; // ByteArray
1347template class PrimitiveArray<uint16_t>; // CharArray
1348template class PrimitiveArray<double>; // DoubleArray
1349template class PrimitiveArray<float>; // FloatArray
1350template class PrimitiveArray<int32_t>; // IntArray
1351template class PrimitiveArray<int64_t>; // LongArray
1352template class PrimitiveArray<int16_t>; // ShortArray
1353
Ian Rogers466bb252011-10-14 03:29:56 -07001354// Explicitly instantiate Class[][]
1355template class ObjectArray<ObjectArray<Class> >;
1356
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001357// TODO: get global references for these
1358Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001359
Brian Carlstroma663ea52011-08-19 23:33:41 -07001360void String::SetClass(Class* java_lang_String) {
1361 CHECK(java_lang_String_ == NULL);
1362 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001363 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001364}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001365
Brian Carlstroma663ea52011-08-19 23:33:41 -07001366void String::ResetClass() {
1367 CHECK(java_lang_String_ != NULL);
1368 java_lang_String_ = NULL;
1369}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001370
Brian Carlstromc74255f2011-09-11 22:47:39 -07001371String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001372 return Runtime::Current()->GetInternTable()->InternWeak(this);
1373}
1374
Brian Carlstrom395520e2011-09-25 19:35:00 -07001375int32_t String::GetHashCode() {
1376 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1377 if (result == 0) {
1378 ComputeHashCode();
1379 }
1380 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1381 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1382 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001383 return result;
1384}
1385
1386int32_t String::GetLength() const {
1387 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1388 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1389 return result;
1390}
1391
1392uint16_t String::CharAt(int32_t index) const {
1393 // TODO: do we need this? Equals is the only caller, and could
1394 // bounds check itself.
1395 if (index < 0 || index >= count_) {
1396 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001397 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001398 "length=%i; index=%i", count_, index);
1399 return 0;
1400 }
1401 return GetCharArray()->Get(index + GetOffset());
1402}
1403
1404String* String::AllocFromUtf16(int32_t utf16_length,
1405 const uint16_t* utf16_data_in,
1406 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001407 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001408 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001409 if (string == NULL) {
1410 return NULL;
1411 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001412 // TODO: use 16-bit wide memset variant
1413 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001414 if (array == NULL) {
1415 return NULL;
1416 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001417 for (int i = 0; i < utf16_length; i++) {
1418 array->Set(i, utf16_data_in[i]);
1419 }
1420 if (hash_code != 0) {
1421 string->SetHashCode(hash_code);
1422 } else {
1423 string->ComputeHashCode();
1424 }
1425 return string;
1426}
1427
1428String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001429 if (utf == NULL) {
1430 return NULL;
1431 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001432 size_t char_count = CountModifiedUtf8Chars(utf);
1433 return AllocFromModifiedUtf8(char_count, utf);
1434}
1435
1436String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1437 const char* utf8_data_in) {
1438 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001439 if (string == NULL) {
1440 return NULL;
1441 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001442 uint16_t* utf16_data_out =
1443 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1444 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1445 string->ComputeHashCode();
1446 return string;
1447}
1448
1449String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001450 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1451 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001452 return NULL;
1453 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001454 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001455}
1456
1457String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001458 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001459 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001460 if (string == NULL) {
1461 return NULL;
1462 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001463 string->SetArray(array);
1464 string->SetCount(array->GetLength());
1465 return string;
1466}
1467
1468bool String::Equals(const String* that) const {
1469 if (this == that) {
1470 // Quick reference equality test
1471 return true;
1472 } else if (that == NULL) {
1473 // Null isn't an instanceof anything
1474 return false;
1475 } else if (this->GetLength() != that->GetLength()) {
1476 // Quick length inequality test
1477 return false;
1478 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001479 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001480 // hash code was already equal
1481 for (int32_t i = 0; i < that->GetLength(); ++i) {
1482 if (this->CharAt(i) != that->CharAt(i)) {
1483 return false;
1484 }
1485 }
1486 return true;
1487 }
1488}
1489
Elliott Hughes5d78d392011-12-13 16:53:05 -08001490bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001491 if (this->GetLength() != that_length) {
1492 return false;
1493 } else {
1494 for (int32_t i = 0; i < that_length; ++i) {
1495 if (this->CharAt(i) != that_chars[that_offset + i]) {
1496 return false;
1497 }
1498 }
1499 return true;
1500 }
1501}
1502
1503bool String::Equals(const char* modified_utf8) const {
1504 for (int32_t i = 0; i < GetLength(); ++i) {
1505 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1506 if (ch == '\0' || ch != CharAt(i)) {
1507 return false;
1508 }
1509 }
1510 return *modified_utf8 == '\0';
1511}
1512
1513bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001514 if (modified_utf8.size() != GetLength()) {
1515 return false;
1516 }
1517 const char* p = modified_utf8.data();
1518 for (int32_t i = 0; i < GetLength(); ++i) {
1519 uint16_t ch = GetUtf16FromUtf8(&p);
1520 if (ch != CharAt(i)) {
1521 return false;
1522 }
1523 }
1524 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001525}
1526
1527// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1528std::string String::ToModifiedUtf8() const {
1529 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001530 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001531 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001532 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1533 return result;
1534}
1535
Ian Rogers1c5eb702012-02-01 09:18:34 -08001536void Throwable::SetCause(Throwable* cause) {
1537 CHECK(cause != NULL);
1538 CHECK(cause != this);
1539 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1540 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1541}
1542
Ian Rogers466bb252011-10-14 03:29:56 -07001543bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001544 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001545 return false;
1546 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001547 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001548}
1549
Ian Rogers9074b992011-10-26 17:41:55 -07001550std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001551 std::string result(PrettyTypeOf(this));
1552 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001553 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001554 if (msg != NULL) {
1555 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001556 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001557 result += "\n";
1558 Object* stack_state = GetStackState();
1559 // check stack state isn't missing or corrupt
1560 if (stack_state != NULL && stack_state->IsObjectArray()) {
1561 // Decode the internal stack trace into the depth and method trace
1562 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1563 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001564 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1565 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001566 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001567 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001568 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001569 uint32_t dex_pc = pc_trace->Get(i);
1570 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001571 const char* source_file = mh.GetDeclaringClassSourceFile();
1572 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1573 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001574 }
Ian Rogers9074b992011-10-26 17:41:55 -07001575 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001576 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001577 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001578 result += "Caused by: ";
1579 result += cause->Dump();
1580 }
Ian Rogers9074b992011-10-26 17:41:55 -07001581 return result;
1582}
1583
Ian Rogers5167c972012-02-03 10:41:20 -08001584
1585Class* Throwable::java_lang_Throwable_ = NULL;
1586
1587void Throwable::SetClass(Class* java_lang_Throwable) {
1588 CHECK(java_lang_Throwable_ == NULL);
1589 CHECK(java_lang_Throwable != NULL);
1590 java_lang_Throwable_ = java_lang_Throwable;
1591}
1592
1593void Throwable::ResetClass() {
1594 CHECK(java_lang_Throwable_ != NULL);
1595 java_lang_Throwable_ = NULL;
1596}
1597
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001598Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1599
1600void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1601 CHECK(java_lang_StackTraceElement_ == NULL);
1602 CHECK(java_lang_StackTraceElement != NULL);
1603 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1604}
1605
1606void StackTraceElement::ResetClass() {
1607 CHECK(java_lang_StackTraceElement_ != NULL);
1608 java_lang_StackTraceElement_ = NULL;
1609}
1610
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001611StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1612 String* method_name,
1613 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001614 int32_t line_number) {
1615 StackTraceElement* trace =
1616 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1617 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1618 const_cast<String*>(declaring_class), false);
1619 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1620 const_cast<String*>(method_name), false);
1621 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1622 const_cast<String*>(file_name), false);
1623 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1624 line_number, false);
1625 return trace;
1626}
1627
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001628} // namespace art