blob: 9189f034fcf531af304b3e8e238641fdce7ae535 [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"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070032#include "interpreter/interpreter.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070033#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070035#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080036#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070037#include "runtime.h"
Ian Rogers60db5ab2012-02-20 17:02:00 -080038#include "runtime_support.h"
Ian Rogers1f539342012-10-03 21:09:42 -070039#include "sirt_ref.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070040#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070041#include "utils.h"
Elliott Hughesa4f94742012-05-29 16:28:38 -070042#include "well_known_classes.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070043
44namespace art {
45
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070046BooleanArray* Object::AsBooleanArray() {
47 DCHECK(GetClass()->IsArrayClass());
48 DCHECK(GetClass()->GetComponentType()->IsPrimitiveBoolean());
49 return down_cast<BooleanArray*>(this);
50}
51
52ByteArray* Object::AsByteArray() {
53 DCHECK(GetClass()->IsArrayClass());
54 DCHECK(GetClass()->GetComponentType()->IsPrimitiveByte());
55 return down_cast<ByteArray*>(this);
56}
57
58CharArray* Object::AsCharArray() {
59 DCHECK(GetClass()->IsArrayClass());
60 DCHECK(GetClass()->GetComponentType()->IsPrimitiveChar());
61 return down_cast<CharArray*>(this);
62}
63
64ShortArray* Object::AsShortArray() {
65 DCHECK(GetClass()->IsArrayClass());
66 DCHECK(GetClass()->GetComponentType()->IsPrimitiveShort());
67 return down_cast<ShortArray*>(this);
68}
69
70IntArray* Object::AsIntArray() {
71 DCHECK(GetClass()->IsArrayClass());
72 DCHECK(GetClass()->GetComponentType()->IsPrimitiveInt());
73 return down_cast<IntArray*>(this);
74}
75
76LongArray* Object::AsLongArray() {
77 DCHECK(GetClass()->IsArrayClass());
78 DCHECK(GetClass()->GetComponentType()->IsPrimitiveLong());
79 return down_cast<LongArray*>(this);
80}
81
Elliott Hughesdbb40792011-11-18 17:05:22 -080082String* Object::AsString() {
83 DCHECK(GetClass()->IsStringClass());
84 return down_cast<String*>(this);
85}
86
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070087Throwable* Object::AsThrowable() {
88 DCHECK(GetClass()->IsThrowableClass());
89 return down_cast<Throwable*>(this);
90}
91
Ian Rogers50b35e22012-10-04 10:09:15 -070092Object* Object::Clone(Thread* self) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070093 Class* c = GetClass();
94 DCHECK(!c->IsClassClass());
95
96 // Object::SizeOf gets the right size even if we're an array.
97 // Using c->AllocObject() here would be wrong.
98 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080099 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700100 SirtRef<Object> copy(self, heap->AllocObject(self, c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700101 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -0700102 return NULL;
103 }
104
105 // Copy instance data. We assume memcpy copies by words.
106 // TODO: expose and use move32.
107 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700108 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -0700109 size_t offset = sizeof(Object);
110 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
111
Mathieu Chartier88c95be2012-09-11 14:06:41 -0700112 // Perform write barriers on copied object references.
113 if (c->IsArrayClass()) {
114 if (!c->GetComponentType()->IsPrimitive()) {
115 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
116 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
117 }
118 } else {
119 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
120 size_t num_reference_fields = klass->NumReferenceInstanceFields();
121 for (size_t i = 0; i < num_reference_fields; ++i) {
122 Field* field = klass->GetInstanceField(i);
123 MemberOffset field_offset = field->GetOffset();
124 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
125 heap->WriteBarrierField(copy.get(), field_offset, ref);
126 }
127 }
128 }
129
Elliott Hughes20cde902011-10-04 17:37:27 -0700130 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800131 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -0700132 }
Elliott Hughes081be7f2011-09-18 16:50:26 -0700133
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700134 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700135}
136
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700137uint32_t Object::GetThinLockId() {
138 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700139}
140
141void Object::MonitorEnter(Thread* thread) {
142 Monitor::MonitorEnter(thread, this);
143}
144
Ian Rogersff1ed472011-09-20 13:46:24 -0700145bool Object::MonitorExit(Thread* thread) {
146 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700147}
148
149void Object::Notify() {
150 Monitor::Notify(Thread::Current(), this);
151}
152
153void Object::NotifyAll() {
154 Monitor::NotifyAll(Thread::Current(), this);
155}
156
157void Object::Wait(int64_t ms, int32_t ns) {
158 Monitor::Wait(Thread::Current(), this, ms, ns, true);
159}
160
Ian Rogers23435d02012-09-24 11:23:12 -0700161#if VERIFY_OBJECT_ENABLED
162void Object::CheckFieldAssignment(MemberOffset field_offset, const Object* new_value) {
163 const Class* c = GetClass();
164 if (Runtime::Current()->GetClassLinker() == NULL ||
165 !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() ||
166 !c->IsResolved()) {
167 return;
168 }
169 for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
170 ObjectArray<Field>* fields = cur->GetIFields();
171 if (fields != NULL) {
172 size_t num_ref_ifields = cur->NumReferenceInstanceFields();
173 for (size_t i = 0; i < num_ref_ifields; ++i) {
174 Field* field = fields->Get(i);
175 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
176 FieldHelper fh(field);
177 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
178 return;
179 }
180 }
181 }
182 }
183 if (c->IsArrayClass()) {
184 // Bounds and assign-ability done in the array setter.
185 return;
186 }
187 if (IsClass()) {
188 ObjectArray<Field>* fields = AsClass()->GetSFields();
189 if (fields != NULL) {
190 size_t num_ref_sfields = AsClass()->NumReferenceStaticFields();
191 for (size_t i = 0; i < num_ref_sfields; ++i) {
192 Field* field = fields->Get(i);
193 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
194 FieldHelper fh(field);
195 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
196 return;
197 }
198 }
199 }
200 }
201 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
202 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
203}
204#endif
205
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206// TODO: get global references for these
207Class* Field::java_lang_reflect_Field_ = NULL;
208
209void Field::SetClass(Class* java_lang_reflect_Field) {
210 CHECK(java_lang_reflect_Field_ == NULL);
211 CHECK(java_lang_reflect_Field != NULL);
212 java_lang_reflect_Field_ = java_lang_reflect_Field;
213}
214
215void Field::ResetClass() {
216 CHECK(java_lang_reflect_Field_ != NULL);
217 java_lang_reflect_Field_ = NULL;
218}
219
Ian Rogers0571d352011-11-03 19:51:38 -0700220void Field::SetOffset(MemberOffset num_bytes) {
221 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222#if 0 // TODO enable later in boot and under !NDEBUG
223 FieldHelper fh(this);
224 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700225 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
226 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
227 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800228#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700229 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
230}
231
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700232uint32_t Field::Get32(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700233 DCHECK(object != NULL) << PrettyField(this);
234 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700235 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700236}
237
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700238void Field::Set32(Object* object, uint32_t new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700239 DCHECK(object != NULL) << PrettyField(this);
240 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700241 object->SetField32(GetOffset(), new_value, IsVolatile());
242}
243
244uint64_t Field::Get64(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700245 DCHECK(object != NULL) << PrettyField(this);
246 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700247 return object->GetField64(GetOffset(), IsVolatile());
248}
249
250void Field::Set64(Object* object, uint64_t new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700251 DCHECK(object != NULL) << PrettyField(this);
252 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700253 object->SetField64(GetOffset(), new_value, IsVolatile());
254}
255
256Object* Field::GetObj(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700257 DCHECK(object != NULL) << PrettyField(this);
258 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
260}
261
262void Field::SetObj(Object* object, const Object* new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700263 DCHECK(object != NULL) << PrettyField(this);
264 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
266}
267
268bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800269 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
270 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271 return Get32(object);
272}
273
274void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
276 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 Set32(object, z);
278}
279
280int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
282 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 return Get32(object);
284}
285
286void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800287 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
288 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 Set32(object, b);
290}
291
292uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800293 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
294 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700295 return Get32(object);
296}
297
298void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800299 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
300 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 Set32(object, c);
302}
303
Ian Rogers466bb252011-10-14 03:29:56 -0700304int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
306 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700307 return Get32(object);
308}
309
Ian Rogers466bb252011-10-14 03:29:56 -0700310void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800311 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
312 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700313 Set32(object, s);
314}
315
316int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800317 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
318 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700319 return Get32(object);
320}
321
322void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800323 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
324 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700325 Set32(object, i);
326}
327
328int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800329 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
330 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 return Get64(object);
332}
333
334void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800335 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
336 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 Set64(object, j);
338}
339
Elliott Hughes1d878f32012-04-11 15:17:54 -0700340union Bits {
341 jdouble d;
342 jfloat f;
343 jint i;
344 jlong j;
345};
346
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800348 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
349 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700350 Bits bits;
351 bits.i = Get32(object);
352 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353}
354
355void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800356 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
357 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700358 Bits bits;
359 bits.f = f;
360 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700361}
362
363double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800364 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
365 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700366 Bits bits;
367 bits.j = Get64(object);
368 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369}
370
371void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800372 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
373 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700374 Bits bits;
375 bits.d = d;
376 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377}
378
379Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800380 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
381 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 return GetObj(object);
383}
384
385void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800386 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
387 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 SetObj(object, l);
389}
390
391// TODO: get global references for these
Mathieu Chartier66f19252012-09-18 08:57:04 -0700392Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
393Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394
Mathieu Chartier66f19252012-09-18 08:57:04 -0700395InvokeType AbstractMethod::GetInvokeType() const {
Ian Rogers08f753d2012-08-24 14:35:25 -0700396 // TODO: kSuper?
397 if (GetDeclaringClass()->IsInterface()) {
398 return kInterface;
399 } else if (IsStatic()) {
400 return kStatic;
401 } else if (IsDirect()) {
402 return kDirect;
403 } else {
404 return kVirtual;
405 }
406}
407
Mathieu Chartier66f19252012-09-18 08:57:04 -0700408void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
Elliott Hughes80609252011-09-23 17:24:51 -0700409 CHECK(java_lang_reflect_Constructor_ == NULL);
410 CHECK(java_lang_reflect_Constructor != NULL);
411 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
412
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700413 CHECK(java_lang_reflect_Method_ == NULL);
414 CHECK(java_lang_reflect_Method != NULL);
415 java_lang_reflect_Method_ = java_lang_reflect_Method;
416}
417
Mathieu Chartier66f19252012-09-18 08:57:04 -0700418void AbstractMethod::ResetClasses() {
Elliott Hughes80609252011-09-23 17:24:51 -0700419 CHECK(java_lang_reflect_Constructor_ != NULL);
420 java_lang_reflect_Constructor_ = NULL;
421
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 CHECK(java_lang_reflect_Method_ != NULL);
423 java_lang_reflect_Method_ = NULL;
424}
425
Mathieu Chartier66f19252012-09-18 08:57:04 -0700426ObjectArray<String>* AbstractMethod::GetDexCacheStrings() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700427 return GetFieldObject<ObjectArray<String>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700428 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700429}
430
Mathieu Chartier66f19252012-09-18 08:57:04 -0700431void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
432 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700433 new_dex_cache_strings, false);
434}
435
Mathieu Chartier66f19252012-09-18 08:57:04 -0700436ObjectArray<AbstractMethod>* AbstractMethod::GetDexCacheResolvedMethods() const {
437 return GetFieldObject<ObjectArray<AbstractMethod>*>(
438 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800439}
440
Mathieu Chartier66f19252012-09-18 08:57:04 -0700441void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
442 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
Ian Rogers19846512012-02-24 11:42:47 -0800443 new_dex_cache_methods, false);
444}
445
Mathieu Chartier66f19252012-09-18 08:57:04 -0700446ObjectArray<Class>* AbstractMethod::GetDexCacheResolvedTypes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700447 return GetFieldObject<ObjectArray<Class>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700448 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700449}
450
Mathieu Chartier66f19252012-09-18 08:57:04 -0700451void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
452 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700453 new_dex_cache_classes, false);
454}
455
Mathieu Chartier66f19252012-09-18 08:57:04 -0700456ObjectArray<StaticStorageBase>* AbstractMethod::GetDexCacheInitializedStaticStorage() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700458 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700459 false);
460}
461
Mathieu Chartier66f19252012-09-18 08:57:04 -0700462void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
463 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700464 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700465}
466
Mathieu Chartier66f19252012-09-18 08:57:04 -0700467size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700468 CHECK_LE(1, shorty.length());
469 uint32_t num_registers = 0;
470 for (int i = 1; i < shorty.length(); ++i) {
471 char ch = shorty[i];
472 if (ch == 'D' || ch == 'J') {
473 num_registers += 2;
474 } else {
475 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700476 }
477 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700478 return num_registers;
479}
480
Mathieu Chartier66f19252012-09-18 08:57:04 -0700481bool AbstractMethod::IsProxyMethod() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800482 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700483}
484
Mathieu Chartier66f19252012-09-18 08:57:04 -0700485AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
Ian Rogers466bb252011-10-14 03:29:56 -0700486 if (IsStatic()) {
487 return NULL;
488 }
489 Class* declaring_class = GetDeclaringClass();
490 Class* super_class = declaring_class->GetSuperClass();
491 uint16_t method_index = GetMethodIndex();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700492 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
493 AbstractMethod* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800494 // Did this method override a super class method? If so load the result from the super class'
495 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700496 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
497 result = super_class_vtable->Get(method_index);
498 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800499 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800500 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800501 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
502 CHECK_EQ(result,
503 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800504 } else {
505 MethodHelper mh(this);
506 MethodHelper interface_mh;
Ian Rogers9bc81912012-10-11 21:43:36 -0700507 IfTable* iftable = GetDeclaringClass()->GetIfTable();
508 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
509 Class* interface = iftable->GetInterface(i);
Ian Rogers16f93672012-02-14 12:29:06 -0800510 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700511 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers16f93672012-02-14 12:29:06 -0800512 interface_mh.ChangeMethod(interface_method);
513 if (mh.HasSameNameAndSignature(&interface_mh)) {
514 result = interface_method;
515 break;
516 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800517 }
518 }
Ian Rogers466bb252011-10-14 03:29:56 -0700519 }
520 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521#ifndef NDEBUG
522 MethodHelper result_mh(result);
523 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
524#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700525 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700526}
527
Mathieu Chartier66f19252012-09-18 08:57:04 -0700528static const void* GetOatCode(const AbstractMethod* m)
529 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800530 Runtime* runtime = Runtime::Current();
531 const void* code = m->GetCode();
532 // Peel off any method tracing trampoline.
533 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
534 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
535 }
536 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800537 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800538 code = runtime->GetClassLinker()->GetOatCodeFor(m);
539 }
540 return code;
541}
542
Mathieu Chartier66f19252012-09-18 08:57:04 -0700543uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700544 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
545}
546
Bill Buzbeea5b30242012-09-28 07:19:44 -0700547// Find the lowest-address native safepoint pc for a given dex pc
Ian Rogers34065b52012-09-28 16:31:26 -0700548uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700549#if !defined(ART_USE_LLVM_COMPILER)
Bill Buzbeea5b30242012-09-28 07:19:44 -0700550 const uint32_t* mapping_table = GetPcToDexMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700551 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800552 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700553 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700554 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700555 size_t mapping_table_length = GetPcToDexMappingTableLength();
556 for (size_t i = 0; i < mapping_table_length; i += 2) {
557 if (mapping_table[i + 1] == dex_pc) {
558 return mapping_table[i] + reinterpret_cast<uintptr_t>(GetOatCode(this));
559 }
560 }
561 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
562 << " in " << PrettyMethod(this);
563 return 0;
564#else
565 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
566 return static_cast<uint32_t>(dex_pc);
567#endif
568}
569
570uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
571#if !defined(ART_USE_LLVM_COMPILER)
572 const uint32_t* mapping_table = GetPcToDexMappingTable();
573 if (mapping_table == NULL) {
574 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
575 return DexFile::kDexNoIndex; // Special no mapping case
576 }
577 size_t mapping_table_length = GetPcToDexMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800578 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700579 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700580 if (mapping_table[i] == sought_offset) {
581 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700582 }
583 }
buzbee8320f382012-09-11 16:29:42 -0700584 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
585 << " in " << PrettyMethod(this);
586 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700587#else
588 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
589 return static_cast<uint32_t>(pc);
590#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700591}
592
Mathieu Chartier66f19252012-09-18 08:57:04 -0700593uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700594 const uint32_t* mapping_table = GetDexToPcMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700595 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700596 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700597 return 0; // Special no mapping/pc == 0 case
598 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700599 size_t mapping_table_length = GetDexToPcMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700600 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700601 uint32_t map_offset = mapping_table[i];
602 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700603 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800604 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700605 }
606 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700607 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
608 << " in " << PrettyMethod(this);
Ian Rogersbdb03912011-09-14 00:55:44 -0700609 return 0;
610}
611
Mathieu Chartier66f19252012-09-18 08:57:04 -0700612uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800613 MethodHelper mh(this);
614 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700615 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700616 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
617 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700618 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700619 if (iter_type_idx == DexFile::kDexNoIndex16) {
620 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700621 }
622 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800623 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700624 if (iter_exception_type == NULL) {
625 // The verifier should take care of resolving all exception classes early
626 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800627 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700628 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700629 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700630 }
631 }
632 // Handler not found
633 return DexFile::kDexNoIndex;
634}
635
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700636void AbstractMethod::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700637 if (kIsDebugBuild) {
638 self->AssertThreadSuspensionIsAllowable();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 CHECK_EQ(kRunnable, self->GetState());
640 }
TDYa12785321912012-04-01 15:24:56 -0700641
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700643 ManagedStack fragment;
644 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700645
646 // Call the invoke stub associated with the method.
647 // Pass everything as arguments.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700648 AbstractMethod::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700649
Elliott Hughes1240dad2011-09-09 16:24:50 -0700650
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700651 if (UNLIKELY(!Runtime::Current()->IsStarted())){
652 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started.";
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700653 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700654 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700655 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700656 } else {
657 if (GetCode() != NULL && stub != NULL) {
658 bool log = false;
659 if (log) {
660 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
661 PrettyMethod(this).c_str(), GetCode(), stub);
662 }
663 (*stub)(this, receiver, self, args, result);
664 if (log) {
665 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
666 PrettyMethod(this).c_str(), GetCode(), stub);
667 }
668 } else {
669 LOG(INFO) << "Not invoking " << PrettyMethod(this)
670 << " code=" << reinterpret_cast<const void*>(GetCode())
671 << " stub=" << reinterpret_cast<void*>(stub);
672 const bool kInterpretMethodsWithNoCode = false;
673 if (kInterpretMethodsWithNoCode) {
674 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args, result);
675 } else if (result != NULL) {
676 result->SetJ(0);
677 }
678 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700679 }
680
681 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700682 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700683}
684
Mathieu Chartier66f19252012-09-18 08:57:04 -0700685bool AbstractMethod::IsRegistered() const {
686 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800687 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800688 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700689 return native_method != jni_stub;
690}
691
Mathieu Chartier66f19252012-09-18 08:57:04 -0700692void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800693 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700694 CHECK(IsNative()) << PrettyMethod(this);
695 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700696#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700697 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700698 native_method, false);
699#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800700 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700701 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800702 native_method, false);
703 } else {
704 // We've been asked to associate this method with the given native method but are working
705 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
706 // the native method to runtime support and store the target somewhere runtime support will
707 // find it.
708#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700709 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800710 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
711#else
712 UNIMPLEMENTED(FATAL);
713#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700714 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800715 reinterpret_cast<const uint8_t*>(native_method), false);
716 }
TDYa12726467572012-04-17 20:51:22 -0700717#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700718}
719
Mathieu Chartier66f19252012-09-18 08:57:04 -0700720void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700721 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700722 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800723 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700724}
725
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700726void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700727 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
728 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700729 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700730 if (new_status > kStatusResolved) {
731 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
732 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800733 if (new_status == kStatusError) {
734 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
735
736 // stash current exception
737 Thread* self = Thread::Current();
Ian Rogers1f539342012-10-03 21:09:42 -0700738 SirtRef<Throwable> exception(self, self->GetException());
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800739 CHECK(exception.get() != NULL);
740
741 // clear exception to call FindSystemClass
742 self->ClearException();
743 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
744 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
745 CHECK(!self->IsExceptionPending());
746
747 // only verification errors, not initialization problems, should set a verify error.
748 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
749 Class* exception_class = exception->GetClass();
750 if (!eiie_class->IsAssignableFrom(exception_class)) {
751 SetVerifyErrorClass(exception_class);
752 }
753
754 // restore exception
755 self->SetException(exception.get());
756 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700757 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700758}
759
760DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700761 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700762}
763
764void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700765 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700766}
767
Ian Rogers50b35e22012-10-04 10:09:15 -0700768Object* Class::AllocObject(Thread* self) {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700769 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700770 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500771 // TODO: decide whether we want this check. It currently fails during bootstrap.
772 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700773 DCHECK_GE(this->object_size_, sizeof(Object));
Ian Rogers50b35e22012-10-04 10:09:15 -0700774 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700775}
776
Ian Rogers0571d352011-11-03 19:51:38 -0700777void Class::SetClassSize(size_t new_class_size) {
778 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
779 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
780}
781
Ian Rogersd418eda2012-01-30 12:14:28 -0800782// Return the class' name. The exact format is bizarre, but it's the specified behavior for
783// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
784// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
785// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
786String* Class::ComputeName() {
787 String* name = GetName();
788 if (name != NULL) {
789 return name;
790 }
791 std::string descriptor(ClassHelper(this).GetDescriptor());
792 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
793 // The descriptor indicates that this is the class for
794 // a primitive type; special-case the return value.
795 const char* c_name = NULL;
796 switch (descriptor[0]) {
797 case 'Z': c_name = "boolean"; break;
798 case 'B': c_name = "byte"; break;
799 case 'C': c_name = "char"; break;
800 case 'S': c_name = "short"; break;
801 case 'I': c_name = "int"; break;
802 case 'J': c_name = "long"; break;
803 case 'F': c_name = "float"; break;
804 case 'D': c_name = "double"; break;
805 case 'V': c_name = "void"; break;
806 default:
807 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
808 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700809 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
Ian Rogersd418eda2012-01-30 12:14:28 -0800810 } else {
811 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
812 // components.
813 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
814 descriptor.erase(0, 1);
815 descriptor.erase(descriptor.size() - 1);
816 }
817 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Ian Rogers50b35e22012-10-04 10:09:15 -0700818 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
Ian Rogersd418eda2012-01-30 12:14:28 -0800819 }
820 SetName(name);
821 return name;
822}
823
Elliott Hughes4681c802011-09-25 18:04:37 -0700824void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700825 if ((flags & kDumpClassFullDetail) == 0) {
826 os << PrettyClass(this);
827 if ((flags & kDumpClassClassLoader) != 0) {
828 os << ' ' << GetClassLoader();
829 }
830 if ((flags & kDumpClassInitialized) != 0) {
831 os << ' ' << GetStatus();
832 }
Elliott Hughese0918552011-10-28 17:18:29 -0700833 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700834 return;
835 }
836
837 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800838 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700839 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800840 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700841 os << " objectSize=" << SizeOf() << " "
842 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
843 os << StringPrintf(" access=0x%04x.%04x\n",
844 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
845 if (super != NULL) {
846 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
847 }
848 if (IsArrayClass()) {
849 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
850 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700851 if (kh.NumDirectInterfaces() > 0) {
852 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
853 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
854 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700855 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800856 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700857 }
858 }
859 os << " vtable (" << NumVirtualMethods() << " entries, "
860 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
861 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800862 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700863 }
864 os << " direct methods (" << NumDirectMethods() << " entries):\n";
865 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800866 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700867 }
868 if (NumStaticFields() > 0) {
869 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700870 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700871 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800872 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700873 }
874 } else {
875 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700876 }
877 }
878 if (NumInstanceFields() > 0) {
879 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700880 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700881 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800882 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700883 }
884 } else {
885 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700886 }
887 }
888}
889
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700890void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
891 if (new_reference_offsets != CLASS_WALK_SUPER) {
892 // Sanity check that the number of bits set in the reference offset bitmap
893 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800894 size_t count = 0;
895 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
896 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700897 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800898 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700899 }
900 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
901 new_reference_offsets, false);
902}
903
904void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
905 if (new_reference_offsets != CLASS_WALK_SUPER) {
906 // Sanity check that the number of bits set in the reference offset bitmap
907 // agrees with the number of references
908 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
909 NumReferenceStaticFieldsDuringLinking());
910 }
911 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
912 new_reference_offsets, false);
913}
914
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700915bool Class::Implements(const Class* klass) const {
916 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700917 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700918 // All interfaces implemented directly and by our superclass, and
919 // recursively all super-interfaces of those interfaces, are listed
920 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700921 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -0700922 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700923 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700924 if (iftable->GetInterface(i) == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700925 return true;
926 }
927 }
928 return false;
929}
930
Elliott Hughese84278b2012-03-22 10:06:53 -0700931// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700932// are array classes.
933//
934// Consider an array class, e.g. Y[][], where Y is a subclass of X.
935// Y[][] = Y[][] --> true (identity)
936// X[][] = Y[][] --> true (element superclass)
937// Y = Y[][] --> false
938// Y[] = Y[][] --> false
939// Object = Y[][] --> true (everything is an object)
940// Object[] = Y[][] --> true
941// Object[][] = Y[][] --> true
942// Object[][][] = Y[][] --> false (too many []s)
943// Serializable = Y[][] --> true (all arrays are Serializable)
944// Serializable[] = Y[][] --> true
945// Serializable[][] = Y[][] --> false (unless Y is Serializable)
946//
947// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700948// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700949//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700950bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700951 DCHECK(IsArrayClass()) << PrettyClass(this);
952 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700953 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700954}
955
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700956bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700957 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
958 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700959 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700960 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700961 // src's super should be java_lang_Object, since it is an array.
962 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700963 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
964 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700965 return this == java_lang_Object;
966 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700967 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700968}
969
970bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700971 DCHECK(!IsInterface()) << PrettyClass(this);
972 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700973 const Class* current = this;
974 do {
975 if (current == klass) {
976 return true;
977 }
978 current = current->GetSuperClass();
979 } while (current != NULL);
980 return false;
981}
982
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800983bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700984 size_t i = 0;
985 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
986 ++i;
987 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700988 if (descriptor1.find('/', i) != StringPiece::npos ||
989 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700990 return false;
991 } else {
992 return true;
993 }
994}
995
996bool Class::IsInSamePackage(const Class* that) const {
997 const Class* klass1 = this;
998 const Class* klass2 = that;
999 if (klass1 == klass2) {
1000 return true;
1001 }
1002 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001003 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001004 return false;
1005 }
1006 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -07001007 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001008 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001009 }
jeffhao4a801a42011-09-23 13:53:40 -07001010 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001011 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001012 }
1013 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001014 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -08001015 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001016 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -08001017 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001018 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001019}
1020
Elliott Hughesdbb40792011-11-18 17:05:22 -08001021bool Class::IsClassClass() const {
1022 Class* java_lang_Class = GetClass()->GetClass();
1023 return this == java_lang_Class;
1024}
1025
1026bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001027 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001028}
1029
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001030bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001031 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001032}
1033
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001034ClassLoader* Class::GetClassLoader() const {
1035 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -07001036}
1037
Ian Rogers365c1022012-06-22 15:05:28 -07001038void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001040}
1041
Mathieu Chartier66f19252012-09-18 08:57:04 -07001042AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001043 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001044 DCHECK(declaring_class != NULL) << PrettyClass(this);
1045 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001046 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001047 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001048 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001049 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001050 if (iftable->GetInterface(i) == declaring_class) {
1051 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001052 }
1053 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001054 return NULL;
1055}
1056
Mathieu Chartier66f19252012-09-18 08:57:04 -07001057AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001058 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001059 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001060 if (method != NULL) {
1061 return method;
1062 }
1063
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001064 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001065 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001066 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001067 method = iftable->GetInterface(i)->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001068 if (method != NULL) {
1069 return method;
1070 }
1071 }
1072 return NULL;
1073}
1074
Mathieu Chartier66f19252012-09-18 08:57:04 -07001075AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001076 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001077 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001078 if (method != NULL) {
1079 return method;
1080 }
1081
1082 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001083 IfTable* iftable = GetIfTable();
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001084 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001085 method = iftable->GetInterface(i)->FindVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001086 if (method != NULL) {
1087 return method;
1088 }
1089 }
1090 return NULL;
1091}
1092
1093
Mathieu Chartier66f19252012-09-18 08:57:04 -07001094AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001095 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001096 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001097 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001098 mh.ChangeMethod(method);
1099 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001100 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001101 }
1102 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001103 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001104}
1105
Mathieu Chartier66f19252012-09-18 08:57:04 -07001106AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001107 if (GetDexCache() == dex_cache) {
1108 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001109 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001110 if (method->GetDexMethodIndex() == dex_method_idx) {
1111 return method;
1112 }
1113 }
1114 }
1115 return NULL;
1116}
1117
Mathieu Chartier66f19252012-09-18 08:57:04 -07001118AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001119 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001120 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001121 if (method != NULL) {
1122 return method;
1123 }
1124 }
1125 return NULL;
1126}
1127
Mathieu Chartier66f19252012-09-18 08:57:04 -07001128AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001129 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001130 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001131 if (method != NULL) {
1132 return method;
1133 }
1134 }
1135 return NULL;
1136}
1137
Mathieu Chartier66f19252012-09-18 08:57:04 -07001138AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001139 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001140 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001141 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001142 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001143 mh.ChangeMethod(method);
1144 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001145 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001146 }
1147 }
1148 return NULL;
1149}
1150
Mathieu Chartier66f19252012-09-18 08:57:04 -07001151AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001152 if (GetDexCache() == dex_cache) {
1153 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001154 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001155 if (method->GetDexMethodIndex() == dex_method_idx) {
1156 return method;
1157 }
1158 }
1159 }
1160 return NULL;
1161}
1162
Mathieu Chartier66f19252012-09-18 08:57:04 -07001163AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001164 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001165 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001166 if (method != NULL) {
1167 return method;
1168 }
1169 }
1170 return NULL;
1171}
1172
Mathieu Chartier66f19252012-09-18 08:57:04 -07001173AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001174 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001175 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001176 if (method != NULL) {
1177 return method;
1178 }
1179 }
1180 return NULL;
1181}
1182
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001183Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 // Is the field in this class?
1185 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001186 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1188 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001189 fh.ChangeField(f);
1190 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001191 return f;
1192 }
1193 }
1194 return NULL;
1195}
1196
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001197Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1198 if (GetDexCache() == dex_cache) {
1199 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1200 Field* f = GetInstanceField(i);
1201 if (f->GetDexFieldIndex() == dex_field_idx) {
1202 return f;
1203 }
1204 }
1205 }
1206 return NULL;
1207}
1208
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001209Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 // Is the field in this class, or any of its superclasses?
1211 // Interfaces are not relevant because they can't contain instance fields.
1212 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001213 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 if (f != NULL) {
1215 return f;
1216 }
1217 }
1218 return NULL;
1219}
1220
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001221Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1222 // Is the field in this class, or any of its superclasses?
1223 // Interfaces are not relevant because they can't contain instance fields.
1224 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1225 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1226 if (f != NULL) {
1227 return f;
1228 }
1229 }
1230 return NULL;
1231}
1232
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001233Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1234 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001235 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001236 for (size_t i = 0; i < NumStaticFields(); ++i) {
1237 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001238 fh.ChangeField(f);
1239 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001240 return f;
1241 }
1242 }
1243 return NULL;
1244}
1245
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001246Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1247 if (dex_cache == GetDexCache()) {
1248 for (size_t i = 0; i < NumStaticFields(); ++i) {
1249 Field* f = GetStaticField(i);
1250 if (f->GetDexFieldIndex() == dex_field_idx) {
1251 return f;
1252 }
1253 }
1254 }
1255 return NULL;
1256}
1257
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001258Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1259 // Is the field in this class (or its interfaces), or any of its
1260 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001261 ClassHelper kh;
1262 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001263 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001264 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001265 if (f != NULL) {
1266 return f;
1267 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001268 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001269 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001270 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1271 Class* interface = kh.GetDirectInterface(i);
1272 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001273 if (f != NULL) {
1274 return f;
1275 }
1276 }
1277 }
1278 return NULL;
1279}
1280
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001281Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1282 ClassHelper kh;
1283 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1284 // Is the field in this class?
1285 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1286 if (f != NULL) {
1287 return f;
1288 }
1289 // Is this field in any of this class' interfaces?
1290 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001291 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1292 Class* interface = kh.GetDirectInterface(i);
1293 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001294 if (f != NULL) {
1295 return f;
1296 }
1297 }
1298 }
1299 return NULL;
1300}
1301
Ian Rogersb067ac22011-12-13 18:05:09 -08001302Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1303 // Find a field using the JLS field resolution order
1304 ClassHelper kh;
1305 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1306 // Is the field in this class?
1307 Field* f = k->FindDeclaredInstanceField(name, type);
1308 if (f != NULL) {
1309 return f;
1310 }
1311 f = k->FindDeclaredStaticField(name, type);
1312 if (f != NULL) {
1313 return f;
1314 }
1315 // Is this field in any of this class' interfaces?
1316 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001317 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1318 Class* interface = kh.GetDirectInterface(i);
1319 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001320 if (f != NULL) {
1321 return f;
1322 }
1323 }
1324 }
1325 return NULL;
1326}
1327
Ian Rogers50b35e22012-10-04 10:09:15 -07001328Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
1329 size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001330 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001331 DCHECK_GE(component_count, 0);
1332 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001333
Ian Rogersa15e67d2012-02-28 13:51:55 -08001334 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001335 size_t data_size = component_count * component_size;
1336 size_t size = header_size + data_size;
1337
1338 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1339 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1340 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001341 self->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001342 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001343 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001344 return NULL;
1345 }
1346
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001347 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001348 Array* array = down_cast<Array*>(heap->AllocObject(self, array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001349 if (array != NULL) {
1350 DCHECK(array->IsArrayInstance());
1351 array->SetLength(component_count);
1352 }
1353 return array;
1354}
1355
Ian Rogers50b35e22012-10-04 10:09:15 -07001356Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357 DCHECK(array_class->IsArrayClass());
Ian Rogers50b35e22012-10-04 10:09:15 -07001358 return Alloc(self, array_class, component_count, array_class->GetComponentSize());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001359}
1360
Elliott Hughes80609252011-09-23 17:24:51 -07001361bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001362 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001363 "length=%i; index=%i", length_, index);
1364 return false;
1365}
1366
1367bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001368 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001369 "Can't store an element of type %s into an array of type %s",
1370 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1371 return false;
1372}
1373
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001374template<typename T>
Ian Rogers50b35e22012-10-04 10:09:15 -07001375PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001376 DCHECK(array_class_ != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -07001377 Array* raw_array = Array::Alloc(self, array_class_, length, sizeof(T));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001378 return down_cast<PrimitiveArray<T>*>(raw_array);
1379}
1380
1381template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1382
1383// Explicitly instantiate all the primitive array types.
1384template class PrimitiveArray<uint8_t>; // BooleanArray
1385template class PrimitiveArray<int8_t>; // ByteArray
1386template class PrimitiveArray<uint16_t>; // CharArray
1387template class PrimitiveArray<double>; // DoubleArray
1388template class PrimitiveArray<float>; // FloatArray
1389template class PrimitiveArray<int32_t>; // IntArray
1390template class PrimitiveArray<int64_t>; // LongArray
1391template class PrimitiveArray<int16_t>; // ShortArray
1392
Ian Rogers466bb252011-10-14 03:29:56 -07001393// Explicitly instantiate Class[][]
1394template class ObjectArray<ObjectArray<Class> >;
1395
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001396// TODO: get global references for these
1397Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001398
Brian Carlstroma663ea52011-08-19 23:33:41 -07001399void String::SetClass(Class* java_lang_String) {
1400 CHECK(java_lang_String_ == NULL);
1401 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001402 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001403}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001404
Brian Carlstroma663ea52011-08-19 23:33:41 -07001405void String::ResetClass() {
1406 CHECK(java_lang_String_ != NULL);
1407 java_lang_String_ = NULL;
1408}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001409
Brian Carlstromc74255f2011-09-11 22:47:39 -07001410String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001411 return Runtime::Current()->GetInternTable()->InternWeak(this);
1412}
1413
Brian Carlstrom395520e2011-09-25 19:35:00 -07001414int32_t String::GetHashCode() {
1415 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1416 if (result == 0) {
1417 ComputeHashCode();
1418 }
1419 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1420 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1421 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001422 return result;
1423}
1424
1425int32_t String::GetLength() const {
1426 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1427 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1428 return result;
1429}
1430
1431uint16_t String::CharAt(int32_t index) const {
1432 // TODO: do we need this? Equals is the only caller, and could
1433 // bounds check itself.
1434 if (index < 0 || index >= count_) {
1435 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001436 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001437 "length=%i; index=%i", count_, index);
1438 return 0;
1439 }
1440 return GetCharArray()->Get(index + GetOffset());
1441}
1442
Ian Rogers50b35e22012-10-04 10:09:15 -07001443String* String::AllocFromUtf16(Thread* self,
1444 int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001445 const uint16_t* utf16_data_in,
1446 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001447 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers50b35e22012-10-04 10:09:15 -07001448 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001449 if (string == NULL) {
1450 return NULL;
1451 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001452 // TODO: use 16-bit wide memset variant
1453 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001454 if (array == NULL) {
1455 return NULL;
1456 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001457 for (int i = 0; i < utf16_length; i++) {
1458 array->Set(i, utf16_data_in[i]);
1459 }
1460 if (hash_code != 0) {
1461 string->SetHashCode(hash_code);
1462 } else {
1463 string->ComputeHashCode();
1464 }
1465 return string;
1466}
1467
Ian Rogers50b35e22012-10-04 10:09:15 -07001468 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001469 if (utf == NULL) {
1470 return NULL;
1471 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001472 size_t char_count = CountModifiedUtf8Chars(utf);
Ian Rogers50b35e22012-10-04 10:09:15 -07001473 return AllocFromModifiedUtf8(self, char_count, utf);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001474}
1475
Ian Rogers50b35e22012-10-04 10:09:15 -07001476String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001477 const char* utf8_data_in) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001478 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001479 if (string == NULL) {
1480 return NULL;
1481 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001482 uint16_t* utf16_data_out =
1483 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1484 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1485 string->ComputeHashCode();
1486 return string;
1487}
1488
Ian Rogers50b35e22012-10-04 10:09:15 -07001489String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) {
1490 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001491 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001492 return NULL;
1493 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001494 return Alloc(self, java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001495}
1496
Ian Rogers50b35e22012-10-04 10:09:15 -07001497String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) {
Ian Rogers1f539342012-10-03 21:09:42 -07001498 // Hold reference in case AllocObject causes GC.
Ian Rogers50b35e22012-10-04 10:09:15 -07001499 SirtRef<CharArray> array_ref(self, array);
1500 String* string = down_cast<String*>(java_lang_String->AllocObject(self));
Elliott Hughesb51036c2011-10-12 23:49:11 -07001501 if (string == NULL) {
1502 return NULL;
1503 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001504 string->SetArray(array);
1505 string->SetCount(array->GetLength());
1506 return string;
1507}
1508
1509bool String::Equals(const String* that) const {
1510 if (this == that) {
1511 // Quick reference equality test
1512 return true;
1513 } else if (that == NULL) {
1514 // Null isn't an instanceof anything
1515 return false;
1516 } else if (this->GetLength() != that->GetLength()) {
1517 // Quick length inequality test
1518 return false;
1519 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001520 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001521 // hash code was already equal
1522 for (int32_t i = 0; i < that->GetLength(); ++i) {
1523 if (this->CharAt(i) != that->CharAt(i)) {
1524 return false;
1525 }
1526 }
1527 return true;
1528 }
1529}
1530
Elliott Hughes5d78d392011-12-13 16:53:05 -08001531bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001532 if (this->GetLength() != that_length) {
1533 return false;
1534 } else {
1535 for (int32_t i = 0; i < that_length; ++i) {
1536 if (this->CharAt(i) != that_chars[that_offset + i]) {
1537 return false;
1538 }
1539 }
1540 return true;
1541 }
1542}
1543
1544bool String::Equals(const char* modified_utf8) const {
1545 for (int32_t i = 0; i < GetLength(); ++i) {
1546 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1547 if (ch == '\0' || ch != CharAt(i)) {
1548 return false;
1549 }
1550 }
1551 return *modified_utf8 == '\0';
1552}
1553
1554bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001555 if (modified_utf8.size() != GetLength()) {
1556 return false;
1557 }
1558 const char* p = modified_utf8.data();
1559 for (int32_t i = 0; i < GetLength(); ++i) {
1560 uint16_t ch = GetUtf16FromUtf8(&p);
1561 if (ch != CharAt(i)) {
1562 return false;
1563 }
1564 }
1565 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001566}
1567
1568// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1569std::string String::ToModifiedUtf8() const {
1570 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001571 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001572 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001573 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1574 return result;
1575}
1576
Ian Rogers1c5eb702012-02-01 09:18:34 -08001577void Throwable::SetCause(Throwable* cause) {
1578 CHECK(cause != NULL);
1579 CHECK(cause != this);
1580 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1581 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1582}
1583
Ian Rogers466bb252011-10-14 03:29:56 -07001584bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001585 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001586 return false;
1587 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001588 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001589}
1590
Ian Rogers9074b992011-10-26 17:41:55 -07001591std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001592 std::string result(PrettyTypeOf(this));
1593 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001594 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001595 if (msg != NULL) {
1596 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001597 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001598 result += "\n";
1599 Object* stack_state = GetStackState();
1600 // check stack state isn't missing or corrupt
1601 if (stack_state != NULL && stack_state->IsObjectArray()) {
1602 // Decode the internal stack trace into the depth and method trace
1603 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1604 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001605 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1606 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001607 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001608 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001609 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001610 uint32_t dex_pc = pc_trace->Get(i);
1611 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001612 const char* source_file = mh.GetDeclaringClassSourceFile();
1613 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1614 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001615 }
Ian Rogers9074b992011-10-26 17:41:55 -07001616 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001617 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001618 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001619 result += "Caused by: ";
1620 result += cause->Dump();
1621 }
Ian Rogers9074b992011-10-26 17:41:55 -07001622 return result;
1623}
1624
Ian Rogers5167c972012-02-03 10:41:20 -08001625
1626Class* Throwable::java_lang_Throwable_ = NULL;
1627
1628void Throwable::SetClass(Class* java_lang_Throwable) {
1629 CHECK(java_lang_Throwable_ == NULL);
1630 CHECK(java_lang_Throwable != NULL);
1631 java_lang_Throwable_ = java_lang_Throwable;
1632}
1633
1634void Throwable::ResetClass() {
1635 CHECK(java_lang_Throwable_ != NULL);
1636 java_lang_Throwable_ = NULL;
1637}
1638
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001639Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1640
1641void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1642 CHECK(java_lang_StackTraceElement_ == NULL);
1643 CHECK(java_lang_StackTraceElement != NULL);
1644 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1645}
1646
1647void StackTraceElement::ResetClass() {
1648 CHECK(java_lang_StackTraceElement_ != NULL);
1649 java_lang_StackTraceElement_ = NULL;
1650}
1651
Ian Rogers50b35e22012-10-04 10:09:15 -07001652StackTraceElement* StackTraceElement::Alloc(Thread* self,
1653 String* declaring_class,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001654 String* method_name,
1655 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001656 int32_t line_number) {
1657 StackTraceElement* trace =
Ian Rogers50b35e22012-10-04 10:09:15 -07001658 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject(self));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001659 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1660 const_cast<String*>(declaring_class), false);
1661 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1662 const_cast<String*>(method_name), false);
1663 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1664 const_cast<String*>(file_name), false);
1665 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1666 line_number, false);
1667 return trace;
1668}
1669
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001670} // namespace art