blob: e8381dbf1c5f51a66dc5ac70674a7216ce9b1c48 [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"
Ian Rogers1f539342012-10-03 21:09:42 -070038#include "sirt_ref.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070039#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070040#include "utils.h"
Elliott Hughesa4f94742012-05-29 16:28:38 -070041#include "well_known_classes.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070042
43namespace art {
44
Elliott Hughesdbb40792011-11-18 17:05:22 -080045String* Object::AsString() {
46 DCHECK(GetClass()->IsStringClass());
47 return down_cast<String*>(this);
48}
49
Ian Rogers50b35e22012-10-04 10:09:15 -070050Object* Object::Clone(Thread* self) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070051 Class* c = GetClass();
52 DCHECK(!c->IsClassClass());
53
54 // Object::SizeOf gets the right size even if we're an array.
55 // Using c->AllocObject() here would be wrong.
56 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080057 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -070058 SirtRef<Object> copy(self, heap->AllocObject(self, c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070059 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070060 return NULL;
61 }
62
63 // Copy instance data. We assume memcpy copies by words.
64 // TODO: expose and use move32.
65 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070066 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070067 size_t offset = sizeof(Object);
68 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
69
Mathieu Chartier88c95be2012-09-11 14:06:41 -070070 // Perform write barriers on copied object references.
71 if (c->IsArrayClass()) {
72 if (!c->GetComponentType()->IsPrimitive()) {
73 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
74 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
75 }
76 } else {
77 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
78 size_t num_reference_fields = klass->NumReferenceInstanceFields();
79 for (size_t i = 0; i < num_reference_fields; ++i) {
80 Field* field = klass->GetInstanceField(i);
81 MemberOffset field_offset = field->GetOffset();
82 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
83 heap->WriteBarrierField(copy.get(), field_offset, ref);
84 }
85 }
86 }
87
Elliott Hughes20cde902011-10-04 17:37:27 -070088 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080089 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070090 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070091
Brian Carlstrom40381fb2011-10-19 14:13:40 -070092 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070093}
94
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070095uint32_t Object::GetThinLockId() {
96 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070097}
98
99void Object::MonitorEnter(Thread* thread) {
100 Monitor::MonitorEnter(thread, this);
101}
102
Ian Rogersff1ed472011-09-20 13:46:24 -0700103bool Object::MonitorExit(Thread* thread) {
104 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700105}
106
107void Object::Notify() {
108 Monitor::Notify(Thread::Current(), this);
109}
110
111void Object::NotifyAll() {
112 Monitor::NotifyAll(Thread::Current(), this);
113}
114
115void Object::Wait(int64_t ms, int32_t ns) {
116 Monitor::Wait(Thread::Current(), this, ms, ns, true);
117}
118
Ian Rogers23435d02012-09-24 11:23:12 -0700119#if VERIFY_OBJECT_ENABLED
120void Object::CheckFieldAssignment(MemberOffset field_offset, const Object* new_value) {
121 const Class* c = GetClass();
122 if (Runtime::Current()->GetClassLinker() == NULL ||
123 !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() ||
124 !c->IsResolved()) {
125 return;
126 }
127 for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
128 ObjectArray<Field>* fields = cur->GetIFields();
129 if (fields != NULL) {
130 size_t num_ref_ifields = cur->NumReferenceInstanceFields();
131 for (size_t i = 0; i < num_ref_ifields; ++i) {
132 Field* field = fields->Get(i);
133 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
134 FieldHelper fh(field);
135 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
136 return;
137 }
138 }
139 }
140 }
141 if (c->IsArrayClass()) {
142 // Bounds and assign-ability done in the array setter.
143 return;
144 }
145 if (IsClass()) {
146 ObjectArray<Field>* fields = AsClass()->GetSFields();
147 if (fields != NULL) {
148 size_t num_ref_sfields = AsClass()->NumReferenceStaticFields();
149 for (size_t i = 0; i < num_ref_sfields; ++i) {
150 Field* field = fields->Get(i);
151 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
152 FieldHelper fh(field);
153 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
154 return;
155 }
156 }
157 }
158 }
159 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
160 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
161}
162#endif
163
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700164// TODO: get global references for these
165Class* Field::java_lang_reflect_Field_ = NULL;
166
167void Field::SetClass(Class* java_lang_reflect_Field) {
168 CHECK(java_lang_reflect_Field_ == NULL);
169 CHECK(java_lang_reflect_Field != NULL);
170 java_lang_reflect_Field_ = java_lang_reflect_Field;
171}
172
173void Field::ResetClass() {
174 CHECK(java_lang_reflect_Field_ != NULL);
175 java_lang_reflect_Field_ = NULL;
176}
177
Ian Rogers0571d352011-11-03 19:51:38 -0700178void Field::SetOffset(MemberOffset num_bytes) {
179 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180#if 0 // TODO enable later in boot and under !NDEBUG
181 FieldHelper fh(this);
182 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700183 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
184 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
185 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800186#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700187 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
188}
189
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700191 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 if (IsStatic()) {
193 object = declaring_class_;
194 }
195 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700196}
197
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700199 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 if (IsStatic()) {
201 object = declaring_class_;
202 }
203 object->SetField32(GetOffset(), new_value, IsVolatile());
204}
205
206uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700207 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 if (IsStatic()) {
209 object = declaring_class_;
210 }
211 return object->GetField64(GetOffset(), IsVolatile());
212}
213
214void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700215 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 if (IsStatic()) {
217 object = declaring_class_;
218 }
219 object->SetField64(GetOffset(), new_value, IsVolatile());
220}
221
222Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700223 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224 if (IsStatic()) {
225 object = declaring_class_;
226 }
227 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
228}
229
230void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700231 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700232 if (IsStatic()) {
233 object = declaring_class_;
234 }
235 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
236}
237
238bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800239 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
240 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700241 return Get32(object);
242}
243
244void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800245 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
246 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700247 Set32(object, z);
248}
249
250int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800251 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
252 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700253 return Get32(object);
254}
255
256void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800257 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
258 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259 Set32(object, b);
260}
261
262uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800263 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
264 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265 return Get32(object);
266}
267
268void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800269 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
270 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271 Set32(object, c);
272}
273
Ian Rogers466bb252011-10-14 03:29:56 -0700274int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
276 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 return Get32(object);
278}
279
Ian Rogers466bb252011-10-14 03:29:56 -0700280void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
282 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 Set32(object, s);
284}
285
286int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800287 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
288 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 return Get32(object);
290}
291
292void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800293 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
294 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700295 Set32(object, i);
296}
297
298int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800299 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
300 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 return Get64(object);
302}
303
304void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
306 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700307 Set64(object, j);
308}
309
Elliott Hughes1d878f32012-04-11 15:17:54 -0700310union Bits {
311 jdouble d;
312 jfloat f;
313 jint i;
314 jlong j;
315};
316
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700317float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800318 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
319 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700320 Bits bits;
321 bits.i = Get32(object);
322 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323}
324
325void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800326 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
327 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700328 Bits bits;
329 bits.f = f;
330 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331}
332
333double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800334 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
335 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700336 Bits bits;
337 bits.j = Get64(object);
338 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700339}
340
341void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800342 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
343 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700344 Bits bits;
345 bits.d = d;
346 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347}
348
349Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800350 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
351 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352 return GetObj(object);
353}
354
355void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800356 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
357 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358 SetObj(object, l);
359}
360
361// TODO: get global references for these
Mathieu Chartier66f19252012-09-18 08:57:04 -0700362Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
363Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700364
Mathieu Chartier66f19252012-09-18 08:57:04 -0700365InvokeType AbstractMethod::GetInvokeType() const {
Ian Rogers08f753d2012-08-24 14:35:25 -0700366 // TODO: kSuper?
367 if (GetDeclaringClass()->IsInterface()) {
368 return kInterface;
369 } else if (IsStatic()) {
370 return kStatic;
371 } else if (IsDirect()) {
372 return kDirect;
373 } else {
374 return kVirtual;
375 }
376}
377
Mathieu Chartier66f19252012-09-18 08:57:04 -0700378void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
Elliott Hughes80609252011-09-23 17:24:51 -0700379 CHECK(java_lang_reflect_Constructor_ == NULL);
380 CHECK(java_lang_reflect_Constructor != NULL);
381 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
382
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 CHECK(java_lang_reflect_Method_ == NULL);
384 CHECK(java_lang_reflect_Method != NULL);
385 java_lang_reflect_Method_ = java_lang_reflect_Method;
386}
387
Mathieu Chartier66f19252012-09-18 08:57:04 -0700388void AbstractMethod::ResetClasses() {
Elliott Hughes80609252011-09-23 17:24:51 -0700389 CHECK(java_lang_reflect_Constructor_ != NULL);
390 java_lang_reflect_Constructor_ = NULL;
391
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 CHECK(java_lang_reflect_Method_ != NULL);
393 java_lang_reflect_Method_ = NULL;
394}
395
Mathieu Chartier66f19252012-09-18 08:57:04 -0700396ObjectArray<String>* AbstractMethod::GetDexCacheStrings() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700397 return GetFieldObject<ObjectArray<String>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700398 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700399}
400
Mathieu Chartier66f19252012-09-18 08:57:04 -0700401void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
402 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700403 new_dex_cache_strings, false);
404}
405
Mathieu Chartier66f19252012-09-18 08:57:04 -0700406ObjectArray<AbstractMethod>* AbstractMethod::GetDexCacheResolvedMethods() const {
407 return GetFieldObject<ObjectArray<AbstractMethod>*>(
408 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800409}
410
Mathieu Chartier66f19252012-09-18 08:57:04 -0700411void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
412 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
Ian Rogers19846512012-02-24 11:42:47 -0800413 new_dex_cache_methods, false);
414}
415
Mathieu Chartier66f19252012-09-18 08:57:04 -0700416ObjectArray<Class>* AbstractMethod::GetDexCacheResolvedTypes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417 return GetFieldObject<ObjectArray<Class>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700418 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700419}
420
Mathieu Chartier66f19252012-09-18 08:57:04 -0700421void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
422 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 new_dex_cache_classes, false);
424}
425
Mathieu Chartier66f19252012-09-18 08:57:04 -0700426ObjectArray<StaticStorageBase>* AbstractMethod::GetDexCacheInitializedStaticStorage() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700427 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700428 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700429 false);
430}
431
Mathieu Chartier66f19252012-09-18 08:57:04 -0700432void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
433 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700434 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700435}
436
Mathieu Chartier66f19252012-09-18 08:57:04 -0700437size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700438 CHECK_LE(1, shorty.length());
439 uint32_t num_registers = 0;
440 for (int i = 1; i < shorty.length(); ++i) {
441 char ch = shorty[i];
442 if (ch == 'D' || ch == 'J') {
443 num_registers += 2;
444 } else {
445 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700446 }
447 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700448 return num_registers;
449}
450
Mathieu Chartier66f19252012-09-18 08:57:04 -0700451bool AbstractMethod::IsProxyMethod() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800452 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700453}
454
Mathieu Chartier66f19252012-09-18 08:57:04 -0700455AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
Ian Rogers466bb252011-10-14 03:29:56 -0700456 if (IsStatic()) {
457 return NULL;
458 }
459 Class* declaring_class = GetDeclaringClass();
460 Class* super_class = declaring_class->GetSuperClass();
461 uint16_t method_index = GetMethodIndex();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700462 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
463 AbstractMethod* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800464 // Did this method override a super class method? If so load the result from the super class'
465 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700466 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
467 result = super_class_vtable->Get(method_index);
468 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800469 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800470 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800471 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
472 CHECK_EQ(result,
473 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800474 } else {
475 MethodHelper mh(this);
476 MethodHelper interface_mh;
477 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
478 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
479 InterfaceEntry* entry = iftable->Get(i);
480 Class* interface = entry->GetInterface();
481 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700482 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers16f93672012-02-14 12:29:06 -0800483 interface_mh.ChangeMethod(interface_method);
484 if (mh.HasSameNameAndSignature(&interface_mh)) {
485 result = interface_method;
486 break;
487 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800488 }
489 }
Ian Rogers466bb252011-10-14 03:29:56 -0700490 }
491 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800492#ifndef NDEBUG
493 MethodHelper result_mh(result);
494 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
495#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700496 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700497}
498
Mathieu Chartier66f19252012-09-18 08:57:04 -0700499static const void* GetOatCode(const AbstractMethod* m)
500 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800501 Runtime* runtime = Runtime::Current();
502 const void* code = m->GetCode();
503 // Peel off any method tracing trampoline.
504 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
505 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
506 }
507 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800508 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800509 code = runtime->GetClassLinker()->GetOatCodeFor(m);
510 }
511 return code;
512}
513
Mathieu Chartier66f19252012-09-18 08:57:04 -0700514uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700515 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
516}
517
Bill Buzbeea5b30242012-09-28 07:19:44 -0700518// Find the lowest-address native safepoint pc for a given dex pc
Ian Rogers34065b52012-09-28 16:31:26 -0700519uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700520#if !defined(ART_USE_LLVM_COMPILER)
Bill Buzbeea5b30242012-09-28 07:19:44 -0700521 const uint32_t* mapping_table = GetPcToDexMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700522 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800523 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700524 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700525 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700526 size_t mapping_table_length = GetPcToDexMappingTableLength();
527 for (size_t i = 0; i < mapping_table_length; i += 2) {
528 if (mapping_table[i + 1] == dex_pc) {
529 return mapping_table[i] + reinterpret_cast<uintptr_t>(GetOatCode(this));
530 }
531 }
532 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
533 << " in " << PrettyMethod(this);
534 return 0;
535#else
536 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
537 return static_cast<uint32_t>(dex_pc);
538#endif
539}
540
541uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
542#if !defined(ART_USE_LLVM_COMPILER)
543 const uint32_t* mapping_table = GetPcToDexMappingTable();
544 if (mapping_table == NULL) {
545 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
546 return DexFile::kDexNoIndex; // Special no mapping case
547 }
548 size_t mapping_table_length = GetPcToDexMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800549 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700550 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700551 if (mapping_table[i] == sought_offset) {
552 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700553 }
554 }
buzbee8320f382012-09-11 16:29:42 -0700555 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
556 << " in " << PrettyMethod(this);
557 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700558#else
559 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
560 return static_cast<uint32_t>(pc);
561#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700562}
563
Mathieu Chartier66f19252012-09-18 08:57:04 -0700564uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700565 const uint32_t* mapping_table = GetDexToPcMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700566 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700567 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700568 return 0; // Special no mapping/pc == 0 case
569 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700570 size_t mapping_table_length = GetDexToPcMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700571 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700572 uint32_t map_offset = mapping_table[i];
573 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700574 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800575 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700576 }
577 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700578 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
579 << " in " << PrettyMethod(this);
Ian Rogersbdb03912011-09-14 00:55:44 -0700580 return 0;
581}
582
Mathieu Chartier66f19252012-09-18 08:57:04 -0700583uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800584 MethodHelper mh(this);
585 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700586 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700587 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
588 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700589 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700590 if (iter_type_idx == DexFile::kDexNoIndex16) {
591 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700592 }
593 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800594 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700595 if (iter_exception_type == NULL) {
596 // The verifier should take care of resolving all exception classes early
597 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800598 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700599 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700600 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700601 }
602 }
603 // Handler not found
604 return DexFile::kDexNoIndex;
605}
606
Mathieu Chartier66f19252012-09-18 08:57:04 -0700607void AbstractMethod::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700608 if (kIsDebugBuild) {
609 self->AssertThreadSuspensionIsAllowable();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700610 CHECK_EQ(kRunnable, self->GetState());
611 }
TDYa12785321912012-04-01 15:24:56 -0700612
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700614 ManagedStack fragment;
615 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700616
617 // Call the invoke stub associated with the method.
618 // Pass everything as arguments.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700619 AbstractMethod::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700620
621 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700622
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500623 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700624 bool log = false;
625 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800626 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
627 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700628 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700629 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700630 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800631 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
632 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700633 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700634 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800635 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
636 PrettyMethod(this).c_str(), GetCode(), stub,
637 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700638 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700639 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700640 }
641 }
642
643 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700644 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700645}
646
Mathieu Chartier66f19252012-09-18 08:57:04 -0700647bool AbstractMethod::IsRegistered() const {
648 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800649 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800650 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700651 return native_method != jni_stub;
652}
653
Mathieu Chartier66f19252012-09-18 08:57:04 -0700654void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800655 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700656 CHECK(IsNative()) << PrettyMethod(this);
657 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700658#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700659 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700660 native_method, false);
661#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800662 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700663 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800664 native_method, false);
665 } else {
666 // We've been asked to associate this method with the given native method but are working
667 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
668 // the native method to runtime support and store the target somewhere runtime support will
669 // find it.
670#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700671 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800672 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
673#else
674 UNIMPLEMENTED(FATAL);
675#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700676 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800677 reinterpret_cast<const uint8_t*>(native_method), false);
678 }
TDYa12726467572012-04-17 20:51:22 -0700679#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700680}
681
Mathieu Chartier66f19252012-09-18 08:57:04 -0700682void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700683 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700684 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800685 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700686}
687
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700688void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700689 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
690 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700691 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700692 if (new_status > kStatusResolved) {
693 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
694 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800695 if (new_status == kStatusError) {
696 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
697
698 // stash current exception
699 Thread* self = Thread::Current();
Ian Rogers1f539342012-10-03 21:09:42 -0700700 SirtRef<Throwable> exception(self, self->GetException());
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800701 CHECK(exception.get() != NULL);
702
703 // clear exception to call FindSystemClass
704 self->ClearException();
705 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
706 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
707 CHECK(!self->IsExceptionPending());
708
709 // only verification errors, not initialization problems, should set a verify error.
710 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
711 Class* exception_class = exception->GetClass();
712 if (!eiie_class->IsAssignableFrom(exception_class)) {
713 SetVerifyErrorClass(exception_class);
714 }
715
716 // restore exception
717 self->SetException(exception.get());
718 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700719 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700720}
721
722DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700723 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700724}
725
726void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700727 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700728}
729
Ian Rogers50b35e22012-10-04 10:09:15 -0700730Object* Class::AllocObject(Thread* self) {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700731 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700732 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500733 // TODO: decide whether we want this check. It currently fails during bootstrap.
734 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700735 DCHECK_GE(this->object_size_, sizeof(Object));
Ian Rogers50b35e22012-10-04 10:09:15 -0700736 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700737}
738
Ian Rogers0571d352011-11-03 19:51:38 -0700739void Class::SetClassSize(size_t new_class_size) {
740 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
741 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
742}
743
Ian Rogersd418eda2012-01-30 12:14:28 -0800744// Return the class' name. The exact format is bizarre, but it's the specified behavior for
745// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
746// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
747// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
748String* Class::ComputeName() {
749 String* name = GetName();
750 if (name != NULL) {
751 return name;
752 }
753 std::string descriptor(ClassHelper(this).GetDescriptor());
754 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
755 // The descriptor indicates that this is the class for
756 // a primitive type; special-case the return value.
757 const char* c_name = NULL;
758 switch (descriptor[0]) {
759 case 'Z': c_name = "boolean"; break;
760 case 'B': c_name = "byte"; break;
761 case 'C': c_name = "char"; break;
762 case 'S': c_name = "short"; break;
763 case 'I': c_name = "int"; break;
764 case 'J': c_name = "long"; break;
765 case 'F': c_name = "float"; break;
766 case 'D': c_name = "double"; break;
767 case 'V': c_name = "void"; break;
768 default:
769 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
770 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700771 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
Ian Rogersd418eda2012-01-30 12:14:28 -0800772 } else {
773 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
774 // components.
775 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
776 descriptor.erase(0, 1);
777 descriptor.erase(descriptor.size() - 1);
778 }
779 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Ian Rogers50b35e22012-10-04 10:09:15 -0700780 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
Ian Rogersd418eda2012-01-30 12:14:28 -0800781 }
782 SetName(name);
783 return name;
784}
785
Elliott Hughes4681c802011-09-25 18:04:37 -0700786void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700787 if ((flags & kDumpClassFullDetail) == 0) {
788 os << PrettyClass(this);
789 if ((flags & kDumpClassClassLoader) != 0) {
790 os << ' ' << GetClassLoader();
791 }
792 if ((flags & kDumpClassInitialized) != 0) {
793 os << ' ' << GetStatus();
794 }
Elliott Hughese0918552011-10-28 17:18:29 -0700795 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700796 return;
797 }
798
799 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800800 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700801 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800802 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700803 os << " objectSize=" << SizeOf() << " "
804 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
805 os << StringPrintf(" access=0x%04x.%04x\n",
806 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
807 if (super != NULL) {
808 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
809 }
810 if (IsArrayClass()) {
811 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
812 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700813 if (kh.NumDirectInterfaces() > 0) {
814 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
815 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
816 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700817 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800818 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700819 }
820 }
821 os << " vtable (" << NumVirtualMethods() << " entries, "
822 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
823 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800824 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700825 }
826 os << " direct methods (" << NumDirectMethods() << " entries):\n";
827 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800828 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700829 }
830 if (NumStaticFields() > 0) {
831 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700832 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700833 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800834 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700835 }
836 } else {
837 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700838 }
839 }
840 if (NumInstanceFields() > 0) {
841 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700842 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700843 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800844 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700845 }
846 } else {
847 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700848 }
849 }
850}
851
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700852void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
853 if (new_reference_offsets != CLASS_WALK_SUPER) {
854 // Sanity check that the number of bits set in the reference offset bitmap
855 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800856 size_t count = 0;
857 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
858 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700859 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800860 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700861 }
862 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
863 new_reference_offsets, false);
864}
865
866void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
867 if (new_reference_offsets != CLASS_WALK_SUPER) {
868 // Sanity check that the number of bits set in the reference offset bitmap
869 // agrees with the number of references
870 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
871 NumReferenceStaticFieldsDuringLinking());
872 }
873 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
874 new_reference_offsets, false);
875}
876
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700877bool Class::Implements(const Class* klass) const {
878 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700879 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700880 // All interfaces implemented directly and by our superclass, and
881 // recursively all super-interfaces of those interfaces, are listed
882 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700883 int32_t iftable_count = GetIfTableCount();
884 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
885 for (int32_t i = 0; i < iftable_count; i++) {
886 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700887 return true;
888 }
889 }
890 return false;
891}
892
Elliott Hughese84278b2012-03-22 10:06:53 -0700893// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700894// are array classes.
895//
896// Consider an array class, e.g. Y[][], where Y is a subclass of X.
897// Y[][] = Y[][] --> true (identity)
898// X[][] = Y[][] --> true (element superclass)
899// Y = Y[][] --> false
900// Y[] = Y[][] --> false
901// Object = Y[][] --> true (everything is an object)
902// Object[] = Y[][] --> true
903// Object[][] = Y[][] --> true
904// Object[][][] = Y[][] --> false (too many []s)
905// Serializable = Y[][] --> true (all arrays are Serializable)
906// Serializable[] = Y[][] --> true
907// Serializable[][] = Y[][] --> false (unless Y is Serializable)
908//
909// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700910// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700911//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700912bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700913 DCHECK(IsArrayClass()) << PrettyClass(this);
914 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700915 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700916}
917
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700918bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700919 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
920 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700921 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700922 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700923 // src's super should be java_lang_Object, since it is an array.
924 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700925 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
926 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700927 return this == java_lang_Object;
928 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700929 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700930}
931
932bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700933 DCHECK(!IsInterface()) << PrettyClass(this);
934 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700935 const Class* current = this;
936 do {
937 if (current == klass) {
938 return true;
939 }
940 current = current->GetSuperClass();
941 } while (current != NULL);
942 return false;
943}
944
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800945bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700946 size_t i = 0;
947 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
948 ++i;
949 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700950 if (descriptor1.find('/', i) != StringPiece::npos ||
951 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700952 return false;
953 } else {
954 return true;
955 }
956}
957
958bool Class::IsInSamePackage(const Class* that) const {
959 const Class* klass1 = this;
960 const Class* klass2 = that;
961 if (klass1 == klass2) {
962 return true;
963 }
964 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700965 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700966 return false;
967 }
968 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700969 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700970 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700971 }
jeffhao4a801a42011-09-23 13:53:40 -0700972 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700973 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700974 }
975 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800976 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800977 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800978 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800979 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800980 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700981}
982
Elliott Hughesdbb40792011-11-18 17:05:22 -0800983bool Class::IsClassClass() const {
984 Class* java_lang_Class = GetClass()->GetClass();
985 return this == java_lang_Class;
986}
987
988bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800989 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800990}
991
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800992bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700993 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800994}
995
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800996ClassLoader* Class::GetClassLoader() const {
997 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700998}
999
Ian Rogers365c1022012-06-22 15:05:28 -07001000void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001002}
1003
Mathieu Chartier66f19252012-09-18 08:57:04 -07001004AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001005 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001006 DCHECK(declaring_class != NULL) << PrettyClass(this);
1007 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001008 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001009 int32_t iftable_count = GetIfTableCount();
1010 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1011 for (int32_t i = 0; i < iftable_count; i++) {
1012 InterfaceEntry* interface_entry = iftable->Get(i);
1013 if (interface_entry->GetInterface() == declaring_class) {
1014 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001015 }
1016 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001017 return NULL;
1018}
1019
Mathieu Chartier66f19252012-09-18 08:57:04 -07001020AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001021 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001022 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001023 if (method != NULL) {
1024 return method;
1025 }
1026
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001027 int32_t iftable_count = GetIfTableCount();
1028 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1029 for (int32_t i = 0; i < iftable_count; i++) {
1030 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001031 if (method != NULL) {
1032 return method;
1033 }
1034 }
1035 return NULL;
1036}
1037
Mathieu Chartier66f19252012-09-18 08:57:04 -07001038AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001039 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001040 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001041 if (method != NULL) {
1042 return method;
1043 }
1044
1045 int32_t iftable_count = GetIfTableCount();
1046 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1047 for (int32_t i = 0; i < iftable_count; i++) {
1048 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
1049 if (method != NULL) {
1050 return method;
1051 }
1052 }
1053 return NULL;
1054}
1055
1056
Mathieu Chartier66f19252012-09-18 08:57:04 -07001057AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001058 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001059 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001060 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001061 mh.ChangeMethod(method);
1062 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001063 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001064 }
1065 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001066 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001067}
1068
Mathieu Chartier66f19252012-09-18 08:57:04 -07001069AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001070 if (GetDexCache() == dex_cache) {
1071 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001072 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001073 if (method->GetDexMethodIndex() == dex_method_idx) {
1074 return method;
1075 }
1076 }
1077 }
1078 return NULL;
1079}
1080
Mathieu Chartier66f19252012-09-18 08:57:04 -07001081AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001082 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001083 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001084 if (method != NULL) {
1085 return method;
1086 }
1087 }
1088 return NULL;
1089}
1090
Mathieu Chartier66f19252012-09-18 08:57:04 -07001091AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001092 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001093 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001094 if (method != NULL) {
1095 return method;
1096 }
1097 }
1098 return NULL;
1099}
1100
Mathieu Chartier66f19252012-09-18 08:57:04 -07001101AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001102 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001103 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001104 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001105 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001106 mh.ChangeMethod(method);
1107 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001108 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001109 }
1110 }
1111 return NULL;
1112}
1113
Mathieu Chartier66f19252012-09-18 08:57:04 -07001114AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001115 if (GetDexCache() == dex_cache) {
1116 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001117 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001118 if (method->GetDexMethodIndex() == dex_method_idx) {
1119 return method;
1120 }
1121 }
1122 }
1123 return NULL;
1124}
1125
Mathieu Chartier66f19252012-09-18 08:57:04 -07001126AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001127 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001128 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001129 if (method != NULL) {
1130 return method;
1131 }
1132 }
1133 return NULL;
1134}
1135
Mathieu Chartier66f19252012-09-18 08:57:04 -07001136AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001137 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001138 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001139 if (method != NULL) {
1140 return method;
1141 }
1142 }
1143 return NULL;
1144}
1145
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001146Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 // Is the field in this class?
1148 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001149 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1151 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001152 fh.ChangeField(f);
1153 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001154 return f;
1155 }
1156 }
1157 return NULL;
1158}
1159
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001160Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1161 if (GetDexCache() == dex_cache) {
1162 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1163 Field* f = GetInstanceField(i);
1164 if (f->GetDexFieldIndex() == dex_field_idx) {
1165 return f;
1166 }
1167 }
1168 }
1169 return NULL;
1170}
1171
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001172Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 // Is the field in this class, or any of its superclasses?
1174 // Interfaces are not relevant because they can't contain instance fields.
1175 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001176 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 if (f != NULL) {
1178 return f;
1179 }
1180 }
1181 return NULL;
1182}
1183
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001184Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1185 // Is the field in this class, or any of its superclasses?
1186 // Interfaces are not relevant because they can't contain instance fields.
1187 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1188 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1189 if (f != NULL) {
1190 return f;
1191 }
1192 }
1193 return NULL;
1194}
1195
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001196Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1197 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001198 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001199 for (size_t i = 0; i < NumStaticFields(); ++i) {
1200 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001201 fh.ChangeField(f);
1202 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001203 return f;
1204 }
1205 }
1206 return NULL;
1207}
1208
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001209Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1210 if (dex_cache == GetDexCache()) {
1211 for (size_t i = 0; i < NumStaticFields(); ++i) {
1212 Field* f = GetStaticField(i);
1213 if (f->GetDexFieldIndex() == dex_field_idx) {
1214 return f;
1215 }
1216 }
1217 }
1218 return NULL;
1219}
1220
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001221Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1222 // Is the field in this class (or its interfaces), or any of its
1223 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001224 ClassHelper kh;
1225 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001226 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001227 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001228 if (f != NULL) {
1229 return f;
1230 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001231 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001232 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001233 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1234 Class* interface = kh.GetDirectInterface(i);
1235 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001236 if (f != NULL) {
1237 return f;
1238 }
1239 }
1240 }
1241 return NULL;
1242}
1243
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001244Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1245 ClassHelper kh;
1246 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1247 // Is the field in this class?
1248 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1249 if (f != NULL) {
1250 return f;
1251 }
1252 // Is this field in any of this class' interfaces?
1253 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001254 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1255 Class* interface = kh.GetDirectInterface(i);
1256 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001257 if (f != NULL) {
1258 return f;
1259 }
1260 }
1261 }
1262 return NULL;
1263}
1264
Ian Rogersb067ac22011-12-13 18:05:09 -08001265Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1266 // Find a field using the JLS field resolution order
1267 ClassHelper kh;
1268 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1269 // Is the field in this class?
1270 Field* f = k->FindDeclaredInstanceField(name, type);
1271 if (f != NULL) {
1272 return f;
1273 }
1274 f = k->FindDeclaredStaticField(name, type);
1275 if (f != NULL) {
1276 return f;
1277 }
1278 // Is this field in any of this class' interfaces?
1279 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001280 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1281 Class* interface = kh.GetDirectInterface(i);
1282 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001283 if (f != NULL) {
1284 return f;
1285 }
1286 }
1287 }
1288 return NULL;
1289}
1290
Ian Rogers50b35e22012-10-04 10:09:15 -07001291Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
1292 size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001293 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001294 DCHECK_GE(component_count, 0);
1295 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001296
Ian Rogersa15e67d2012-02-28 13:51:55 -08001297 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001298 size_t data_size = component_count * component_size;
1299 size_t size = header_size + data_size;
1300
1301 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1302 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1303 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001304 self->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001305 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001306 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001307 return NULL;
1308 }
1309
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001310 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001311 Array* array = down_cast<Array*>(heap->AllocObject(self, array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001312 if (array != NULL) {
1313 DCHECK(array->IsArrayInstance());
1314 array->SetLength(component_count);
1315 }
1316 return array;
1317}
1318
Ian Rogers50b35e22012-10-04 10:09:15 -07001319Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 DCHECK(array_class->IsArrayClass());
Ian Rogers50b35e22012-10-04 10:09:15 -07001321 return Alloc(self, array_class, component_count, array_class->GetComponentSize());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001322}
1323
Elliott Hughes80609252011-09-23 17:24:51 -07001324bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001325 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001326 "length=%i; index=%i", length_, index);
1327 return false;
1328}
1329
1330bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001331 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001332 "Can't store an element of type %s into an array of type %s",
1333 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1334 return false;
1335}
1336
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001337template<typename T>
Ian Rogers50b35e22012-10-04 10:09:15 -07001338PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001339 DCHECK(array_class_ != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -07001340 Array* raw_array = Array::Alloc(self, array_class_, length, sizeof(T));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001341 return down_cast<PrimitiveArray<T>*>(raw_array);
1342}
1343
1344template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1345
1346// Explicitly instantiate all the primitive array types.
1347template class PrimitiveArray<uint8_t>; // BooleanArray
1348template class PrimitiveArray<int8_t>; // ByteArray
1349template class PrimitiveArray<uint16_t>; // CharArray
1350template class PrimitiveArray<double>; // DoubleArray
1351template class PrimitiveArray<float>; // FloatArray
1352template class PrimitiveArray<int32_t>; // IntArray
1353template class PrimitiveArray<int64_t>; // LongArray
1354template class PrimitiveArray<int16_t>; // ShortArray
1355
Ian Rogers466bb252011-10-14 03:29:56 -07001356// Explicitly instantiate Class[][]
1357template class ObjectArray<ObjectArray<Class> >;
1358
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001359// TODO: get global references for these
1360Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001361
Brian Carlstroma663ea52011-08-19 23:33:41 -07001362void String::SetClass(Class* java_lang_String) {
1363 CHECK(java_lang_String_ == NULL);
1364 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001365 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001366}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367
Brian Carlstroma663ea52011-08-19 23:33:41 -07001368void String::ResetClass() {
1369 CHECK(java_lang_String_ != NULL);
1370 java_lang_String_ = NULL;
1371}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001372
Brian Carlstromc74255f2011-09-11 22:47:39 -07001373String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001374 return Runtime::Current()->GetInternTable()->InternWeak(this);
1375}
1376
Brian Carlstrom395520e2011-09-25 19:35:00 -07001377int32_t String::GetHashCode() {
1378 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1379 if (result == 0) {
1380 ComputeHashCode();
1381 }
1382 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1383 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1384 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001385 return result;
1386}
1387
1388int32_t String::GetLength() const {
1389 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1390 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1391 return result;
1392}
1393
1394uint16_t String::CharAt(int32_t index) const {
1395 // TODO: do we need this? Equals is the only caller, and could
1396 // bounds check itself.
1397 if (index < 0 || index >= count_) {
1398 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001399 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001400 "length=%i; index=%i", count_, index);
1401 return 0;
1402 }
1403 return GetCharArray()->Get(index + GetOffset());
1404}
1405
Ian Rogers50b35e22012-10-04 10:09:15 -07001406String* String::AllocFromUtf16(Thread* self,
1407 int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001408 const uint16_t* utf16_data_in,
1409 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001410 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers50b35e22012-10-04 10:09:15 -07001411 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001412 if (string == NULL) {
1413 return NULL;
1414 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001415 // TODO: use 16-bit wide memset variant
1416 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001417 if (array == NULL) {
1418 return NULL;
1419 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001420 for (int i = 0; i < utf16_length; i++) {
1421 array->Set(i, utf16_data_in[i]);
1422 }
1423 if (hash_code != 0) {
1424 string->SetHashCode(hash_code);
1425 } else {
1426 string->ComputeHashCode();
1427 }
1428 return string;
1429}
1430
Ian Rogers50b35e22012-10-04 10:09:15 -07001431 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001432 if (utf == NULL) {
1433 return NULL;
1434 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001435 size_t char_count = CountModifiedUtf8Chars(utf);
Ian Rogers50b35e22012-10-04 10:09:15 -07001436 return AllocFromModifiedUtf8(self, char_count, utf);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001437}
1438
Ian Rogers50b35e22012-10-04 10:09:15 -07001439String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001440 const char* utf8_data_in) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001441 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001442 if (string == NULL) {
1443 return NULL;
1444 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001445 uint16_t* utf16_data_out =
1446 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1447 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1448 string->ComputeHashCode();
1449 return string;
1450}
1451
Ian Rogers50b35e22012-10-04 10:09:15 -07001452String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) {
1453 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001454 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001455 return NULL;
1456 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001457 return Alloc(self, java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001458}
1459
Ian Rogers50b35e22012-10-04 10:09:15 -07001460String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) {
Ian Rogers1f539342012-10-03 21:09:42 -07001461 // Hold reference in case AllocObject causes GC.
Ian Rogers50b35e22012-10-04 10:09:15 -07001462 SirtRef<CharArray> array_ref(self, array);
1463 String* string = down_cast<String*>(java_lang_String->AllocObject(self));
Elliott Hughesb51036c2011-10-12 23:49:11 -07001464 if (string == NULL) {
1465 return NULL;
1466 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001467 string->SetArray(array);
1468 string->SetCount(array->GetLength());
1469 return string;
1470}
1471
1472bool String::Equals(const String* that) const {
1473 if (this == that) {
1474 // Quick reference equality test
1475 return true;
1476 } else if (that == NULL) {
1477 // Null isn't an instanceof anything
1478 return false;
1479 } else if (this->GetLength() != that->GetLength()) {
1480 // Quick length inequality test
1481 return false;
1482 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001483 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001484 // hash code was already equal
1485 for (int32_t i = 0; i < that->GetLength(); ++i) {
1486 if (this->CharAt(i) != that->CharAt(i)) {
1487 return false;
1488 }
1489 }
1490 return true;
1491 }
1492}
1493
Elliott Hughes5d78d392011-12-13 16:53:05 -08001494bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001495 if (this->GetLength() != that_length) {
1496 return false;
1497 } else {
1498 for (int32_t i = 0; i < that_length; ++i) {
1499 if (this->CharAt(i) != that_chars[that_offset + i]) {
1500 return false;
1501 }
1502 }
1503 return true;
1504 }
1505}
1506
1507bool String::Equals(const char* modified_utf8) const {
1508 for (int32_t i = 0; i < GetLength(); ++i) {
1509 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1510 if (ch == '\0' || ch != CharAt(i)) {
1511 return false;
1512 }
1513 }
1514 return *modified_utf8 == '\0';
1515}
1516
1517bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001518 if (modified_utf8.size() != GetLength()) {
1519 return false;
1520 }
1521 const char* p = modified_utf8.data();
1522 for (int32_t i = 0; i < GetLength(); ++i) {
1523 uint16_t ch = GetUtf16FromUtf8(&p);
1524 if (ch != CharAt(i)) {
1525 return false;
1526 }
1527 }
1528 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001529}
1530
1531// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1532std::string String::ToModifiedUtf8() const {
1533 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001534 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001535 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001536 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1537 return result;
1538}
1539
Ian Rogers1c5eb702012-02-01 09:18:34 -08001540void Throwable::SetCause(Throwable* cause) {
1541 CHECK(cause != NULL);
1542 CHECK(cause != this);
1543 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1544 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1545}
1546
Ian Rogers466bb252011-10-14 03:29:56 -07001547bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001548 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001549 return false;
1550 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001551 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001552}
1553
Ian Rogers9074b992011-10-26 17:41:55 -07001554std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001555 std::string result(PrettyTypeOf(this));
1556 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001557 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001558 if (msg != NULL) {
1559 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001560 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001561 result += "\n";
1562 Object* stack_state = GetStackState();
1563 // check stack state isn't missing or corrupt
1564 if (stack_state != NULL && stack_state->IsObjectArray()) {
1565 // Decode the internal stack trace into the depth and method trace
1566 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1567 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001568 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1569 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001570 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001571 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001572 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001573 uint32_t dex_pc = pc_trace->Get(i);
1574 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001575 const char* source_file = mh.GetDeclaringClassSourceFile();
1576 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1577 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001578 }
Ian Rogers9074b992011-10-26 17:41:55 -07001579 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001580 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001581 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001582 result += "Caused by: ";
1583 result += cause->Dump();
1584 }
Ian Rogers9074b992011-10-26 17:41:55 -07001585 return result;
1586}
1587
Ian Rogers5167c972012-02-03 10:41:20 -08001588
1589Class* Throwable::java_lang_Throwable_ = NULL;
1590
1591void Throwable::SetClass(Class* java_lang_Throwable) {
1592 CHECK(java_lang_Throwable_ == NULL);
1593 CHECK(java_lang_Throwable != NULL);
1594 java_lang_Throwable_ = java_lang_Throwable;
1595}
1596
1597void Throwable::ResetClass() {
1598 CHECK(java_lang_Throwable_ != NULL);
1599 java_lang_Throwable_ = NULL;
1600}
1601
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001602Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1603
1604void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1605 CHECK(java_lang_StackTraceElement_ == NULL);
1606 CHECK(java_lang_StackTraceElement != NULL);
1607 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1608}
1609
1610void StackTraceElement::ResetClass() {
1611 CHECK(java_lang_StackTraceElement_ != NULL);
1612 java_lang_StackTraceElement_ = NULL;
1613}
1614
Ian Rogers50b35e22012-10-04 10:09:15 -07001615StackTraceElement* StackTraceElement::Alloc(Thread* self,
1616 String* declaring_class,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001617 String* method_name,
1618 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001619 int32_t line_number) {
1620 StackTraceElement* trace =
Ian Rogers50b35e22012-10-04 10:09:15 -07001621 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject(self));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001622 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1623 const_cast<String*>(declaring_class), false);
1624 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1625 const_cast<String*>(method_name), false);
1626 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1627 const_cast<String*>(file_name), false);
1628 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1629 line_number, false);
1630 return trace;
1631}
1632
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001633} // namespace art