blob: 145f7efe506bdd9ce5f2fa1ca9c9e2d19f54949b [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();
jeffhao26c0a1a2012-01-17 16:28:33 -0800450 const void* code_offset;
451 if (Runtime::Current()->IsMethodTracingActive() &&
452 Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this) != NULL) {
453 code_offset = Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this);
454 } else {
455 code_offset = GetCode();
456 }
jeffhaoe343b762011-12-05 16:36:44 -0800457 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code_offset);
Ian Rogersbdb03912011-09-14 00:55:44 -0700458 uint32_t best_offset = 0;
459 uint32_t best_dex_offset = 0;
460 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700461 uint32_t map_offset = mapping_table[i];
462 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700463 if (map_offset == sought_offset) {
464 best_offset = map_offset;
465 best_dex_offset = map_dex_offset;
466 break;
467 }
468 if (map_offset < sought_offset && map_offset > best_offset) {
469 best_offset = map_offset;
470 best_dex_offset = map_dex_offset;
471 }
472 }
473 return best_dex_offset;
474}
475
476uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700478 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700479 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700480 return 0; // Special no mapping/pc == 0 case
481 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700483 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700484 uint32_t map_offset = mapping_table[i];
485 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700486 if (map_dex_offset == dex_pc) {
jeffhao2692b572011-12-16 15:42:28 -0800487 if (Runtime::Current()->IsMethodTracingActive()) {
488 Trace* tracer = Runtime::Current()->GetTracer();
489 return reinterpret_cast<uintptr_t>(tracer->GetSavedCodeFromMap(this)) + map_offset;
jeffhaoe343b762011-12-05 16:36:44 -0800490 } else {
491 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
492 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700493 }
494 }
495 LOG(FATAL) << "Looking up Dex PC not contained in method";
496 return 0;
497}
498
499uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800500 MethodHelper mh(this);
501 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700502 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700503 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
504 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700505 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700506 if (iter_type_idx == DexFile::kDexNoIndex16) {
507 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700508 }
509 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800510 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700511 if (iter_exception_type == NULL) {
512 // The verifier should take care of resolving all exception classes early
513 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800514 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700515 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700516 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700517 }
518 }
519 // Handler not found
520 return DexFile::kDexNoIndex;
521}
522
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700523void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
524 // Push a transition back into managed code onto the linked list in thread.
525 CHECK_EQ(Thread::kRunnable, self->GetState());
526 NativeToManagedRecord record;
527 self->PushNativeToManagedRecord(&record);
528
529 // Call the invoke stub associated with the method.
530 // Pass everything as arguments.
531 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700532
533 bool have_executable_code = (GetCode() != NULL);
534#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700535 // Currently we can only compile non-native methods for ARM.
536 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700537#endif
538
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500539 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700540 bool log = false;
541 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800542 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
543 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700544 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700545 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700546 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800547 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
548 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700549 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700550 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800551 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
552 PrettyMethod(this).c_str(), GetCode(), stub,
553 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700554 if (result != NULL) {
555 result->j = 0;
556 }
557 }
558
559 // Pop transition.
560 self->PopNativeToManagedRecord(record);
561}
562
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700564 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers169c9a72011-11-13 20:13:17 -0800565 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700566 return native_method != jni_stub;
567}
568
569void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700570 CHECK(IsNative()) << PrettyMethod(this);
571 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700572 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
573 native_method, false);
574}
575
576void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700577 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700578 // restore stub to lookup native pointer via dlsym
Ian Rogers169c9a72011-11-13 20:13:17 -0800579 RegisterNative(Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700580}
581
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700582void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700583 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
584 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700585 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700586 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700587}
588
589DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700590 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700591}
592
593void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700594 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700595}
596
Brian Carlstrom1f870082011-08-23 16:02:11 -0700597Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700598 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700599 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500600 // TODO: decide whether we want this check. It currently fails during bootstrap.
601 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700602 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700603 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700604}
605
Ian Rogers0571d352011-11-03 19:51:38 -0700606void Class::SetClassSize(size_t new_class_size) {
607 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
608 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
609}
610
Elliott Hughes4681c802011-09-25 18:04:37 -0700611void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700612 if ((flags & kDumpClassFullDetail) == 0) {
613 os << PrettyClass(this);
614 if ((flags & kDumpClassClassLoader) != 0) {
615 os << ' ' << GetClassLoader();
616 }
617 if ((flags & kDumpClassInitialized) != 0) {
618 os << ' ' << GetStatus();
619 }
Elliott Hughese0918552011-10-28 17:18:29 -0700620 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700621 return;
622 }
623
624 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800625 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700626 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800627 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700628 os << " objectSize=" << SizeOf() << " "
629 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
630 os << StringPrintf(" access=0x%04x.%04x\n",
631 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
632 if (super != NULL) {
633 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
634 }
635 if (IsArrayClass()) {
636 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
637 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800638 if (kh.NumInterfaces() > 0) {
639 os << " interfaces (" << kh.NumInterfaces() << "):\n";
640 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
641 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700642 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800643 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700644 }
645 }
646 os << " vtable (" << NumVirtualMethods() << " entries, "
647 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
648 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800649 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700650 }
651 os << " direct methods (" << NumDirectMethods() << " entries):\n";
652 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800653 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700654 }
655 if (NumStaticFields() > 0) {
656 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700657 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700658 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800659 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700660 }
661 } else {
662 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700663 }
664 }
665 if (NumInstanceFields() > 0) {
666 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700667 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700668 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800669 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700670 }
671 } else {
672 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700673 }
674 }
675}
676
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700677void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
678 if (new_reference_offsets != CLASS_WALK_SUPER) {
679 // Sanity check that the number of bits set in the reference offset bitmap
680 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800681 size_t count = 0;
682 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
683 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700684 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800685 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700686 }
687 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
688 new_reference_offsets, false);
689}
690
691void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
692 if (new_reference_offsets != CLASS_WALK_SUPER) {
693 // Sanity check that the number of bits set in the reference offset bitmap
694 // agrees with the number of references
695 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
696 NumReferenceStaticFieldsDuringLinking());
697 }
698 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
699 new_reference_offsets, false);
700}
701
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700702bool Class::Implements(const Class* klass) const {
703 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700704 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700705 // All interfaces implemented directly and by our superclass, and
706 // recursively all super-interfaces of those interfaces, are listed
707 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700708 int32_t iftable_count = GetIfTableCount();
709 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
710 for (int32_t i = 0; i < iftable_count; i++) {
711 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700712 return true;
713 }
714 }
715 return false;
716}
717
718// Determine whether "this" is assignable from "klazz", where both of these
719// are array classes.
720//
721// Consider an array class, e.g. Y[][], where Y is a subclass of X.
722// Y[][] = Y[][] --> true (identity)
723// X[][] = Y[][] --> true (element superclass)
724// Y = Y[][] --> false
725// Y[] = Y[][] --> false
726// Object = Y[][] --> true (everything is an object)
727// Object[] = Y[][] --> true
728// Object[][] = Y[][] --> true
729// Object[][][] = Y[][] --> false (too many []s)
730// Serializable = Y[][] --> true (all arrays are Serializable)
731// Serializable[] = Y[][] --> true
732// Serializable[][] = Y[][] --> false (unless Y is Serializable)
733//
734// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700735// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700736//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700737bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700738 DCHECK(IsArrayClass()) << PrettyClass(this);
739 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700740 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700741}
742
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700743bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700744 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
745 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700746 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700747 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700748 // src's super should be java_lang_Object, since it is an array.
749 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700750 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
751 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700752 return this == java_lang_Object;
753 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700754 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700755}
756
757bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700758 DCHECK(!IsInterface()) << PrettyClass(this);
759 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700760 const Class* current = this;
761 do {
762 if (current == klass) {
763 return true;
764 }
765 current = current->GetSuperClass();
766 } while (current != NULL);
767 return false;
768}
769
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800770bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700771 size_t i = 0;
772 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
773 ++i;
774 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700775 if (descriptor1.find('/', i) != StringPiece::npos ||
776 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700777 return false;
778 } else {
779 return true;
780 }
781}
782
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700783#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700784bool Class::IsInSamePackage(const StringPiece& descriptor1,
785 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700786 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700787 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700788 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
789 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700790 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
791}
792#endif
793
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700794bool Class::IsInSamePackage(const Class* that) const {
795 const Class* klass1 = this;
796 const Class* klass2 = that;
797 if (klass1 == klass2) {
798 return true;
799 }
800 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700801 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700802 return false;
803 }
804 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700805 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700806 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700807 }
jeffhao4a801a42011-09-23 13:53:40 -0700808 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700809 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700810 }
811 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800812 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800813 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800814 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800815 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800816 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700817}
818
Elliott Hughesdbb40792011-11-18 17:05:22 -0800819bool Class::IsClassClass() const {
820 Class* java_lang_Class = GetClass()->GetClass();
821 return this == java_lang_Class;
822}
823
824bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800825 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800826}
827
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800828bool Class::IsThrowableClass() const {
829 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
830 return throwable->IsAssignableFrom(this);
831}
832
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800833ClassLoader* Class::GetClassLoader() const {
834 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700835}
836
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700837void Class::SetClassLoader(const ClassLoader* new_cl) {
838 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700840}
841
Ian Rogersb04f69f2011-10-17 00:40:54 -0700842Method* Class::FindVirtualMethodForInterface(Method* method, bool can_throw) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700843 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700844 DCHECK(declaring_class != NULL) << PrettyClass(this);
845 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700846 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700847 int32_t iftable_count = GetIfTableCount();
848 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
849 for (int32_t i = 0; i < iftable_count; i++) {
850 InterfaceEntry* interface_entry = iftable->Get(i);
851 if (interface_entry->GetInterface() == declaring_class) {
852 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700853 }
854 }
Ian Rogersb04f69f2011-10-17 00:40:54 -0700855 if (can_throw) {
856 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
857 "Class %s does not implement interface %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800858 PrettyDescriptor(this).c_str(), PrettyDescriptor(declaring_class).c_str());
Ian Rogersb04f69f2011-10-17 00:40:54 -0700859 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700860 return NULL;
861}
862
Ian Rogers466bb252011-10-14 03:29:56 -0700863Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700864 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800865 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700866 if (method != NULL) {
867 return method;
868 }
869
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700870 int32_t iftable_count = GetIfTableCount();
871 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
872 for (int32_t i = 0; i < iftable_count; i++) {
873 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700874 if (method != NULL) {
875 return method;
876 }
877 }
878 return NULL;
879}
880
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700881Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700882 const StringPiece& signature) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800883 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700884 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700885 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800886 mh.ChangeMethod(method);
887 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700888 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700889 }
890 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700891 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700892}
893
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700894Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700895 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700896 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700897 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700898 if (method != NULL) {
899 return method;
900 }
901 }
902 return NULL;
903}
904
905Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -0700906 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800907 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700908 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700909 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800910 mh.ChangeMethod(method);
911 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -0700912 return method;
Ian Rogers466bb252011-10-14 03:29:56 -0700913 }
914 }
915 return NULL;
916}
917
918Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
919 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
920 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
921 if (method != NULL) {
922 return method;
923 }
924 }
925 return NULL;
926}
927
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700928Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 // Is the field in this class?
930 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800931 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 for (size_t i = 0; i < NumInstanceFields(); ++i) {
933 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800934 fh.ChangeField(f);
935 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700936 return f;
937 }
938 }
939 return NULL;
940}
941
942Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 // Is the field in this class, or any of its superclasses?
944 // Interfaces are not relevant because they can't contain instance fields.
945 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700946 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 if (f != NULL) {
948 return f;
949 }
950 }
951 return NULL;
952}
953
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700954Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
955 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800956 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700957 for (size_t i = 0; i < NumStaticFields(); ++i) {
958 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800959 fh.ChangeField(f);
960 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700961 return f;
962 }
963 }
964 return NULL;
965}
966
967Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
968 // Is the field in this class (or its interfaces), or any of its
969 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -0800970 ClassHelper kh;
971 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700972 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -0800973 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700974 if (f != NULL) {
975 return f;
976 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700977 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -0800978 kh.ChangeClass(k);
979 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
980 Class* interface = kh.GetInterface(i);
981 f = interface->FindDeclaredStaticField(name, type);
982 if (f != NULL) {
983 return f;
984 }
985 }
986 }
987 return NULL;
988}
989
990Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
991 // Find a field using the JLS field resolution order
992 ClassHelper kh;
993 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
994 // Is the field in this class?
995 Field* f = k->FindDeclaredInstanceField(name, type);
996 if (f != NULL) {
997 return f;
998 }
999 f = k->FindDeclaredStaticField(name, type);
1000 if (f != NULL) {
1001 return f;
1002 }
1003 // Is this field in any of this class' interfaces?
1004 kh.ChangeClass(k);
1005 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1006 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001007 f = interface->FindDeclaredStaticField(name, type);
1008 if (f != NULL) {
1009 return f;
1010 }
1011 }
1012 }
1013 return NULL;
1014}
1015
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001016Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001017 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001018 DCHECK_GE(component_count, 0);
1019 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001020
1021 size_t header_size = sizeof(Array);
1022 size_t data_size = component_count * component_size;
1023 size_t size = header_size + data_size;
1024
1025 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1026 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1027 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1028 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001029 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001030 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001031 return NULL;
1032 }
1033
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001034 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1035 if (array != NULL) {
1036 DCHECK(array->IsArrayInstance());
1037 array->SetLength(component_count);
1038 }
1039 return array;
1040}
1041
1042Array* Array::Alloc(Class* array_class, int32_t component_count) {
1043 return Alloc(array_class, component_count, array_class->GetComponentSize());
1044}
1045
Elliott Hughes80609252011-09-23 17:24:51 -07001046bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001047 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001048 "length=%i; index=%i", length_, index);
1049 return false;
1050}
1051
1052bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001053 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001054 "Can't store an element of type %s into an array of type %s",
1055 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1056 return false;
1057}
1058
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001059template<typename T>
1060PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001061 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001062 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1063 return down_cast<PrimitiveArray<T>*>(raw_array);
1064}
1065
1066template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1067
1068// Explicitly instantiate all the primitive array types.
1069template class PrimitiveArray<uint8_t>; // BooleanArray
1070template class PrimitiveArray<int8_t>; // ByteArray
1071template class PrimitiveArray<uint16_t>; // CharArray
1072template class PrimitiveArray<double>; // DoubleArray
1073template class PrimitiveArray<float>; // FloatArray
1074template class PrimitiveArray<int32_t>; // IntArray
1075template class PrimitiveArray<int64_t>; // LongArray
1076template class PrimitiveArray<int16_t>; // ShortArray
1077
Ian Rogers466bb252011-10-14 03:29:56 -07001078// Explicitly instantiate Class[][]
1079template class ObjectArray<ObjectArray<Class> >;
1080
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001081// TODO: get global references for these
1082Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001083
Brian Carlstroma663ea52011-08-19 23:33:41 -07001084void String::SetClass(Class* java_lang_String) {
1085 CHECK(java_lang_String_ == NULL);
1086 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001087 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001088}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001089
Brian Carlstroma663ea52011-08-19 23:33:41 -07001090void String::ResetClass() {
1091 CHECK(java_lang_String_ != NULL);
1092 java_lang_String_ = NULL;
1093}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001094
Brian Carlstromc74255f2011-09-11 22:47:39 -07001095String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001096 return Runtime::Current()->GetInternTable()->InternWeak(this);
1097}
1098
Brian Carlstrom395520e2011-09-25 19:35:00 -07001099int32_t String::GetHashCode() {
1100 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1101 if (result == 0) {
1102 ComputeHashCode();
1103 }
1104 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1105 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1106 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001107 return result;
1108}
1109
1110int32_t String::GetLength() const {
1111 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1112 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1113 return result;
1114}
1115
1116uint16_t String::CharAt(int32_t index) const {
1117 // TODO: do we need this? Equals is the only caller, and could
1118 // bounds check itself.
1119 if (index < 0 || index >= count_) {
1120 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001121 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001122 "length=%i; index=%i", count_, index);
1123 return 0;
1124 }
1125 return GetCharArray()->Get(index + GetOffset());
1126}
1127
1128String* String::AllocFromUtf16(int32_t utf16_length,
1129 const uint16_t* utf16_data_in,
1130 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001131 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001132 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001133 if (string == NULL) {
1134 return NULL;
1135 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001136 // TODO: use 16-bit wide memset variant
1137 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001138 if (array == NULL) {
1139 return NULL;
1140 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001141 for (int i = 0; i < utf16_length; i++) {
1142 array->Set(i, utf16_data_in[i]);
1143 }
1144 if (hash_code != 0) {
1145 string->SetHashCode(hash_code);
1146 } else {
1147 string->ComputeHashCode();
1148 }
1149 return string;
1150}
1151
1152String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001153 if (utf == NULL) {
1154 return NULL;
1155 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001156 size_t char_count = CountModifiedUtf8Chars(utf);
1157 return AllocFromModifiedUtf8(char_count, utf);
1158}
1159
1160String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1161 const char* utf8_data_in) {
1162 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001163 if (string == NULL) {
1164 return NULL;
1165 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001166 uint16_t* utf16_data_out =
1167 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1168 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1169 string->ComputeHashCode();
1170 return string;
1171}
1172
1173String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001174 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1175 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001176 return NULL;
1177 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001178 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001179}
1180
1181String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001182 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001183 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001184 if (string == NULL) {
1185 return NULL;
1186 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001187 string->SetArray(array);
1188 string->SetCount(array->GetLength());
1189 return string;
1190}
1191
1192bool String::Equals(const String* that) const {
1193 if (this == that) {
1194 // Quick reference equality test
1195 return true;
1196 } else if (that == NULL) {
1197 // Null isn't an instanceof anything
1198 return false;
1199 } else if (this->GetLength() != that->GetLength()) {
1200 // Quick length inequality test
1201 return false;
1202 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001203 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001204 // hash code was already equal
1205 for (int32_t i = 0; i < that->GetLength(); ++i) {
1206 if (this->CharAt(i) != that->CharAt(i)) {
1207 return false;
1208 }
1209 }
1210 return true;
1211 }
1212}
1213
Elliott Hughes5d78d392011-12-13 16:53:05 -08001214bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001215 if (this->GetLength() != that_length) {
1216 return false;
1217 } else {
1218 for (int32_t i = 0; i < that_length; ++i) {
1219 if (this->CharAt(i) != that_chars[that_offset + i]) {
1220 return false;
1221 }
1222 }
1223 return true;
1224 }
1225}
1226
1227bool String::Equals(const char* modified_utf8) const {
1228 for (int32_t i = 0; i < GetLength(); ++i) {
1229 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1230 if (ch == '\0' || ch != CharAt(i)) {
1231 return false;
1232 }
1233 }
1234 return *modified_utf8 == '\0';
1235}
1236
1237bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001238 if (modified_utf8.size() != GetLength()) {
1239 return false;
1240 }
1241 const char* p = modified_utf8.data();
1242 for (int32_t i = 0; i < GetLength(); ++i) {
1243 uint16_t ch = GetUtf16FromUtf8(&p);
1244 if (ch != CharAt(i)) {
1245 return false;
1246 }
1247 }
1248 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001249}
1250
1251// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1252std::string String::ToModifiedUtf8() const {
1253 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1254 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1255 std::string result(byte_count, char(0));
1256 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1257 return result;
1258}
1259
Ian Rogers466bb252011-10-14 03:29:56 -07001260bool Throwable::IsCheckedException() const {
1261 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1262 if (InstanceOf(error)) {
1263 return false;
1264 }
1265 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1266 return !InstanceOf(jlre);
1267}
1268
Ian Rogers9074b992011-10-26 17:41:55 -07001269std::string Throwable::Dump() const {
1270 Object* stack_state = GetStackState();
1271 if (stack_state == NULL || !stack_state->IsObjectArray()) {
1272 // missing or corrupt stack state
1273 return "";
1274 }
1275 // Decode the internal stack trace into the depth and method trace
1276 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1277 int32_t depth = method_trace->GetLength() - 1;
1278 std::string result;
1279 for (int32_t i = 0; i < depth; ++i) {
1280 Method* method = down_cast<Method*>(method_trace->Get(i));
1281 result += " at ";
1282 result += PrettyMethod(method, true);
1283 result += "\n";
1284 }
1285 return result;
1286}
1287
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001288Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1289
1290void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1291 CHECK(java_lang_StackTraceElement_ == NULL);
1292 CHECK(java_lang_StackTraceElement != NULL);
1293 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1294}
1295
1296void StackTraceElement::ResetClass() {
1297 CHECK(java_lang_StackTraceElement_ != NULL);
1298 java_lang_StackTraceElement_ = NULL;
1299}
1300
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001301StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1302 String* method_name,
1303 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001304 int32_t line_number) {
1305 StackTraceElement* trace =
1306 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1307 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1308 const_cast<String*>(declaring_class), false);
1309 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1310 const_cast<String*>(method_name), false);
1311 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1312 const_cast<String*>(file_name), false);
1313 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1314 line_number, false);
1315 return trace;
1316}
1317
Elliott Hughes1f359b02011-07-17 14:27:17 -07001318static const char* kClassStatusNames[] = {
1319 "Error",
1320 "NotReady",
1321 "Idx",
1322 "Loaded",
1323 "Resolved",
1324 "Verifying",
1325 "Verified",
1326 "Initializing",
1327 "Initialized"
1328};
1329std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1330 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001331 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001332 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001333 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001334 }
1335 return os;
1336}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001337
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001338} // namespace art