blob: bf32913148c68f05a5dc916bffe94aada7a717ac [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) {
542 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
543 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700544 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700545 if (log) {
546 LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
547 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700548 } else {
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500549 LOG(INFO) << "not invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub
Brian Carlstromebd1fd22011-12-07 15:46:26 -0800550 << " started=" << Runtime::Current()->IsStarted();
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700551 if (result != NULL) {
552 result->j = 0;
553 }
554 }
555
556 // Pop transition.
557 self->PopNativeToManagedRecord(record);
558}
559
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700560bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700561 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers169c9a72011-11-13 20:13:17 -0800562 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700563 return native_method != jni_stub;
564}
565
566void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700567 CHECK(IsNative()) << PrettyMethod(this);
568 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700569 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
570 native_method, false);
571}
572
573void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700574 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700575 // restore stub to lookup native pointer via dlsym
Ian Rogers169c9a72011-11-13 20:13:17 -0800576 RegisterNative(Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700577}
578
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700579void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700580 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
581 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700582 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700583 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700584}
585
586DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700587 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700588}
589
590void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700591 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700592}
593
Brian Carlstrom1f870082011-08-23 16:02:11 -0700594Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700595 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700596 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500597 // TODO: decide whether we want this check. It currently fails during bootstrap.
598 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700599 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700600 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700601}
602
Ian Rogers0571d352011-11-03 19:51:38 -0700603void Class::SetClassSize(size_t new_class_size) {
604 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
605 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
606}
607
Elliott Hughes4681c802011-09-25 18:04:37 -0700608void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700609 if ((flags & kDumpClassFullDetail) == 0) {
610 os << PrettyClass(this);
611 if ((flags & kDumpClassClassLoader) != 0) {
612 os << ' ' << GetClassLoader();
613 }
614 if ((flags & kDumpClassInitialized) != 0) {
615 os << ' ' << GetStatus();
616 }
Elliott Hughese0918552011-10-28 17:18:29 -0700617 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700618 return;
619 }
620
621 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800622 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700623 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800624 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700625 os << " objectSize=" << SizeOf() << " "
626 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
627 os << StringPrintf(" access=0x%04x.%04x\n",
628 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
629 if (super != NULL) {
630 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
631 }
632 if (IsArrayClass()) {
633 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
634 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800635 if (kh.NumInterfaces() > 0) {
636 os << " interfaces (" << kh.NumInterfaces() << "):\n";
637 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
638 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700639 const ClassLoader* cl = interface->GetClassLoader();
640 os << StringPrintf(" %2d: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
641 }
642 }
643 os << " vtable (" << NumVirtualMethods() << " entries, "
644 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
645 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700646 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700647 }
648 os << " direct methods (" << NumDirectMethods() << " entries):\n";
649 for (size_t i = 0; i < NumDirectMethods(); ++i) {
650 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
651 }
652 if (NumStaticFields() > 0) {
653 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700654 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700655 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700656 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700657 }
658 } else {
659 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700660 }
661 }
662 if (NumInstanceFields() > 0) {
663 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700664 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700665 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700666 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700667 }
668 } else {
669 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700670 }
671 }
672}
673
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700674void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
675 if (new_reference_offsets != CLASS_WALK_SUPER) {
676 // Sanity check that the number of bits set in the reference offset bitmap
677 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800678 size_t count = 0;
679 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
680 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700681 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800682 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700683 }
684 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
685 new_reference_offsets, false);
686}
687
688void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
689 if (new_reference_offsets != CLASS_WALK_SUPER) {
690 // Sanity check that the number of bits set in the reference offset bitmap
691 // agrees with the number of references
692 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
693 NumReferenceStaticFieldsDuringLinking());
694 }
695 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
696 new_reference_offsets, false);
697}
698
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700699bool Class::Implements(const Class* klass) const {
700 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700701 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700702 // All interfaces implemented directly and by our superclass, and
703 // recursively all super-interfaces of those interfaces, are listed
704 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700705 int32_t iftable_count = GetIfTableCount();
706 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
707 for (int32_t i = 0; i < iftable_count; i++) {
708 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700709 return true;
710 }
711 }
712 return false;
713}
714
715// Determine whether "this" is assignable from "klazz", where both of these
716// are array classes.
717//
718// Consider an array class, e.g. Y[][], where Y is a subclass of X.
719// Y[][] = Y[][] --> true (identity)
720// X[][] = Y[][] --> true (element superclass)
721// Y = Y[][] --> false
722// Y[] = Y[][] --> false
723// Object = Y[][] --> true (everything is an object)
724// Object[] = Y[][] --> true
725// Object[][] = Y[][] --> true
726// Object[][][] = Y[][] --> false (too many []s)
727// Serializable = Y[][] --> true (all arrays are Serializable)
728// Serializable[] = Y[][] --> true
729// Serializable[][] = Y[][] --> false (unless Y is Serializable)
730//
731// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700732// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700733//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700734bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700735 DCHECK(IsArrayClass()) << PrettyClass(this);
736 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700737 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700738}
739
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700740bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700741 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
742 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700743 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700744 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700745 // src's super should be java_lang_Object, since it is an array.
746 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700747 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
748 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700749 return this == java_lang_Object;
750 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700751 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700752}
753
754bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700755 DCHECK(!IsInterface()) << PrettyClass(this);
756 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700757 const Class* current = this;
758 do {
759 if (current == klass) {
760 return true;
761 }
762 current = current->GetSuperClass();
763 } while (current != NULL);
764 return false;
765}
766
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800767bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700768 size_t i = 0;
769 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
770 ++i;
771 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700772 if (descriptor1.find('/', i) != StringPiece::npos ||
773 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700774 return false;
775 } else {
776 return true;
777 }
778}
779
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700781bool Class::IsInSamePackage(const StringPiece& descriptor1,
782 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700783 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700784 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700785 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
786 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700787 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
788}
789#endif
790
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700791bool Class::IsInSamePackage(const Class* that) const {
792 const Class* klass1 = this;
793 const Class* klass2 = that;
794 if (klass1 == klass2) {
795 return true;
796 }
797 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700798 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700799 return false;
800 }
801 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700802 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700803 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700804 }
jeffhao4a801a42011-09-23 13:53:40 -0700805 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700806 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700807 }
808 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800809 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800810 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800811 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800812 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800813 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700814}
815
Elliott Hughesdbb40792011-11-18 17:05:22 -0800816bool Class::IsClassClass() const {
817 Class* java_lang_Class = GetClass()->GetClass();
818 return this == java_lang_Class;
819}
820
821bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800822 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800823}
824
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800825bool Class::IsThrowableClass() const {
826 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
827 return throwable->IsAssignableFrom(this);
828}
829
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800830ClassLoader* Class::GetClassLoader() const {
831 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700832}
833
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700834void Class::SetClassLoader(const ClassLoader* new_cl) {
835 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700837}
838
Ian Rogersb04f69f2011-10-17 00:40:54 -0700839Method* Class::FindVirtualMethodForInterface(Method* method, bool can_throw) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700840 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700841 DCHECK(declaring_class != NULL) << PrettyClass(this);
842 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700843 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700844 int32_t iftable_count = GetIfTableCount();
845 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
846 for (int32_t i = 0; i < iftable_count; i++) {
847 InterfaceEntry* interface_entry = iftable->Get(i);
848 if (interface_entry->GetInterface() == declaring_class) {
849 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700850 }
851 }
Ian Rogersb04f69f2011-10-17 00:40:54 -0700852 if (can_throw) {
853 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
854 "Class %s does not implement interface %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800855 PrettyDescriptor(this).c_str(), PrettyDescriptor(declaring_class).c_str());
Ian Rogersb04f69f2011-10-17 00:40:54 -0700856 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700857 return NULL;
858}
859
Ian Rogers466bb252011-10-14 03:29:56 -0700860Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700861 // Check the current class before checking the interfaces.
862 Method* method = FindVirtualMethod(name, signature);
863 if (method != NULL) {
864 return method;
865 }
866
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700867 int32_t iftable_count = GetIfTableCount();
868 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
869 for (int32_t i = 0; i < iftable_count; i++) {
870 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700871 if (method != NULL) {
872 return method;
873 }
874 }
875 return NULL;
876}
877
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700878Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700879 const StringPiece& signature) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800880 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700881 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700882 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800883 mh.ChangeMethod(method);
884 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700885 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700886 }
887 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700888 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700889}
890
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700891Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700892 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700893 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700894 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700895 if (method != NULL) {
896 return method;
897 }
898 }
899 return NULL;
900}
901
902Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -0700903 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800904 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700905 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700906 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800907 mh.ChangeMethod(method);
908 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -0700909 return method;
Ian Rogers466bb252011-10-14 03:29:56 -0700910 }
911 }
912 return NULL;
913}
914
915Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
916 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
917 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
918 if (method != NULL) {
919 return method;
920 }
921 }
922 return NULL;
923}
924
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700925Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 // Is the field in this class?
927 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800928 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 for (size_t i = 0; i < NumInstanceFields(); ++i) {
930 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800931 fh.ChangeField(f);
932 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700933 return f;
934 }
935 }
936 return NULL;
937}
938
939Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 // Is the field in this class, or any of its superclasses?
941 // Interfaces are not relevant because they can't contain instance fields.
942 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700943 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 if (f != NULL) {
945 return f;
946 }
947 }
948 return NULL;
949}
950
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700951Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
952 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800953 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700954 for (size_t i = 0; i < NumStaticFields(); ++i) {
955 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800956 fh.ChangeField(f);
957 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700958 return f;
959 }
960 }
961 return NULL;
962}
963
964Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
965 // Is the field in this class (or its interfaces), or any of its
966 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -0800967 ClassHelper kh;
968 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700969 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -0800970 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700971 if (f != NULL) {
972 return f;
973 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700974 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -0800975 kh.ChangeClass(k);
976 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
977 Class* interface = kh.GetInterface(i);
978 f = interface->FindDeclaredStaticField(name, type);
979 if (f != NULL) {
980 return f;
981 }
982 }
983 }
984 return NULL;
985}
986
987Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
988 // Find a field using the JLS field resolution order
989 ClassHelper kh;
990 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
991 // Is the field in this class?
992 Field* f = k->FindDeclaredInstanceField(name, type);
993 if (f != NULL) {
994 return f;
995 }
996 f = k->FindDeclaredStaticField(name, type);
997 if (f != NULL) {
998 return f;
999 }
1000 // Is this field in any of this class' interfaces?
1001 kh.ChangeClass(k);
1002 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1003 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001004 f = interface->FindDeclaredStaticField(name, type);
1005 if (f != NULL) {
1006 return f;
1007 }
1008 }
1009 }
1010 return NULL;
1011}
1012
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001013Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001014 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001015 DCHECK_GE(component_count, 0);
1016 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001017
1018 size_t header_size = sizeof(Array);
1019 size_t data_size = component_count * component_size;
1020 size_t size = header_size + data_size;
1021
1022 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1023 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1024 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1025 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
1026 "%s of length %zd exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001027 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001028 return NULL;
1029 }
1030
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001031 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1032 if (array != NULL) {
1033 DCHECK(array->IsArrayInstance());
1034 array->SetLength(component_count);
1035 }
1036 return array;
1037}
1038
1039Array* Array::Alloc(Class* array_class, int32_t component_count) {
1040 return Alloc(array_class, component_count, array_class->GetComponentSize());
1041}
1042
Elliott Hughes80609252011-09-23 17:24:51 -07001043bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001044 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001045 "length=%i; index=%i", length_, index);
1046 return false;
1047}
1048
1049bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001050 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001051 "Can't store an element of type %s into an array of type %s",
1052 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1053 return false;
1054}
1055
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001056template<typename T>
1057PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001058 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001059 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1060 return down_cast<PrimitiveArray<T>*>(raw_array);
1061}
1062
1063template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1064
1065// Explicitly instantiate all the primitive array types.
1066template class PrimitiveArray<uint8_t>; // BooleanArray
1067template class PrimitiveArray<int8_t>; // ByteArray
1068template class PrimitiveArray<uint16_t>; // CharArray
1069template class PrimitiveArray<double>; // DoubleArray
1070template class PrimitiveArray<float>; // FloatArray
1071template class PrimitiveArray<int32_t>; // IntArray
1072template class PrimitiveArray<int64_t>; // LongArray
1073template class PrimitiveArray<int16_t>; // ShortArray
1074
Ian Rogers466bb252011-10-14 03:29:56 -07001075// Explicitly instantiate Class[][]
1076template class ObjectArray<ObjectArray<Class> >;
1077
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001078// TODO: get global references for these
1079Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001080
Brian Carlstroma663ea52011-08-19 23:33:41 -07001081void String::SetClass(Class* java_lang_String) {
1082 CHECK(java_lang_String_ == NULL);
1083 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001084 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001085}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001086
Brian Carlstroma663ea52011-08-19 23:33:41 -07001087void String::ResetClass() {
1088 CHECK(java_lang_String_ != NULL);
1089 java_lang_String_ = NULL;
1090}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001091
Brian Carlstromc74255f2011-09-11 22:47:39 -07001092String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001093 return Runtime::Current()->GetInternTable()->InternWeak(this);
1094}
1095
Brian Carlstrom395520e2011-09-25 19:35:00 -07001096int32_t String::GetHashCode() {
1097 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1098 if (result == 0) {
1099 ComputeHashCode();
1100 }
1101 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1102 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1103 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001104 return result;
1105}
1106
1107int32_t String::GetLength() const {
1108 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1109 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1110 return result;
1111}
1112
1113uint16_t String::CharAt(int32_t index) const {
1114 // TODO: do we need this? Equals is the only caller, and could
1115 // bounds check itself.
1116 if (index < 0 || index >= count_) {
1117 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001118 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001119 "length=%i; index=%i", count_, index);
1120 return 0;
1121 }
1122 return GetCharArray()->Get(index + GetOffset());
1123}
1124
1125String* String::AllocFromUtf16(int32_t utf16_length,
1126 const uint16_t* utf16_data_in,
1127 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001128 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001129 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001130 if (string == NULL) {
1131 return NULL;
1132 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001133 // TODO: use 16-bit wide memset variant
1134 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001135 if (array == NULL) {
1136 return NULL;
1137 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001138 for (int i = 0; i < utf16_length; i++) {
1139 array->Set(i, utf16_data_in[i]);
1140 }
1141 if (hash_code != 0) {
1142 string->SetHashCode(hash_code);
1143 } else {
1144 string->ComputeHashCode();
1145 }
1146 return string;
1147}
1148
1149String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001150 if (utf == NULL) {
1151 return NULL;
1152 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001153 size_t char_count = CountModifiedUtf8Chars(utf);
1154 return AllocFromModifiedUtf8(char_count, utf);
1155}
1156
1157String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1158 const char* utf8_data_in) {
1159 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001160 if (string == NULL) {
1161 return NULL;
1162 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001163 uint16_t* utf16_data_out =
1164 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1165 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1166 string->ComputeHashCode();
1167 return string;
1168}
1169
1170String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001171 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1172 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001173 return NULL;
1174 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001175 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001176}
1177
1178String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001179 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001180 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001181 if (string == NULL) {
1182 return NULL;
1183 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001184 string->SetArray(array);
1185 string->SetCount(array->GetLength());
1186 return string;
1187}
1188
1189bool String::Equals(const String* that) const {
1190 if (this == that) {
1191 // Quick reference equality test
1192 return true;
1193 } else if (that == NULL) {
1194 // Null isn't an instanceof anything
1195 return false;
1196 } else if (this->GetLength() != that->GetLength()) {
1197 // Quick length inequality test
1198 return false;
1199 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001200 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001201 // hash code was already equal
1202 for (int32_t i = 0; i < that->GetLength(); ++i) {
1203 if (this->CharAt(i) != that->CharAt(i)) {
1204 return false;
1205 }
1206 }
1207 return true;
1208 }
1209}
1210
Elliott Hughes5d78d392011-12-13 16:53:05 -08001211bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001212 if (this->GetLength() != that_length) {
1213 return false;
1214 } else {
1215 for (int32_t i = 0; i < that_length; ++i) {
1216 if (this->CharAt(i) != that_chars[that_offset + i]) {
1217 return false;
1218 }
1219 }
1220 return true;
1221 }
1222}
1223
1224bool String::Equals(const char* modified_utf8) const {
1225 for (int32_t i = 0; i < GetLength(); ++i) {
1226 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1227 if (ch == '\0' || ch != CharAt(i)) {
1228 return false;
1229 }
1230 }
1231 return *modified_utf8 == '\0';
1232}
1233
1234bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001235 if (modified_utf8.size() != GetLength()) {
1236 return false;
1237 }
1238 const char* p = modified_utf8.data();
1239 for (int32_t i = 0; i < GetLength(); ++i) {
1240 uint16_t ch = GetUtf16FromUtf8(&p);
1241 if (ch != CharAt(i)) {
1242 return false;
1243 }
1244 }
1245 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001246}
1247
1248// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1249std::string String::ToModifiedUtf8() const {
1250 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1251 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1252 std::string result(byte_count, char(0));
1253 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1254 return result;
1255}
1256
Ian Rogers466bb252011-10-14 03:29:56 -07001257bool Throwable::IsCheckedException() const {
1258 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1259 if (InstanceOf(error)) {
1260 return false;
1261 }
1262 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1263 return !InstanceOf(jlre);
1264}
1265
Ian Rogers9074b992011-10-26 17:41:55 -07001266std::string Throwable::Dump() const {
1267 Object* stack_state = GetStackState();
1268 if (stack_state == NULL || !stack_state->IsObjectArray()) {
1269 // missing or corrupt stack state
1270 return "";
1271 }
1272 // Decode the internal stack trace into the depth and method trace
1273 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1274 int32_t depth = method_trace->GetLength() - 1;
1275 std::string result;
1276 for (int32_t i = 0; i < depth; ++i) {
1277 Method* method = down_cast<Method*>(method_trace->Get(i));
1278 result += " at ";
1279 result += PrettyMethod(method, true);
1280 result += "\n";
1281 }
1282 return result;
1283}
1284
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001285Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1286
1287void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1288 CHECK(java_lang_StackTraceElement_ == NULL);
1289 CHECK(java_lang_StackTraceElement != NULL);
1290 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1291}
1292
1293void StackTraceElement::ResetClass() {
1294 CHECK(java_lang_StackTraceElement_ != NULL);
1295 java_lang_StackTraceElement_ = NULL;
1296}
1297
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001298StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1299 String* method_name,
1300 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001301 int32_t line_number) {
1302 StackTraceElement* trace =
1303 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1304 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1305 const_cast<String*>(declaring_class), false);
1306 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1307 const_cast<String*>(method_name), false);
1308 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1309 const_cast<String*>(file_name), false);
1310 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1311 line_number, false);
1312 return trace;
1313}
1314
Elliott Hughes1f359b02011-07-17 14:27:17 -07001315static const char* kClassStatusNames[] = {
1316 "Error",
1317 "NotReady",
1318 "Idx",
1319 "Loaded",
1320 "Resolved",
1321 "Verifying",
1322 "Verified",
1323 "Initializing",
1324 "Initialized"
1325};
1326std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1327 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001328 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001329 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001330 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001331 }
1332 return os;
1333}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001334
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001335} // namespace art