blob: f43bec9a87d126561d0d9ead443c0f4754d0bf3f [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"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070022#include "runtime.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070023#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070024#include "utils.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070025
26namespace art {
27
Elliott Hughesdbb40792011-11-18 17:05:22 -080028String* Object::AsString() {
29 DCHECK(GetClass()->IsStringClass());
30 return down_cast<String*>(this);
31}
32
Elliott Hughes081be7f2011-09-18 16:50:26 -070033Object* Object::Clone() {
34 Class* c = GetClass();
35 DCHECK(!c->IsClassClass());
36
37 // Object::SizeOf gets the right size even if we're an array.
38 // Using c->AllocObject() here would be wrong.
39 size_t num_bytes = SizeOf();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070040 SirtRef<Object> copy(Heap::AllocObject(c, num_bytes));
41 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070042 return NULL;
43 }
44
45 // Copy instance data. We assume memcpy copies by words.
46 // TODO: expose and use move32.
47 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070048 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070049 size_t offset = sizeof(Object);
50 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
51
Elliott Hughes20cde902011-10-04 17:37:27 -070052 if (c->IsFinalizable()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -070053 Heap::AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070054 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070055
Brian Carlstrom40381fb2011-10-19 14:13:40 -070056 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070057}
58
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070059uint32_t Object::GetThinLockId() {
60 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070061}
62
63void Object::MonitorEnter(Thread* thread) {
64 Monitor::MonitorEnter(thread, this);
65}
66
Ian Rogersff1ed472011-09-20 13:46:24 -070067bool Object::MonitorExit(Thread* thread) {
68 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070069}
70
71void Object::Notify() {
72 Monitor::Notify(Thread::Current(), this);
73}
74
75void Object::NotifyAll() {
76 Monitor::NotifyAll(Thread::Current(), this);
77}
78
79void Object::Wait(int64_t ms, int32_t ns) {
80 Monitor::Wait(Thread::Current(), this, ms, ns, true);
81}
82
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070083// TODO: get global references for these
84Class* Field::java_lang_reflect_Field_ = NULL;
85
86void Field::SetClass(Class* java_lang_reflect_Field) {
87 CHECK(java_lang_reflect_Field_ == NULL);
88 CHECK(java_lang_reflect_Field != NULL);
89 java_lang_reflect_Field_ = java_lang_reflect_Field;
90}
91
92void Field::ResetClass() {
93 CHECK(java_lang_reflect_Field_ != NULL);
94 java_lang_reflect_Field_ = NULL;
95}
96
Ian Rogers0571d352011-11-03 19:51:38 -070097void Field::SetOffset(MemberOffset num_bytes) {
98 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080099#if 0 // TODO enable later in boot and under !NDEBUG
100 FieldHelper fh(this);
101 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700102 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
103 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
104 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800105#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700106 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
107}
108
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700109uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700110 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700111 if (IsStatic()) {
112 object = declaring_class_;
113 }
114 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700115}
116
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700118 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700119 if (IsStatic()) {
120 object = declaring_class_;
121 }
122 object->SetField32(GetOffset(), new_value, IsVolatile());
123}
124
125uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700126 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700127 if (IsStatic()) {
128 object = declaring_class_;
129 }
130 return object->GetField64(GetOffset(), IsVolatile());
131}
132
133void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700134 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700135 if (IsStatic()) {
136 object = declaring_class_;
137 }
138 object->SetField64(GetOffset(), new_value, IsVolatile());
139}
140
141Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700142 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700143 if (IsStatic()) {
144 object = declaring_class_;
145 }
146 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
147}
148
149void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700150 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700151 if (IsStatic()) {
152 object = declaring_class_;
153 }
154 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
155}
156
157bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800158 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
159 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700160 return Get32(object);
161}
162
163void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800164 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
165 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 Set32(object, z);
167}
168
169int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800170 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
171 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700172 return Get32(object);
173}
174
175void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800176 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
177 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700178 Set32(object, b);
179}
180
181uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800182 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
183 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184 return Get32(object);
185}
186
187void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800188 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
189 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190 Set32(object, c);
191}
192
Ian Rogers466bb252011-10-14 03:29:56 -0700193int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800194 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
195 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 return Get32(object);
197}
198
Ian Rogers466bb252011-10-14 03:29:56 -0700199void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800200 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
201 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 Set32(object, s);
203}
204
205int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800206 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
207 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 return Get32(object);
209}
210
211void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800212 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
213 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700214 Set32(object, i);
215}
216
217int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800218 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
219 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700220 return Get64(object);
221}
222
223void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800224 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
225 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700226 Set64(object, j);
227}
228
229float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800230 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
231 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700232 JValue float_bits;
233 float_bits.i = Get32(object);
234 return float_bits.f;
235}
236
237void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800238 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
239 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 JValue float_bits;
241 float_bits.f = f;
242 Set32(object, float_bits.i);
243}
244
245double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800246 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
247 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700248 JValue double_bits;
249 double_bits.j = Get64(object);
250 return double_bits.d;
251}
252
253void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800254 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
255 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700256 JValue double_bits;
257 double_bits.d = d;
258 Set64(object, double_bits.j);
259}
260
261Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800262 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
263 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700264 return GetObj(object);
265}
266
267void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
269 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700270 SetObj(object, l);
271}
272
273// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700274Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275Class* Method::java_lang_reflect_Method_ = NULL;
276
Elliott Hughes80609252011-09-23 17:24:51 -0700277void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
278 CHECK(java_lang_reflect_Constructor_ == NULL);
279 CHECK(java_lang_reflect_Constructor != NULL);
280 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
281
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700282 CHECK(java_lang_reflect_Method_ == NULL);
283 CHECK(java_lang_reflect_Method != NULL);
284 java_lang_reflect_Method_ = java_lang_reflect_Method;
285}
286
Elliott Hughes80609252011-09-23 17:24:51 -0700287void Method::ResetClasses() {
288 CHECK(java_lang_reflect_Constructor_ != NULL);
289 java_lang_reflect_Constructor_ = NULL;
290
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 CHECK(java_lang_reflect_Method_ != NULL);
292 java_lang_reflect_Method_ = NULL;
293}
294
Elliott Hughes418d20f2011-09-22 14:00:39 -0700295Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
296 if (*p == '[') {
297 // Something like "[[[Ljava/lang/String;".
298 const char* start = p;
299 while (*p == '[') {
300 ++p;
301 }
302 if (*p == 'L') {
303 while (*p != ';') {
304 ++p;
305 }
306 }
307 ++p; // Either the ';' or the primitive type.
308
Brian Carlstromaded5f72011-10-07 17:15:04 -0700309 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800310 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700311 } else if (*p == 'L') {
312 const char* start = p;
313 while (*p != ';') {
314 ++p;
315 }
316 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800317 std::string descriptor(start, (p - start));
318 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700319 } else {
320 return class_linker->FindPrimitiveClass(*p++);
321 }
322}
323
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700324ObjectArray<String>* Method::GetDexCacheStrings() const {
325 return GetFieldObject<ObjectArray<String>*>(
326 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
327}
328
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
330 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
331 new_dex_cache_strings, false);
332}
333
334ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
335 return GetFieldObject<ObjectArray<Class>*>(
336 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
337}
338
339void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
340 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
341 new_dex_cache_classes, false);
342}
343
344ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
345 return GetFieldObject<ObjectArray<Method>*>(
346 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
347}
348
349void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
350 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
351 new_dex_cache_methods, false);
352}
353
354ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
355 return GetFieldObject<ObjectArray<Field>*>(
356 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
357}
358
359void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
360 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
361 new_dex_cache_fields, false);
362}
363
364CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
365 return GetFieldPtr<CodeAndDirectMethods*>(
366 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
367 false);
368}
369
370void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
371 SetFieldPtr<CodeAndDirectMethods*>(
372 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
373 new_value, false);
374}
375
376ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
377 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
378 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
379 false);
380}
381
382void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700383 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700385}
386
387size_t Method::NumArgRegisters(const StringPiece& shorty) {
388 CHECK_LE(1, shorty.length());
389 uint32_t num_registers = 0;
390 for (int i = 1; i < shorty.length(); ++i) {
391 char ch = shorty[i];
392 if (ch == 'D' || ch == 'J') {
393 num_registers += 2;
394 } else {
395 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700396 }
397 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 return num_registers;
399}
400
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800401bool Method::IsProxyMethod() const {
402 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700403}
404
Ian Rogers466bb252011-10-14 03:29:56 -0700405Method* Method::FindOverriddenMethod() const {
406 if (IsStatic()) {
407 return NULL;
408 }
409 Class* declaring_class = GetDeclaringClass();
410 Class* super_class = declaring_class->GetSuperClass();
411 uint16_t method_index = GetMethodIndex();
412 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
413 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800414 // Did this method override a super class method? If so load the result from the super class'
415 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700416 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
417 result = super_class_vtable->Get(method_index);
418 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800419 // Method didn't override superclass method so search interfaces
420 MethodHelper mh(this);
421 MethodHelper interface_mh;
422 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
423 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
424 InterfaceEntry* entry = iftable->Get(i);
425 Class* interface = entry->GetInterface();
426 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
427 Method* interface_method = interface->GetVirtualMethod(j);
428 interface_mh.ChangeMethod(interface_method);
429 if (mh.HasSameNameAndSignature(&interface_mh)) {
430 result = interface_method;
431 break;
432 }
433 }
Ian Rogers466bb252011-10-14 03:29:56 -0700434 }
435 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800436#ifndef NDEBUG
437 MethodHelper result_mh(result);
438 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
439#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700440 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700441}
442
Ian Rogersbdb03912011-09-14 00:55:44 -0700443uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700444 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700445 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700447 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700448 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700449 size_t mapping_table_length = GetMappingTableLength();
jeffhao2692b572011-12-16 15:42:28 -0800450 const void* code_offset = Runtime::Current()->IsMethodTracingActive() ? Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this) : GetCode();
jeffhaoe343b762011-12-05 16:36:44 -0800451 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code_offset);
Ian Rogersbdb03912011-09-14 00:55:44 -0700452 uint32_t best_offset = 0;
453 uint32_t best_dex_offset = 0;
454 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700455 uint32_t map_offset = mapping_table[i];
456 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700457 if (map_offset == sought_offset) {
458 best_offset = map_offset;
459 best_dex_offset = map_dex_offset;
460 break;
461 }
462 if (map_offset < sought_offset && map_offset > best_offset) {
463 best_offset = map_offset;
464 best_dex_offset = map_dex_offset;
465 }
466 }
467 return best_dex_offset;
468}
469
470uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700471 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700472 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700473 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700474 return 0; // Special no mapping/pc == 0 case
475 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700476 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700477 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700478 uint32_t map_offset = mapping_table[i];
479 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700480 if (map_dex_offset == dex_pc) {
jeffhao2692b572011-12-16 15:42:28 -0800481 if (Runtime::Current()->IsMethodTracingActive()) {
482 Trace* tracer = Runtime::Current()->GetTracer();
483 return reinterpret_cast<uintptr_t>(tracer->GetSavedCodeFromMap(this)) + map_offset;
jeffhaoe343b762011-12-05 16:36:44 -0800484 } else {
485 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
486 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700487 }
488 }
489 LOG(FATAL) << "Looking up Dex PC not contained in method";
490 return 0;
491}
492
493uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800494 MethodHelper mh(this);
495 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700496 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700497 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
498 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700499 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700500 if (iter_type_idx == DexFile::kDexNoIndex16) {
501 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700502 }
503 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800504 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700505 if (iter_exception_type == NULL) {
506 // The verifier should take care of resolving all exception classes early
507 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700509 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700510 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700511 }
512 }
513 // Handler not found
514 return DexFile::kDexNoIndex;
515}
516
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700517void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
518 // Push a transition back into managed code onto the linked list in thread.
519 CHECK_EQ(Thread::kRunnable, self->GetState());
520 NativeToManagedRecord record;
521 self->PushNativeToManagedRecord(&record);
522
523 // Call the invoke stub associated with the method.
524 // Pass everything as arguments.
525 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700526
527 bool have_executable_code = (GetCode() != NULL);
528#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700529 // Currently we can only compile non-native methods for ARM.
530 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700531#endif
532
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500533 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700534 bool log = false;
535 if (log) {
536 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
537 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700538 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700539 if (log) {
540 LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
541 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700542 } else {
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500543 LOG(INFO) << "not invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub
Brian Carlstromebd1fd22011-12-07 15:46:26 -0800544 << " started=" << Runtime::Current()->IsStarted();
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700545 if (result != NULL) {
546 result->j = 0;
547 }
548 }
549
550 // Pop transition.
551 self->PopNativeToManagedRecord(record);
552}
553
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700554bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700555 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers169c9a72011-11-13 20:13:17 -0800556 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700557 return native_method != jni_stub;
558}
559
560void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700561 CHECK(IsNative()) << PrettyMethod(this);
562 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700563 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
564 native_method, false);
565}
566
567void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700568 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700569 // restore stub to lookup native pointer via dlsym
Ian Rogers169c9a72011-11-13 20:13:17 -0800570 RegisterNative(Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700571}
572
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700573void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700574 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
575 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700576 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700577 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700578}
579
580DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700581 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700582}
583
584void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700585 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700586}
587
Brian Carlstrom1f870082011-08-23 16:02:11 -0700588Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700589 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700590 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500591 // TODO: decide whether we want this check. It currently fails during bootstrap.
592 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700593 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700594 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700595}
596
Ian Rogers0571d352011-11-03 19:51:38 -0700597void Class::SetClassSize(size_t new_class_size) {
598 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
599 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
600}
601
Elliott Hughes4681c802011-09-25 18:04:37 -0700602void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700603 if ((flags & kDumpClassFullDetail) == 0) {
604 os << PrettyClass(this);
605 if ((flags & kDumpClassClassLoader) != 0) {
606 os << ' ' << GetClassLoader();
607 }
608 if ((flags & kDumpClassInitialized) != 0) {
609 os << ' ' << GetStatus();
610 }
Elliott Hughese0918552011-10-28 17:18:29 -0700611 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700612 return;
613 }
614
615 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800616 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700617 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800618 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700619 os << " objectSize=" << SizeOf() << " "
620 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
621 os << StringPrintf(" access=0x%04x.%04x\n",
622 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
623 if (super != NULL) {
624 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
625 }
626 if (IsArrayClass()) {
627 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
628 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800629 if (kh.NumInterfaces() > 0) {
630 os << " interfaces (" << kh.NumInterfaces() << "):\n";
631 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
632 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700633 const ClassLoader* cl = interface->GetClassLoader();
634 os << StringPrintf(" %2d: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
635 }
636 }
637 os << " vtable (" << NumVirtualMethods() << " entries, "
638 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
639 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700640 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700641 }
642 os << " direct methods (" << NumDirectMethods() << " entries):\n";
643 for (size_t i = 0; i < NumDirectMethods(); ++i) {
644 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
645 }
646 if (NumStaticFields() > 0) {
647 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700648 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700649 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700650 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700651 }
652 } else {
653 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700654 }
655 }
656 if (NumInstanceFields() > 0) {
657 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700658 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700659 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700660 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700661 }
662 } else {
663 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700664 }
665 }
666}
667
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700668void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
669 if (new_reference_offsets != CLASS_WALK_SUPER) {
670 // Sanity check that the number of bits set in the reference offset bitmap
671 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800672 size_t count = 0;
673 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
674 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700675 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800676 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700677 }
678 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
679 new_reference_offsets, false);
680}
681
682void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
683 if (new_reference_offsets != CLASS_WALK_SUPER) {
684 // Sanity check that the number of bits set in the reference offset bitmap
685 // agrees with the number of references
686 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
687 NumReferenceStaticFieldsDuringLinking());
688 }
689 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
690 new_reference_offsets, false);
691}
692
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700693bool Class::Implements(const Class* klass) const {
694 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700695 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700696 // All interfaces implemented directly and by our superclass, and
697 // recursively all super-interfaces of those interfaces, are listed
698 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700699 int32_t iftable_count = GetIfTableCount();
700 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
701 for (int32_t i = 0; i < iftable_count; i++) {
702 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700703 return true;
704 }
705 }
706 return false;
707}
708
709// Determine whether "this" is assignable from "klazz", where both of these
710// are array classes.
711//
712// Consider an array class, e.g. Y[][], where Y is a subclass of X.
713// Y[][] = Y[][] --> true (identity)
714// X[][] = Y[][] --> true (element superclass)
715// Y = Y[][] --> false
716// Y[] = Y[][] --> false
717// Object = Y[][] --> true (everything is an object)
718// Object[] = Y[][] --> true
719// Object[][] = Y[][] --> true
720// Object[][][] = Y[][] --> false (too many []s)
721// Serializable = Y[][] --> true (all arrays are Serializable)
722// Serializable[] = Y[][] --> true
723// Serializable[][] = Y[][] --> false (unless Y is Serializable)
724//
725// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700726// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700727//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700728bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700729 DCHECK(IsArrayClass()) << PrettyClass(this);
730 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700731 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700732}
733
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700734bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700735 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
736 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700737 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700738 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700739 // src's super should be java_lang_Object, since it is an array.
740 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700741 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
742 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700743 return this == java_lang_Object;
744 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700745 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700746}
747
748bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700749 DCHECK(!IsInterface()) << PrettyClass(this);
750 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700751 const Class* current = this;
752 do {
753 if (current == klass) {
754 return true;
755 }
756 current = current->GetSuperClass();
757 } while (current != NULL);
758 return false;
759}
760
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800761bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700762 size_t i = 0;
763 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
764 ++i;
765 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700766 if (descriptor1.find('/', i) != StringPiece::npos ||
767 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700768 return false;
769 } else {
770 return true;
771 }
772}
773
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700774#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700775bool Class::IsInSamePackage(const StringPiece& descriptor1,
776 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700777 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700778 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700779 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
780 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700781 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
782}
783#endif
784
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700785bool Class::IsInSamePackage(const Class* that) const {
786 const Class* klass1 = this;
787 const Class* klass2 = that;
788 if (klass1 == klass2) {
789 return true;
790 }
791 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700792 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700793 return false;
794 }
795 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700796 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700797 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700798 }
jeffhao4a801a42011-09-23 13:53:40 -0700799 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700800 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700801 }
802 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800803 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800804 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800805 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800806 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800807 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700808}
809
Elliott Hughesdbb40792011-11-18 17:05:22 -0800810bool Class::IsClassClass() const {
811 Class* java_lang_Class = GetClass()->GetClass();
812 return this == java_lang_Class;
813}
814
815bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800816 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800817}
818
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800819bool Class::IsThrowableClass() const {
820 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
821 return throwable->IsAssignableFrom(this);
822}
823
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800824ClassLoader* Class::GetClassLoader() const {
825 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700826}
827
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700828void Class::SetClassLoader(const ClassLoader* new_cl) {
829 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700831}
832
Ian Rogersb04f69f2011-10-17 00:40:54 -0700833Method* Class::FindVirtualMethodForInterface(Method* method, bool can_throw) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700834 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700835 DCHECK(declaring_class != NULL) << PrettyClass(this);
836 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700837 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700838 int32_t iftable_count = GetIfTableCount();
839 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
840 for (int32_t i = 0; i < iftable_count; i++) {
841 InterfaceEntry* interface_entry = iftable->Get(i);
842 if (interface_entry->GetInterface() == declaring_class) {
843 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700844 }
845 }
Ian Rogersb04f69f2011-10-17 00:40:54 -0700846 if (can_throw) {
847 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
848 "Class %s does not implement interface %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800849 PrettyDescriptor(this).c_str(), PrettyDescriptor(declaring_class).c_str());
Ian Rogersb04f69f2011-10-17 00:40:54 -0700850 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700851 return NULL;
852}
853
Ian Rogers466bb252011-10-14 03:29:56 -0700854Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700855 // Check the current class before checking the interfaces.
856 Method* method = FindVirtualMethod(name, signature);
857 if (method != NULL) {
858 return method;
859 }
860
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700861 int32_t iftable_count = GetIfTableCount();
862 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
863 for (int32_t i = 0; i < iftable_count; i++) {
864 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700865 if (method != NULL) {
866 return method;
867 }
868 }
869 return NULL;
870}
871
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700872Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700873 const StringPiece& signature) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800874 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700875 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700876 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800877 mh.ChangeMethod(method);
878 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700879 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700880 }
881 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700882 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700883}
884
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700885Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700886 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700887 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700888 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700889 if (method != NULL) {
890 return method;
891 }
892 }
893 return NULL;
894}
895
896Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -0700897 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800898 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700899 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700900 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800901 mh.ChangeMethod(method);
902 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -0700903 return method;
Ian Rogers466bb252011-10-14 03:29:56 -0700904 }
905 }
906 return NULL;
907}
908
909Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
910 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
911 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
912 if (method != NULL) {
913 return method;
914 }
915 }
916 return NULL;
917}
918
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700919Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 // Is the field in this class?
921 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800922 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 for (size_t i = 0; i < NumInstanceFields(); ++i) {
924 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800925 fh.ChangeField(f);
926 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700927 return f;
928 }
929 }
930 return NULL;
931}
932
933Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 // Is the field in this class, or any of its superclasses?
935 // Interfaces are not relevant because they can't contain instance fields.
936 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700937 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 if (f != NULL) {
939 return f;
940 }
941 }
942 return NULL;
943}
944
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700945Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
946 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800947 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700948 for (size_t i = 0; i < NumStaticFields(); ++i) {
949 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800950 fh.ChangeField(f);
951 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700952 return f;
953 }
954 }
955 return NULL;
956}
957
958Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
959 // Is the field in this class (or its interfaces), or any of its
960 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -0800961 ClassHelper kh;
962 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700963 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -0800964 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700965 if (f != NULL) {
966 return f;
967 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700968 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -0800969 kh.ChangeClass(k);
970 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
971 Class* interface = kh.GetInterface(i);
972 f = interface->FindDeclaredStaticField(name, type);
973 if (f != NULL) {
974 return f;
975 }
976 }
977 }
978 return NULL;
979}
980
981Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
982 // Find a field using the JLS field resolution order
983 ClassHelper kh;
984 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
985 // Is the field in this class?
986 Field* f = k->FindDeclaredInstanceField(name, type);
987 if (f != NULL) {
988 return f;
989 }
990 f = k->FindDeclaredStaticField(name, type);
991 if (f != NULL) {
992 return f;
993 }
994 // Is this field in any of this class' interfaces?
995 kh.ChangeClass(k);
996 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
997 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700998 f = interface->FindDeclaredStaticField(name, type);
999 if (f != NULL) {
1000 return f;
1001 }
1002 }
1003 }
1004 return NULL;
1005}
1006
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001007Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001008 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001009 DCHECK_GE(component_count, 0);
1010 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001011
1012 size_t header_size = sizeof(Array);
1013 size_t data_size = component_count * component_size;
1014 size_t size = header_size + data_size;
1015
1016 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1017 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1018 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1019 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
1020 "%s of length %zd exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001021 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001022 return NULL;
1023 }
1024
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001025 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1026 if (array != NULL) {
1027 DCHECK(array->IsArrayInstance());
1028 array->SetLength(component_count);
1029 }
1030 return array;
1031}
1032
1033Array* Array::Alloc(Class* array_class, int32_t component_count) {
1034 return Alloc(array_class, component_count, array_class->GetComponentSize());
1035}
1036
Elliott Hughes80609252011-09-23 17:24:51 -07001037bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001038 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001039 "length=%i; index=%i", length_, index);
1040 return false;
1041}
1042
1043bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001044 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001045 "Can't store an element of type %s into an array of type %s",
1046 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1047 return false;
1048}
1049
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001050template<typename T>
1051PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001052 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001053 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1054 return down_cast<PrimitiveArray<T>*>(raw_array);
1055}
1056
1057template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1058
1059// Explicitly instantiate all the primitive array types.
1060template class PrimitiveArray<uint8_t>; // BooleanArray
1061template class PrimitiveArray<int8_t>; // ByteArray
1062template class PrimitiveArray<uint16_t>; // CharArray
1063template class PrimitiveArray<double>; // DoubleArray
1064template class PrimitiveArray<float>; // FloatArray
1065template class PrimitiveArray<int32_t>; // IntArray
1066template class PrimitiveArray<int64_t>; // LongArray
1067template class PrimitiveArray<int16_t>; // ShortArray
1068
Ian Rogers466bb252011-10-14 03:29:56 -07001069// Explicitly instantiate Class[][]
1070template class ObjectArray<ObjectArray<Class> >;
1071
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001072// TODO: get global references for these
1073Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001074
Brian Carlstroma663ea52011-08-19 23:33:41 -07001075void String::SetClass(Class* java_lang_String) {
1076 CHECK(java_lang_String_ == NULL);
1077 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001078 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001079}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001080
Brian Carlstroma663ea52011-08-19 23:33:41 -07001081void String::ResetClass() {
1082 CHECK(java_lang_String_ != NULL);
1083 java_lang_String_ = NULL;
1084}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001085
Brian Carlstromc74255f2011-09-11 22:47:39 -07001086String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001087 return Runtime::Current()->GetInternTable()->InternWeak(this);
1088}
1089
Brian Carlstrom395520e2011-09-25 19:35:00 -07001090int32_t String::GetHashCode() {
1091 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1092 if (result == 0) {
1093 ComputeHashCode();
1094 }
1095 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1096 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1097 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001098 return result;
1099}
1100
1101int32_t String::GetLength() const {
1102 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1103 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1104 return result;
1105}
1106
1107uint16_t String::CharAt(int32_t index) const {
1108 // TODO: do we need this? Equals is the only caller, and could
1109 // bounds check itself.
1110 if (index < 0 || index >= count_) {
1111 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001112 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001113 "length=%i; index=%i", count_, index);
1114 return 0;
1115 }
1116 return GetCharArray()->Get(index + GetOffset());
1117}
1118
1119String* String::AllocFromUtf16(int32_t utf16_length,
1120 const uint16_t* utf16_data_in,
1121 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001122 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001123 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001124 if (string == NULL) {
1125 return NULL;
1126 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001127 // TODO: use 16-bit wide memset variant
1128 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001129 if (array == NULL) {
1130 return NULL;
1131 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001132 for (int i = 0; i < utf16_length; i++) {
1133 array->Set(i, utf16_data_in[i]);
1134 }
1135 if (hash_code != 0) {
1136 string->SetHashCode(hash_code);
1137 } else {
1138 string->ComputeHashCode();
1139 }
1140 return string;
1141}
1142
1143String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001144 if (utf == NULL) {
1145 return NULL;
1146 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001147 size_t char_count = CountModifiedUtf8Chars(utf);
1148 return AllocFromModifiedUtf8(char_count, utf);
1149}
1150
1151String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1152 const char* utf8_data_in) {
1153 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001154 if (string == NULL) {
1155 return NULL;
1156 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001157 uint16_t* utf16_data_out =
1158 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1159 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1160 string->ComputeHashCode();
1161 return string;
1162}
1163
1164String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001165 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1166 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001167 return NULL;
1168 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001169 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001170}
1171
1172String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001173 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001174 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001175 if (string == NULL) {
1176 return NULL;
1177 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001178 string->SetArray(array);
1179 string->SetCount(array->GetLength());
1180 return string;
1181}
1182
1183bool String::Equals(const String* that) const {
1184 if (this == that) {
1185 // Quick reference equality test
1186 return true;
1187 } else if (that == NULL) {
1188 // Null isn't an instanceof anything
1189 return false;
1190 } else if (this->GetLength() != that->GetLength()) {
1191 // Quick length inequality test
1192 return false;
1193 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001194 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001195 // hash code was already equal
1196 for (int32_t i = 0; i < that->GetLength(); ++i) {
1197 if (this->CharAt(i) != that->CharAt(i)) {
1198 return false;
1199 }
1200 }
1201 return true;
1202 }
1203}
1204
Elliott Hughes5d78d392011-12-13 16:53:05 -08001205bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001206 if (this->GetLength() != that_length) {
1207 return false;
1208 } else {
1209 for (int32_t i = 0; i < that_length; ++i) {
1210 if (this->CharAt(i) != that_chars[that_offset + i]) {
1211 return false;
1212 }
1213 }
1214 return true;
1215 }
1216}
1217
1218bool String::Equals(const char* modified_utf8) const {
1219 for (int32_t i = 0; i < GetLength(); ++i) {
1220 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1221 if (ch == '\0' || ch != CharAt(i)) {
1222 return false;
1223 }
1224 }
1225 return *modified_utf8 == '\0';
1226}
1227
1228bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001229 if (modified_utf8.size() != GetLength()) {
1230 return false;
1231 }
1232 const char* p = modified_utf8.data();
1233 for (int32_t i = 0; i < GetLength(); ++i) {
1234 uint16_t ch = GetUtf16FromUtf8(&p);
1235 if (ch != CharAt(i)) {
1236 return false;
1237 }
1238 }
1239 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001240}
1241
1242// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1243std::string String::ToModifiedUtf8() const {
1244 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1245 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1246 std::string result(byte_count, char(0));
1247 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1248 return result;
1249}
1250
Ian Rogers466bb252011-10-14 03:29:56 -07001251bool Throwable::IsCheckedException() const {
1252 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1253 if (InstanceOf(error)) {
1254 return false;
1255 }
1256 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1257 return !InstanceOf(jlre);
1258}
1259
Ian Rogers9074b992011-10-26 17:41:55 -07001260std::string Throwable::Dump() const {
1261 Object* stack_state = GetStackState();
1262 if (stack_state == NULL || !stack_state->IsObjectArray()) {
1263 // missing or corrupt stack state
1264 return "";
1265 }
1266 // Decode the internal stack trace into the depth and method trace
1267 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1268 int32_t depth = method_trace->GetLength() - 1;
1269 std::string result;
1270 for (int32_t i = 0; i < depth; ++i) {
1271 Method* method = down_cast<Method*>(method_trace->Get(i));
1272 result += " at ";
1273 result += PrettyMethod(method, true);
1274 result += "\n";
1275 }
1276 return result;
1277}
1278
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001279Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1280
1281void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1282 CHECK(java_lang_StackTraceElement_ == NULL);
1283 CHECK(java_lang_StackTraceElement != NULL);
1284 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1285}
1286
1287void StackTraceElement::ResetClass() {
1288 CHECK(java_lang_StackTraceElement_ != NULL);
1289 java_lang_StackTraceElement_ = NULL;
1290}
1291
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001292StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1293 String* method_name,
1294 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001295 int32_t line_number) {
1296 StackTraceElement* trace =
1297 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1298 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1299 const_cast<String*>(declaring_class), false);
1300 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1301 const_cast<String*>(method_name), false);
1302 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1303 const_cast<String*>(file_name), false);
1304 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1305 line_number, false);
1306 return trace;
1307}
1308
Elliott Hughes1f359b02011-07-17 14:27:17 -07001309static const char* kClassStatusNames[] = {
1310 "Error",
1311 "NotReady",
1312 "Idx",
1313 "Loaded",
1314 "Resolved",
1315 "Verifying",
1316 "Verified",
1317 "Initializing",
1318 "Initialized"
1319};
1320std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1321 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001322 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001323 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001324 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001325 }
1326 return os;
1327}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001328
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001329} // namespace art