blob: 5fdea71e04770c0777b69c9d00d9694a3899cf94 [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;
Ian Rogers9bc81912012-10-11 21:43:36 -0700477 IfTable* iftable = GetDeclaringClass()->GetIfTable();
478 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
479 Class* interface = iftable->GetInterface(i);
Ian Rogers16f93672012-02-14 12:29:06 -0800480 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();
Ian Rogers1f539342012-10-03 21:09:42 -0700699 SirtRef<Throwable> exception(self, self->GetException());
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800700 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
Ian Rogers50b35e22012-10-04 10:09:15 -0700729Object* Class::AllocObject(Thread* self) {
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));
Ian Rogers50b35e22012-10-04 10:09:15 -0700735 return Runtime::Current()->GetHeap()->AllocObject(self, 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 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700770 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
Ian Rogersd418eda2012-01-30 12:14:28 -0800771 } 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(), '/', '.');
Ian Rogers50b35e22012-10-04 10:09:15 -0700779 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
Ian Rogersd418eda2012-01-30 12:14:28 -0800780 }
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();
Ian Rogers9bc81912012-10-11 21:43:36 -0700883 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700884 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700885 if (iftable->GetInterface(i) == 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();
Ian Rogers9bc81912012-10-11 21:43:36 -07001009 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001010 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001011 if (iftable->GetInterface(i) == declaring_class) {
1012 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001013 }
1014 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001015 return NULL;
1016}
1017
Mathieu Chartier66f19252012-09-18 08:57:04 -07001018AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001019 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001020 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001021 if (method != NULL) {
1022 return method;
1023 }
1024
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001025 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001026 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001027 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001028 method = iftable->GetInterface(i)->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001029 if (method != NULL) {
1030 return method;
1031 }
1032 }
1033 return NULL;
1034}
1035
Mathieu Chartier66f19252012-09-18 08:57:04 -07001036AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001037 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001038 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001039 if (method != NULL) {
1040 return method;
1041 }
1042
1043 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001044 IfTable* iftable = GetIfTable();
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001045 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001046 method = iftable->GetInterface(i)->FindVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001047 if (method != NULL) {
1048 return method;
1049 }
1050 }
1051 return NULL;
1052}
1053
1054
Mathieu Chartier66f19252012-09-18 08:57:04 -07001055AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001056 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001057 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001058 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001059 mh.ChangeMethod(method);
1060 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001061 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001062 }
1063 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001064 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001065}
1066
Mathieu Chartier66f19252012-09-18 08:57:04 -07001067AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001068 if (GetDexCache() == dex_cache) {
1069 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001070 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001071 if (method->GetDexMethodIndex() == dex_method_idx) {
1072 return method;
1073 }
1074 }
1075 }
1076 return NULL;
1077}
1078
Mathieu Chartier66f19252012-09-18 08:57:04 -07001079AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001080 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001081 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001082 if (method != NULL) {
1083 return method;
1084 }
1085 }
1086 return NULL;
1087}
1088
Mathieu Chartier66f19252012-09-18 08:57:04 -07001089AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001090 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001091 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001092 if (method != NULL) {
1093 return method;
1094 }
1095 }
1096 return NULL;
1097}
1098
Mathieu Chartier66f19252012-09-18 08:57:04 -07001099AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001100 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001101 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001102 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001103 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001104 mh.ChangeMethod(method);
1105 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001106 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001107 }
1108 }
1109 return NULL;
1110}
1111
Mathieu Chartier66f19252012-09-18 08:57:04 -07001112AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001113 if (GetDexCache() == dex_cache) {
1114 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001115 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001116 if (method->GetDexMethodIndex() == dex_method_idx) {
1117 return method;
1118 }
1119 }
1120 }
1121 return NULL;
1122}
1123
Mathieu Chartier66f19252012-09-18 08:57:04 -07001124AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001125 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001126 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001127 if (method != NULL) {
1128 return method;
1129 }
1130 }
1131 return NULL;
1132}
1133
Mathieu Chartier66f19252012-09-18 08:57:04 -07001134AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001135 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001136 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001137 if (method != NULL) {
1138 return method;
1139 }
1140 }
1141 return NULL;
1142}
1143
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001144Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 // Is the field in this class?
1146 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001147 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1149 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001150 fh.ChangeField(f);
1151 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001152 return f;
1153 }
1154 }
1155 return NULL;
1156}
1157
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001158Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1159 if (GetDexCache() == dex_cache) {
1160 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1161 Field* f = GetInstanceField(i);
1162 if (f->GetDexFieldIndex() == dex_field_idx) {
1163 return f;
1164 }
1165 }
1166 }
1167 return NULL;
1168}
1169
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001170Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 // Is the field in this class, or any of its superclasses?
1172 // Interfaces are not relevant because they can't contain instance fields.
1173 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001174 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 if (f != NULL) {
1176 return f;
1177 }
1178 }
1179 return NULL;
1180}
1181
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001182Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1183 // Is the field in this class, or any of its superclasses?
1184 // Interfaces are not relevant because they can't contain instance fields.
1185 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1186 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1187 if (f != NULL) {
1188 return f;
1189 }
1190 }
1191 return NULL;
1192}
1193
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001194Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1195 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001196 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001197 for (size_t i = 0; i < NumStaticFields(); ++i) {
1198 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001199 fh.ChangeField(f);
1200 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001201 return f;
1202 }
1203 }
1204 return NULL;
1205}
1206
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001207Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1208 if (dex_cache == GetDexCache()) {
1209 for (size_t i = 0; i < NumStaticFields(); ++i) {
1210 Field* f = GetStaticField(i);
1211 if (f->GetDexFieldIndex() == dex_field_idx) {
1212 return f;
1213 }
1214 }
1215 }
1216 return NULL;
1217}
1218
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001219Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1220 // Is the field in this class (or its interfaces), or any of its
1221 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001222 ClassHelper kh;
1223 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001224 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001225 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001226 if (f != NULL) {
1227 return f;
1228 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001229 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001230 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001231 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1232 Class* interface = kh.GetDirectInterface(i);
1233 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001234 if (f != NULL) {
1235 return f;
1236 }
1237 }
1238 }
1239 return NULL;
1240}
1241
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001242Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1243 ClassHelper kh;
1244 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1245 // Is the field in this class?
1246 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1247 if (f != NULL) {
1248 return f;
1249 }
1250 // Is this field in any of this class' interfaces?
1251 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001252 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1253 Class* interface = kh.GetDirectInterface(i);
1254 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001255 if (f != NULL) {
1256 return f;
1257 }
1258 }
1259 }
1260 return NULL;
1261}
1262
Ian Rogersb067ac22011-12-13 18:05:09 -08001263Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1264 // Find a field using the JLS field resolution order
1265 ClassHelper kh;
1266 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1267 // Is the field in this class?
1268 Field* f = k->FindDeclaredInstanceField(name, type);
1269 if (f != NULL) {
1270 return f;
1271 }
1272 f = k->FindDeclaredStaticField(name, type);
1273 if (f != NULL) {
1274 return f;
1275 }
1276 // Is this field in any of this class' interfaces?
1277 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001278 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1279 Class* interface = kh.GetDirectInterface(i);
1280 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001281 if (f != NULL) {
1282 return f;
1283 }
1284 }
1285 }
1286 return NULL;
1287}
1288
Ian Rogers50b35e22012-10-04 10:09:15 -07001289Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
1290 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) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001302 self->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();
Ian Rogers50b35e22012-10-04 10:09:15 -07001309 Array* array = down_cast<Array*>(heap->AllocObject(self, 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
Ian Rogers50b35e22012-10-04 10:09:15 -07001317Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 DCHECK(array_class->IsArrayClass());
Ian Rogers50b35e22012-10-04 10:09:15 -07001319 return Alloc(self, array_class, component_count, array_class->GetComponentSize());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001320}
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>
Ian Rogers50b35e22012-10-04 10:09:15 -07001336PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001337 DCHECK(array_class_ != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -07001338 Array* raw_array = Array::Alloc(self, array_class_, length, sizeof(T));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001339 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
Ian Rogers50b35e22012-10-04 10:09:15 -07001404String* String::AllocFromUtf16(Thread* self,
1405 int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001406 const uint16_t* utf16_data_in,
1407 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001408 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers50b35e22012-10-04 10:09:15 -07001409 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001410 if (string == NULL) {
1411 return NULL;
1412 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001413 // TODO: use 16-bit wide memset variant
1414 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001415 if (array == NULL) {
1416 return NULL;
1417 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001418 for (int i = 0; i < utf16_length; i++) {
1419 array->Set(i, utf16_data_in[i]);
1420 }
1421 if (hash_code != 0) {
1422 string->SetHashCode(hash_code);
1423 } else {
1424 string->ComputeHashCode();
1425 }
1426 return string;
1427}
1428
Ian Rogers50b35e22012-10-04 10:09:15 -07001429 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001430 if (utf == NULL) {
1431 return NULL;
1432 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001433 size_t char_count = CountModifiedUtf8Chars(utf);
Ian Rogers50b35e22012-10-04 10:09:15 -07001434 return AllocFromModifiedUtf8(self, char_count, utf);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001435}
1436
Ian Rogers50b35e22012-10-04 10:09:15 -07001437String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001438 const char* utf8_data_in) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001439 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001440 if (string == NULL) {
1441 return NULL;
1442 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001443 uint16_t* utf16_data_out =
1444 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1445 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1446 string->ComputeHashCode();
1447 return string;
1448}
1449
Ian Rogers50b35e22012-10-04 10:09:15 -07001450String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) {
1451 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001452 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001453 return NULL;
1454 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001455 return Alloc(self, java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001456}
1457
Ian Rogers50b35e22012-10-04 10:09:15 -07001458String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) {
Ian Rogers1f539342012-10-03 21:09:42 -07001459 // Hold reference in case AllocObject causes GC.
Ian Rogers50b35e22012-10-04 10:09:15 -07001460 SirtRef<CharArray> array_ref(self, array);
1461 String* string = down_cast<String*>(java_lang_String->AllocObject(self));
Elliott Hughesb51036c2011-10-12 23:49:11 -07001462 if (string == NULL) {
1463 return NULL;
1464 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001465 string->SetArray(array);
1466 string->SetCount(array->GetLength());
1467 return string;
1468}
1469
1470bool String::Equals(const String* that) const {
1471 if (this == that) {
1472 // Quick reference equality test
1473 return true;
1474 } else if (that == NULL) {
1475 // Null isn't an instanceof anything
1476 return false;
1477 } else if (this->GetLength() != that->GetLength()) {
1478 // Quick length inequality test
1479 return false;
1480 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001481 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001482 // hash code was already equal
1483 for (int32_t i = 0; i < that->GetLength(); ++i) {
1484 if (this->CharAt(i) != that->CharAt(i)) {
1485 return false;
1486 }
1487 }
1488 return true;
1489 }
1490}
1491
Elliott Hughes5d78d392011-12-13 16:53:05 -08001492bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001493 if (this->GetLength() != that_length) {
1494 return false;
1495 } else {
1496 for (int32_t i = 0; i < that_length; ++i) {
1497 if (this->CharAt(i) != that_chars[that_offset + i]) {
1498 return false;
1499 }
1500 }
1501 return true;
1502 }
1503}
1504
1505bool String::Equals(const char* modified_utf8) const {
1506 for (int32_t i = 0; i < GetLength(); ++i) {
1507 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1508 if (ch == '\0' || ch != CharAt(i)) {
1509 return false;
1510 }
1511 }
1512 return *modified_utf8 == '\0';
1513}
1514
1515bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001516 if (modified_utf8.size() != GetLength()) {
1517 return false;
1518 }
1519 const char* p = modified_utf8.data();
1520 for (int32_t i = 0; i < GetLength(); ++i) {
1521 uint16_t ch = GetUtf16FromUtf8(&p);
1522 if (ch != CharAt(i)) {
1523 return false;
1524 }
1525 }
1526 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001527}
1528
1529// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1530std::string String::ToModifiedUtf8() const {
1531 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001532 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001533 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001534 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1535 return result;
1536}
1537
Ian Rogers1c5eb702012-02-01 09:18:34 -08001538void Throwable::SetCause(Throwable* cause) {
1539 CHECK(cause != NULL);
1540 CHECK(cause != this);
1541 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1542 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1543}
1544
Ian Rogers466bb252011-10-14 03:29:56 -07001545bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001546 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001547 return false;
1548 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001549 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001550}
1551
Ian Rogers9074b992011-10-26 17:41:55 -07001552std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001553 std::string result(PrettyTypeOf(this));
1554 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001555 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001556 if (msg != NULL) {
1557 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001558 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001559 result += "\n";
1560 Object* stack_state = GetStackState();
1561 // check stack state isn't missing or corrupt
1562 if (stack_state != NULL && stack_state->IsObjectArray()) {
1563 // Decode the internal stack trace into the depth and method trace
1564 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1565 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001566 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1567 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001568 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001569 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001570 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001571 uint32_t dex_pc = pc_trace->Get(i);
1572 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001573 const char* source_file = mh.GetDeclaringClassSourceFile();
1574 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1575 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001576 }
Ian Rogers9074b992011-10-26 17:41:55 -07001577 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001578 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001579 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001580 result += "Caused by: ";
1581 result += cause->Dump();
1582 }
Ian Rogers9074b992011-10-26 17:41:55 -07001583 return result;
1584}
1585
Ian Rogers5167c972012-02-03 10:41:20 -08001586
1587Class* Throwable::java_lang_Throwable_ = NULL;
1588
1589void Throwable::SetClass(Class* java_lang_Throwable) {
1590 CHECK(java_lang_Throwable_ == NULL);
1591 CHECK(java_lang_Throwable != NULL);
1592 java_lang_Throwable_ = java_lang_Throwable;
1593}
1594
1595void Throwable::ResetClass() {
1596 CHECK(java_lang_Throwable_ != NULL);
1597 java_lang_Throwable_ = NULL;
1598}
1599
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001600Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1601
1602void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1603 CHECK(java_lang_StackTraceElement_ == NULL);
1604 CHECK(java_lang_StackTraceElement != NULL);
1605 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1606}
1607
1608void StackTraceElement::ResetClass() {
1609 CHECK(java_lang_StackTraceElement_ != NULL);
1610 java_lang_StackTraceElement_ = NULL;
1611}
1612
Ian Rogers50b35e22012-10-04 10:09:15 -07001613StackTraceElement* StackTraceElement::Alloc(Thread* self,
1614 String* declaring_class,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001615 String* method_name,
1616 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001617 int32_t line_number) {
1618 StackTraceElement* trace =
Ian Rogers50b35e22012-10-04 10:09:15 -07001619 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject(self));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001620 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1621 const_cast<String*>(declaring_class), false);
1622 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1623 const_cast<String*>(method_name), false);
1624 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1625 const_cast<String*>(file_name), false);
1626 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1627 line_number, false);
1628 return trace;
1629}
1630
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001631} // namespace art