blob: 9f225d766dd3c318a065b1d5821a3cdf4fc2e9bc [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"
Elliott Hughes68e76522011-10-05 13:22:16 -070022#include "stack.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070023
24namespace art {
25
Elliott Hughes081be7f2011-09-18 16:50:26 -070026Object* Object::Clone() {
27 Class* c = GetClass();
28 DCHECK(!c->IsClassClass());
29
30 // Object::SizeOf gets the right size even if we're an array.
31 // Using c->AllocObject() here would be wrong.
32 size_t num_bytes = SizeOf();
33 Object* copy = Heap::AllocObject(c, num_bytes);
34 if (copy == NULL) {
35 return NULL;
36 }
37
38 // Copy instance data. We assume memcpy copies by words.
39 // TODO: expose and use move32.
40 byte* src_bytes = reinterpret_cast<byte*>(this);
41 byte* dst_bytes = reinterpret_cast<byte*>(copy);
42 size_t offset = sizeof(Object);
43 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
44
Elliott Hughes20cde902011-10-04 17:37:27 -070045 if (c->IsFinalizable()) {
Elliott Hughesadb460d2011-10-05 17:02:34 -070046 Heap::AddFinalizerReference(copy);
Elliott Hughes20cde902011-10-04 17:37:27 -070047 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070048
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
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700326bool Method::IsClassInitializer() const {
327 return IsStatic() && GetName()->Equals("<clinit>");
328}
329
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700331Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700332Class* Method::java_lang_reflect_Method_ = NULL;
333
Elliott Hughes80609252011-09-23 17:24:51 -0700334void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
335 CHECK(java_lang_reflect_Constructor_ == NULL);
336 CHECK(java_lang_reflect_Constructor != NULL);
337 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
338
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700339 CHECK(java_lang_reflect_Method_ == NULL);
340 CHECK(java_lang_reflect_Method != NULL);
341 java_lang_reflect_Method_ = java_lang_reflect_Method;
342}
343
Elliott Hughes80609252011-09-23 17:24:51 -0700344void Method::ResetClasses() {
345 CHECK(java_lang_reflect_Constructor_ != NULL);
346 java_lang_reflect_Constructor_ = NULL;
347
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 CHECK(java_lang_reflect_Method_ != NULL);
349 java_lang_reflect_Method_ = NULL;
350}
351
Elliott Hughes418d20f2011-09-22 14:00:39 -0700352Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
353 if (*p == '[') {
354 // Something like "[[[Ljava/lang/String;".
355 const char* start = p;
356 while (*p == '[') {
357 ++p;
358 }
359 if (*p == 'L') {
360 while (*p != ';') {
361 ++p;
362 }
363 }
364 ++p; // Either the ';' or the primitive type.
365
366 StringPiece descriptor(start, (p - start));
367 return class_linker->FindClass(descriptor, cl);
368 } else if (*p == 'L') {
369 const char* start = p;
370 while (*p != ';') {
371 ++p;
372 }
373 ++p;
374 StringPiece descriptor(start, (p - start));
375 return class_linker->FindClass(descriptor, cl);
376 } else {
377 return class_linker->FindPrimitiveClass(*p++);
378 }
379}
380
381void Method::InitJavaFieldsLocked() {
382 // Create the array.
383 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
384 size_t arg_count = GetShorty()->GetLength() - 1;
385 Class* array_class = class_linker->FindSystemClass("[Ljava/lang/Class;");
386 ObjectArray<Class>* parameters = ObjectArray<Class>::Alloc(array_class, arg_count);
387 if (parameters == NULL) {
388 return;
389 }
390
391 // Parse the signature, filling the array.
392 const ClassLoader* cl = GetDeclaringClass()->GetClassLoader();
393 std::string signature(GetSignature()->ToModifiedUtf8());
394 const char* p = signature.c_str();
395 DCHECK_EQ(*p, '(');
396 ++p;
397 for (size_t i = 0; i < arg_count; ++i) {
398 Class* c = ExtractNextClassFromSignature(class_linker, cl, p);
399 if (c == NULL) {
400 return;
401 }
402 parameters->Set(i, c);
403 }
404
405 DCHECK_EQ(*p, ')');
406 ++p;
407
408 java_parameter_types_ = parameters;
409 java_return_type_ = ExtractNextClassFromSignature(class_linker, cl, p);
410}
411
412void Method::InitJavaFields() {
413 Thread* self = Thread::Current();
414 ScopedThreadStateChange tsc(self, Thread::kRunnable);
415 MonitorEnter(self);
416 if (java_parameter_types_ == NULL || java_return_type_ == NULL) {
417 InitJavaFieldsLocked();
418 }
419 MonitorExit(self);
420}
421
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422ObjectArray<String>* Method::GetDexCacheStrings() const {
423 return GetFieldObject<ObjectArray<String>*>(
424 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
425}
426
427void Method::SetReturnTypeIdx(uint32_t new_return_type_idx) {
428 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
429 new_return_type_idx, false);
430}
431
432Class* Method::GetReturnType() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700433 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700434 // Short-cut
435 Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx());
436 if (result == NULL) {
437 // Do full linkage and set cache value for next call
438 result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this);
439 }
Elliott Hughes14134a12011-09-30 16:55:51 -0700440 CHECK(result != NULL) << PrettyMethod(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700441 return result;
442}
443
444void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
445 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
446 new_dex_cache_strings, false);
447}
448
449ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
450 return GetFieldObject<ObjectArray<Class>*>(
451 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
452}
453
454void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
455 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
456 new_dex_cache_classes, false);
457}
458
459ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
460 return GetFieldObject<ObjectArray<Method>*>(
461 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
462}
463
464void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
465 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
466 new_dex_cache_methods, false);
467}
468
469ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
470 return GetFieldObject<ObjectArray<Field>*>(
471 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
472}
473
474void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
475 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
476 new_dex_cache_fields, false);
477}
478
479CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
480 return GetFieldPtr<CodeAndDirectMethods*>(
481 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
482 false);
483}
484
485void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
486 SetFieldPtr<CodeAndDirectMethods*>(
487 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
488 new_value, false);
489}
490
491ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
492 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
493 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
494 false);
495}
496
497void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
498 SetFieldObject(
499 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
500 new_value, false);
501
502}
503
504size_t Method::NumArgRegisters(const StringPiece& shorty) {
505 CHECK_LE(1, shorty.length());
506 uint32_t num_registers = 0;
507 for (int i = 1; i < shorty.length(); ++i) {
508 char ch = shorty[i];
509 if (ch == 'D' || ch == 'J') {
510 num_registers += 2;
511 } else {
512 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700513 }
514 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700515 return num_registers;
516}
517
518size_t Method::NumArgArrayBytes() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700519 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700520 size_t num_bytes = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700521 for (int i = 1; i < shorty->GetLength(); ++i) {
522 char ch = shorty->CharAt(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700523 if (ch == 'D' || ch == 'J') {
524 num_bytes += 8;
525 } else if (ch == 'L') {
526 // Argument is a reference or an array. The shorty descriptor
527 // does not distinguish between these types.
528 num_bytes += sizeof(Object*);
529 } else {
530 num_bytes += 4;
531 }
532 }
533 return num_bytes;
534}
535
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700536size_t Method::NumArgs() const {
537 // "1 +" because the first in Args is the receiver.
538 // "- 1" because we don't count the return type.
539 return (IsStatic() ? 0 : 1) + GetShorty()->GetLength() - 1;
540}
541
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700542// The number of reference arguments to this method including implicit this
543// pointer
544size_t Method::NumReferenceArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700545 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700546 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700547 for (int i = 1; i < shorty->GetLength(); i++) {
548 char ch = shorty->CharAt(i);
549 if ((ch == 'L') || (ch == '[')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700550 result++;
551 }
552 }
553 return result;
554}
555
556// The number of long or double arguments
557size_t Method::NumLongOrDoubleArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700558 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700559 size_t result = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700560 for (int i = 1; i < shorty->GetLength(); i++) {
561 char ch = shorty->CharAt(i);
562 if ((ch == 'D') || (ch == 'J')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700563 result++;
564 }
565 }
566 return result;
567}
568
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700569// Is the given method parameter a reference?
570bool Method::IsParamAReference(unsigned int param) const {
571 CHECK_LT(param, NumArgs());
572 if (IsStatic()) {
573 param++; // 0th argument must skip return value at start of the shorty
574 } else if (param == 0) {
575 return true; // this argument
576 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700577 return GetShorty()->CharAt(param) == 'L';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700578}
579
580// Is the given method parameter a long or double?
581bool Method::IsParamALongOrDouble(unsigned int param) const {
582 CHECK_LT(param, NumArgs());
583 if (IsStatic()) {
584 param++; // 0th argument must skip return value at start of the shorty
585 } else if (param == 0) {
586 return false; // this argument
587 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700588 char ch = GetShorty()->CharAt(param);
589 return (ch == 'J' || ch == 'D');
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700590}
591
592static size_t ShortyCharToSize(char x) {
593 switch (x) {
594 case 'V': return 0;
595 case '[': return kPointerSize;
596 case 'L': return kPointerSize;
597 case 'D': return 8;
598 case 'J': return 8;
599 default: return 4;
600 }
601}
602
603size_t Method::ParamSize(unsigned int param) const {
604 CHECK_LT(param, NumArgs());
605 if (IsStatic()) {
606 param++; // 0th argument must skip return value at start of the shorty
607 } else if (param == 0) {
608 return kPointerSize; // this argument
609 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700610 return ShortyCharToSize(GetShorty()->CharAt(param));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700611}
612
613size_t Method::ReturnSize() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700614 return ShortyCharToSize(GetShorty()->CharAt(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700615}
616
617bool Method::HasSameNameAndDescriptor(const Method* that) const {
618 return (this->GetName()->Equals(that->GetName()) &&
619 this->GetSignature()->Equals(that->GetSignature()));
620}
621
Ian Rogersbdb03912011-09-14 00:55:44 -0700622uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700623 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700624 if (mapping_table == NULL) {
Ian Rogers67375ac2011-09-14 00:55:44 -0700625 DCHECK(IsNative());
626 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700627 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700628 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700629 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetCode());
Ian Rogersbdb03912011-09-14 00:55:44 -0700630 uint32_t best_offset = 0;
631 uint32_t best_dex_offset = 0;
632 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700633 uint32_t map_offset = mapping_table[i];
634 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700635 if (map_offset == sought_offset) {
636 best_offset = map_offset;
637 best_dex_offset = map_dex_offset;
638 break;
639 }
640 if (map_offset < sought_offset && map_offset > best_offset) {
641 best_offset = map_offset;
642 best_dex_offset = map_dex_offset;
643 }
644 }
645 return best_dex_offset;
646}
647
648uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700649 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700650 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700651 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700652 return 0; // Special no mapping/pc == 0 case
653 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700654 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700655 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700656 uint32_t map_offset = mapping_table[i];
657 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700658 if (map_dex_offset == dex_pc) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700659 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
660 }
661 }
662 LOG(FATAL) << "Looking up Dex PC not contained in method";
663 return 0;
664}
665
666uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
667 DexCache* dex_cache = GetDeclaringClass()->GetDexCache();
668 const ClassLoader* class_loader = GetDeclaringClass()->GetClassLoader();
669 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
670 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
671 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(GetCodeItemOffset());
672 // Iterate over the catch handlers associated with dex_pc
673 for (DexFile::CatchHandlerIterator iter = dex_file.dexFindCatchHandler(*code_item, dex_pc);
674 !iter.HasNext(); iter.Next()) {
675 uint32_t iter_type_idx = iter.Get().type_idx_;
676 // Catch all case
Elliott Hughes80609252011-09-23 17:24:51 -0700677 if (iter_type_idx == DexFile::kDexNoIndex) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700678 return iter.Get().address_;
679 }
680 // Does this catch exception type apply?
681 Class* iter_exception_type =
682 class_linker->ResolveType(dex_file, iter_type_idx, dex_cache, class_loader);
683 if (iter_exception_type->IsAssignableFrom(exception_type)) {
684 return iter.Get().address_;
685 }
686 }
687 // Handler not found
688 return DexFile::kDexNoIndex;
689}
690
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700691void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
692 // Push a transition back into managed code onto the linked list in thread.
693 CHECK_EQ(Thread::kRunnable, self->GetState());
694 NativeToManagedRecord record;
695 self->PushNativeToManagedRecord(&record);
696
697 // Call the invoke stub associated with the method.
698 // Pass everything as arguments.
699 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700700
701 bool have_executable_code = (GetCode() != NULL);
702#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700703 // Currently we can only compile non-native methods for ARM.
704 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700705#endif
706
707 if (have_executable_code && stub != NULL) {
708 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700709 (*stub)(this, receiver, self, args, result);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700710 LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700711 } else {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700712 if (Runtime::Current()->IsStarted()) {
713 LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
714 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700715 if (result != NULL) {
716 result->j = 0;
717 }
718 }
719
720 // Pop transition.
721 self->PopNativeToManagedRecord(record);
722}
723
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700724bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700725 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
726 void* jni_stub = Runtime::Current()->GetJniStubArray()->GetData();
727 return native_method != jni_stub;
728}
729
730void Method::RegisterNative(const void* native_method) {
731 CHECK(IsNative());
732 CHECK(native_method != NULL);
733 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
734 native_method, false);
735}
736
737void Method::UnregisterNative() {
738 CHECK(IsNative());
739 // restore stub to lookup native pointer via dlsym
740 RegisterNative(Runtime::Current()->GetJniStubArray()->GetData());
741}
742
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700743void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700744 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
745 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700746 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700747 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700748}
749
750DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700751 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700752}
753
754void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700755 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700756}
757
Brian Carlstrom1f870082011-08-23 16:02:11 -0700758Object* Class::AllocObject() {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700759 DCHECK(!IsAbstract()) << PrettyClass(this);
760 DCHECK(!IsInterface()) << PrettyClass(this);
761 DCHECK(!IsPrimitive()) << PrettyClass(this);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700762 DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700763 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700764}
765
Elliott Hughes4681c802011-09-25 18:04:37 -0700766void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700767 if ((flags & kDumpClassFullDetail) == 0) {
768 os << PrettyClass(this);
769 if ((flags & kDumpClassClassLoader) != 0) {
770 os << ' ' << GetClassLoader();
771 }
772 if ((flags & kDumpClassInitialized) != 0) {
773 os << ' ' << GetStatus();
774 }
775 os << std::endl;
776 return;
777 }
778
779 Class* super = GetSuperClass();
780 os << "----- " << (IsInterface() ? "interface" : "class") << " "
781 << "'" << GetDescriptor()->ToModifiedUtf8() << "' cl=" << GetClassLoader() << " -----\n",
782 os << " objectSize=" << SizeOf() << " "
783 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
784 os << StringPrintf(" access=0x%04x.%04x\n",
785 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
786 if (super != NULL) {
787 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
788 }
789 if (IsArrayClass()) {
790 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
791 }
792 if (NumInterfaces() > 0) {
793 os << " interfaces (" << NumInterfaces() << "):\n";
794 for (size_t i = 0; i < NumInterfaces(); ++i) {
795 Class* interface = GetInterface(i);
796 const ClassLoader* cl = interface->GetClassLoader();
797 os << StringPrintf(" %2d: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
798 }
799 }
800 os << " vtable (" << NumVirtualMethods() << " entries, "
801 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
802 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700803 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700804 }
805 os << " direct methods (" << NumDirectMethods() << " entries):\n";
806 for (size_t i = 0; i < NumDirectMethods(); ++i) {
807 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
808 }
809 if (NumStaticFields() > 0) {
810 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700811 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700812 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700813 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700814 }
815 } else {
816 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700817 }
818 }
819 if (NumInstanceFields() > 0) {
820 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700821 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700822 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700823 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700824 }
825 } else {
826 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700827 }
828 }
829}
830
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700831void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
832 if (new_reference_offsets != CLASS_WALK_SUPER) {
833 // Sanity check that the number of bits set in the reference offset bitmap
834 // agrees with the number of references
835 Class* cur = this;
836 size_t cnt = 0;
837 while (cur) {
838 cnt += cur->NumReferenceInstanceFieldsDuringLinking();
839 cur = cur->GetSuperClass();
840 }
841 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt);
842 }
843 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
844 new_reference_offsets, false);
845}
846
847void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
848 if (new_reference_offsets != CLASS_WALK_SUPER) {
849 // Sanity check that the number of bits set in the reference offset bitmap
850 // agrees with the number of references
851 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
852 NumReferenceStaticFieldsDuringLinking());
853 }
854 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
855 new_reference_offsets, false);
856}
857
858size_t Class::PrimitiveSize() const {
859 switch (GetPrimitiveType()) {
860 case kPrimBoolean:
861 case kPrimByte:
862 case kPrimChar:
863 case kPrimShort:
864 case kPrimInt:
865 case kPrimFloat:
866 return sizeof(int32_t);
867 case kPrimLong:
868 case kPrimDouble:
869 return sizeof(int64_t);
870 default:
871 LOG(FATAL) << "Primitive type size calculation on invalid type " << this;
872 return 0;
873 }
874}
875
876size_t Class::GetTypeSize(const String* descriptor) {
877 switch (descriptor->CharAt(0)) {
878 case 'B': return 1; // byte
879 case 'C': return 2; // char
880 case 'D': return 8; // double
881 case 'F': return 4; // float
882 case 'I': return 4; // int
883 case 'J': return 8; // long
884 case 'S': return 2; // short
885 case 'Z': return 1; // boolean
886 case 'L': return sizeof(Object*);
887 case '[': return sizeof(Array*);
888 default:
889 LOG(ERROR) << "Unknown type " << descriptor;
890 return 0;
891 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700892}
893
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700894bool Class::Implements(const Class* klass) const {
895 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700896 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700897 // All interfaces implemented directly and by our superclass, and
898 // recursively all super-interfaces of those interfaces, are listed
899 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700900 int32_t iftable_count = GetIfTableCount();
901 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
902 for (int32_t i = 0; i < iftable_count; i++) {
903 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700904 return true;
905 }
906 }
907 return false;
908}
909
910// Determine whether "this" is assignable from "klazz", where both of these
911// are array classes.
912//
913// Consider an array class, e.g. Y[][], where Y is a subclass of X.
914// Y[][] = Y[][] --> true (identity)
915// X[][] = Y[][] --> true (element superclass)
916// Y = Y[][] --> false
917// Y[] = Y[][] --> false
918// Object = Y[][] --> true (everything is an object)
919// Object[] = Y[][] --> true
920// Object[][] = Y[][] --> true
921// Object[][][] = Y[][] --> false (too many []s)
922// Serializable = Y[][] --> true (all arrays are Serializable)
923// Serializable[] = Y[][] --> true
924// Serializable[][] = Y[][] --> false (unless Y is Serializable)
925//
926// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700927// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700928//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700929bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700930 DCHECK(IsArrayClass()) << PrettyClass(this);
931 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700932 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700933}
934
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700935bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700936 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
937 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700938 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700939 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700940 // src's super should be java_lang_Object, since it is an array.
941 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700942 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
943 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700944 return this == java_lang_Object;
945 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700946 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700947}
948
949bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700950 DCHECK(!IsInterface()) << PrettyClass(this);
951 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700952 const Class* current = this;
953 do {
954 if (current == klass) {
955 return true;
956 }
957 current = current->GetSuperClass();
958 } while (current != NULL);
959 return false;
960}
961
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700962bool Class::IsInSamePackage(const String* descriptor_string_1,
963 const String* descriptor_string_2) {
964 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
965 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
966
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700967 size_t i = 0;
968 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
969 ++i;
970 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700971 if (descriptor1.find('/', i) != StringPiece::npos ||
972 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700973 return false;
974 } else {
975 return true;
976 }
977}
978
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700979#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700980bool Class::IsInSamePackage(const StringPiece& descriptor1,
981 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700982 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700983 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700984 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
985 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700986 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
987}
988#endif
989
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700990bool Class::IsInSamePackage(const Class* that) const {
991 const Class* klass1 = this;
992 const Class* klass2 = that;
993 if (klass1 == klass2) {
994 return true;
995 }
996 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700997 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700998 return false;
999 }
1000 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -07001001 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001002 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001003 }
jeffhao4a801a42011-09-23 13:53:40 -07001004 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001005 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001006 }
1007 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001008 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001009}
1010
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001011const ClassLoader* Class::GetClassLoader() const {
1012 return GetFieldObject<const ClassLoader*>(
1013 OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -07001014}
1015
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001016void Class::SetClassLoader(const ClassLoader* new_cl) {
1017 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
1018 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_),
1019 new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001020}
1021
Brian Carlstrom30b94452011-08-25 21:35:26 -07001022Method* Class::FindVirtualMethodForInterface(Method* method) {
1023 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001024 DCHECK(declaring_class != NULL) << PrettyClass(this);
1025 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001026 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001027 int32_t iftable_count = GetIfTableCount();
1028 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1029 for (int32_t i = 0; i < iftable_count; i++) {
1030 InterfaceEntry* interface_entry = iftable->Get(i);
1031 if (interface_entry->GetInterface() == declaring_class) {
1032 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001033 }
1034 }
Elliott Hughesefdbac52011-10-06 15:06:18 -07001035 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
1036 "Class %s does not implement interface %s",
1037 PrettyDescriptor(GetDescriptor()).c_str(),
1038 PrettyDescriptor(declaring_class->GetDescriptor()).c_str());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001039 return NULL;
1040}
1041
jeffhaobdb76512011-09-07 11:43:16 -07001042Method* Class::FindInterfaceMethod(const StringPiece& name,
1043 const StringPiece& signature) {
1044 // Check the current class before checking the interfaces.
1045 Method* method = FindVirtualMethod(name, signature);
1046 if (method != NULL) {
1047 return method;
1048 }
1049
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001050 int32_t iftable_count = GetIfTableCount();
1051 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1052 for (int32_t i = 0; i < iftable_count; i++) {
1053 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001054 if (method != NULL) {
1055 return method;
1056 }
1057 }
1058 return NULL;
1059}
1060
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001061Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001062 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001063 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001064 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001065 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001066 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001067 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001068 }
1069 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001070 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001071}
1072
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001073Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001074 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001075 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001076 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001077 if (method != NULL) {
1078 return method;
1079 }
1080 }
1081 return NULL;
1082}
1083
1084Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001085 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001086 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001087 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001088 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001089 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001090 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001091 }
1092 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001093 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001094}
1095
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001096Method* Class::FindVirtualMethod(const StringPiece& name,
Elliott Hughescc5f9a92011-09-28 19:17:29 -07001097 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001098 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Elliott Hughescc5f9a92011-09-28 19:17:29 -07001099 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001100 if (method != NULL) {
1101 return method;
1102 }
1103 }
1104 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001105}
1106
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001107Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 // Is the field in this class?
1109 // Interfaces are not relevant because they can't contain instance fields.
1110 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1111 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001112 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 return f;
1114 }
1115 }
1116 return NULL;
1117}
1118
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001119Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 // Is the field in this class, or any of its superclasses?
1121 // Interfaces are not relevant because they can't contain instance fields.
1122 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001123 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 if (f != NULL) {
1125 return f;
1126 }
1127 }
1128 return NULL;
1129}
1130
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001131Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
1132 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 for (size_t i = 0; i < NumStaticFields(); ++i) {
1134 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001135 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 return f;
1137 }
1138 }
1139 return NULL;
1140}
1141
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001142Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 // Is the field in this class (or its interfaces), or any of its
1144 // superclasses (or their interfaces)?
1145 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1146 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001147 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 if (f != NULL) {
1149 return f;
1150 }
1151
1152 // Is this field in any of this class' interfaces?
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001153 for (int32_t i = 0; i < c->GetIfTableCount(); ++i) {
1154 InterfaceEntry* interface_entry = c->GetIfTable()->Get(i);
1155 Class* interface = interface_entry->GetInterface();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001156 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 if (f != NULL) {
1158 return f;
1159 }
1160 }
1161 }
1162 return NULL;
1163}
1164
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001165Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001166 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001167 DCHECK_GE(component_count, 0);
1168 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001169
1170 size_t header_size = sizeof(Array);
1171 size_t data_size = component_count * component_size;
1172 size_t size = header_size + data_size;
1173
1174 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1175 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1176 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1177 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
1178 "%s of length %zd exceeds the VM limit",
1179 PrettyDescriptor(array_class->GetDescriptor()).c_str(), component_count);
1180 return NULL;
1181 }
1182
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001183 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1184 if (array != NULL) {
1185 DCHECK(array->IsArrayInstance());
1186 array->SetLength(component_count);
1187 }
1188 return array;
1189}
1190
1191Array* Array::Alloc(Class* array_class, int32_t component_count) {
1192 return Alloc(array_class, component_count, array_class->GetComponentSize());
1193}
1194
Elliott Hughes80609252011-09-23 17:24:51 -07001195bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001196 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001197 "length=%i; index=%i", length_, index);
1198 return false;
1199}
1200
1201bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001202 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001203 "Can't store an element of type %s into an array of type %s",
1204 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1205 return false;
1206}
1207
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001208template<typename T>
1209PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001210 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001211 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1212 return down_cast<PrimitiveArray<T>*>(raw_array);
1213}
1214
1215template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1216
1217// Explicitly instantiate all the primitive array types.
1218template class PrimitiveArray<uint8_t>; // BooleanArray
1219template class PrimitiveArray<int8_t>; // ByteArray
1220template class PrimitiveArray<uint16_t>; // CharArray
1221template class PrimitiveArray<double>; // DoubleArray
1222template class PrimitiveArray<float>; // FloatArray
1223template class PrimitiveArray<int32_t>; // IntArray
1224template class PrimitiveArray<int64_t>; // LongArray
1225template class PrimitiveArray<int16_t>; // ShortArray
1226
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001227// TODO: get global references for these
1228Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001229
Brian Carlstroma663ea52011-08-19 23:33:41 -07001230void String::SetClass(Class* java_lang_String) {
1231 CHECK(java_lang_String_ == NULL);
1232 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001233 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001234}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001235
Brian Carlstroma663ea52011-08-19 23:33:41 -07001236void String::ResetClass() {
1237 CHECK(java_lang_String_ != NULL);
1238 java_lang_String_ = NULL;
1239}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001240
Brian Carlstromc74255f2011-09-11 22:47:39 -07001241String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001242 return Runtime::Current()->GetInternTable()->InternWeak(this);
1243}
1244
Brian Carlstrom395520e2011-09-25 19:35:00 -07001245int32_t String::GetHashCode() {
1246 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1247 if (result == 0) {
1248 ComputeHashCode();
1249 }
1250 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1251 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1252 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001253 return result;
1254}
1255
1256int32_t String::GetLength() const {
1257 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1258 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1259 return result;
1260}
1261
1262uint16_t String::CharAt(int32_t index) const {
1263 // TODO: do we need this? Equals is the only caller, and could
1264 // bounds check itself.
1265 if (index < 0 || index >= count_) {
1266 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001267 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001268 "length=%i; index=%i", count_, index);
1269 return 0;
1270 }
1271 return GetCharArray()->Get(index + GetOffset());
1272}
1273
1274String* String::AllocFromUtf16(int32_t utf16_length,
1275 const uint16_t* utf16_data_in,
1276 int32_t hash_code) {
1277 String* string = Alloc(GetJavaLangString(), utf16_length);
1278 // TODO: use 16-bit wide memset variant
1279 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
1280 for (int i = 0; i < utf16_length; i++) {
1281 array->Set(i, utf16_data_in[i]);
1282 }
1283 if (hash_code != 0) {
1284 string->SetHashCode(hash_code);
1285 } else {
1286 string->ComputeHashCode();
1287 }
1288 return string;
1289}
1290
1291String* String::AllocFromModifiedUtf8(const char* utf) {
1292 size_t char_count = CountModifiedUtf8Chars(utf);
1293 return AllocFromModifiedUtf8(char_count, utf);
1294}
1295
1296String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1297 const char* utf8_data_in) {
1298 String* string = Alloc(GetJavaLangString(), utf16_length);
1299 uint16_t* utf16_data_out =
1300 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1301 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1302 string->ComputeHashCode();
1303 return string;
1304}
1305
1306String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
1307 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1308}
1309
1310String* String::Alloc(Class* java_lang_String, CharArray* array) {
1311 String* string = down_cast<String*>(java_lang_String->AllocObject());
1312 string->SetArray(array);
1313 string->SetCount(array->GetLength());
1314 return string;
1315}
1316
1317bool String::Equals(const String* that) const {
1318 if (this == that) {
1319 // Quick reference equality test
1320 return true;
1321 } else if (that == NULL) {
1322 // Null isn't an instanceof anything
1323 return false;
1324 } else if (this->GetLength() != that->GetLength()) {
1325 // Quick length inequality test
1326 return false;
1327 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001328 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001329 // hash code was already equal
1330 for (int32_t i = 0; i < that->GetLength(); ++i) {
1331 if (this->CharAt(i) != that->CharAt(i)) {
1332 return false;
1333 }
1334 }
1335 return true;
1336 }
1337}
1338
1339bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1340 int32_t that_length) const {
1341 if (this->GetLength() != that_length) {
1342 return false;
1343 } else {
1344 for (int32_t i = 0; i < that_length; ++i) {
1345 if (this->CharAt(i) != that_chars[that_offset + i]) {
1346 return false;
1347 }
1348 }
1349 return true;
1350 }
1351}
1352
1353bool String::Equals(const char* modified_utf8) const {
1354 for (int32_t i = 0; i < GetLength(); ++i) {
1355 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1356 if (ch == '\0' || ch != CharAt(i)) {
1357 return false;
1358 }
1359 }
1360 return *modified_utf8 == '\0';
1361}
1362
1363bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001364 if (modified_utf8.size() != GetLength()) {
1365 return false;
1366 }
1367 const char* p = modified_utf8.data();
1368 for (int32_t i = 0; i < GetLength(); ++i) {
1369 uint16_t ch = GetUtf16FromUtf8(&p);
1370 if (ch != CharAt(i)) {
1371 return false;
1372 }
1373 }
1374 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001375}
1376
1377// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1378std::string String::ToModifiedUtf8() const {
1379 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1380 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1381 std::string result(byte_count, char(0));
1382 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1383 return result;
1384}
1385
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001386Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1387
1388void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1389 CHECK(java_lang_StackTraceElement_ == NULL);
1390 CHECK(java_lang_StackTraceElement != NULL);
1391 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1392}
1393
1394void StackTraceElement::ResetClass() {
1395 CHECK(java_lang_StackTraceElement_ != NULL);
1396 java_lang_StackTraceElement_ = NULL;
1397}
1398
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001399StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1400 const String* method_name,
1401 const String* file_name,
1402 int32_t line_number) {
1403 StackTraceElement* trace =
1404 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1405 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1406 const_cast<String*>(declaring_class), false);
1407 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1408 const_cast<String*>(method_name), false);
1409 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1410 const_cast<String*>(file_name), false);
1411 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1412 line_number, false);
1413 return trace;
1414}
1415
Elliott Hughes1f359b02011-07-17 14:27:17 -07001416static const char* kClassStatusNames[] = {
1417 "Error",
1418 "NotReady",
1419 "Idx",
1420 "Loaded",
1421 "Resolved",
1422 "Verifying",
1423 "Verified",
1424 "Initializing",
1425 "Initialized"
1426};
1427std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1428 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001429 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001430 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001431 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001432 }
1433 return os;
1434}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001435
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001436} // namespace art