blob: e8aeecdaff4f37f598dcc96b5c5c1430d29cd607 [file] [log] [blame]
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "object.h"
4
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07006
Ian Rogersdf20fe02011-07-20 20:34:16 -07007#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07008#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07009#include <string>
10#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070012#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070013#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070014#include "dex_cache.h"
15#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070017#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070018#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070020#include "monitor.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "runtime.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070022#include "stack.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070023
24namespace art {
25
Elliott Hughes081be7f2011-09-18 16:50:26 -070026Object* Object::Clone() {
27 Class* c = GetClass();
28 DCHECK(!c->IsClassClass());
29
30 // Object::SizeOf gets the right size even if we're an array.
31 // Using c->AllocObject() here would be wrong.
32 size_t num_bytes = SizeOf();
33 Object* copy = Heap::AllocObject(c, num_bytes);
34 if (copy == NULL) {
35 return NULL;
36 }
37
38 // Copy instance data. We assume memcpy copies by words.
39 // TODO: expose and use move32.
40 byte* src_bytes = reinterpret_cast<byte*>(this);
41 byte* dst_bytes = reinterpret_cast<byte*>(copy);
42 size_t offset = sizeof(Object);
43 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
44
Elliott Hughes20cde902011-10-04 17:37:27 -070045 if (c->IsFinalizable()) {
Elliott Hughesadb460d2011-10-05 17:02:34 -070046 Heap::AddFinalizerReference(copy);
Elliott Hughes20cde902011-10-04 17:37:27 -070047 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070048
49 return copy;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070050}
51
Elliott Hughes5f791332011-09-15 17:45:30 -070052uint32_t Object::GetLockOwner() {
53 return Monitor::GetLockOwner(monitor_);
54}
55
Elliott Hughes081be7f2011-09-18 16:50:26 -070056bool Object::IsString() const {
57 // TODO use "klass_ == String::GetJavaLangString()" instead?
58 return GetClass() == GetClass()->GetDescriptor()->GetClass();
59}
60
Elliott Hughes5f791332011-09-15 17:45:30 -070061void Object::MonitorEnter(Thread* thread) {
62 Monitor::MonitorEnter(thread, this);
63}
64
Ian Rogersff1ed472011-09-20 13:46:24 -070065bool Object::MonitorExit(Thread* thread) {
66 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070067}
68
69void Object::Notify() {
70 Monitor::Notify(Thread::Current(), this);
71}
72
73void Object::NotifyAll() {
74 Monitor::NotifyAll(Thread::Current(), this);
75}
76
77void Object::Wait(int64_t ms, int32_t ns) {
78 Monitor::Wait(Thread::Current(), this, ms, ns, true);
79}
80
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070081// TODO: get global references for these
82Class* Field::java_lang_reflect_Field_ = NULL;
83
84void Field::SetClass(Class* java_lang_reflect_Field) {
85 CHECK(java_lang_reflect_Field_ == NULL);
86 CHECK(java_lang_reflect_Field != NULL);
87 java_lang_reflect_Field_ = java_lang_reflect_Field;
88}
89
90void Field::ResetClass() {
91 CHECK(java_lang_reflect_Field_ != NULL);
92 java_lang_reflect_Field_ = NULL;
93}
94
95void Field::SetTypeIdx(uint32_t type_idx) {
96 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), type_idx, false);
97}
98
99Class* Field::GetTypeDuringLinking() const {
100 // We are assured that the necessary primitive types are in the dex cache
101 // early during class linking
102 return GetDeclaringClass()->GetDexCache()->GetResolvedType(GetTypeIdx());
103}
104
105Class* Field::GetType() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700106 if (type_ == NULL) {
107 type_ = Runtime::Current()->GetClassLinker()->ResolveType(GetTypeIdx(), this);
108 }
109 return type_;
110}
111
112void Field::InitJavaFields() {
113 Thread* self = Thread::Current();
114 ScopedThreadStateChange tsc(self, Thread::kRunnable);
115 MonitorEnter(self);
116 if (type_ == NULL) {
117 InitJavaFieldsLocked();
118 }
119 MonitorExit(self);
120}
121
122void Field::InitJavaFieldsLocked() {
123 GetType(); // Sets type_ as a side-effect. May throw.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700124}
125
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700126uint32_t Field::Get32(const Object* object) const {
127 CHECK((object == NULL) == IsStatic());
128 if (IsStatic()) {
129 object = declaring_class_;
130 }
131 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700132}
133
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700134void Field::Set32(Object* object, uint32_t new_value) const {
135 CHECK((object == NULL) == IsStatic());
136 if (IsStatic()) {
137 object = declaring_class_;
138 }
139 object->SetField32(GetOffset(), new_value, IsVolatile());
140}
141
142uint64_t Field::Get64(const Object* object) const {
143 CHECK((object == NULL) == IsStatic());
144 if (IsStatic()) {
145 object = declaring_class_;
146 }
147 return object->GetField64(GetOffset(), IsVolatile());
148}
149
150void Field::Set64(Object* object, uint64_t new_value) const {
151 CHECK((object == NULL) == IsStatic());
152 if (IsStatic()) {
153 object = declaring_class_;
154 }
155 object->SetField64(GetOffset(), new_value, IsVolatile());
156}
157
158Object* Field::GetObj(const Object* object) const {
159 CHECK((object == NULL) == IsStatic());
160 if (IsStatic()) {
161 object = declaring_class_;
162 }
163 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
164}
165
166void Field::SetObj(Object* object, const Object* new_value) const {
167 CHECK((object == NULL) == IsStatic());
168 if (IsStatic()) {
169 object = declaring_class_;
170 }
171 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
172}
173
174bool Field::GetBoolean(const Object* object) const {
175 DCHECK(GetType()->IsPrimitiveBoolean());
176 return Get32(object);
177}
178
179void Field::SetBoolean(Object* object, bool z) const {
180 DCHECK(GetType()->IsPrimitiveBoolean());
181 Set32(object, z);
182}
183
184int8_t Field::GetByte(const Object* object) const {
185 DCHECK(GetType()->IsPrimitiveByte());
186 return Get32(object);
187}
188
189void Field::SetByte(Object* object, int8_t b) const {
190 DCHECK(GetType()->IsPrimitiveByte());
191 Set32(object, b);
192}
193
194uint16_t Field::GetChar(const Object* object) const {
195 DCHECK(GetType()->IsPrimitiveChar());
196 return Get32(object);
197}
198
199void Field::SetChar(Object* object, uint16_t c) const {
200 DCHECK(GetType()->IsPrimitiveChar());
201 Set32(object, c);
202}
203
Ian Rogers466bb252011-10-14 03:29:56 -0700204int16_t Field::GetShort(const Object* object) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 DCHECK(GetType()->IsPrimitiveShort());
206 return Get32(object);
207}
208
Ian Rogers466bb252011-10-14 03:29:56 -0700209void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 DCHECK(GetType()->IsPrimitiveShort());
211 Set32(object, s);
212}
213
214int32_t Field::GetInt(const Object* object) const {
215 DCHECK(GetType()->IsPrimitiveInt());
216 return Get32(object);
217}
218
219void Field::SetInt(Object* object, int32_t i) const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700220 DCHECK(GetType()->IsPrimitiveInt()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700221 Set32(object, i);
222}
223
224int64_t Field::GetLong(const Object* object) const {
225 DCHECK(GetType()->IsPrimitiveLong());
226 return Get64(object);
227}
228
229void Field::SetLong(Object* object, int64_t j) const {
230 DCHECK(GetType()->IsPrimitiveLong());
231 Set64(object, j);
232}
233
234float Field::GetFloat(const Object* object) const {
235 DCHECK(GetType()->IsPrimitiveFloat());
236 JValue float_bits;
237 float_bits.i = Get32(object);
238 return float_bits.f;
239}
240
241void Field::SetFloat(Object* object, float f) const {
242 DCHECK(GetType()->IsPrimitiveFloat());
243 JValue float_bits;
244 float_bits.f = f;
245 Set32(object, float_bits.i);
246}
247
248double Field::GetDouble(const Object* object) const {
249 DCHECK(GetType()->IsPrimitiveDouble());
250 JValue double_bits;
251 double_bits.j = Get64(object);
252 return double_bits.d;
253}
254
255void Field::SetDouble(Object* object, double d) const {
256 DCHECK(GetType()->IsPrimitiveDouble());
257 JValue double_bits;
258 double_bits.d = d;
259 Set64(object, double_bits.j);
260}
261
262Object* Field::GetObject(const Object* object) const {
263 CHECK(!GetType()->IsPrimitive());
264 return GetObj(object);
265}
266
267void Field::SetObject(Object* object, const Object* l) const {
268 CHECK(!GetType()->IsPrimitive());
269 SetObj(object, l);
270}
271
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700272bool Method::IsClassInitializer() const {
273 return IsStatic() && GetName()->Equals("<clinit>");
274}
275
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700276// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700277Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700278Class* Method::java_lang_reflect_Method_ = NULL;
279
Elliott Hughes80609252011-09-23 17:24:51 -0700280void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
281 CHECK(java_lang_reflect_Constructor_ == NULL);
282 CHECK(java_lang_reflect_Constructor != NULL);
283 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
284
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 CHECK(java_lang_reflect_Method_ == NULL);
286 CHECK(java_lang_reflect_Method != NULL);
287 java_lang_reflect_Method_ = java_lang_reflect_Method;
288}
289
Elliott Hughes80609252011-09-23 17:24:51 -0700290void Method::ResetClasses() {
291 CHECK(java_lang_reflect_Constructor_ != NULL);
292 java_lang_reflect_Constructor_ = NULL;
293
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 CHECK(java_lang_reflect_Method_ != NULL);
295 java_lang_reflect_Method_ = NULL;
296}
297
Elliott Hughes418d20f2011-09-22 14:00:39 -0700298Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
299 if (*p == '[') {
300 // Something like "[[[Ljava/lang/String;".
301 const char* start = p;
302 while (*p == '[') {
303 ++p;
304 }
305 if (*p == 'L') {
306 while (*p != ';') {
307 ++p;
308 }
309 }
310 ++p; // Either the ';' or the primitive type.
311
Brian Carlstromaded5f72011-10-07 17:15:04 -0700312 std::string descriptor(start, (p - start));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700313 return class_linker->FindClass(descriptor, cl);
314 } else if (*p == 'L') {
315 const char* start = p;
316 while (*p != ';') {
317 ++p;
318 }
319 ++p;
320 StringPiece descriptor(start, (p - start));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700321 return class_linker->FindClass(descriptor.ToString(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700322 } else {
323 return class_linker->FindPrimitiveClass(*p++);
324 }
325}
326
327void Method::InitJavaFieldsLocked() {
328 // Create the array.
329 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
330 size_t arg_count = GetShorty()->GetLength() - 1;
331 Class* array_class = class_linker->FindSystemClass("[Ljava/lang/Class;");
332 ObjectArray<Class>* parameters = ObjectArray<Class>::Alloc(array_class, arg_count);
333 if (parameters == NULL) {
334 return;
335 }
336
337 // Parse the signature, filling the array.
338 const ClassLoader* cl = GetDeclaringClass()->GetClassLoader();
339 std::string signature(GetSignature()->ToModifiedUtf8());
340 const char* p = signature.c_str();
341 DCHECK_EQ(*p, '(');
342 ++p;
343 for (size_t i = 0; i < arg_count; ++i) {
344 Class* c = ExtractNextClassFromSignature(class_linker, cl, p);
345 if (c == NULL) {
346 return;
347 }
348 parameters->Set(i, c);
349 }
350
351 DCHECK_EQ(*p, ')');
352 ++p;
353
354 java_parameter_types_ = parameters;
355 java_return_type_ = ExtractNextClassFromSignature(class_linker, cl, p);
356}
357
358void Method::InitJavaFields() {
359 Thread* self = Thread::Current();
360 ScopedThreadStateChange tsc(self, Thread::kRunnable);
361 MonitorEnter(self);
362 if (java_parameter_types_ == NULL || java_return_type_ == NULL) {
363 InitJavaFieldsLocked();
364 }
365 MonitorExit(self);
366}
367
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700368ObjectArray<String>* Method::GetDexCacheStrings() const {
369 return GetFieldObject<ObjectArray<String>*>(
370 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
371}
372
373void Method::SetReturnTypeIdx(uint32_t new_return_type_idx) {
374 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
375 new_return_type_idx, false);
376}
377
378Class* Method::GetReturnType() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700379 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 // Short-cut
381 Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx());
382 if (result == NULL) {
383 // Do full linkage and set cache value for next call
384 result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this);
385 }
Elliott Hughes14134a12011-09-30 16:55:51 -0700386 CHECK(result != NULL) << PrettyMethod(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 return result;
388}
389
390void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
391 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
392 new_dex_cache_strings, false);
393}
394
395ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
396 return GetFieldObject<ObjectArray<Class>*>(
397 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
398}
399
400void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
401 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
402 new_dex_cache_classes, false);
403}
404
405ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
406 return GetFieldObject<ObjectArray<Method>*>(
407 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
408}
409
410void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
411 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
412 new_dex_cache_methods, false);
413}
414
415ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
416 return GetFieldObject<ObjectArray<Field>*>(
417 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
418}
419
420void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
421 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
422 new_dex_cache_fields, false);
423}
424
425CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
426 return GetFieldPtr<CodeAndDirectMethods*>(
427 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
428 false);
429}
430
431void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
432 SetFieldPtr<CodeAndDirectMethods*>(
433 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
434 new_value, false);
435}
436
437ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
438 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
439 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
440 false);
441}
442
443void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
444 SetFieldObject(
445 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
446 new_value, false);
447
448}
449
450size_t Method::NumArgRegisters(const StringPiece& shorty) {
451 CHECK_LE(1, shorty.length());
452 uint32_t num_registers = 0;
453 for (int i = 1; i < shorty.length(); ++i) {
454 char ch = shorty[i];
455 if (ch == 'D' || ch == 'J') {
456 num_registers += 2;
457 } else {
458 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700459 }
460 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700461 return num_registers;
462}
463
464size_t Method::NumArgArrayBytes() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700465 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700466 size_t num_bytes = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700467 for (int i = 1; i < shorty->GetLength(); ++i) {
468 char ch = shorty->CharAt(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700469 if (ch == 'D' || ch == 'J') {
470 num_bytes += 8;
471 } else if (ch == 'L') {
472 // Argument is a reference or an array. The shorty descriptor
473 // does not distinguish between these types.
474 num_bytes += sizeof(Object*);
475 } else {
476 num_bytes += 4;
477 }
478 }
479 return num_bytes;
480}
481
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700482size_t Method::NumArgs() const {
483 // "1 +" because the first in Args is the receiver.
484 // "- 1" because we don't count the return type.
485 return (IsStatic() ? 0 : 1) + GetShorty()->GetLength() - 1;
486}
487
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700488// The number of reference arguments to this method including implicit this
489// pointer
490size_t Method::NumReferenceArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700491 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700492 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700493 for (int i = 1; i < shorty->GetLength(); i++) {
494 char ch = shorty->CharAt(i);
495 if ((ch == 'L') || (ch == '[')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700496 result++;
497 }
498 }
499 return result;
500}
501
502// The number of long or double arguments
503size_t Method::NumLongOrDoubleArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700504 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700505 size_t result = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700506 for (int i = 1; i < shorty->GetLength(); i++) {
507 char ch = shorty->CharAt(i);
508 if ((ch == 'D') || (ch == 'J')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700509 result++;
510 }
511 }
512 return result;
513}
514
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700515// Is the given method parameter a reference?
516bool Method::IsParamAReference(unsigned int param) const {
517 CHECK_LT(param, NumArgs());
518 if (IsStatic()) {
519 param++; // 0th argument must skip return value at start of the shorty
520 } else if (param == 0) {
521 return true; // this argument
522 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700523 return GetShorty()->CharAt(param) == 'L';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700524}
525
526// Is the given method parameter a long or double?
527bool Method::IsParamALongOrDouble(unsigned int param) const {
528 CHECK_LT(param, NumArgs());
529 if (IsStatic()) {
530 param++; // 0th argument must skip return value at start of the shorty
531 } else if (param == 0) {
532 return false; // this argument
533 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700534 char ch = GetShorty()->CharAt(param);
535 return (ch == 'J' || ch == 'D');
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700536}
537
538static size_t ShortyCharToSize(char x) {
539 switch (x) {
540 case 'V': return 0;
541 case '[': return kPointerSize;
542 case 'L': return kPointerSize;
543 case 'D': return 8;
544 case 'J': return 8;
545 default: return 4;
546 }
547}
548
549size_t Method::ParamSize(unsigned int param) const {
550 CHECK_LT(param, NumArgs());
551 if (IsStatic()) {
552 param++; // 0th argument must skip return value at start of the shorty
553 } else if (param == 0) {
554 return kPointerSize; // this argument
555 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700556 return ShortyCharToSize(GetShorty()->CharAt(param));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700557}
558
559size_t Method::ReturnSize() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700560 return ShortyCharToSize(GetShorty()->CharAt(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700561}
562
Ian Rogers466bb252011-10-14 03:29:56 -0700563Method* Method::FindOverriddenMethod() const {
564 if (IsStatic()) {
565 return NULL;
566 }
567 Class* declaring_class = GetDeclaringClass();
568 Class* super_class = declaring_class->GetSuperClass();
569 uint16_t method_index = GetMethodIndex();
570 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
571 Method* result = NULL;
572 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
573 result = super_class_vtable->Get(method_index);
574 } else {
575 ObjectArray<Class>* interfaces = declaring_class->GetInterfaces();
576 String* name = GetName();
577 String* signature = GetSignature();
578 for (int32_t i = 0; i < interfaces->GetLength() && result == NULL; i++) {
579 Class* interface = interfaces->Get(i);
580 result = interface->FindInterfaceMethod(name, signature);
581 }
582 }
583 DCHECK (result == NULL || HasSameNameAndSignature(result));
584 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700585}
586
Ian Rogersbdb03912011-09-14 00:55:44 -0700587uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700588 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700589 if (mapping_table == NULL) {
Ian Rogers67375ac2011-09-14 00:55:44 -0700590 DCHECK(IsNative());
591 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700592 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700593 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700594 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetCode());
Ian Rogersbdb03912011-09-14 00:55:44 -0700595 uint32_t best_offset = 0;
596 uint32_t best_dex_offset = 0;
597 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700598 uint32_t map_offset = mapping_table[i];
599 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700600 if (map_offset == sought_offset) {
601 best_offset = map_offset;
602 best_dex_offset = map_dex_offset;
603 break;
604 }
605 if (map_offset < sought_offset && map_offset > best_offset) {
606 best_offset = map_offset;
607 best_dex_offset = map_dex_offset;
608 }
609 }
610 return best_dex_offset;
611}
612
613uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700614 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700615 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700616 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700617 return 0; // Special no mapping/pc == 0 case
618 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700619 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700620 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700621 uint32_t map_offset = mapping_table[i];
622 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700623 if (map_dex_offset == dex_pc) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700624 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
625 }
626 }
627 LOG(FATAL) << "Looking up Dex PC not contained in method";
628 return 0;
629}
630
631uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
632 DexCache* dex_cache = GetDeclaringClass()->GetDexCache();
633 const ClassLoader* class_loader = GetDeclaringClass()->GetClassLoader();
634 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
635 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
636 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(GetCodeItemOffset());
637 // Iterate over the catch handlers associated with dex_pc
638 for (DexFile::CatchHandlerIterator iter = dex_file.dexFindCatchHandler(*code_item, dex_pc);
639 !iter.HasNext(); iter.Next()) {
640 uint32_t iter_type_idx = iter.Get().type_idx_;
641 // Catch all case
Elliott Hughes80609252011-09-23 17:24:51 -0700642 if (iter_type_idx == DexFile::kDexNoIndex) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700643 return iter.Get().address_;
644 }
645 // Does this catch exception type apply?
646 Class* iter_exception_type =
647 class_linker->ResolveType(dex_file, iter_type_idx, dex_cache, class_loader);
648 if (iter_exception_type->IsAssignableFrom(exception_type)) {
649 return iter.Get().address_;
650 }
651 }
652 // Handler not found
653 return DexFile::kDexNoIndex;
654}
655
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700656void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
657 // Push a transition back into managed code onto the linked list in thread.
658 CHECK_EQ(Thread::kRunnable, self->GetState());
659 NativeToManagedRecord record;
660 self->PushNativeToManagedRecord(&record);
661
662 // Call the invoke stub associated with the method.
663 // Pass everything as arguments.
664 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700665
666 bool have_executable_code = (GetCode() != NULL);
667#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700668 // Currently we can only compile non-native methods for ARM.
669 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700670#endif
671
672 if (have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700673 bool log = false;
674 if (log) {
675 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
676 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700677 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700678 if (log) {
679 LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
680 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700681 } else {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700682 if (Runtime::Current()->IsStarted()) {
683 LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
684 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700685 if (result != NULL) {
686 result->j = 0;
687 }
688 }
689
690 // Pop transition.
691 self->PopNativeToManagedRecord(record);
692}
693
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700694bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700695 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
696 void* jni_stub = Runtime::Current()->GetJniStubArray()->GetData();
697 return native_method != jni_stub;
698}
699
700void Method::RegisterNative(const void* native_method) {
701 CHECK(IsNative());
702 CHECK(native_method != NULL);
703 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
704 native_method, false);
705}
706
707void Method::UnregisterNative() {
708 CHECK(IsNative());
709 // restore stub to lookup native pointer via dlsym
710 RegisterNative(Runtime::Current()->GetJniStubArray()->GetData());
711}
712
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700713void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700714 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
715 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700716 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700717 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700718}
719
720DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700721 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700722}
723
724void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700725 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700726}
727
Brian Carlstrom1f870082011-08-23 16:02:11 -0700728Object* Class::AllocObject() {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700729 DCHECK(!IsAbstract()) << PrettyClass(this);
730 DCHECK(!IsInterface()) << PrettyClass(this);
731 DCHECK(!IsPrimitive()) << PrettyClass(this);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700732 DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700733 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700734}
735
Elliott Hughes4681c802011-09-25 18:04:37 -0700736void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700737 if ((flags & kDumpClassFullDetail) == 0) {
738 os << PrettyClass(this);
739 if ((flags & kDumpClassClassLoader) != 0) {
740 os << ' ' << GetClassLoader();
741 }
742 if ((flags & kDumpClassInitialized) != 0) {
743 os << ' ' << GetStatus();
744 }
745 os << std::endl;
746 return;
747 }
748
749 Class* super = GetSuperClass();
750 os << "----- " << (IsInterface() ? "interface" : "class") << " "
751 << "'" << GetDescriptor()->ToModifiedUtf8() << "' cl=" << GetClassLoader() << " -----\n",
752 os << " objectSize=" << SizeOf() << " "
753 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
754 os << StringPrintf(" access=0x%04x.%04x\n",
755 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
756 if (super != NULL) {
757 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
758 }
759 if (IsArrayClass()) {
760 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
761 }
762 if (NumInterfaces() > 0) {
763 os << " interfaces (" << NumInterfaces() << "):\n";
764 for (size_t i = 0; i < NumInterfaces(); ++i) {
765 Class* interface = GetInterface(i);
766 const ClassLoader* cl = interface->GetClassLoader();
767 os << StringPrintf(" %2d: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
768 }
769 }
770 os << " vtable (" << NumVirtualMethods() << " entries, "
771 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
772 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700773 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700774 }
775 os << " direct methods (" << NumDirectMethods() << " entries):\n";
776 for (size_t i = 0; i < NumDirectMethods(); ++i) {
777 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
778 }
779 if (NumStaticFields() > 0) {
780 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700781 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700782 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700783 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700784 }
785 } else {
786 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700787 }
788 }
789 if (NumInstanceFields() > 0) {
790 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700791 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700792 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700793 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700794 }
795 } else {
796 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700797 }
798 }
799}
800
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700801void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
802 if (new_reference_offsets != CLASS_WALK_SUPER) {
803 // Sanity check that the number of bits set in the reference offset bitmap
804 // agrees with the number of references
805 Class* cur = this;
806 size_t cnt = 0;
807 while (cur) {
808 cnt += cur->NumReferenceInstanceFieldsDuringLinking();
809 cur = cur->GetSuperClass();
810 }
811 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt);
812 }
813 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
814 new_reference_offsets, false);
815}
816
817void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
818 if (new_reference_offsets != CLASS_WALK_SUPER) {
819 // Sanity check that the number of bits set in the reference offset bitmap
820 // agrees with the number of references
821 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
822 NumReferenceStaticFieldsDuringLinking());
823 }
824 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
825 new_reference_offsets, false);
826}
827
828size_t Class::PrimitiveSize() const {
829 switch (GetPrimitiveType()) {
830 case kPrimBoolean:
831 case kPrimByte:
832 case kPrimChar:
833 case kPrimShort:
834 case kPrimInt:
835 case kPrimFloat:
836 return sizeof(int32_t);
837 case kPrimLong:
838 case kPrimDouble:
839 return sizeof(int64_t);
840 default:
841 LOG(FATAL) << "Primitive type size calculation on invalid type " << this;
842 return 0;
843 }
844}
845
846size_t Class::GetTypeSize(const String* descriptor) {
847 switch (descriptor->CharAt(0)) {
848 case 'B': return 1; // byte
849 case 'C': return 2; // char
850 case 'D': return 8; // double
851 case 'F': return 4; // float
852 case 'I': return 4; // int
853 case 'J': return 8; // long
854 case 'S': return 2; // short
855 case 'Z': return 1; // boolean
856 case 'L': return sizeof(Object*);
857 case '[': return sizeof(Array*);
858 default:
859 LOG(ERROR) << "Unknown type " << descriptor;
860 return 0;
861 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700862}
863
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700864bool Class::Implements(const Class* klass) const {
865 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700866 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700867 // All interfaces implemented directly and by our superclass, and
868 // recursively all super-interfaces of those interfaces, are listed
869 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700870 int32_t iftable_count = GetIfTableCount();
871 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
872 for (int32_t i = 0; i < iftable_count; i++) {
873 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700874 return true;
875 }
876 }
877 return false;
878}
879
880// Determine whether "this" is assignable from "klazz", where both of these
881// are array classes.
882//
883// Consider an array class, e.g. Y[][], where Y is a subclass of X.
884// Y[][] = Y[][] --> true (identity)
885// X[][] = Y[][] --> true (element superclass)
886// Y = Y[][] --> false
887// Y[] = Y[][] --> false
888// Object = Y[][] --> true (everything is an object)
889// Object[] = Y[][] --> true
890// Object[][] = Y[][] --> true
891// Object[][][] = Y[][] --> false (too many []s)
892// Serializable = Y[][] --> true (all arrays are Serializable)
893// Serializable[] = Y[][] --> true
894// Serializable[][] = Y[][] --> false (unless Y is Serializable)
895//
896// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700897// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700898//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700899bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700900 DCHECK(IsArrayClass()) << PrettyClass(this);
901 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700902 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700903}
904
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700905bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700906 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
907 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700908 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700909 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700910 // src's super should be java_lang_Object, since it is an array.
911 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700912 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
913 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700914 return this == java_lang_Object;
915 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700916 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700917}
918
919bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700920 DCHECK(!IsInterface()) << PrettyClass(this);
921 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700922 const Class* current = this;
923 do {
924 if (current == klass) {
925 return true;
926 }
927 current = current->GetSuperClass();
928 } while (current != NULL);
929 return false;
930}
931
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700932bool Class::IsInSamePackage(const String* descriptor_string_1,
933 const String* descriptor_string_2) {
934 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
935 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
936
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700937 size_t i = 0;
938 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
939 ++i;
940 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700941 if (descriptor1.find('/', i) != StringPiece::npos ||
942 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700943 return false;
944 } else {
945 return true;
946 }
947}
948
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700950bool Class::IsInSamePackage(const StringPiece& descriptor1,
951 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700952 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700953 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700954 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
955 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700956 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
957}
958#endif
959
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700960bool Class::IsInSamePackage(const Class* that) const {
961 const Class* klass1 = this;
962 const Class* klass2 = that;
963 if (klass1 == klass2) {
964 return true;
965 }
966 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700967 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700968 return false;
969 }
970 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700971 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700972 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700973 }
jeffhao4a801a42011-09-23 13:53:40 -0700974 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700975 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700976 }
977 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700978 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700979}
980
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700981const ClassLoader* Class::GetClassLoader() const {
982 return GetFieldObject<const ClassLoader*>(
983 OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700984}
985
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700986void Class::SetClassLoader(const ClassLoader* new_cl) {
987 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
988 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_),
989 new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700990}
991
Brian Carlstrom30b94452011-08-25 21:35:26 -0700992Method* Class::FindVirtualMethodForInterface(Method* method) {
993 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700994 DCHECK(declaring_class != NULL) << PrettyClass(this);
995 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700996 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700997 int32_t iftable_count = GetIfTableCount();
998 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
999 for (int32_t i = 0; i < iftable_count; i++) {
1000 InterfaceEntry* interface_entry = iftable->Get(i);
1001 if (interface_entry->GetInterface() == declaring_class) {
1002 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001003 }
1004 }
Elliott Hughesefdbac52011-10-06 15:06:18 -07001005 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
1006 "Class %s does not implement interface %s",
1007 PrettyDescriptor(GetDescriptor()).c_str(),
1008 PrettyDescriptor(declaring_class->GetDescriptor()).c_str());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001009 return NULL;
1010}
1011
Ian Rogers466bb252011-10-14 03:29:56 -07001012Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001013 // Check the current class before checking the interfaces.
1014 Method* method = FindVirtualMethod(name, signature);
1015 if (method != NULL) {
1016 return method;
1017 }
1018
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001019 int32_t iftable_count = GetIfTableCount();
1020 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1021 for (int32_t i = 0; i < iftable_count; i++) {
1022 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001023 if (method != NULL) {
1024 return method;
1025 }
1026 }
1027 return NULL;
1028}
1029
Ian Rogers466bb252011-10-14 03:29:56 -07001030Method* Class::FindInterfaceMethod(String* name, String* signature) const {
1031 // Check the current class before checking the interfaces.
1032 Method* method = FindVirtualMethod(name, signature);
1033 if (method != NULL) {
1034 return method;
1035 }
1036 int32_t iftable_count = GetIfTableCount();
1037 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1038 for (int32_t i = 0; i < iftable_count; i++) {
1039 Class* interface = iftable->Get(i)->GetInterface();
1040 method = interface->FindVirtualMethod(name, signature);
1041 if (method != NULL) {
1042 return method;
1043 }
1044 }
1045 return NULL;
1046}
1047
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001048Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001049 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001050 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001051 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001052 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001053 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001054 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001055 }
1056 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001057 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001058}
1059
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001060Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001061 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001062 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001063 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001064 if (method != NULL) {
1065 return method;
1066 }
1067 }
1068 return NULL;
1069}
1070
1071Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001072 const StringPiece& signature) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001073 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001074 Method* method = GetVirtualMethod(i);
Ian Rogers466bb252011-10-14 03:29:56 -07001075 if (method->GetName()->Equals(name) && method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001076 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001077 }
1078 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001079 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001080}
1081
Ian Rogers466bb252011-10-14 03:29:56 -07001082Method* Class::FindDeclaredVirtualMethod(String* name, String* signature) const {
1083 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1084 Method* method = GetVirtualMethod(i);
1085 if (method->GetName() == name && method->GetSignature() == signature) {
1086 return method;
1087 } else {
1088 LOG(INFO) << "Find (" << name->ToModifiedUtf8() << ", " << signature->ToModifiedUtf8()
1089 << ") != " << PrettyMethod(method);
1090 }
1091 }
1092 return NULL;
1093}
1094
1095Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1096 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1097 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1098 if (method != NULL) {
1099 return method;
1100 }
1101 }
1102 return NULL;
1103}
1104
1105Method* Class::FindVirtualMethod(String* name, String* signature) const {
1106 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Elliott Hughescc5f9a92011-09-28 19:17:29 -07001107 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001108 if (method != NULL) {
1109 return method;
1110 }
1111 }
1112 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001113}
1114
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001115Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 // Is the field in this class?
1117 // Interfaces are not relevant because they can't contain instance fields.
1118 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1119 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001120 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 return f;
1122 }
1123 }
1124 return NULL;
1125}
1126
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001127Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 // Is the field in this class, or any of its superclasses?
1129 // Interfaces are not relevant because they can't contain instance fields.
1130 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001131 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 if (f != NULL) {
1133 return f;
1134 }
1135 }
1136 return NULL;
1137}
1138
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001139Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
1140 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 for (size_t i = 0; i < NumStaticFields(); ++i) {
1142 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001143 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 return f;
1145 }
1146 }
1147 return NULL;
1148}
1149
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001150Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 // Is the field in this class (or its interfaces), or any of its
1152 // superclasses (or their interfaces)?
1153 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1154 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001155 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 if (f != NULL) {
1157 return f;
1158 }
1159
1160 // Is this field in any of this class' interfaces?
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001161 for (int32_t i = 0; i < c->GetIfTableCount(); ++i) {
1162 InterfaceEntry* interface_entry = c->GetIfTable()->Get(i);
1163 Class* interface = interface_entry->GetInterface();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001164 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 if (f != NULL) {
1166 return f;
1167 }
1168 }
1169 }
1170 return NULL;
1171}
1172
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001173Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001174 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001175 DCHECK_GE(component_count, 0);
1176 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001177
1178 size_t header_size = sizeof(Array);
1179 size_t data_size = component_count * component_size;
1180 size_t size = header_size + data_size;
1181
1182 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1183 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1184 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1185 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
1186 "%s of length %zd exceeds the VM limit",
1187 PrettyDescriptor(array_class->GetDescriptor()).c_str(), component_count);
1188 return NULL;
1189 }
1190
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001191 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1192 if (array != NULL) {
1193 DCHECK(array->IsArrayInstance());
1194 array->SetLength(component_count);
1195 }
1196 return array;
1197}
1198
1199Array* Array::Alloc(Class* array_class, int32_t component_count) {
1200 return Alloc(array_class, component_count, array_class->GetComponentSize());
1201}
1202
Elliott Hughes80609252011-09-23 17:24:51 -07001203bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001204 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001205 "length=%i; index=%i", length_, index);
1206 return false;
1207}
1208
1209bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001210 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001211 "Can't store an element of type %s into an array of type %s",
1212 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1213 return false;
1214}
1215
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001216template<typename T>
1217PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001218 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001219 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1220 return down_cast<PrimitiveArray<T>*>(raw_array);
1221}
1222
1223template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1224
1225// Explicitly instantiate all the primitive array types.
1226template class PrimitiveArray<uint8_t>; // BooleanArray
1227template class PrimitiveArray<int8_t>; // ByteArray
1228template class PrimitiveArray<uint16_t>; // CharArray
1229template class PrimitiveArray<double>; // DoubleArray
1230template class PrimitiveArray<float>; // FloatArray
1231template class PrimitiveArray<int32_t>; // IntArray
1232template class PrimitiveArray<int64_t>; // LongArray
1233template class PrimitiveArray<int16_t>; // ShortArray
1234
Ian Rogers466bb252011-10-14 03:29:56 -07001235// Explicitly instantiate Class[][]
1236template class ObjectArray<ObjectArray<Class> >;
1237
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001238// TODO: get global references for these
1239Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001240
Brian Carlstroma663ea52011-08-19 23:33:41 -07001241void String::SetClass(Class* java_lang_String) {
1242 CHECK(java_lang_String_ == NULL);
1243 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001244 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001245}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001246
Brian Carlstroma663ea52011-08-19 23:33:41 -07001247void String::ResetClass() {
1248 CHECK(java_lang_String_ != NULL);
1249 java_lang_String_ = NULL;
1250}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001251
Brian Carlstromc74255f2011-09-11 22:47:39 -07001252String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001253 return Runtime::Current()->GetInternTable()->InternWeak(this);
1254}
1255
Brian Carlstrom395520e2011-09-25 19:35:00 -07001256int32_t String::GetHashCode() {
1257 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1258 if (result == 0) {
1259 ComputeHashCode();
1260 }
1261 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1262 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1263 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001264 return result;
1265}
1266
1267int32_t String::GetLength() const {
1268 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1269 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1270 return result;
1271}
1272
1273uint16_t String::CharAt(int32_t index) const {
1274 // TODO: do we need this? Equals is the only caller, and could
1275 // bounds check itself.
1276 if (index < 0 || index >= count_) {
1277 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001278 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001279 "length=%i; index=%i", count_, index);
1280 return 0;
1281 }
1282 return GetCharArray()->Get(index + GetOffset());
1283}
1284
1285String* String::AllocFromUtf16(int32_t utf16_length,
1286 const uint16_t* utf16_data_in,
1287 int32_t hash_code) {
1288 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001289 if (string == NULL) {
1290 return NULL;
1291 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001292 // TODO: use 16-bit wide memset variant
1293 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001294 if (array == NULL) {
1295 return NULL;
1296 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001297 for (int i = 0; i < utf16_length; i++) {
1298 array->Set(i, utf16_data_in[i]);
1299 }
1300 if (hash_code != 0) {
1301 string->SetHashCode(hash_code);
1302 } else {
1303 string->ComputeHashCode();
1304 }
1305 return string;
1306}
1307
1308String* String::AllocFromModifiedUtf8(const char* utf) {
1309 size_t char_count = CountModifiedUtf8Chars(utf);
1310 return AllocFromModifiedUtf8(char_count, utf);
1311}
1312
1313String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1314 const char* utf8_data_in) {
1315 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001316 if (string == NULL) {
1317 return NULL;
1318 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001319 uint16_t* utf16_data_out =
1320 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1321 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1322 string->ComputeHashCode();
1323 return string;
1324}
1325
1326String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001327 CharArray* array = CharArray::Alloc(utf16_length);
1328 if (array == NULL) {
1329 return NULL;
1330 }
1331 return Alloc(java_lang_String, array);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001332}
1333
1334String* String::Alloc(Class* java_lang_String, CharArray* array) {
1335 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001336 if (string == NULL) {
1337 return NULL;
1338 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001339 string->SetArray(array);
1340 string->SetCount(array->GetLength());
1341 return string;
1342}
1343
1344bool String::Equals(const String* that) const {
1345 if (this == that) {
1346 // Quick reference equality test
1347 return true;
1348 } else if (that == NULL) {
1349 // Null isn't an instanceof anything
1350 return false;
1351 } else if (this->GetLength() != that->GetLength()) {
1352 // Quick length inequality test
1353 return false;
1354 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001355 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001356 // hash code was already equal
1357 for (int32_t i = 0; i < that->GetLength(); ++i) {
1358 if (this->CharAt(i) != that->CharAt(i)) {
1359 return false;
1360 }
1361 }
1362 return true;
1363 }
1364}
1365
1366bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1367 int32_t that_length) const {
1368 if (this->GetLength() != that_length) {
1369 return false;
1370 } else {
1371 for (int32_t i = 0; i < that_length; ++i) {
1372 if (this->CharAt(i) != that_chars[that_offset + i]) {
1373 return false;
1374 }
1375 }
1376 return true;
1377 }
1378}
1379
1380bool String::Equals(const char* modified_utf8) const {
1381 for (int32_t i = 0; i < GetLength(); ++i) {
1382 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1383 if (ch == '\0' || ch != CharAt(i)) {
1384 return false;
1385 }
1386 }
1387 return *modified_utf8 == '\0';
1388}
1389
1390bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001391 if (modified_utf8.size() != GetLength()) {
1392 return false;
1393 }
1394 const char* p = modified_utf8.data();
1395 for (int32_t i = 0; i < GetLength(); ++i) {
1396 uint16_t ch = GetUtf16FromUtf8(&p);
1397 if (ch != CharAt(i)) {
1398 return false;
1399 }
1400 }
1401 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001402}
1403
1404// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1405std::string String::ToModifiedUtf8() const {
1406 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1407 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1408 std::string result(byte_count, char(0));
1409 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1410 return result;
1411}
1412
Ian Rogers466bb252011-10-14 03:29:56 -07001413bool Throwable::IsCheckedException() const {
1414 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1415 if (InstanceOf(error)) {
1416 return false;
1417 }
1418 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1419 return !InstanceOf(jlre);
1420}
1421
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001422Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1423
1424void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1425 CHECK(java_lang_StackTraceElement_ == NULL);
1426 CHECK(java_lang_StackTraceElement != NULL);
1427 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1428}
1429
1430void StackTraceElement::ResetClass() {
1431 CHECK(java_lang_StackTraceElement_ != NULL);
1432 java_lang_StackTraceElement_ = NULL;
1433}
1434
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001435StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1436 const String* method_name,
1437 const String* file_name,
1438 int32_t line_number) {
1439 StackTraceElement* trace =
1440 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1441 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1442 const_cast<String*>(declaring_class), false);
1443 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1444 const_cast<String*>(method_name), false);
1445 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1446 const_cast<String*>(file_name), false);
1447 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1448 line_number, false);
1449 return trace;
1450}
1451
Elliott Hughes1f359b02011-07-17 14:27:17 -07001452static const char* kClassStatusNames[] = {
1453 "Error",
1454 "NotReady",
1455 "Idx",
1456 "Loaded",
1457 "Resolved",
1458 "Verifying",
1459 "Verified",
1460 "Initializing",
1461 "Initialized"
1462};
1463std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1464 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001465 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001466 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001467 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001468 }
1469 return os;
1470}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001471
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001472} // namespace art