blob: 174388a0e345586a7c07d7b7816c06fb161ddfca [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 Carlstrom5de8fe52011-10-16 14:10:09 -0700379 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous())
380 << PrettyMethod(this);
Jesse Wilsond81cdcc2011-10-17 14:36:48 -0400381 Class* java_return_type = java_return_type_;
382 if (java_return_type != NULL) {
383 return java_return_type;
384 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700385 // Short-cut
386 Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx());
387 if (result == NULL) {
388 // Do full linkage and set cache value for next call
389 result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this);
390 }
Elliott Hughes14134a12011-09-30 16:55:51 -0700391 CHECK(result != NULL) << PrettyMethod(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 return result;
393}
394
395void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
396 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
397 new_dex_cache_strings, false);
398}
399
400ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
401 return GetFieldObject<ObjectArray<Class>*>(
402 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
403}
404
405void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
406 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
407 new_dex_cache_classes, false);
408}
409
410ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
411 return GetFieldObject<ObjectArray<Method>*>(
412 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
413}
414
415void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
416 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
417 new_dex_cache_methods, false);
418}
419
420ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
421 return GetFieldObject<ObjectArray<Field>*>(
422 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
423}
424
425void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
426 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
427 new_dex_cache_fields, false);
428}
429
430CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
431 return GetFieldPtr<CodeAndDirectMethods*>(
432 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
433 false);
434}
435
436void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
437 SetFieldPtr<CodeAndDirectMethods*>(
438 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
439 new_value, false);
440}
441
442ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
443 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
444 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
445 false);
446}
447
448void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
449 SetFieldObject(
450 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
451 new_value, false);
452
453}
454
455size_t Method::NumArgRegisters(const StringPiece& shorty) {
456 CHECK_LE(1, shorty.length());
457 uint32_t num_registers = 0;
458 for (int i = 1; i < shorty.length(); ++i) {
459 char ch = shorty[i];
460 if (ch == 'D' || ch == 'J') {
461 num_registers += 2;
462 } else {
463 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700464 }
465 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700466 return num_registers;
467}
468
469size_t Method::NumArgArrayBytes() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700470 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700471 size_t num_bytes = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700472 for (int i = 1; i < shorty->GetLength(); ++i) {
473 char ch = shorty->CharAt(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474 if (ch == 'D' || ch == 'J') {
475 num_bytes += 8;
476 } else if (ch == 'L') {
477 // Argument is a reference or an array. The shorty descriptor
478 // does not distinguish between these types.
479 num_bytes += sizeof(Object*);
480 } else {
481 num_bytes += 4;
482 }
483 }
484 return num_bytes;
485}
486
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700487size_t Method::NumArgs() const {
488 // "1 +" because the first in Args is the receiver.
489 // "- 1" because we don't count the return type.
490 return (IsStatic() ? 0 : 1) + GetShorty()->GetLength() - 1;
491}
492
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700493// The number of reference arguments to this method including implicit this
494// pointer
495size_t Method::NumReferenceArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700496 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700497 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700498 for (int i = 1; i < shorty->GetLength(); i++) {
499 char ch = shorty->CharAt(i);
500 if ((ch == 'L') || (ch == '[')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700501 result++;
502 }
503 }
504 return result;
505}
506
507// The number of long or double arguments
508size_t Method::NumLongOrDoubleArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700509 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700510 size_t result = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700511 for (int i = 1; i < shorty->GetLength(); i++) {
512 char ch = shorty->CharAt(i);
513 if ((ch == 'D') || (ch == 'J')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700514 result++;
515 }
516 }
517 return result;
518}
519
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700520// Is the given method parameter a reference?
521bool Method::IsParamAReference(unsigned int param) const {
522 CHECK_LT(param, NumArgs());
523 if (IsStatic()) {
524 param++; // 0th argument must skip return value at start of the shorty
525 } else if (param == 0) {
526 return true; // this argument
527 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700528 return GetShorty()->CharAt(param) == 'L';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700529}
530
531// Is the given method parameter a long or double?
532bool Method::IsParamALongOrDouble(unsigned int param) const {
533 CHECK_LT(param, NumArgs());
534 if (IsStatic()) {
535 param++; // 0th argument must skip return value at start of the shorty
536 } else if (param == 0) {
537 return false; // this argument
538 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700539 char ch = GetShorty()->CharAt(param);
540 return (ch == 'J' || ch == 'D');
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700541}
542
543static size_t ShortyCharToSize(char x) {
544 switch (x) {
545 case 'V': return 0;
546 case '[': return kPointerSize;
547 case 'L': return kPointerSize;
548 case 'D': return 8;
549 case 'J': return 8;
550 default: return 4;
551 }
552}
553
554size_t Method::ParamSize(unsigned int param) const {
555 CHECK_LT(param, NumArgs());
556 if (IsStatic()) {
557 param++; // 0th argument must skip return value at start of the shorty
558 } else if (param == 0) {
559 return kPointerSize; // this argument
560 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700561 return ShortyCharToSize(GetShorty()->CharAt(param));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700562}
563
564size_t Method::ReturnSize() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700565 return ShortyCharToSize(GetShorty()->CharAt(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700566}
567
Ian Rogers466bb252011-10-14 03:29:56 -0700568Method* Method::FindOverriddenMethod() const {
569 if (IsStatic()) {
570 return NULL;
571 }
572 Class* declaring_class = GetDeclaringClass();
573 Class* super_class = declaring_class->GetSuperClass();
574 uint16_t method_index = GetMethodIndex();
575 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
576 Method* result = NULL;
577 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
578 result = super_class_vtable->Get(method_index);
579 } else {
580 ObjectArray<Class>* interfaces = declaring_class->GetInterfaces();
581 String* name = GetName();
582 String* signature = GetSignature();
583 for (int32_t i = 0; i < interfaces->GetLength() && result == NULL; i++) {
584 Class* interface = interfaces->Get(i);
585 result = interface->FindInterfaceMethod(name, signature);
586 }
587 }
588 DCHECK (result == NULL || HasSameNameAndSignature(result));
589 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700590}
591
Ian Rogersbdb03912011-09-14 00:55:44 -0700592uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700593 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700594 if (mapping_table == NULL) {
Brian Carlstrom26c935a2011-10-16 14:52:35 -0700595 DCHECK(IsNative() || IsCalleeSaveMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700596 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700597 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700598 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700599 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetCode());
Ian Rogersbdb03912011-09-14 00:55:44 -0700600 uint32_t best_offset = 0;
601 uint32_t best_dex_offset = 0;
602 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700603 uint32_t map_offset = mapping_table[i];
604 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700605 if (map_offset == sought_offset) {
606 best_offset = map_offset;
607 best_dex_offset = map_dex_offset;
608 break;
609 }
610 if (map_offset < sought_offset && map_offset > best_offset) {
611 best_offset = map_offset;
612 best_dex_offset = map_dex_offset;
613 }
614 }
615 return best_dex_offset;
616}
617
618uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700619 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700620 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700621 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700622 return 0; // Special no mapping/pc == 0 case
623 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700624 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700625 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700626 uint32_t map_offset = mapping_table[i];
627 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700628 if (map_dex_offset == dex_pc) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700629 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
630 }
631 }
632 LOG(FATAL) << "Looking up Dex PC not contained in method";
633 return 0;
634}
635
636uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
637 DexCache* dex_cache = GetDeclaringClass()->GetDexCache();
638 const ClassLoader* class_loader = GetDeclaringClass()->GetClassLoader();
639 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
640 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
641 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(GetCodeItemOffset());
642 // Iterate over the catch handlers associated with dex_pc
643 for (DexFile::CatchHandlerIterator iter = dex_file.dexFindCatchHandler(*code_item, dex_pc);
644 !iter.HasNext(); iter.Next()) {
645 uint32_t iter_type_idx = iter.Get().type_idx_;
646 // Catch all case
Elliott Hughes80609252011-09-23 17:24:51 -0700647 if (iter_type_idx == DexFile::kDexNoIndex) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700648 return iter.Get().address_;
649 }
650 // Does this catch exception type apply?
651 Class* iter_exception_type =
652 class_linker->ResolveType(dex_file, iter_type_idx, dex_cache, class_loader);
653 if (iter_exception_type->IsAssignableFrom(exception_type)) {
654 return iter.Get().address_;
655 }
656 }
657 // Handler not found
658 return DexFile::kDexNoIndex;
659}
660
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700661void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
662 // Push a transition back into managed code onto the linked list in thread.
663 CHECK_EQ(Thread::kRunnable, self->GetState());
664 NativeToManagedRecord record;
665 self->PushNativeToManagedRecord(&record);
666
667 // Call the invoke stub associated with the method.
668 // Pass everything as arguments.
669 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700670
671 bool have_executable_code = (GetCode() != NULL);
672#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700673 // Currently we can only compile non-native methods for ARM.
674 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700675#endif
676
677 if (have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700678 bool log = false;
679 if (log) {
680 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
681 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700682 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700683 if (log) {
684 LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
685 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700686 } else {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700687 if (Runtime::Current()->IsStarted()) {
688 LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
689 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700690 if (result != NULL) {
691 result->j = 0;
692 }
693 }
694
695 // Pop transition.
696 self->PopNativeToManagedRecord(record);
697}
698
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700699bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700700 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
701 void* jni_stub = Runtime::Current()->GetJniStubArray()->GetData();
702 return native_method != jni_stub;
703}
704
705void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700706 CHECK(IsNative()) << PrettyMethod(this);
707 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700708 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
709 native_method, false);
710}
711
712void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700713 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700714 // restore stub to lookup native pointer via dlsym
715 RegisterNative(Runtime::Current()->GetJniStubArray()->GetData());
716}
717
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700718void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700719 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
720 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700721 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700722 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700723}
724
725DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700726 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700727}
728
729void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700730 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700731}
732
Brian Carlstrom1f870082011-08-23 16:02:11 -0700733Object* Class::AllocObject() {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700734 DCHECK(!IsAbstract()) << PrettyClass(this);
735 DCHECK(!IsInterface()) << PrettyClass(this);
736 DCHECK(!IsPrimitive()) << PrettyClass(this);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700737 DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700738 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700739}
740
Elliott Hughes4681c802011-09-25 18:04:37 -0700741void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700742 if ((flags & kDumpClassFullDetail) == 0) {
743 os << PrettyClass(this);
744 if ((flags & kDumpClassClassLoader) != 0) {
745 os << ' ' << GetClassLoader();
746 }
747 if ((flags & kDumpClassInitialized) != 0) {
748 os << ' ' << GetStatus();
749 }
750 os << std::endl;
751 return;
752 }
753
754 Class* super = GetSuperClass();
755 os << "----- " << (IsInterface() ? "interface" : "class") << " "
756 << "'" << GetDescriptor()->ToModifiedUtf8() << "' cl=" << GetClassLoader() << " -----\n",
757 os << " objectSize=" << SizeOf() << " "
758 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
759 os << StringPrintf(" access=0x%04x.%04x\n",
760 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
761 if (super != NULL) {
762 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
763 }
764 if (IsArrayClass()) {
765 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
766 }
767 if (NumInterfaces() > 0) {
768 os << " interfaces (" << NumInterfaces() << "):\n";
769 for (size_t i = 0; i < NumInterfaces(); ++i) {
770 Class* interface = GetInterface(i);
771 const ClassLoader* cl = interface->GetClassLoader();
772 os << StringPrintf(" %2d: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
773 }
774 }
775 os << " vtable (" << NumVirtualMethods() << " entries, "
776 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
777 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700778 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700779 }
780 os << " direct methods (" << NumDirectMethods() << " entries):\n";
781 for (size_t i = 0; i < NumDirectMethods(); ++i) {
782 os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
783 }
784 if (NumStaticFields() > 0) {
785 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700786 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700787 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700788 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700789 }
790 } else {
791 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700792 }
793 }
794 if (NumInstanceFields() > 0) {
795 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700796 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700797 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughes03f03492011-09-26 13:38:08 -0700798 os << StringPrintf(" %2d: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700799 }
800 } else {
801 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700802 }
803 }
804}
805
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700806void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
807 if (new_reference_offsets != CLASS_WALK_SUPER) {
808 // Sanity check that the number of bits set in the reference offset bitmap
809 // agrees with the number of references
810 Class* cur = this;
811 size_t cnt = 0;
812 while (cur) {
813 cnt += cur->NumReferenceInstanceFieldsDuringLinking();
814 cur = cur->GetSuperClass();
815 }
816 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt);
817 }
818 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
819 new_reference_offsets, false);
820}
821
822void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
823 if (new_reference_offsets != CLASS_WALK_SUPER) {
824 // Sanity check that the number of bits set in the reference offset bitmap
825 // agrees with the number of references
826 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
827 NumReferenceStaticFieldsDuringLinking());
828 }
829 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
830 new_reference_offsets, false);
831}
832
833size_t Class::PrimitiveSize() const {
834 switch (GetPrimitiveType()) {
835 case kPrimBoolean:
836 case kPrimByte:
837 case kPrimChar:
838 case kPrimShort:
839 case kPrimInt:
840 case kPrimFloat:
841 return sizeof(int32_t);
842 case kPrimLong:
843 case kPrimDouble:
844 return sizeof(int64_t);
845 default:
846 LOG(FATAL) << "Primitive type size calculation on invalid type " << this;
847 return 0;
848 }
849}
850
851size_t Class::GetTypeSize(const String* descriptor) {
852 switch (descriptor->CharAt(0)) {
853 case 'B': return 1; // byte
854 case 'C': return 2; // char
855 case 'D': return 8; // double
856 case 'F': return 4; // float
857 case 'I': return 4; // int
858 case 'J': return 8; // long
859 case 'S': return 2; // short
860 case 'Z': return 1; // boolean
861 case 'L': return sizeof(Object*);
862 case '[': return sizeof(Array*);
863 default:
864 LOG(ERROR) << "Unknown type " << descriptor;
865 return 0;
866 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700867}
868
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700869bool Class::Implements(const Class* klass) const {
870 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700871 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700872 // All interfaces implemented directly and by our superclass, and
873 // recursively all super-interfaces of those interfaces, are listed
874 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700875 int32_t iftable_count = GetIfTableCount();
876 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
877 for (int32_t i = 0; i < iftable_count; i++) {
878 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700879 return true;
880 }
881 }
882 return false;
883}
884
885// Determine whether "this" is assignable from "klazz", where both of these
886// are array classes.
887//
888// Consider an array class, e.g. Y[][], where Y is a subclass of X.
889// Y[][] = Y[][] --> true (identity)
890// X[][] = Y[][] --> true (element superclass)
891// Y = Y[][] --> false
892// Y[] = Y[][] --> false
893// Object = Y[][] --> true (everything is an object)
894// Object[] = Y[][] --> true
895// Object[][] = Y[][] --> true
896// Object[][][] = Y[][] --> false (too many []s)
897// Serializable = Y[][] --> true (all arrays are Serializable)
898// Serializable[] = Y[][] --> true
899// Serializable[][] = Y[][] --> false (unless Y is Serializable)
900//
901// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700902// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700903//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700904bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700905 DCHECK(IsArrayClass()) << PrettyClass(this);
906 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700907 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700908}
909
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700910bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700911 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
912 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700913 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700914 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700915 // src's super should be java_lang_Object, since it is an array.
916 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700917 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
918 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700919 return this == java_lang_Object;
920 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700921 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700922}
923
924bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700925 DCHECK(!IsInterface()) << PrettyClass(this);
926 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700927 const Class* current = this;
928 do {
929 if (current == klass) {
930 return true;
931 }
932 current = current->GetSuperClass();
933 } while (current != NULL);
934 return false;
935}
936
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700937bool Class::IsInSamePackage(const String* descriptor_string_1,
938 const String* descriptor_string_2) {
939 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
940 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
941
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700942 size_t i = 0;
943 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
944 ++i;
945 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700946 if (descriptor1.find('/', i) != StringPiece::npos ||
947 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700948 return false;
949 } else {
950 return true;
951 }
952}
953
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700954#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700955bool Class::IsInSamePackage(const StringPiece& descriptor1,
956 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700957 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700958 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700959 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
960 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700961 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
962}
963#endif
964
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700965bool Class::IsInSamePackage(const Class* that) const {
966 const Class* klass1 = this;
967 const Class* klass2 = that;
968 if (klass1 == klass2) {
969 return true;
970 }
971 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700972 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700973 return false;
974 }
975 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700976 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700977 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700978 }
jeffhao4a801a42011-09-23 13:53:40 -0700979 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700980 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700981 }
982 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700983 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700984}
985
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700986const ClassLoader* Class::GetClassLoader() const {
987 return GetFieldObject<const ClassLoader*>(
988 OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700989}
990
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700991void Class::SetClassLoader(const ClassLoader* new_cl) {
992 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
993 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_),
994 new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700995}
996
Ian Rogersb04f69f2011-10-17 00:40:54 -0700997Method* Class::FindVirtualMethodForInterface(Method* method, bool can_throw) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700998 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700999 DCHECK(declaring_class != NULL) << PrettyClass(this);
1000 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001001 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001002 int32_t iftable_count = GetIfTableCount();
1003 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1004 for (int32_t i = 0; i < iftable_count; i++) {
1005 InterfaceEntry* interface_entry = iftable->Get(i);
1006 if (interface_entry->GetInterface() == declaring_class) {
1007 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001008 }
1009 }
Ian Rogersb04f69f2011-10-17 00:40:54 -07001010 if (can_throw) {
1011 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
1012 "Class %s does not implement interface %s",
1013 PrettyDescriptor(GetDescriptor()).c_str(),
1014 PrettyDescriptor(declaring_class->GetDescriptor()).c_str());
1015 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001016 return NULL;
1017}
1018
Ian Rogers466bb252011-10-14 03:29:56 -07001019Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001020 // Check the current class before checking the interfaces.
1021 Method* method = FindVirtualMethod(name, signature);
1022 if (method != NULL) {
1023 return method;
1024 }
1025
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001026 int32_t iftable_count = GetIfTableCount();
1027 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1028 for (int32_t i = 0; i < iftable_count; i++) {
1029 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001030 if (method != NULL) {
1031 return method;
1032 }
1033 }
1034 return NULL;
1035}
1036
Ian Rogers466bb252011-10-14 03:29:56 -07001037Method* Class::FindInterfaceMethod(String* name, String* signature) const {
1038 // Check the current class before checking the interfaces.
1039 Method* method = FindVirtualMethod(name, signature);
1040 if (method != NULL) {
1041 return method;
1042 }
1043 int32_t iftable_count = GetIfTableCount();
1044 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1045 for (int32_t i = 0; i < iftable_count; i++) {
1046 Class* interface = iftable->Get(i)->GetInterface();
1047 method = interface->FindVirtualMethod(name, signature);
1048 if (method != NULL) {
1049 return method;
1050 }
1051 }
1052 return NULL;
1053}
1054
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001055Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001056 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001057 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001058 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001059 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001060 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001061 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001062 }
1063 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001064 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001065}
1066
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001067Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001068 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001069 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001070 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001071 if (method != NULL) {
1072 return method;
1073 }
1074 }
1075 return NULL;
1076}
1077
1078Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001079 const StringPiece& signature) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001080 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001081 Method* method = GetVirtualMethod(i);
Ian Rogers466bb252011-10-14 03:29:56 -07001082 if (method->GetName()->Equals(name) && method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001083 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001084 }
1085 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001086 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001087}
1088
Ian Rogers466bb252011-10-14 03:29:56 -07001089Method* Class::FindDeclaredVirtualMethod(String* name, String* signature) const {
1090 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1091 Method* method = GetVirtualMethod(i);
1092 if (method->GetName() == name && method->GetSignature() == signature) {
1093 return method;
1094 } else {
1095 LOG(INFO) << "Find (" << name->ToModifiedUtf8() << ", " << signature->ToModifiedUtf8()
1096 << ") != " << PrettyMethod(method);
1097 }
1098 }
1099 return NULL;
1100}
1101
1102Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1103 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1104 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1105 if (method != NULL) {
1106 return method;
1107 }
1108 }
1109 return NULL;
1110}
1111
1112Method* Class::FindVirtualMethod(String* name, String* signature) const {
1113 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Elliott Hughescc5f9a92011-09-28 19:17:29 -07001114 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001115 if (method != NULL) {
1116 return method;
1117 }
1118 }
1119 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001120}
1121
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001122Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 // Is the field in this class?
1124 // Interfaces are not relevant because they can't contain instance fields.
1125 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1126 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001127 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 return f;
1129 }
1130 }
1131 return NULL;
1132}
1133
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001134Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 // Is the field in this class, or any of its superclasses?
1136 // Interfaces are not relevant because they can't contain instance fields.
1137 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001138 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 if (f != NULL) {
1140 return f;
1141 }
1142 }
1143 return NULL;
1144}
1145
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001146Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
1147 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 for (size_t i = 0; i < NumStaticFields(); ++i) {
1149 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001150 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 return f;
1152 }
1153 }
1154 return NULL;
1155}
1156
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001157Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 // Is the field in this class (or its interfaces), or any of its
1159 // superclasses (or their interfaces)?
1160 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1161 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001162 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 if (f != NULL) {
1164 return f;
1165 }
1166
1167 // Is this field in any of this class' interfaces?
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001168 for (int32_t i = 0; i < c->GetIfTableCount(); ++i) {
1169 InterfaceEntry* interface_entry = c->GetIfTable()->Get(i);
1170 Class* interface = interface_entry->GetInterface();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001171 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 if (f != NULL) {
1173 return f;
1174 }
1175 }
1176 }
1177 return NULL;
1178}
1179
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001180Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001181 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001182 DCHECK_GE(component_count, 0);
1183 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001184
1185 size_t header_size = sizeof(Array);
1186 size_t data_size = component_count * component_size;
1187 size_t size = header_size + data_size;
1188
1189 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1190 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1191 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1192 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
1193 "%s of length %zd exceeds the VM limit",
1194 PrettyDescriptor(array_class->GetDescriptor()).c_str(), component_count);
1195 return NULL;
1196 }
1197
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001198 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1199 if (array != NULL) {
1200 DCHECK(array->IsArrayInstance());
1201 array->SetLength(component_count);
1202 }
1203 return array;
1204}
1205
1206Array* Array::Alloc(Class* array_class, int32_t component_count) {
1207 return Alloc(array_class, component_count, array_class->GetComponentSize());
1208}
1209
Elliott Hughes80609252011-09-23 17:24:51 -07001210bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001211 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001212 "length=%i; index=%i", length_, index);
1213 return false;
1214}
1215
1216bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001217 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001218 "Can't store an element of type %s into an array of type %s",
1219 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1220 return false;
1221}
1222
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001223template<typename T>
1224PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001225 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001226 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1227 return down_cast<PrimitiveArray<T>*>(raw_array);
1228}
1229
1230template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1231
1232// Explicitly instantiate all the primitive array types.
1233template class PrimitiveArray<uint8_t>; // BooleanArray
1234template class PrimitiveArray<int8_t>; // ByteArray
1235template class PrimitiveArray<uint16_t>; // CharArray
1236template class PrimitiveArray<double>; // DoubleArray
1237template class PrimitiveArray<float>; // FloatArray
1238template class PrimitiveArray<int32_t>; // IntArray
1239template class PrimitiveArray<int64_t>; // LongArray
1240template class PrimitiveArray<int16_t>; // ShortArray
1241
Ian Rogers466bb252011-10-14 03:29:56 -07001242// Explicitly instantiate Class[][]
1243template class ObjectArray<ObjectArray<Class> >;
1244
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001245// TODO: get global references for these
1246Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001247
Brian Carlstroma663ea52011-08-19 23:33:41 -07001248void String::SetClass(Class* java_lang_String) {
1249 CHECK(java_lang_String_ == NULL);
1250 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001251 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001252}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001253
Brian Carlstroma663ea52011-08-19 23:33:41 -07001254void String::ResetClass() {
1255 CHECK(java_lang_String_ != NULL);
1256 java_lang_String_ = NULL;
1257}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001258
Brian Carlstromc74255f2011-09-11 22:47:39 -07001259String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001260 return Runtime::Current()->GetInternTable()->InternWeak(this);
1261}
1262
Brian Carlstrom395520e2011-09-25 19:35:00 -07001263int32_t String::GetHashCode() {
1264 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1265 if (result == 0) {
1266 ComputeHashCode();
1267 }
1268 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1269 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1270 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001271 return result;
1272}
1273
1274int32_t String::GetLength() const {
1275 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1276 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1277 return result;
1278}
1279
1280uint16_t String::CharAt(int32_t index) const {
1281 // TODO: do we need this? Equals is the only caller, and could
1282 // bounds check itself.
1283 if (index < 0 || index >= count_) {
1284 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001285 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001286 "length=%i; index=%i", count_, index);
1287 return 0;
1288 }
1289 return GetCharArray()->Get(index + GetOffset());
1290}
1291
1292String* String::AllocFromUtf16(int32_t utf16_length,
1293 const uint16_t* utf16_data_in,
1294 int32_t hash_code) {
1295 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001296 if (string == NULL) {
1297 return NULL;
1298 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001299 // TODO: use 16-bit wide memset variant
1300 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001301 if (array == NULL) {
1302 return NULL;
1303 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001304 for (int i = 0; i < utf16_length; i++) {
1305 array->Set(i, utf16_data_in[i]);
1306 }
1307 if (hash_code != 0) {
1308 string->SetHashCode(hash_code);
1309 } else {
1310 string->ComputeHashCode();
1311 }
1312 return string;
1313}
1314
1315String* String::AllocFromModifiedUtf8(const char* utf) {
1316 size_t char_count = CountModifiedUtf8Chars(utf);
1317 return AllocFromModifiedUtf8(char_count, utf);
1318}
1319
1320String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1321 const char* utf8_data_in) {
1322 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001323 if (string == NULL) {
1324 return NULL;
1325 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001326 uint16_t* utf16_data_out =
1327 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1328 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1329 string->ComputeHashCode();
1330 return string;
1331}
1332
1333String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001334 CharArray* array = CharArray::Alloc(utf16_length);
1335 if (array == NULL) {
1336 return NULL;
1337 }
1338 return Alloc(java_lang_String, array);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001339}
1340
1341String* String::Alloc(Class* java_lang_String, CharArray* array) {
1342 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001343 if (string == NULL) {
1344 return NULL;
1345 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001346 string->SetArray(array);
1347 string->SetCount(array->GetLength());
1348 return string;
1349}
1350
1351bool String::Equals(const String* that) const {
1352 if (this == that) {
1353 // Quick reference equality test
1354 return true;
1355 } else if (that == NULL) {
1356 // Null isn't an instanceof anything
1357 return false;
1358 } else if (this->GetLength() != that->GetLength()) {
1359 // Quick length inequality test
1360 return false;
1361 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001362 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363 // hash code was already equal
1364 for (int32_t i = 0; i < that->GetLength(); ++i) {
1365 if (this->CharAt(i) != that->CharAt(i)) {
1366 return false;
1367 }
1368 }
1369 return true;
1370 }
1371}
1372
1373bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1374 int32_t that_length) const {
1375 if (this->GetLength() != that_length) {
1376 return false;
1377 } else {
1378 for (int32_t i = 0; i < that_length; ++i) {
1379 if (this->CharAt(i) != that_chars[that_offset + i]) {
1380 return false;
1381 }
1382 }
1383 return true;
1384 }
1385}
1386
1387bool String::Equals(const char* modified_utf8) const {
1388 for (int32_t i = 0; i < GetLength(); ++i) {
1389 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1390 if (ch == '\0' || ch != CharAt(i)) {
1391 return false;
1392 }
1393 }
1394 return *modified_utf8 == '\0';
1395}
1396
1397bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001398 if (modified_utf8.size() != GetLength()) {
1399 return false;
1400 }
1401 const char* p = modified_utf8.data();
1402 for (int32_t i = 0; i < GetLength(); ++i) {
1403 uint16_t ch = GetUtf16FromUtf8(&p);
1404 if (ch != CharAt(i)) {
1405 return false;
1406 }
1407 }
1408 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001409}
1410
1411// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1412std::string String::ToModifiedUtf8() const {
1413 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1414 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1415 std::string result(byte_count, char(0));
1416 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1417 return result;
1418}
1419
Ian Rogers466bb252011-10-14 03:29:56 -07001420bool Throwable::IsCheckedException() const {
1421 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1422 if (InstanceOf(error)) {
1423 return false;
1424 }
1425 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1426 return !InstanceOf(jlre);
1427}
1428
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001429Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1430
1431void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1432 CHECK(java_lang_StackTraceElement_ == NULL);
1433 CHECK(java_lang_StackTraceElement != NULL);
1434 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1435}
1436
1437void StackTraceElement::ResetClass() {
1438 CHECK(java_lang_StackTraceElement_ != NULL);
1439 java_lang_StackTraceElement_ = NULL;
1440}
1441
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001442StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1443 const String* method_name,
1444 const String* file_name,
1445 int32_t line_number) {
1446 StackTraceElement* trace =
1447 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1448 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1449 const_cast<String*>(declaring_class), false);
1450 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1451 const_cast<String*>(method_name), false);
1452 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1453 const_cast<String*>(file_name), false);
1454 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1455 line_number, false);
1456 return trace;
1457}
1458
Elliott Hughes1f359b02011-07-17 14:27:17 -07001459static const char* kClassStatusNames[] = {
1460 "Error",
1461 "NotReady",
1462 "Idx",
1463 "Loaded",
1464 "Resolved",
1465 "Verifying",
1466 "Verified",
1467 "Initializing",
1468 "Initialized"
1469};
1470std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1471 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001472 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001473 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001474 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001475 }
1476 return os;
1477}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001478
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001479} // namespace art