blob: 1ef720120206de47f3b838d7cc8c45b44fcdf49b [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 Hughes418d20f2011-09-22 14:00:39 -0700310 return class_linker->FindClass(descriptor, cl);
311 } else if (*p == 'L') {
312 const char* start = p;
313 while (*p != ';') {
314 ++p;
315 }
316 ++p;
317 StringPiece descriptor(start, (p - start));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700318 return class_linker->FindClass(descriptor.ToString(), 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();
jeffhaoe343b762011-12-05 16:36:44 -0800450 const void* code_offset = Trace::IsMethodTracingActive() ? Trace::GetSavedCodeFromMap(this) : GetCode();
451 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) {
jeffhaoe343b762011-12-05 16:36:44 -0800481 if (Trace::IsMethodTracingActive()) {
482 return reinterpret_cast<uintptr_t>(Trace::GetSavedCodeFromMap(this)) + map_offset;
483 } else {
484 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
485 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700486 }
487 }
488 LOG(FATAL) << "Looking up Dex PC not contained in method";
489 return 0;
490}
491
492uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800493 MethodHelper mh(this);
494 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700495 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700496 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
497 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700498 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700499 if (iter_type_idx == DexFile::kDexNoIndex16) {
500 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700501 }
502 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800503 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700504 if (iter_exception_type == NULL) {
505 // The verifier should take care of resolving all exception classes early
506 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800507 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700508 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700509 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700510 }
511 }
512 // Handler not found
513 return DexFile::kDexNoIndex;
514}
515
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700516void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
517 // Push a transition back into managed code onto the linked list in thread.
518 CHECK_EQ(Thread::kRunnable, self->GetState());
519 NativeToManagedRecord record;
520 self->PushNativeToManagedRecord(&record);
521
522 // Call the invoke stub associated with the method.
523 // Pass everything as arguments.
524 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700525
526 bool have_executable_code = (GetCode() != NULL);
527#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700528 // Currently we can only compile non-native methods for ARM.
529 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700530#endif
531
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500532 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700533 bool log = false;
534 if (log) {
535 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
536 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700537 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700538 if (log) {
539 LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
540 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700541 } else {
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500542 LOG(INFO) << "not invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub
Brian Carlstromebd1fd22011-12-07 15:46:26 -0800543 << " started=" << Runtime::Current()->IsStarted();
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700544 if (result != NULL) {
545 result->j = 0;
546 }
547 }
548
549 // Pop transition.
550 self->PopNativeToManagedRecord(record);
551}
552
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700553bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700554 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers169c9a72011-11-13 20:13:17 -0800555 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700556 return native_method != jni_stub;
557}
558
559void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700560 CHECK(IsNative()) << PrettyMethod(this);
561 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700562 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
563 native_method, false);
564}
565
566void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700567 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700568 // restore stub to lookup native pointer via dlsym
Ian Rogers169c9a72011-11-13 20:13:17 -0800569 RegisterNative(Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700570}
571
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700572void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700573 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
574 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700575 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700576 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700577}
578
579DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700580 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700581}
582
583void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700584 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700585}
586
Brian Carlstrom1f870082011-08-23 16:02:11 -0700587Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700588 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700589 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500590 // TODO: decide whether we want this check. It currently fails during bootstrap.
591 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700592 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700593 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700594}
595
Ian Rogers0571d352011-11-03 19:51:38 -0700596void Class::SetClassSize(size_t new_class_size) {
597 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
598 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
599}
600
Elliott Hughes4681c802011-09-25 18:04:37 -0700601void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700602 if ((flags & kDumpClassFullDetail) == 0) {
603 os << PrettyClass(this);
604 if ((flags & kDumpClassClassLoader) != 0) {
605 os << ' ' << GetClassLoader();
606 }
607 if ((flags & kDumpClassInitialized) != 0) {
608 os << ' ' << GetStatus();
609 }
Elliott Hughese0918552011-10-28 17:18:29 -0700610 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700611 return;
612 }
613
614 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800615 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700616 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800617 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700618 os << " objectSize=" << SizeOf() << " "
619 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
620 os << StringPrintf(" access=0x%04x.%04x\n",
621 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
622 if (super != NULL) {
623 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
624 }
625 if (IsArrayClass()) {
626 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
627 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800628 if (kh.NumInterfaces() > 0) {
629 os << " interfaces (" << kh.NumInterfaces() << "):\n";
630 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
631 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700632 const ClassLoader* cl = interface->GetClassLoader();
633 os << StringPrintf(" %2d: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
634 }
635 }
636 os << " vtable (" << NumVirtualMethods() << " entries, "
637 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
638 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700639 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700640 }
641 os << " direct methods (" << NumDirectMethods() << " entries):\n";
642 for (size_t i = 0; i < NumDirectMethods(); ++i) {
643 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
644 }
645 if (NumStaticFields() > 0) {
646 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700647 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700648 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700649 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700650 }
651 } else {
652 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700653 }
654 }
655 if (NumInstanceFields() > 0) {
656 os << " instance fields (" << NumInstanceFields() << " 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 < NumInstanceFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700659 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetInstanceField(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}
666
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700667void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
668 if (new_reference_offsets != CLASS_WALK_SUPER) {
669 // Sanity check that the number of bits set in the reference offset bitmap
670 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800671 size_t count = 0;
672 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
673 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700674 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800675 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700676 }
677 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
678 new_reference_offsets, false);
679}
680
681void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
682 if (new_reference_offsets != CLASS_WALK_SUPER) {
683 // Sanity check that the number of bits set in the reference offset bitmap
684 // agrees with the number of references
685 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
686 NumReferenceStaticFieldsDuringLinking());
687 }
688 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
689 new_reference_offsets, false);
690}
691
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700692bool Class::Implements(const Class* klass) const {
693 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700694 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700695 // All interfaces implemented directly and by our superclass, and
696 // recursively all super-interfaces of those interfaces, are listed
697 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700698 int32_t iftable_count = GetIfTableCount();
699 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
700 for (int32_t i = 0; i < iftable_count; i++) {
701 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700702 return true;
703 }
704 }
705 return false;
706}
707
708// Determine whether "this" is assignable from "klazz", where both of these
709// are array classes.
710//
711// Consider an array class, e.g. Y[][], where Y is a subclass of X.
712// Y[][] = Y[][] --> true (identity)
713// X[][] = Y[][] --> true (element superclass)
714// Y = Y[][] --> false
715// Y[] = Y[][] --> false
716// Object = Y[][] --> true (everything is an object)
717// Object[] = Y[][] --> true
718// Object[][] = Y[][] --> true
719// Object[][][] = Y[][] --> false (too many []s)
720// Serializable = Y[][] --> true (all arrays are Serializable)
721// Serializable[] = Y[][] --> true
722// Serializable[][] = Y[][] --> false (unless Y is Serializable)
723//
724// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700725// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700726//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700727bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700728 DCHECK(IsArrayClass()) << PrettyClass(this);
729 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700730 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700731}
732
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700733bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700734 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
735 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700736 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700737 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700738 // src's super should be java_lang_Object, since it is an array.
739 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700740 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
741 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700742 return this == java_lang_Object;
743 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700744 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700745}
746
747bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700748 DCHECK(!IsInterface()) << PrettyClass(this);
749 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700750 const Class* current = this;
751 do {
752 if (current == klass) {
753 return true;
754 }
755 current = current->GetSuperClass();
756 } while (current != NULL);
757 return false;
758}
759
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800760bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700761 size_t i = 0;
762 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
763 ++i;
764 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700765 if (descriptor1.find('/', i) != StringPiece::npos ||
766 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700767 return false;
768 } else {
769 return true;
770 }
771}
772
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700773#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700774bool Class::IsInSamePackage(const StringPiece& descriptor1,
775 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700776 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700777 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700778 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
779 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
781}
782#endif
783
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700784bool Class::IsInSamePackage(const Class* that) const {
785 const Class* klass1 = this;
786 const Class* klass2 = that;
787 if (klass1 == klass2) {
788 return true;
789 }
790 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700791 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700792 return false;
793 }
794 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700795 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700796 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700797 }
jeffhao4a801a42011-09-23 13:53:40 -0700798 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700799 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700800 }
801 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800802 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800803 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800804 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800805 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800806 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700807}
808
Elliott Hughesdbb40792011-11-18 17:05:22 -0800809bool Class::IsClassClass() const {
810 Class* java_lang_Class = GetClass()->GetClass();
811 return this == java_lang_Class;
812}
813
814bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800815 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800816}
817
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800818bool Class::IsThrowableClass() const {
819 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
820 return throwable->IsAssignableFrom(this);
821}
822
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800823ClassLoader* Class::GetClassLoader() const {
824 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700825}
826
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700827void Class::SetClassLoader(const ClassLoader* new_cl) {
828 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700830}
831
Ian Rogersb04f69f2011-10-17 00:40:54 -0700832Method* Class::FindVirtualMethodForInterface(Method* method, bool can_throw) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700833 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700834 DCHECK(declaring_class != NULL) << PrettyClass(this);
835 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700836 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700837 int32_t iftable_count = GetIfTableCount();
838 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
839 for (int32_t i = 0; i < iftable_count; i++) {
840 InterfaceEntry* interface_entry = iftable->Get(i);
841 if (interface_entry->GetInterface() == declaring_class) {
842 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700843 }
844 }
Ian Rogersb04f69f2011-10-17 00:40:54 -0700845 if (can_throw) {
846 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
847 "Class %s does not implement interface %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800848 PrettyDescriptor(this).c_str(), PrettyDescriptor(declaring_class).c_str());
Ian Rogersb04f69f2011-10-17 00:40:54 -0700849 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700850 return NULL;
851}
852
Ian Rogers466bb252011-10-14 03:29:56 -0700853Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700854 // Check the current class before checking the interfaces.
855 Method* method = FindVirtualMethod(name, signature);
856 if (method != NULL) {
857 return method;
858 }
859
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700860 int32_t iftable_count = GetIfTableCount();
861 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
862 for (int32_t i = 0; i < iftable_count; i++) {
863 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700864 if (method != NULL) {
865 return method;
866 }
867 }
868 return NULL;
869}
870
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700871Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700872 const StringPiece& signature) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800873 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700874 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700875 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800876 mh.ChangeMethod(method);
877 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700878 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700879 }
880 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700881 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700882}
883
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700884Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700885 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700886 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700887 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700888 if (method != NULL) {
889 return method;
890 }
891 }
892 return NULL;
893}
894
895Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -0700896 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800897 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700898 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700899 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 mh.ChangeMethod(method);
901 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -0700902 return method;
Ian Rogers466bb252011-10-14 03:29:56 -0700903 }
904 }
905 return NULL;
906}
907
908Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
909 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
910 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
911 if (method != NULL) {
912 return method;
913 }
914 }
915 return NULL;
916}
917
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700918Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 // Is the field in this class?
920 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800921 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 for (size_t i = 0; i < NumInstanceFields(); ++i) {
923 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800924 fh.ChangeField(f);
925 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700926 return f;
927 }
928 }
929 return NULL;
930}
931
932Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 // Is the field in this class, or any of its superclasses?
934 // Interfaces are not relevant because they can't contain instance fields.
935 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700936 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 if (f != NULL) {
938 return f;
939 }
940 }
941 return NULL;
942}
943
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700944Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
945 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800946 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700947 for (size_t i = 0; i < NumStaticFields(); ++i) {
948 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800949 fh.ChangeField(f);
950 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700951 return f;
952 }
953 }
954 return NULL;
955}
956
957Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
958 // Is the field in this class (or its interfaces), or any of its
959 // superclasses (or their interfaces)?
960 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
961 // Is the field in this class?
962 Field* f = c->FindDeclaredStaticField(name, type);
963 if (f != NULL) {
964 return f;
965 }
966
967 // Is this field in any of this class' interfaces?
968 for (int32_t i = 0; i < c->GetIfTableCount(); ++i) {
969 InterfaceEntry* interface_entry = c->GetIfTable()->Get(i);
970 Class* interface = interface_entry->GetInterface();
971 f = interface->FindDeclaredStaticField(name, type);
972 if (f != NULL) {
973 return f;
974 }
975 }
976 }
977 return NULL;
978}
979
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700980Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700981 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700982 DCHECK_GE(component_count, 0);
983 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -0700984
985 size_t header_size = sizeof(Array);
986 size_t data_size = component_count * component_size;
987 size_t size = header_size + data_size;
988
989 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
990 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
991 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
992 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
993 "%s of length %zd exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800994 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -0700995 return NULL;
996 }
997
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700998 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
999 if (array != NULL) {
1000 DCHECK(array->IsArrayInstance());
1001 array->SetLength(component_count);
1002 }
1003 return array;
1004}
1005
1006Array* Array::Alloc(Class* array_class, int32_t component_count) {
1007 return Alloc(array_class, component_count, array_class->GetComponentSize());
1008}
1009
Elliott Hughes80609252011-09-23 17:24:51 -07001010bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001011 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001012 "length=%i; index=%i", length_, index);
1013 return false;
1014}
1015
1016bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001017 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001018 "Can't store an element of type %s into an array of type %s",
1019 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1020 return false;
1021}
1022
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001023template<typename T>
1024PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001025 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001026 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1027 return down_cast<PrimitiveArray<T>*>(raw_array);
1028}
1029
1030template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1031
1032// Explicitly instantiate all the primitive array types.
1033template class PrimitiveArray<uint8_t>; // BooleanArray
1034template class PrimitiveArray<int8_t>; // ByteArray
1035template class PrimitiveArray<uint16_t>; // CharArray
1036template class PrimitiveArray<double>; // DoubleArray
1037template class PrimitiveArray<float>; // FloatArray
1038template class PrimitiveArray<int32_t>; // IntArray
1039template class PrimitiveArray<int64_t>; // LongArray
1040template class PrimitiveArray<int16_t>; // ShortArray
1041
Ian Rogers466bb252011-10-14 03:29:56 -07001042// Explicitly instantiate Class[][]
1043template class ObjectArray<ObjectArray<Class> >;
1044
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001045// TODO: get global references for these
1046Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001047
Brian Carlstroma663ea52011-08-19 23:33:41 -07001048void String::SetClass(Class* java_lang_String) {
1049 CHECK(java_lang_String_ == NULL);
1050 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001051 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001052}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001053
Brian Carlstroma663ea52011-08-19 23:33:41 -07001054void String::ResetClass() {
1055 CHECK(java_lang_String_ != NULL);
1056 java_lang_String_ = NULL;
1057}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001058
Brian Carlstromc74255f2011-09-11 22:47:39 -07001059String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001060 return Runtime::Current()->GetInternTable()->InternWeak(this);
1061}
1062
Brian Carlstrom395520e2011-09-25 19:35:00 -07001063int32_t String::GetHashCode() {
1064 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1065 if (result == 0) {
1066 ComputeHashCode();
1067 }
1068 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1069 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1070 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001071 return result;
1072}
1073
1074int32_t String::GetLength() const {
1075 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1076 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1077 return result;
1078}
1079
1080uint16_t String::CharAt(int32_t index) const {
1081 // TODO: do we need this? Equals is the only caller, and could
1082 // bounds check itself.
1083 if (index < 0 || index >= count_) {
1084 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001085 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001086 "length=%i; index=%i", count_, index);
1087 return 0;
1088 }
1089 return GetCharArray()->Get(index + GetOffset());
1090}
1091
1092String* String::AllocFromUtf16(int32_t utf16_length,
1093 const uint16_t* utf16_data_in,
1094 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001095 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001096 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001097 if (string == NULL) {
1098 return NULL;
1099 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001100 // TODO: use 16-bit wide memset variant
1101 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001102 if (array == NULL) {
1103 return NULL;
1104 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001105 for (int i = 0; i < utf16_length; i++) {
1106 array->Set(i, utf16_data_in[i]);
1107 }
1108 if (hash_code != 0) {
1109 string->SetHashCode(hash_code);
1110 } else {
1111 string->ComputeHashCode();
1112 }
1113 return string;
1114}
1115
1116String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001117 if (utf == NULL) {
1118 return NULL;
1119 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001120 size_t char_count = CountModifiedUtf8Chars(utf);
1121 return AllocFromModifiedUtf8(char_count, utf);
1122}
1123
1124String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1125 const char* utf8_data_in) {
1126 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001127 if (string == NULL) {
1128 return NULL;
1129 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001130 uint16_t* utf16_data_out =
1131 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1132 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1133 string->ComputeHashCode();
1134 return string;
1135}
1136
1137String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001138 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1139 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001140 return NULL;
1141 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001142 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001143}
1144
1145String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001146 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001147 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001148 if (string == NULL) {
1149 return NULL;
1150 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001151 string->SetArray(array);
1152 string->SetCount(array->GetLength());
1153 return string;
1154}
1155
1156bool String::Equals(const String* that) const {
1157 if (this == that) {
1158 // Quick reference equality test
1159 return true;
1160 } else if (that == NULL) {
1161 // Null isn't an instanceof anything
1162 return false;
1163 } else if (this->GetLength() != that->GetLength()) {
1164 // Quick length inequality test
1165 return false;
1166 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001167 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001168 // hash code was already equal
1169 for (int32_t i = 0; i < that->GetLength(); ++i) {
1170 if (this->CharAt(i) != that->CharAt(i)) {
1171 return false;
1172 }
1173 }
1174 return true;
1175 }
1176}
1177
Elliott Hughes5d78d392011-12-13 16:53:05 -08001178bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001179 if (this->GetLength() != that_length) {
1180 return false;
1181 } else {
1182 for (int32_t i = 0; i < that_length; ++i) {
1183 if (this->CharAt(i) != that_chars[that_offset + i]) {
1184 return false;
1185 }
1186 }
1187 return true;
1188 }
1189}
1190
1191bool String::Equals(const char* modified_utf8) const {
1192 for (int32_t i = 0; i < GetLength(); ++i) {
1193 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1194 if (ch == '\0' || ch != CharAt(i)) {
1195 return false;
1196 }
1197 }
1198 return *modified_utf8 == '\0';
1199}
1200
1201bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001202 if (modified_utf8.size() != GetLength()) {
1203 return false;
1204 }
1205 const char* p = modified_utf8.data();
1206 for (int32_t i = 0; i < GetLength(); ++i) {
1207 uint16_t ch = GetUtf16FromUtf8(&p);
1208 if (ch != CharAt(i)) {
1209 return false;
1210 }
1211 }
1212 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001213}
1214
1215// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1216std::string String::ToModifiedUtf8() const {
1217 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1218 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1219 std::string result(byte_count, char(0));
1220 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1221 return result;
1222}
1223
Ian Rogers466bb252011-10-14 03:29:56 -07001224bool Throwable::IsCheckedException() const {
1225 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1226 if (InstanceOf(error)) {
1227 return false;
1228 }
1229 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1230 return !InstanceOf(jlre);
1231}
1232
Ian Rogers9074b992011-10-26 17:41:55 -07001233std::string Throwable::Dump() const {
1234 Object* stack_state = GetStackState();
1235 if (stack_state == NULL || !stack_state->IsObjectArray()) {
1236 // missing or corrupt stack state
1237 return "";
1238 }
1239 // Decode the internal stack trace into the depth and method trace
1240 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1241 int32_t depth = method_trace->GetLength() - 1;
1242 std::string result;
1243 for (int32_t i = 0; i < depth; ++i) {
1244 Method* method = down_cast<Method*>(method_trace->Get(i));
1245 result += " at ";
1246 result += PrettyMethod(method, true);
1247 result += "\n";
1248 }
1249 return result;
1250}
1251
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001252Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1253
1254void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1255 CHECK(java_lang_StackTraceElement_ == NULL);
1256 CHECK(java_lang_StackTraceElement != NULL);
1257 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1258}
1259
1260void StackTraceElement::ResetClass() {
1261 CHECK(java_lang_StackTraceElement_ != NULL);
1262 java_lang_StackTraceElement_ = NULL;
1263}
1264
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001265StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1266 String* method_name,
1267 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001268 int32_t line_number) {
1269 StackTraceElement* trace =
1270 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1271 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1272 const_cast<String*>(declaring_class), false);
1273 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1274 const_cast<String*>(method_name), false);
1275 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1276 const_cast<String*>(file_name), false);
1277 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1278 line_number, false);
1279 return trace;
1280}
1281
Elliott Hughes1f359b02011-07-17 14:27:17 -07001282static const char* kClassStatusNames[] = {
1283 "Error",
1284 "NotReady",
1285 "Idx",
1286 "Loaded",
1287 "Resolved",
1288 "Verifying",
1289 "Verified",
1290 "Initializing",
1291 "Initialized"
1292};
1293std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1294 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001295 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001296 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001297 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001298 }
1299 return os;
1300}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001301
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001302} // namespace art