blob: ff49ad3ac699a469abb7fa1766d828c787f962e9 [file] [log] [blame]
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "object.h"
4
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07006
Ian Rogersdf20fe02011-07-20 20:34:16 -07007#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07008#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07009#include <string>
10#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070012#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070013#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070014#include "dex_cache.h"
15#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070017#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070018#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070020#include "monitor.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "runtime.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070022
23namespace art {
24
Elliott Hughes081be7f2011-09-18 16:50:26 -070025Object* Object::Clone() {
26 Class* c = GetClass();
27 DCHECK(!c->IsClassClass());
28
29 // Object::SizeOf gets the right size even if we're an array.
30 // Using c->AllocObject() here would be wrong.
31 size_t num_bytes = SizeOf();
32 Object* copy = Heap::AllocObject(c, num_bytes);
33 if (copy == NULL) {
34 return NULL;
35 }
36
37 // Copy instance data. We assume memcpy copies by words.
38 // TODO: expose and use move32.
39 byte* src_bytes = reinterpret_cast<byte*>(this);
40 byte* dst_bytes = reinterpret_cast<byte*>(copy);
41 size_t offset = sizeof(Object);
42 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
43
44 // TODO: Mark the clone as finalizable if appropriate.
45// if (IS_CLASS_FLAG_SET(clazz, CLASS_ISFINALIZABLE)) {
46// dvmSetFinalizable(copy);
47// }
48
49 return copy;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070050}
51
Elliott Hughes5f791332011-09-15 17:45:30 -070052uint32_t Object::GetLockOwner() {
53 return Monitor::GetLockOwner(monitor_);
54}
55
Elliott Hughes081be7f2011-09-18 16:50:26 -070056bool Object::IsString() const {
57 // TODO use "klass_ == String::GetJavaLangString()" instead?
58 return GetClass() == GetClass()->GetDescriptor()->GetClass();
59}
60
Elliott Hughes5f791332011-09-15 17:45:30 -070061void Object::MonitorEnter(Thread* thread) {
62 Monitor::MonitorEnter(thread, this);
63}
64
Ian Rogersff1ed472011-09-20 13:46:24 -070065bool Object::MonitorExit(Thread* thread) {
66 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070067}
68
69void Object::Notify() {
70 Monitor::Notify(Thread::Current(), this);
71}
72
73void Object::NotifyAll() {
74 Monitor::NotifyAll(Thread::Current(), this);
75}
76
77void Object::Wait(int64_t ms, int32_t ns) {
78 Monitor::Wait(Thread::Current(), this, ms, ns, true);
79}
80
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070081// TODO: get global references for these
82Class* Field::java_lang_reflect_Field_ = NULL;
83
84void Field::SetClass(Class* java_lang_reflect_Field) {
85 CHECK(java_lang_reflect_Field_ == NULL);
86 CHECK(java_lang_reflect_Field != NULL);
87 java_lang_reflect_Field_ = java_lang_reflect_Field;
88}
89
90void Field::ResetClass() {
91 CHECK(java_lang_reflect_Field_ != NULL);
92 java_lang_reflect_Field_ = NULL;
93}
94
95void Field::SetTypeIdx(uint32_t type_idx) {
96 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), type_idx, false);
97}
98
99Class* Field::GetTypeDuringLinking() const {
100 // We are assured that the necessary primitive types are in the dex cache
101 // early during class linking
102 return GetDeclaringClass()->GetDexCache()->GetResolvedType(GetTypeIdx());
103}
104
105Class* Field::GetType() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700106 if (type_ == NULL) {
107 type_ = Runtime::Current()->GetClassLinker()->ResolveType(GetTypeIdx(), this);
108 }
109 return type_;
110}
111
112void Field::InitJavaFields() {
113 Thread* self = Thread::Current();
114 ScopedThreadStateChange tsc(self, Thread::kRunnable);
115 MonitorEnter(self);
116 if (type_ == NULL) {
117 InitJavaFieldsLocked();
118 }
119 MonitorExit(self);
120}
121
122void Field::InitJavaFieldsLocked() {
123 GetType(); // Sets type_ as a side-effect. May throw.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700124}
125
Brian Carlstrom845490b2011-09-19 15:56:53 -0700126Field* Field::FindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer) {
127 return FindFieldFromCode(field_idx, referrer, false);
128}
129
130Field* Field::FindStaticFieldFromCode(uint32_t field_idx, const Method* referrer) {
131 return FindFieldFromCode(field_idx, referrer, true);
132}
133
134Field* Field::FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700135 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700136 Field* f = class_linker->ResolveField(field_idx, referrer, is_static);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700137 if (f != NULL) {
138 Class* c = f->GetDeclaringClass();
139 // If the class is already initializing, we must be inside <clinit>, or
140 // we'd still be waiting for the lock.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700141 if (c->GetStatus() == Class::kStatusInitializing || class_linker->EnsureInitialized(c, true)) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700142 return f;
143 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700144 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700145 UNIMPLEMENTED(FATAL) << "throw an error and unwind";
146 return NULL;
147}
148
149uint32_t Field::Get32StaticFromCode(uint32_t field_idx, const Method* referrer) {
Brian Carlstrom845490b2011-09-19 15:56:53 -0700150 Field* field = FindStaticFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700151 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t));
152 return field->Get32(NULL);
153}
154void Field::Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value) {
Brian Carlstrom845490b2011-09-19 15:56:53 -0700155 Field* field = FindStaticFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700156 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t));
157 field->Set32(NULL, new_value);
158}
159uint64_t Field::Get64StaticFromCode(uint32_t field_idx, const Method* referrer) {
Brian Carlstrom845490b2011-09-19 15:56:53 -0700160 Field* field = FindStaticFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t));
162 return field->Get64(NULL);
163}
164void Field::Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value) {
Brian Carlstrom845490b2011-09-19 15:56:53 -0700165 Field* field = FindStaticFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t));
167 field->Set64(NULL, new_value);
168}
169Object* Field::GetObjStaticFromCode(uint32_t field_idx, const Method* referrer) {
Brian Carlstrom845490b2011-09-19 15:56:53 -0700170 Field* field = FindStaticFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700171 DCHECK(!field->GetType()->IsPrimitive());
172 return field->GetObj(NULL);
173}
174void Field::SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value) {
Brian Carlstrom845490b2011-09-19 15:56:53 -0700175 Field* field = FindStaticFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700176 DCHECK(!field->GetType()->IsPrimitive());
177 field->SetObj(NULL, new_value);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700178}
179
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700180uint32_t Field::Get32(const Object* object) const {
181 CHECK((object == NULL) == IsStatic());
182 if (IsStatic()) {
183 object = declaring_class_;
184 }
185 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700186}
187
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188void Field::Set32(Object* object, uint32_t new_value) const {
189 CHECK((object == NULL) == IsStatic());
190 if (IsStatic()) {
191 object = declaring_class_;
192 }
193 object->SetField32(GetOffset(), new_value, IsVolatile());
194}
195
196uint64_t Field::Get64(const Object* object) const {
197 CHECK((object == NULL) == IsStatic());
198 if (IsStatic()) {
199 object = declaring_class_;
200 }
201 return object->GetField64(GetOffset(), IsVolatile());
202}
203
204void Field::Set64(Object* object, uint64_t new_value) const {
205 CHECK((object == NULL) == IsStatic());
206 if (IsStatic()) {
207 object = declaring_class_;
208 }
209 object->SetField64(GetOffset(), new_value, IsVolatile());
210}
211
212Object* Field::GetObj(const Object* object) const {
213 CHECK((object == NULL) == IsStatic());
214 if (IsStatic()) {
215 object = declaring_class_;
216 }
217 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
218}
219
220void Field::SetObj(Object* object, const Object* new_value) const {
221 CHECK((object == NULL) == IsStatic());
222 if (IsStatic()) {
223 object = declaring_class_;
224 }
225 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
226}
227
228bool Field::GetBoolean(const Object* object) const {
229 DCHECK(GetType()->IsPrimitiveBoolean());
230 return Get32(object);
231}
232
233void Field::SetBoolean(Object* object, bool z) const {
234 DCHECK(GetType()->IsPrimitiveBoolean());
235 Set32(object, z);
236}
237
238int8_t Field::GetByte(const Object* object) const {
239 DCHECK(GetType()->IsPrimitiveByte());
240 return Get32(object);
241}
242
243void Field::SetByte(Object* object, int8_t b) const {
244 DCHECK(GetType()->IsPrimitiveByte());
245 Set32(object, b);
246}
247
248uint16_t Field::GetChar(const Object* object) const {
249 DCHECK(GetType()->IsPrimitiveChar());
250 return Get32(object);
251}
252
253void Field::SetChar(Object* object, uint16_t c) const {
254 DCHECK(GetType()->IsPrimitiveChar());
255 Set32(object, c);
256}
257
258uint16_t Field::GetShort(const Object* object) const {
259 DCHECK(GetType()->IsPrimitiveShort());
260 return Get32(object);
261}
262
263void Field::SetShort(Object* object, uint16_t s) const {
264 DCHECK(GetType()->IsPrimitiveShort());
265 Set32(object, s);
266}
267
268int32_t Field::GetInt(const Object* object) const {
269 DCHECK(GetType()->IsPrimitiveInt());
270 return Get32(object);
271}
272
273void Field::SetInt(Object* object, int32_t i) const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700274 DCHECK(GetType()->IsPrimitiveInt()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 Set32(object, i);
276}
277
278int64_t Field::GetLong(const Object* object) const {
279 DCHECK(GetType()->IsPrimitiveLong());
280 return Get64(object);
281}
282
283void Field::SetLong(Object* object, int64_t j) const {
284 DCHECK(GetType()->IsPrimitiveLong());
285 Set64(object, j);
286}
287
288float Field::GetFloat(const Object* object) const {
289 DCHECK(GetType()->IsPrimitiveFloat());
290 JValue float_bits;
291 float_bits.i = Get32(object);
292 return float_bits.f;
293}
294
295void Field::SetFloat(Object* object, float f) const {
296 DCHECK(GetType()->IsPrimitiveFloat());
297 JValue float_bits;
298 float_bits.f = f;
299 Set32(object, float_bits.i);
300}
301
302double Field::GetDouble(const Object* object) const {
303 DCHECK(GetType()->IsPrimitiveDouble());
304 JValue double_bits;
305 double_bits.j = Get64(object);
306 return double_bits.d;
307}
308
309void Field::SetDouble(Object* object, double d) const {
310 DCHECK(GetType()->IsPrimitiveDouble());
311 JValue double_bits;
312 double_bits.d = d;
313 Set64(object, double_bits.j);
314}
315
316Object* Field::GetObject(const Object* object) const {
317 CHECK(!GetType()->IsPrimitive());
318 return GetObj(object);
319}
320
321void Field::SetObject(Object* object, const Object* l) const {
322 CHECK(!GetType()->IsPrimitive());
323 SetObj(object, l);
324}
325
326// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700327Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700328Class* Method::java_lang_reflect_Method_ = NULL;
329
Elliott Hughes80609252011-09-23 17:24:51 -0700330void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
331 CHECK(java_lang_reflect_Constructor_ == NULL);
332 CHECK(java_lang_reflect_Constructor != NULL);
333 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
334
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 CHECK(java_lang_reflect_Method_ == NULL);
336 CHECK(java_lang_reflect_Method != NULL);
337 java_lang_reflect_Method_ = java_lang_reflect_Method;
338}
339
Elliott Hughes80609252011-09-23 17:24:51 -0700340void Method::ResetClasses() {
341 CHECK(java_lang_reflect_Constructor_ != NULL);
342 java_lang_reflect_Constructor_ = NULL;
343
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700344 CHECK(java_lang_reflect_Method_ != NULL);
345 java_lang_reflect_Method_ = NULL;
346}
347
Elliott Hughes418d20f2011-09-22 14:00:39 -0700348Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
349 if (*p == '[') {
350 // Something like "[[[Ljava/lang/String;".
351 const char* start = p;
352 while (*p == '[') {
353 ++p;
354 }
355 if (*p == 'L') {
356 while (*p != ';') {
357 ++p;
358 }
359 }
360 ++p; // Either the ';' or the primitive type.
361
362 StringPiece descriptor(start, (p - start));
363 return class_linker->FindClass(descriptor, cl);
364 } else if (*p == 'L') {
365 const char* start = p;
366 while (*p != ';') {
367 ++p;
368 }
369 ++p;
370 StringPiece descriptor(start, (p - start));
371 return class_linker->FindClass(descriptor, cl);
372 } else {
373 return class_linker->FindPrimitiveClass(*p++);
374 }
375}
376
377void Method::InitJavaFieldsLocked() {
378 // Create the array.
379 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
380 size_t arg_count = GetShorty()->GetLength() - 1;
381 Class* array_class = class_linker->FindSystemClass("[Ljava/lang/Class;");
382 ObjectArray<Class>* parameters = ObjectArray<Class>::Alloc(array_class, arg_count);
383 if (parameters == NULL) {
384 return;
385 }
386
387 // Parse the signature, filling the array.
388 const ClassLoader* cl = GetDeclaringClass()->GetClassLoader();
389 std::string signature(GetSignature()->ToModifiedUtf8());
390 const char* p = signature.c_str();
391 DCHECK_EQ(*p, '(');
392 ++p;
393 for (size_t i = 0; i < arg_count; ++i) {
394 Class* c = ExtractNextClassFromSignature(class_linker, cl, p);
395 if (c == NULL) {
396 return;
397 }
398 parameters->Set(i, c);
399 }
400
401 DCHECK_EQ(*p, ')');
402 ++p;
403
404 java_parameter_types_ = parameters;
405 java_return_type_ = ExtractNextClassFromSignature(class_linker, cl, p);
406}
407
408void Method::InitJavaFields() {
409 Thread* self = Thread::Current();
410 ScopedThreadStateChange tsc(self, Thread::kRunnable);
411 MonitorEnter(self);
412 if (java_parameter_types_ == NULL || java_return_type_ == NULL) {
413 InitJavaFieldsLocked();
414 }
415 MonitorExit(self);
416}
417
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700418ObjectArray<String>* Method::GetDexCacheStrings() const {
419 return GetFieldObject<ObjectArray<String>*>(
420 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
421}
422
423void Method::SetReturnTypeIdx(uint32_t new_return_type_idx) {
424 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
425 new_return_type_idx, false);
426}
427
428Class* Method::GetReturnType() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700429 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430 // Short-cut
431 Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx());
432 if (result == NULL) {
433 // Do full linkage and set cache value for next call
434 result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this);
435 }
436 CHECK(result != NULL);
437 return result;
438}
439
440void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
441 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
442 new_dex_cache_strings, false);
443}
444
445ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
446 return GetFieldObject<ObjectArray<Class>*>(
447 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
448}
449
450void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
451 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
452 new_dex_cache_classes, false);
453}
454
455ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
456 return GetFieldObject<ObjectArray<Method>*>(
457 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
458}
459
460void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
461 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
462 new_dex_cache_methods, false);
463}
464
465ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
466 return GetFieldObject<ObjectArray<Field>*>(
467 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
468}
469
470void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
471 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
472 new_dex_cache_fields, false);
473}
474
475CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
476 return GetFieldPtr<CodeAndDirectMethods*>(
477 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
478 false);
479}
480
481void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
482 SetFieldPtr<CodeAndDirectMethods*>(
483 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
484 new_value, false);
485}
486
487ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
488 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
489 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
490 false);
491}
492
493void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
494 SetFieldObject(
495 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
496 new_value, false);
497
498}
499
500size_t Method::NumArgRegisters(const StringPiece& shorty) {
501 CHECK_LE(1, shorty.length());
502 uint32_t num_registers = 0;
503 for (int i = 1; i < shorty.length(); ++i) {
504 char ch = shorty[i];
505 if (ch == 'D' || ch == 'J') {
506 num_registers += 2;
507 } else {
508 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700509 }
510 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700511 return num_registers;
512}
513
514size_t Method::NumArgArrayBytes() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700515 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700516 size_t num_bytes = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700517 for (int i = 1; i < shorty->GetLength(); ++i) {
518 char ch = shorty->CharAt(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700519 if (ch == 'D' || ch == 'J') {
520 num_bytes += 8;
521 } else if (ch == 'L') {
522 // Argument is a reference or an array. The shorty descriptor
523 // does not distinguish between these types.
524 num_bytes += sizeof(Object*);
525 } else {
526 num_bytes += 4;
527 }
528 }
529 return num_bytes;
530}
531
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700532size_t Method::NumArgs() const {
533 // "1 +" because the first in Args is the receiver.
534 // "- 1" because we don't count the return type.
535 return (IsStatic() ? 0 : 1) + GetShorty()->GetLength() - 1;
536}
537
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700538// The number of reference arguments to this method including implicit this
539// pointer
540size_t Method::NumReferenceArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700541 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700542 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700543 for (int i = 1; i < shorty->GetLength(); i++) {
544 char ch = shorty->CharAt(i);
545 if ((ch == 'L') || (ch == '[')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700546 result++;
547 }
548 }
549 return result;
550}
551
552// The number of long or double arguments
553size_t Method::NumLongOrDoubleArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700554 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700555 size_t result = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700556 for (int i = 1; i < shorty->GetLength(); i++) {
557 char ch = shorty->CharAt(i);
558 if ((ch == 'D') || (ch == 'J')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700559 result++;
560 }
561 }
562 return result;
563}
564
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700565// Is the given method parameter a reference?
566bool Method::IsParamAReference(unsigned int param) const {
567 CHECK_LT(param, NumArgs());
568 if (IsStatic()) {
569 param++; // 0th argument must skip return value at start of the shorty
570 } else if (param == 0) {
571 return true; // this argument
572 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700573 return GetShorty()->CharAt(param) == 'L';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700574}
575
576// Is the given method parameter a long or double?
577bool Method::IsParamALongOrDouble(unsigned int param) const {
578 CHECK_LT(param, NumArgs());
579 if (IsStatic()) {
580 param++; // 0th argument must skip return value at start of the shorty
581 } else if (param == 0) {
582 return false; // this argument
583 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700584 char ch = GetShorty()->CharAt(param);
585 return (ch == 'J' || ch == 'D');
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700586}
587
588static size_t ShortyCharToSize(char x) {
589 switch (x) {
590 case 'V': return 0;
591 case '[': return kPointerSize;
592 case 'L': return kPointerSize;
593 case 'D': return 8;
594 case 'J': return 8;
595 default: return 4;
596 }
597}
598
599size_t Method::ParamSize(unsigned int param) const {
600 CHECK_LT(param, NumArgs());
601 if (IsStatic()) {
602 param++; // 0th argument must skip return value at start of the shorty
603 } else if (param == 0) {
604 return kPointerSize; // this argument
605 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700606 return ShortyCharToSize(GetShorty()->CharAt(param));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700607}
608
609size_t Method::ReturnSize() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700610 return ShortyCharToSize(GetShorty()->CharAt(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700611}
612
613bool Method::HasSameNameAndDescriptor(const Method* that) const {
614 return (this->GetName()->Equals(that->GetName()) &&
615 this->GetSignature()->Equals(that->GetSignature()));
616}
617
Ian Rogersbdb03912011-09-14 00:55:44 -0700618uint32_t Method::ToDexPC(const uintptr_t pc) const {
619 IntArray* mapping_table = GetMappingTable();
620 if (mapping_table == NULL) {
Ian Rogers67375ac2011-09-14 00:55:44 -0700621 DCHECK(IsNative());
622 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700623 }
624 size_t mapping_table_length = mapping_table->GetLength();
625 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetCode());
626 CHECK_LT(sought_offset, static_cast<uint32_t>(GetCodeArray()->GetLength()));
627 uint32_t best_offset = 0;
628 uint32_t best_dex_offset = 0;
629 for (size_t i = 0; i < mapping_table_length; i += 2) {
630 uint32_t map_offset = mapping_table->Get(i);
631 uint32_t map_dex_offset = mapping_table->Get(i + 1);
632 if (map_offset == sought_offset) {
633 best_offset = map_offset;
634 best_dex_offset = map_dex_offset;
635 break;
636 }
637 if (map_offset < sought_offset && map_offset > best_offset) {
638 best_offset = map_offset;
639 best_dex_offset = map_dex_offset;
640 }
641 }
642 return best_dex_offset;
643}
644
645uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
646 IntArray* mapping_table = GetMappingTable();
647 if (mapping_table == NULL) {
648 DCHECK(dex_pc == 0);
649 return 0; // Special no mapping/pc == 0 case
650 }
651 size_t mapping_table_length = mapping_table->GetLength();
652 for (size_t i = 0; i < mapping_table_length; i += 2) {
653 uint32_t map_offset = mapping_table->Get(i);
654 uint32_t map_dex_offset = mapping_table->Get(i + 1);
655 if (map_dex_offset == dex_pc) {
656 DCHECK_LT(map_offset, static_cast<uint32_t>(GetCodeArray()->GetLength()));
657 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
658 }
659 }
660 LOG(FATAL) << "Looking up Dex PC not contained in method";
661 return 0;
662}
663
664uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
665 DexCache* dex_cache = GetDeclaringClass()->GetDexCache();
666 const ClassLoader* class_loader = GetDeclaringClass()->GetClassLoader();
667 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
668 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
669 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(GetCodeItemOffset());
670 // Iterate over the catch handlers associated with dex_pc
671 for (DexFile::CatchHandlerIterator iter = dex_file.dexFindCatchHandler(*code_item, dex_pc);
672 !iter.HasNext(); iter.Next()) {
673 uint32_t iter_type_idx = iter.Get().type_idx_;
674 // Catch all case
Elliott Hughes80609252011-09-23 17:24:51 -0700675 if (iter_type_idx == DexFile::kDexNoIndex) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700676 return iter.Get().address_;
677 }
678 // Does this catch exception type apply?
679 Class* iter_exception_type =
680 class_linker->ResolveType(dex_file, iter_type_idx, dex_cache, class_loader);
681 if (iter_exception_type->IsAssignableFrom(exception_type)) {
682 return iter.Get().address_;
683 }
684 }
685 // Handler not found
686 return DexFile::kDexNoIndex;
687}
688
buzbee4ef76522011-09-08 10:00:32 -0700689void Method::SetCode(ByteArray* code_array, InstructionSet instruction_set,
buzbeec41e5b52011-09-23 12:46:19 -0700690 IntArray* mapping_table, ShortArray* vmap_table) {
Elliott Hughes1240dad2011-09-09 16:24:50 -0700691 CHECK(GetCode() == NULL || IsNative());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700692 SetFieldPtr<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), code_array, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700693 SetFieldPtr<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
buzbee4ef76522011-09-08 10:00:32 -0700694 mapping_table, false);
buzbeec41e5b52011-09-23 12:46:19 -0700695 SetFieldPtr<ShortArray*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_),
696 vmap_table, false);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700697 int8_t* code = code_array->GetData();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700698 uintptr_t address = reinterpret_cast<uintptr_t>(code);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700699 if (instruction_set == kThumb2) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700700 // Set the low-order bit so a BLX will switch to Thumb mode
701 address |= 0x1;
702 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700703 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_),
704 reinterpret_cast<const void*>(address), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700705}
706
Ian Rogersbdb03912011-09-14 00:55:44 -0700707bool Method::IsWithinCode(uintptr_t pc) const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700708 if (pc == 0) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700709 // PC of 0 represents the beginning of a stack trace either a native or where we have a callee
710 // save method that has no code
711 DCHECK(IsNative() || IsPhony());
Ian Rogersbdb03912011-09-14 00:55:44 -0700712 return true;
713 } else {
Ian Rogers93dd9662011-09-17 23:21:22 -0700714#if defined(__arm__)
715 pc &= ~0x1; // clear any possible thumb instruction mode bit
716#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700717 uint32_t rel_offset = pc - reinterpret_cast<uintptr_t>(GetCodeArray()->GetData());
Ian Rogers93dd9662011-09-17 23:21:22 -0700718 // Strictly the following test should be a less-than, however, if the last
719 // instruction is a call to an exception throw we may see return addresses
720 // that are 1 beyond the end of code.
721 return rel_offset <= static_cast<uint32_t>(GetCodeArray()->GetLength());
Ian Rogersbdb03912011-09-14 00:55:44 -0700722 }
723}
724
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700725void Method::SetInvokeStub(const ByteArray* invoke_stub_array) {
726 const InvokeStub* invoke_stub = reinterpret_cast<InvokeStub*>(invoke_stub_array->GetData());
727 SetFieldPtr<const ByteArray*>(
728 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), invoke_stub_array, false);
729 SetFieldPtr<const InvokeStub*>(
730 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), invoke_stub, false);
731}
732
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700733void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
734 // Push a transition back into managed code onto the linked list in thread.
735 CHECK_EQ(Thread::kRunnable, self->GetState());
736 NativeToManagedRecord record;
737 self->PushNativeToManagedRecord(&record);
738
739 // Call the invoke stub associated with the method.
740 // Pass everything as arguments.
741 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700742
743 bool have_executable_code = (GetCode() != NULL);
744#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700745 // Currently we can only compile non-native methods for ARM.
746 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700747#endif
748
749 if (have_executable_code && stub != NULL) {
750 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700751 (*stub)(this, receiver, self, args, result);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700752 LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700753 } else {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700754 if (Runtime::Current()->IsStarted()) {
755 LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
756 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700757 if (result != NULL) {
758 result->j = 0;
759 }
760 }
761
762 // Pop transition.
763 self->PopNativeToManagedRecord(record);
764}
765
Brian Carlstrom16192862011-09-12 17:50:06 -0700766bool Method::IsRegistered() {
767 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
768 void* jni_stub = Runtime::Current()->GetJniStubArray()->GetData();
769 return native_method != jni_stub;
770}
771
772void Method::RegisterNative(const void* native_method) {
773 CHECK(IsNative());
774 CHECK(native_method != NULL);
775 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
776 native_method, false);
777}
778
779void Method::UnregisterNative() {
780 CHECK(IsNative());
781 // restore stub to lookup native pointer via dlsym
782 RegisterNative(Runtime::Current()->GetJniStubArray()->GetData());
783}
784
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700785void Class::SetStatus(Status new_status) {
786 CHECK(new_status > GetStatus() || new_status == kStatusError ||
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700787 !Runtime::Current()->IsStarted()) << PrettyClass(this);
788 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700789 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_),
790 new_status, false);
791}
792
793DexCache* Class::GetDexCache() const {
794 return GetFieldObject<DexCache*>(
795 OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
796}
797
798void Class::SetDexCache(DexCache* new_dex_cache) {
799 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_),
800 new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700801}
802
Brian Carlstrom1f870082011-08-23 16:02:11 -0700803Object* Class::AllocObject() {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700804 DCHECK(!IsAbstract()) << PrettyClass(this);
805 DCHECK(!IsInterface()) << PrettyClass(this);
806 DCHECK(!IsPrimitive()) << PrettyClass(this);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700807 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700808}
809
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700810void Class::DumpClass(std::ostream& os, int flags) {
811 if ((flags & kDumpClassFullDetail) == 0) {
812 os << PrettyClass(this);
813 if ((flags & kDumpClassClassLoader) != 0) {
814 os << ' ' << GetClassLoader();
815 }
816 if ((flags & kDumpClassInitialized) != 0) {
817 os << ' ' << GetStatus();
818 }
819 os << std::endl;
820 return;
821 }
822
823 Class* super = GetSuperClass();
824 os << "----- " << (IsInterface() ? "interface" : "class") << " "
825 << "'" << GetDescriptor()->ToModifiedUtf8() << "' cl=" << GetClassLoader() << " -----\n",
826 os << " objectSize=" << SizeOf() << " "
827 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
828 os << StringPrintf(" access=0x%04x.%04x\n",
829 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
830 if (super != NULL) {
831 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
832 }
833 if (IsArrayClass()) {
834 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
835 }
836 if (NumInterfaces() > 0) {
837 os << " interfaces (" << NumInterfaces() << "):\n";
838 for (size_t i = 0; i < NumInterfaces(); ++i) {
839 Class* interface = GetInterface(i);
840 const ClassLoader* cl = interface->GetClassLoader();
841 os << StringPrintf(" %2d: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
842 }
843 }
844 os << " vtable (" << NumVirtualMethods() << " entries, "
845 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
846 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
847 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetVirtualMethod(i)).c_str());
848 }
849 os << " direct methods (" << NumDirectMethods() << " entries):\n";
850 for (size_t i = 0; i < NumDirectMethods(); ++i) {
851 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
852 }
853 if (NumStaticFields() > 0) {
854 os << " static fields (" << NumStaticFields() << " entries):\n";
855 for (size_t i = 0; i < NumStaticFields(); ++i) {
856 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetStaticField(i)).c_str());
857 }
858 }
859 if (NumInstanceFields() > 0) {
860 os << " instance fields (" << NumInstanceFields() << " entries):\n";
861 for (size_t i = 0; i < NumInstanceFields(); ++i) {
862 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
863 }
864 }
865}
866
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700867void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
868 if (new_reference_offsets != CLASS_WALK_SUPER) {
869 // Sanity check that the number of bits set in the reference offset bitmap
870 // agrees with the number of references
871 Class* cur = this;
872 size_t cnt = 0;
873 while (cur) {
874 cnt += cur->NumReferenceInstanceFieldsDuringLinking();
875 cur = cur->GetSuperClass();
876 }
877 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt);
878 }
879 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
880 new_reference_offsets, false);
881}
882
883void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
884 if (new_reference_offsets != CLASS_WALK_SUPER) {
885 // Sanity check that the number of bits set in the reference offset bitmap
886 // agrees with the number of references
887 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
888 NumReferenceStaticFieldsDuringLinking());
889 }
890 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
891 new_reference_offsets, false);
892}
893
894size_t Class::PrimitiveSize() const {
895 switch (GetPrimitiveType()) {
896 case kPrimBoolean:
897 case kPrimByte:
898 case kPrimChar:
899 case kPrimShort:
900 case kPrimInt:
901 case kPrimFloat:
902 return sizeof(int32_t);
903 case kPrimLong:
904 case kPrimDouble:
905 return sizeof(int64_t);
906 default:
907 LOG(FATAL) << "Primitive type size calculation on invalid type " << this;
908 return 0;
909 }
910}
911
912size_t Class::GetTypeSize(const String* descriptor) {
913 switch (descriptor->CharAt(0)) {
914 case 'B': return 1; // byte
915 case 'C': return 2; // char
916 case 'D': return 8; // double
917 case 'F': return 4; // float
918 case 'I': return 4; // int
919 case 'J': return 8; // long
920 case 'S': return 2; // short
921 case 'Z': return 1; // boolean
922 case 'L': return sizeof(Object*);
923 case '[': return sizeof(Array*);
924 default:
925 LOG(ERROR) << "Unknown type " << descriptor;
926 return 0;
927 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700928}
929
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700930bool Class::Implements(const Class* klass) const {
931 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700932 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700933 // All interfaces implemented directly and by our superclass, and
934 // recursively all super-interfaces of those interfaces, are listed
935 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700936 int32_t iftable_count = GetIfTableCount();
937 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
938 for (int32_t i = 0; i < iftable_count; i++) {
939 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700940 return true;
941 }
942 }
943 return false;
944}
945
946// Determine whether "this" is assignable from "klazz", where both of these
947// are array classes.
948//
949// Consider an array class, e.g. Y[][], where Y is a subclass of X.
950// Y[][] = Y[][] --> true (identity)
951// X[][] = Y[][] --> true (element superclass)
952// Y = Y[][] --> false
953// Y[] = Y[][] --> false
954// Object = Y[][] --> true (everything is an object)
955// Object[] = Y[][] --> true
956// Object[][] = Y[][] --> true
957// Object[][][] = Y[][] --> false (too many []s)
958// Serializable = Y[][] --> true (all arrays are Serializable)
959// Serializable[] = Y[][] --> true
960// Serializable[][] = Y[][] --> false (unless Y is Serializable)
961//
962// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700963// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700964//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700965bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700966 DCHECK(IsArrayClass()) << PrettyClass(this);
967 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700968 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700969}
970
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700971bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700972 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
973 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700974 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700975 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700976 // src's super should be java_lang_Object, since it is an array.
977 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700978 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
979 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700980 return this == java_lang_Object;
981 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700982 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700983}
984
985bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700986 DCHECK(!IsInterface()) << PrettyClass(this);
987 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700988 const Class* current = this;
989 do {
990 if (current == klass) {
991 return true;
992 }
993 current = current->GetSuperClass();
994 } while (current != NULL);
995 return false;
996}
997
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700998bool Class::IsInSamePackage(const String* descriptor_string_1,
999 const String* descriptor_string_2) {
1000 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
1001 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
1002
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001003 size_t i = 0;
1004 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
1005 ++i;
1006 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001007 if (descriptor1.find('/', i) != StringPiece::npos ||
1008 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001009 return false;
1010 } else {
1011 return true;
1012 }
1013}
1014
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001015#if 0
Ian Rogersb033c752011-07-20 12:22:35 -07001016bool Class::IsInSamePackage(const StringPiece& descriptor1,
1017 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001018 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001019 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -07001020 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
1021 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001022 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
1023}
1024#endif
1025
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001026bool Class::IsInSamePackage(const Class* that) const {
1027 const Class* klass1 = this;
1028 const Class* klass2 = that;
1029 if (klass1 == klass2) {
1030 return true;
1031 }
1032 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001033 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001034 return false;
1035 }
1036 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -07001037 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001038 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001039 }
jeffhao4a801a42011-09-23 13:53:40 -07001040 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001041 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001042 }
1043 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001044 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001045}
1046
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001047const ClassLoader* Class::GetClassLoader() const {
1048 return GetFieldObject<const ClassLoader*>(
1049 OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -07001050}
1051
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001052void Class::SetClassLoader(const ClassLoader* new_cl) {
1053 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
1054 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_),
1055 new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001056}
1057
Brian Carlstrom30b94452011-08-25 21:35:26 -07001058Method* Class::FindVirtualMethodForInterface(Method* method) {
1059 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001060 DCHECK(declaring_class != NULL) << PrettyClass(this);
1061 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001062 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001063 int32_t iftable_count = GetIfTableCount();
1064 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1065 for (int32_t i = 0; i < iftable_count; i++) {
1066 InterfaceEntry* interface_entry = iftable->Get(i);
1067 if (interface_entry->GetInterface() == declaring_class) {
1068 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001069 }
1070 }
Brian Carlstrom16192862011-09-12 17:50:06 -07001071 UNIMPLEMENTED(FATAL) << "Need to throw an error of some kind " << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001072 return NULL;
1073}
1074
jeffhaobdb76512011-09-07 11:43:16 -07001075Method* Class::FindInterfaceMethod(const StringPiece& name,
1076 const StringPiece& signature) {
1077 // Check the current class before checking the interfaces.
1078 Method* method = FindVirtualMethod(name, signature);
1079 if (method != NULL) {
1080 return method;
1081 }
1082
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001083 int32_t iftable_count = GetIfTableCount();
1084 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1085 for (int32_t i = 0; i < iftable_count; i++) {
1086 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001087 if (method != NULL) {
1088 return method;
1089 }
1090 }
1091 return NULL;
1092}
1093
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001094Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001095 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001096 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001097 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001098 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001099 method->GetSignature()->Equals(signature)) {
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
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001106Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001107 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001108 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001109 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001110 if (method != NULL) {
1111 return method;
1112 }
1113 }
1114 return NULL;
1115}
1116
1117Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001118 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001119 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001120 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001121 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001122 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001123 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001124 }
1125 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001126 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001127}
1128
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001129Method* Class::FindVirtualMethod(const StringPiece& name,
1130 const StringPiece& descriptor) {
1131 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1132 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
1133 if (method != NULL) {
1134 return method;
1135 }
1136 }
1137 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001138}
1139
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001140Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 // Is the field in this class?
1142 // Interfaces are not relevant because they can't contain instance fields.
1143 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1144 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001145 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 return f;
1147 }
1148 }
1149 return NULL;
1150}
1151
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001152Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 // Is the field in this class, or any of its superclasses?
1154 // Interfaces are not relevant because they can't contain instance fields.
1155 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001156 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 if (f != NULL) {
1158 return f;
1159 }
1160 }
1161 return NULL;
1162}
1163
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001164Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
1165 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 for (size_t i = 0; i < NumStaticFields(); ++i) {
1167 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001168 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 return f;
1170 }
1171 }
1172 return NULL;
1173}
1174
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001175Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 // Is the field in this class (or its interfaces), or any of its
1177 // superclasses (or their interfaces)?
1178 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1179 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001180 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 if (f != NULL) {
1182 return f;
1183 }
1184
1185 // Is this field in any of this class' interfaces?
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001186 for (int32_t i = 0; i < c->GetIfTableCount(); ++i) {
1187 InterfaceEntry* interface_entry = c->GetIfTable()->Get(i);
1188 Class* interface = interface_entry->GetInterface();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001189 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 if (f != NULL) {
1191 return f;
1192 }
1193 }
1194 }
1195 return NULL;
1196}
1197
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001198Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001199 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001200 DCHECK_GE(component_count, 0);
1201 DCHECK(array_class->IsArrayClass());
1202 size_t size = SizeOf(component_count, component_size);
1203 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1204 if (array != NULL) {
1205 DCHECK(array->IsArrayInstance());
1206 array->SetLength(component_count);
1207 }
1208 return array;
1209}
1210
1211Array* Array::Alloc(Class* array_class, int32_t component_count) {
1212 return Alloc(array_class, component_count, array_class->GetComponentSize());
1213}
1214
Elliott Hughes80609252011-09-23 17:24:51 -07001215bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
1216 Thread::Current()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
1217 "length=%i; index=%i", length_, index);
1218 return false;
1219}
1220
1221bool Array::ThrowArrayStoreException(Object* object) const {
1222 Thread::Current()->ThrowNewException("Ljava/lang/ArrayStoreException;",
1223 "Can't store an element of type %s into an array of type %s",
1224 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1225 return false;
1226}
1227
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001228template<typename T>
1229PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001230 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001231 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1232 return down_cast<PrimitiveArray<T>*>(raw_array);
1233}
1234
1235template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1236
1237// Explicitly instantiate all the primitive array types.
1238template class PrimitiveArray<uint8_t>; // BooleanArray
1239template class PrimitiveArray<int8_t>; // ByteArray
1240template class PrimitiveArray<uint16_t>; // CharArray
1241template class PrimitiveArray<double>; // DoubleArray
1242template class PrimitiveArray<float>; // FloatArray
1243template class PrimitiveArray<int32_t>; // IntArray
1244template class PrimitiveArray<int64_t>; // LongArray
1245template class PrimitiveArray<int16_t>; // ShortArray
1246
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001247// TODO: get global references for these
1248Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001249
Brian Carlstroma663ea52011-08-19 23:33:41 -07001250void String::SetClass(Class* java_lang_String) {
1251 CHECK(java_lang_String_ == NULL);
1252 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001253 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001254}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001255
Brian Carlstroma663ea52011-08-19 23:33:41 -07001256void String::ResetClass() {
1257 CHECK(java_lang_String_ != NULL);
1258 java_lang_String_ = NULL;
1259}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001260
Brian Carlstromc74255f2011-09-11 22:47:39 -07001261String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001262 return Runtime::Current()->GetInternTable()->InternWeak(this);
1263}
1264
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001265int32_t String::GetHashCode() const {
1266 int32_t result = GetField32(
1267 OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1268 DCHECK(result != 0 ||
1269 ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0);
1270 return result;
1271}
1272
1273int32_t String::GetLength() const {
1274 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1275 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1276 return result;
1277}
1278
1279uint16_t String::CharAt(int32_t index) const {
1280 // TODO: do we need this? Equals is the only caller, and could
1281 // bounds check itself.
1282 if (index < 0 || index >= count_) {
1283 Thread* self = Thread::Current();
1284 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1285 "length=%i; index=%i", count_, index);
1286 return 0;
1287 }
1288 return GetCharArray()->Get(index + GetOffset());
1289}
1290
1291String* String::AllocFromUtf16(int32_t utf16_length,
1292 const uint16_t* utf16_data_in,
1293 int32_t hash_code) {
1294 String* string = Alloc(GetJavaLangString(), utf16_length);
1295 // TODO: use 16-bit wide memset variant
1296 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
1297 for (int i = 0; i < utf16_length; i++) {
1298 array->Set(i, utf16_data_in[i]);
1299 }
1300 if (hash_code != 0) {
1301 string->SetHashCode(hash_code);
1302 } else {
1303 string->ComputeHashCode();
1304 }
1305 return string;
1306}
1307
1308String* String::AllocFromModifiedUtf8(const char* utf) {
1309 size_t char_count = CountModifiedUtf8Chars(utf);
1310 return AllocFromModifiedUtf8(char_count, utf);
1311}
1312
1313String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1314 const char* utf8_data_in) {
1315 String* string = Alloc(GetJavaLangString(), utf16_length);
1316 uint16_t* utf16_data_out =
1317 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1318 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1319 string->ComputeHashCode();
1320 return string;
1321}
1322
1323String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
1324 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1325}
1326
1327String* String::Alloc(Class* java_lang_String, CharArray* array) {
1328 String* string = down_cast<String*>(java_lang_String->AllocObject());
1329 string->SetArray(array);
1330 string->SetCount(array->GetLength());
1331 return string;
1332}
1333
1334bool String::Equals(const String* that) const {
1335 if (this == that) {
1336 // Quick reference equality test
1337 return true;
1338 } else if (that == NULL) {
1339 // Null isn't an instanceof anything
1340 return false;
1341 } else if (this->GetLength() != that->GetLength()) {
1342 // Quick length inequality test
1343 return false;
1344 } else {
1345 // NB don't short circuit on hash code as we're presumably here as the
1346 // hash code was already equal
1347 for (int32_t i = 0; i < that->GetLength(); ++i) {
1348 if (this->CharAt(i) != that->CharAt(i)) {
1349 return false;
1350 }
1351 }
1352 return true;
1353 }
1354}
1355
1356bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1357 int32_t that_length) const {
1358 if (this->GetLength() != that_length) {
1359 return false;
1360 } else {
1361 for (int32_t i = 0; i < that_length; ++i) {
1362 if (this->CharAt(i) != that_chars[that_offset + i]) {
1363 return false;
1364 }
1365 }
1366 return true;
1367 }
1368}
1369
1370bool String::Equals(const char* modified_utf8) const {
1371 for (int32_t i = 0; i < GetLength(); ++i) {
1372 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1373 if (ch == '\0' || ch != CharAt(i)) {
1374 return false;
1375 }
1376 }
1377 return *modified_utf8 == '\0';
1378}
1379
1380bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001381 if (modified_utf8.size() != GetLength()) {
1382 return false;
1383 }
1384 const char* p = modified_utf8.data();
1385 for (int32_t i = 0; i < GetLength(); ++i) {
1386 uint16_t ch = GetUtf16FromUtf8(&p);
1387 if (ch != CharAt(i)) {
1388 return false;
1389 }
1390 }
1391 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001392}
1393
1394// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1395std::string String::ToModifiedUtf8() const {
1396 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1397 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1398 std::string result(byte_count, char(0));
1399 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1400 return result;
1401}
1402
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001403Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1404
1405void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1406 CHECK(java_lang_StackTraceElement_ == NULL);
1407 CHECK(java_lang_StackTraceElement != NULL);
1408 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1409}
1410
1411void StackTraceElement::ResetClass() {
1412 CHECK(java_lang_StackTraceElement_ != NULL);
1413 java_lang_StackTraceElement_ = NULL;
1414}
1415
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001416StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1417 const String* method_name,
1418 const String* file_name,
1419 int32_t line_number) {
1420 StackTraceElement* trace =
1421 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1422 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1423 const_cast<String*>(declaring_class), false);
1424 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1425 const_cast<String*>(method_name), false);
1426 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1427 const_cast<String*>(file_name), false);
1428 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1429 line_number, false);
1430 return trace;
1431}
1432
Elliott Hughes1f359b02011-07-17 14:27:17 -07001433static const char* kClassStatusNames[] = {
1434 "Error",
1435 "NotReady",
1436 "Idx",
1437 "Loaded",
1438 "Resolved",
1439 "Verifying",
1440 "Verified",
1441 "Initializing",
1442 "Initialized"
1443};
1444std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1445 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001446 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001447 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001448 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001449 }
1450 return os;
1451}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001452
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001453} // namespace art