blob: edc5bddf6c9d19de1e8ae7a464a2b17432ad5223 [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>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07008#include <string>
9#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070011#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070012#include "class_loader.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070014#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070015#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "logging.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070017#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "dex_file.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070019#include "runtime.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070020
21namespace art {
22
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023bool Object::IsString() const {
24 // TODO use "klass_ == String::GetJavaLangString()" instead?
25 return GetClass() == GetClass()->GetDescriptor()->GetClass();
26}
27
28// TODO: get global references for these
29Class* Field::java_lang_reflect_Field_ = NULL;
30
31void Field::SetClass(Class* java_lang_reflect_Field) {
32 CHECK(java_lang_reflect_Field_ == NULL);
33 CHECK(java_lang_reflect_Field != NULL);
34 java_lang_reflect_Field_ = java_lang_reflect_Field;
35}
36
37void Field::ResetClass() {
38 CHECK(java_lang_reflect_Field_ != NULL);
39 java_lang_reflect_Field_ = NULL;
40}
41
42void Field::SetTypeIdx(uint32_t type_idx) {
43 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), type_idx, false);
44}
45
46Class* Field::GetTypeDuringLinking() const {
47 // We are assured that the necessary primitive types are in the dex cache
48 // early during class linking
49 return GetDeclaringClass()->GetDexCache()->GetResolvedType(GetTypeIdx());
50}
51
52Class* Field::GetType() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -070053 DCHECK(Runtime::Current()->IsStarted())
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070054 << "Can't call GetType without an initialized runtime";
55 // Do full linkage (which sets dex cache value to speed next call)
56 return Runtime::Current()->GetClassLinker()->ResolveType(GetTypeIdx(), this);
57}
58
Elliott Hughesf5ecf062011-09-06 17:37:59 -070059Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer) {
60 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 Field* f = class_linker->ResolveField(field_idx, referrer);
62 if (f != NULL) {
63 Class* c = f->GetDeclaringClass();
64 // If the class is already initializing, we must be inside <clinit>, or
65 // we'd still be waiting for the lock.
66 if (c->GetStatus() == Class::kStatusInitializing || class_linker->EnsureInitialized(c)) {
67 return f;
68 }
Brian Carlstromb63ec392011-08-27 17:38:27 -070069 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -070070 UNIMPLEMENTED(FATAL) << "throw an error and unwind";
71 return NULL;
72}
73
74uint32_t Field::Get32StaticFromCode(uint32_t field_idx, const Method* referrer) {
75 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070076 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t));
77 return field->Get32(NULL);
78}
79void Field::Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070080 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070081 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t));
82 field->Set32(NULL, new_value);
83}
84uint64_t Field::Get64StaticFromCode(uint32_t field_idx, const Method* referrer) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070085 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070086 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t));
87 return field->Get64(NULL);
88}
89void Field::Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070090 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070091 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t));
92 field->Set64(NULL, new_value);
93}
94Object* Field::GetObjStaticFromCode(uint32_t field_idx, const Method* referrer) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070095 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070096 DCHECK(!field->GetType()->IsPrimitive());
97 return field->GetObj(NULL);
98}
99void Field::SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700100 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700101 DCHECK(!field->GetType()->IsPrimitive());
102 field->SetObj(NULL, new_value);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700103}
104
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105uint32_t Field::Get32(const Object* object) const {
106 CHECK((object == NULL) == IsStatic());
107 if (IsStatic()) {
108 object = declaring_class_;
109 }
110 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700111}
112
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700113void Field::Set32(Object* object, uint32_t new_value) const {
114 CHECK((object == NULL) == IsStatic());
115 if (IsStatic()) {
116 object = declaring_class_;
117 }
118 object->SetField32(GetOffset(), new_value, IsVolatile());
119}
120
121uint64_t Field::Get64(const Object* object) const {
122 CHECK((object == NULL) == IsStatic());
123 if (IsStatic()) {
124 object = declaring_class_;
125 }
126 return object->GetField64(GetOffset(), IsVolatile());
127}
128
129void Field::Set64(Object* object, uint64_t new_value) const {
130 CHECK((object == NULL) == IsStatic());
131 if (IsStatic()) {
132 object = declaring_class_;
133 }
134 object->SetField64(GetOffset(), new_value, IsVolatile());
135}
136
137Object* Field::GetObj(const Object* object) const {
138 CHECK((object == NULL) == IsStatic());
139 if (IsStatic()) {
140 object = declaring_class_;
141 }
142 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
143}
144
145void Field::SetObj(Object* object, const Object* new_value) const {
146 CHECK((object == NULL) == IsStatic());
147 if (IsStatic()) {
148 object = declaring_class_;
149 }
150 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
151}
152
153bool Field::GetBoolean(const Object* object) const {
154 DCHECK(GetType()->IsPrimitiveBoolean());
155 return Get32(object);
156}
157
158void Field::SetBoolean(Object* object, bool z) const {
159 DCHECK(GetType()->IsPrimitiveBoolean());
160 Set32(object, z);
161}
162
163int8_t Field::GetByte(const Object* object) const {
164 DCHECK(GetType()->IsPrimitiveByte());
165 return Get32(object);
166}
167
168void Field::SetByte(Object* object, int8_t b) const {
169 DCHECK(GetType()->IsPrimitiveByte());
170 Set32(object, b);
171}
172
173uint16_t Field::GetChar(const Object* object) const {
174 DCHECK(GetType()->IsPrimitiveChar());
175 return Get32(object);
176}
177
178void Field::SetChar(Object* object, uint16_t c) const {
179 DCHECK(GetType()->IsPrimitiveChar());
180 Set32(object, c);
181}
182
183uint16_t Field::GetShort(const Object* object) const {
184 DCHECK(GetType()->IsPrimitiveShort());
185 return Get32(object);
186}
187
188void Field::SetShort(Object* object, uint16_t s) const {
189 DCHECK(GetType()->IsPrimitiveShort());
190 Set32(object, s);
191}
192
193int32_t Field::GetInt(const Object* object) const {
194 DCHECK(GetType()->IsPrimitiveInt());
195 return Get32(object);
196}
197
198void Field::SetInt(Object* object, int32_t i) const {
199 DCHECK(GetType()->IsPrimitiveInt());
200 Set32(object, i);
201}
202
203int64_t Field::GetLong(const Object* object) const {
204 DCHECK(GetType()->IsPrimitiveLong());
205 return Get64(object);
206}
207
208void Field::SetLong(Object* object, int64_t j) const {
209 DCHECK(GetType()->IsPrimitiveLong());
210 Set64(object, j);
211}
212
213float Field::GetFloat(const Object* object) const {
214 DCHECK(GetType()->IsPrimitiveFloat());
215 JValue float_bits;
216 float_bits.i = Get32(object);
217 return float_bits.f;
218}
219
220void Field::SetFloat(Object* object, float f) const {
221 DCHECK(GetType()->IsPrimitiveFloat());
222 JValue float_bits;
223 float_bits.f = f;
224 Set32(object, float_bits.i);
225}
226
227double Field::GetDouble(const Object* object) const {
228 DCHECK(GetType()->IsPrimitiveDouble());
229 JValue double_bits;
230 double_bits.j = Get64(object);
231 return double_bits.d;
232}
233
234void Field::SetDouble(Object* object, double d) const {
235 DCHECK(GetType()->IsPrimitiveDouble());
236 JValue double_bits;
237 double_bits.d = d;
238 Set64(object, double_bits.j);
239}
240
241Object* Field::GetObject(const Object* object) const {
242 CHECK(!GetType()->IsPrimitive());
243 return GetObj(object);
244}
245
246void Field::SetObject(Object* object, const Object* l) const {
247 CHECK(!GetType()->IsPrimitive());
248 SetObj(object, l);
249}
250
251// TODO: get global references for these
252Class* Method::java_lang_reflect_Method_ = NULL;
253
254void Method::SetClass(Class* java_lang_reflect_Method) {
255 CHECK(java_lang_reflect_Method_ == NULL);
256 CHECK(java_lang_reflect_Method != NULL);
257 java_lang_reflect_Method_ = java_lang_reflect_Method;
258}
259
260void Method::ResetClass() {
261 CHECK(java_lang_reflect_Method_ != NULL);
262 java_lang_reflect_Method_ = NULL;
263}
264
265ObjectArray<String>* Method::GetDexCacheStrings() const {
266 return GetFieldObject<ObjectArray<String>*>(
267 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
268}
269
270void Method::SetReturnTypeIdx(uint32_t new_return_type_idx) {
271 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
272 new_return_type_idx, false);
273}
274
275Class* Method::GetReturnType() const {
276 DCHECK(GetDeclaringClass()->IsLinked());
277 // Short-cut
278 Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx());
279 if (result == NULL) {
280 // Do full linkage and set cache value for next call
281 result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this);
282 }
283 CHECK(result != NULL);
284 return result;
285}
286
287void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
288 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
289 new_dex_cache_strings, false);
290}
291
292ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
293 return GetFieldObject<ObjectArray<Class>*>(
294 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
295}
296
297void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
298 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
299 new_dex_cache_classes, false);
300}
301
302ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
303 return GetFieldObject<ObjectArray<Method>*>(
304 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
305}
306
307void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
308 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
309 new_dex_cache_methods, false);
310}
311
312ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
313 return GetFieldObject<ObjectArray<Field>*>(
314 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
315}
316
317void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
318 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
319 new_dex_cache_fields, false);
320}
321
322CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
323 return GetFieldPtr<CodeAndDirectMethods*>(
324 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
325 false);
326}
327
328void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
329 SetFieldPtr<CodeAndDirectMethods*>(
330 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
331 new_value, false);
332}
333
334ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
335 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
336 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
337 false);
338}
339
340void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
341 SetFieldObject(
342 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
343 new_value, false);
344
345}
346
347size_t Method::NumArgRegisters(const StringPiece& shorty) {
348 CHECK_LE(1, shorty.length());
349 uint32_t num_registers = 0;
350 for (int i = 1; i < shorty.length(); ++i) {
351 char ch = shorty[i];
352 if (ch == 'D' || ch == 'J') {
353 num_registers += 2;
354 } else {
355 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700356 }
357 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358 return num_registers;
359}
360
361size_t Method::NumArgArrayBytes() const {
362 const StringPiece& shorty = GetShorty();
363 size_t num_bytes = 0;
364 for (int i = 1; i < shorty.length(); ++i) {
365 char ch = shorty[i];
366 if (ch == 'D' || ch == 'J') {
367 num_bytes += 8;
368 } else if (ch == 'L') {
369 // Argument is a reference or an array. The shorty descriptor
370 // does not distinguish between these types.
371 num_bytes += sizeof(Object*);
372 } else {
373 num_bytes += 4;
374 }
375 }
376 return num_bytes;
377}
378
379// The number of reference arguments to this method including implicit this
380// pointer
381size_t Method::NumReferenceArgs() const {
382 const StringPiece& shorty = GetShorty();
383 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
384 for (int i = 1; i < shorty.length(); i++) {
385 if ((shorty[i] == 'L') || (shorty[i] == '[')) {
386 result++;
387 }
388 }
389 return result;
390}
391
392// The number of long or double arguments
393size_t Method::NumLongOrDoubleArgs() const {
394 const StringPiece& shorty = GetShorty();
395 size_t result = 0;
396 for (int i = 1; i < shorty.length(); i++) {
397 if ((shorty[i] == 'D') || (shorty[i] == 'J')) {
398 result++;
399 }
400 }
401 return result;
402}
403
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700404// Is the given method parameter a reference?
405bool Method::IsParamAReference(unsigned int param) const {
406 CHECK_LT(param, NumArgs());
407 if (IsStatic()) {
408 param++; // 0th argument must skip return value at start of the shorty
409 } else if (param == 0) {
410 return true; // this argument
411 }
412 return GetShorty()[param] == 'L';
413}
414
415// Is the given method parameter a long or double?
416bool Method::IsParamALongOrDouble(unsigned int param) const {
417 CHECK_LT(param, NumArgs());
418 if (IsStatic()) {
419 param++; // 0th argument must skip return value at start of the shorty
420 } else if (param == 0) {
421 return false; // this argument
422 }
423 return (GetShorty()[param] == 'J') || (GetShorty()[param] == 'D');
424}
425
426static size_t ShortyCharToSize(char x) {
427 switch (x) {
428 case 'V': return 0;
429 case '[': return kPointerSize;
430 case 'L': return kPointerSize;
431 case 'D': return 8;
432 case 'J': return 8;
433 default: return 4;
434 }
435}
436
437size_t Method::ParamSize(unsigned int param) const {
438 CHECK_LT(param, NumArgs());
439 if (IsStatic()) {
440 param++; // 0th argument must skip return value at start of the shorty
441 } else if (param == 0) {
442 return kPointerSize; // this argument
443 }
444 return ShortyCharToSize(GetShorty()[param]);
445}
446
447size_t Method::ReturnSize() const {
448 return ShortyCharToSize(GetShorty()[0]);
449}
450
451bool Method::HasSameNameAndDescriptor(const Method* that) const {
452 return (this->GetName()->Equals(that->GetName()) &&
453 this->GetSignature()->Equals(that->GetSignature()));
454}
455
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700456void Method::SetCode(ByteArray* code_array,
457 InstructionSet instruction_set) {
458 CHECK(!HasCode() || IsNative());
459 SetFieldPtr<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), code_array, false);
460 int8_t* code = code_array->GetData();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700461 uintptr_t address = reinterpret_cast<uintptr_t>(code);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700462 if (instruction_set == kThumb2) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700463 // Set the low-order bit so a BLX will switch to Thumb mode
464 address |= 0x1;
465 }
466 SetFieldPtr<uintptr_t>(OFFSET_OF_OBJECT_MEMBER(Method, code_), address, false);
467}
468
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700469void Method::SetInvokeStub(const ByteArray* invoke_stub_array) {
470 const InvokeStub* invoke_stub = reinterpret_cast<InvokeStub*>(invoke_stub_array->GetData());
471 SetFieldPtr<const ByteArray*>(
472 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), invoke_stub_array, false);
473 SetFieldPtr<const InvokeStub*>(
474 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), invoke_stub, false);
475}
476
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700477void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
478 // Push a transition back into managed code onto the linked list in thread.
479 CHECK_EQ(Thread::kRunnable, self->GetState());
480 NativeToManagedRecord record;
481 self->PushNativeToManagedRecord(&record);
482
483 // Call the invoke stub associated with the method.
484 // Pass everything as arguments.
485 const Method::InvokeStub* stub = GetInvokeStub();
486 if (HasCode() && stub != NULL) {
487 (*stub)(this, receiver, self, args, result);
488 } else {
489 LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
490 if (result != NULL) {
491 result->j = 0;
492 }
493 }
494
495 // Pop transition.
496 self->PopNativeToManagedRecord(record);
497}
498
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700499void Class::SetStatus(Status new_status) {
500 CHECK(new_status > GetStatus() || new_status == kStatusError ||
Elliott Hughesdcc24742011-09-07 14:02:44 -0700501 !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700502 CHECK(sizeof(Status) == sizeof(uint32_t));
503 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_),
504 new_status, false);
505}
506
507DexCache* Class::GetDexCache() const {
508 return GetFieldObject<DexCache*>(
509 OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
510}
511
512void Class::SetDexCache(DexCache* new_dex_cache) {
513 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_),
514 new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700515}
516
Brian Carlstrom1f870082011-08-23 16:02:11 -0700517Object* Class::AllocObjectFromCode(uint32_t type_idx, Method* method) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700518 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700519 if (klass == NULL) {
520 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
521 if (klass == NULL) {
522 UNIMPLEMENTED(FATAL) << "throw an error";
523 return NULL;
524 }
525 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700526 return klass->AllocObject();
527}
528
529Object* Class::AllocObject() {
530 DCHECK(!IsAbstract());
531 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700532}
533
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700534void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
535 if (new_reference_offsets != CLASS_WALK_SUPER) {
536 // Sanity check that the number of bits set in the reference offset bitmap
537 // agrees with the number of references
538 Class* cur = this;
539 size_t cnt = 0;
540 while (cur) {
541 cnt += cur->NumReferenceInstanceFieldsDuringLinking();
542 cur = cur->GetSuperClass();
543 }
544 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt);
545 }
546 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
547 new_reference_offsets, false);
548}
549
550void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
551 if (new_reference_offsets != CLASS_WALK_SUPER) {
552 // Sanity check that the number of bits set in the reference offset bitmap
553 // agrees with the number of references
554 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
555 NumReferenceStaticFieldsDuringLinking());
556 }
557 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
558 new_reference_offsets, false);
559}
560
561size_t Class::PrimitiveSize() const {
562 switch (GetPrimitiveType()) {
563 case kPrimBoolean:
564 case kPrimByte:
565 case kPrimChar:
566 case kPrimShort:
567 case kPrimInt:
568 case kPrimFloat:
569 return sizeof(int32_t);
570 case kPrimLong:
571 case kPrimDouble:
572 return sizeof(int64_t);
573 default:
574 LOG(FATAL) << "Primitive type size calculation on invalid type " << this;
575 return 0;
576 }
577}
578
579size_t Class::GetTypeSize(const String* descriptor) {
580 switch (descriptor->CharAt(0)) {
581 case 'B': return 1; // byte
582 case 'C': return 2; // char
583 case 'D': return 8; // double
584 case 'F': return 4; // float
585 case 'I': return 4; // int
586 case 'J': return 8; // long
587 case 'S': return 2; // short
588 case 'Z': return 1; // boolean
589 case 'L': return sizeof(Object*);
590 case '[': return sizeof(Array*);
591 default:
592 LOG(ERROR) << "Unknown type " << descriptor;
593 return 0;
594 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700595}
596
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700597bool Class::Implements(const Class* klass) const {
598 DCHECK(klass != NULL);
599 DCHECK(klass->IsInterface());
600 // All interfaces implemented directly and by our superclass, and
601 // recursively all super-interfaces of those interfaces, are listed
602 // in iftable_, so we can just do a linear scan through that.
603 for (size_t i = 0; i < iftable_count_; i++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700604 if (iftable_[i].GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700605 return true;
606 }
607 }
608 return false;
609}
610
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700611bool Class::CanPutArrayElement(const Class* object_class, const Class* array_class) {
612 if (object_class->IsArrayClass()) {
613 return array_class->IsArrayAssignableFromArray(object_class);
614 } else {
615 return array_class->GetComponentType()->IsAssignableFrom(object_class);
616 }
617}
618
619void Class::CanPutArrayElementFromCode(const Class* object_class, const Class* array_class) {
620 if (!CanPutArrayElement(object_class, array_class)) {
621 LOG(ERROR) << "Can't put a " << PrettyDescriptor(object_class->GetDescriptor())
622 << " into a " << PrettyDescriptor(array_class->GetDescriptor());
623 UNIMPLEMENTED(FATAL) << "need to throw ArrayStoreException and unwind stack";
624 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700625}
626
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700627// Determine whether "this" is assignable from "klazz", where both of these
628// are array classes.
629//
630// Consider an array class, e.g. Y[][], where Y is a subclass of X.
631// Y[][] = Y[][] --> true (identity)
632// X[][] = Y[][] --> true (element superclass)
633// Y = Y[][] --> false
634// Y[] = Y[][] --> false
635// Object = Y[][] --> true (everything is an object)
636// Object[] = Y[][] --> true
637// Object[][] = Y[][] --> true
638// Object[][][] = Y[][] --> false (too many []s)
639// Serializable = Y[][] --> true (all arrays are Serializable)
640// Serializable[] = Y[][] --> true
641// Serializable[][] = Y[][] --> false (unless Y is Serializable)
642//
643// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700644// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700645//
646bool Class::IsArrayAssignableFromArray(const Class* klass) const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700647 DCHECK(IsArrayClass());
648 DCHECK(klass->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700649 DCHECK_GT(GetArrayRank(), 0);
650 DCHECK_GT(klass->GetArrayRank(), 0);
651 DCHECK(GetComponentType() != NULL);
652 DCHECK(klass->GetComponentType() != NULL);
653 if (GetArrayRank() > klass->GetArrayRank()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700654 // Too many []s.
655 return false;
656 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657 if (GetArrayRank() == klass->GetArrayRank()) {
658 return GetComponentType()->IsAssignableFrom(klass->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700659 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700660 DCHECK_LT(GetArrayRank(), klass->GetArrayRank());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700661 // The thing we might be assignable from has more dimensions. We
662 // must be an Object or array of Object, or a standard array
663 // interface or array of standard array interfaces (the standard
664 // interfaces being java/lang/Cloneable and java/io/Serializable).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700665 if (GetComponentType()->IsInterface()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700666 // See if we implement our component type. We know the
667 // base element is an interface; if the array class implements
668 // it, we know it's a standard array interface.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700669 return Implements(GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700670 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700671 // See if this is an array of Object, Object[], etc.
672 return GetComponentType()->IsObjectClass();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700673}
674
675bool Class::IsAssignableFromArray(const Class* klass) const {
676 DCHECK(!IsInterface()); // handled first in IsAssignableFrom
Brian Carlstromb63ec392011-08-27 17:38:27 -0700677 DCHECK(klass->IsArrayClass());
678 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700679 // If "this" is not also an array, it must be Object.
680 // klass's super should be java_lang_Object, since it is an array.
681 Class* java_lang_Object = klass->GetSuperClass();
682 DCHECK(java_lang_Object != NULL);
683 DCHECK(java_lang_Object->GetSuperClass() == NULL);
684 return this == java_lang_Object;
685 }
686 return IsArrayAssignableFromArray(klass);
687}
688
689bool Class::IsSubClass(const Class* klass) const {
690 DCHECK(!IsInterface());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700691 DCHECK(!klass->IsArrayClass());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700692 const Class* current = this;
693 do {
694 if (current == klass) {
695 return true;
696 }
697 current = current->GetSuperClass();
698 } while (current != NULL);
699 return false;
700}
701
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700702bool Class::IsInSamePackage(const String* descriptor_string_1,
703 const String* descriptor_string_2) {
704 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
705 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
706
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700707 size_t i = 0;
708 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
709 ++i;
710 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700711 if (descriptor1.find('/', i) != StringPiece::npos ||
712 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700713 return false;
714 } else {
715 return true;
716 }
717}
718
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700719#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700720bool Class::IsInSamePackage(const StringPiece& descriptor1,
721 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700722 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700723 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700724 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
725 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700726 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
727}
728#endif
729
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700730bool Class::IsInSamePackage(const Class* that) const {
731 const Class* klass1 = this;
732 const Class* klass2 = that;
733 if (klass1 == klass2) {
734 return true;
735 }
736 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700737 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700738 return false;
739 }
740 // Arrays are in the same package when their element classes are.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700741 if (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700742 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700743 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700744 if (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700746 }
747 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700748 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700749}
750
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700751const ClassLoader* Class::GetClassLoader() const {
752 return GetFieldObject<const ClassLoader*>(
753 OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700754}
755
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700756void Class::SetClassLoader(const ClassLoader* new_cl) {
757 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
758 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_),
759 new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700760}
761
Brian Carlstrom30b94452011-08-25 21:35:26 -0700762Method* Class::FindVirtualMethodForInterface(Method* method) {
763 Class* declaring_class = method->GetDeclaringClass();
764 DCHECK(declaring_class->IsInterface());
765 // TODO cache to improve lookup speed
766 for (size_t i = 0; i < iftable_count_; i++) {
767 InterfaceEntry& interface_entry = iftable_[i];
768 if (interface_entry.GetInterface() == declaring_class) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700769 return GetVTable()->Get(
770 interface_entry.GetMethodIndexArray()[method->GetMethodIndex()]);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700771 }
772 }
773 UNIMPLEMENTED(FATAL) << "Need to throw an error of some kind";
774 return NULL;
775}
776
jeffhaobdb76512011-09-07 11:43:16 -0700777Method* Class::FindInterfaceMethod(const StringPiece& name,
778 const StringPiece& signature) {
779 // Check the current class before checking the interfaces.
780 Method* method = FindVirtualMethod(name, signature);
781 if (method != NULL) {
782 return method;
783 }
784
785 InterfaceEntry* iftable = GetIFTable();
786 for (size_t i = 0; i < GetIFTableCount(); i++) {
787 method = iftable[i].GetInterface()->FindVirtualMethod(name, signature);
788 if (method != NULL) {
789 return method;
790 }
791 }
792 return NULL;
793}
794
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700795Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700796 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700797 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700798 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700799 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700800 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700801 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700802 }
803 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700804 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700805}
806
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700807Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700808 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700809 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700810 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700811 if (method != NULL) {
812 return method;
813 }
814 }
815 return NULL;
816}
817
818Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700819 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700820 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700821 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700822 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700823 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700824 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700825 }
826 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700827 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700828}
829
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700830Method* Class::FindVirtualMethod(const StringPiece& name,
831 const StringPiece& descriptor) {
832 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
833 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
834 if (method != NULL) {
835 return method;
836 }
837 }
838 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700839}
840
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700841Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 // Is the field in this class?
843 // Interfaces are not relevant because they can't contain instance fields.
844 for (size_t i = 0; i < NumInstanceFields(); ++i) {
845 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700846 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 return f;
848 }
849 }
850 return NULL;
851}
852
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700853Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 // Is the field in this class, or any of its superclasses?
855 // Interfaces are not relevant because they can't contain instance fields.
856 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700857 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 if (f != NULL) {
859 return f;
860 }
861 }
862 return NULL;
863}
864
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700865Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
866 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 for (size_t i = 0; i < NumStaticFields(); ++i) {
868 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700869 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 return f;
871 }
872 }
873 return NULL;
874}
875
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700876Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 // Is the field in this class (or its interfaces), or any of its
878 // superclasses (or their interfaces)?
879 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
880 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700881 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 if (f != NULL) {
883 return f;
884 }
885
886 // Is this field in any of this class' interfaces?
887 for (size_t i = 0; i < c->NumInterfaces(); ++i) {
888 Class* interface = c->GetInterface(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700889 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 if (f != NULL) {
891 return f;
892 }
893 }
894 }
895 return NULL;
896}
897
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700898Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700899 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700900 DCHECK_GE(component_count, 0);
901 DCHECK(array_class->IsArrayClass());
902 size_t size = SizeOf(component_count, component_size);
903 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
904 if (array != NULL) {
905 DCHECK(array->IsArrayInstance());
906 array->SetLength(component_count);
907 }
908 return array;
909}
910
911Array* Array::Alloc(Class* array_class, int32_t component_count) {
912 return Alloc(array_class, component_count, array_class->GetComponentSize());
913}
914
915Array* Array::AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
916 // TODO: throw on negative component_count
917 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
918 if (klass == NULL) {
919 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
920 if (klass == NULL || !klass->IsArrayClass()) {
921 UNIMPLEMENTED(FATAL) << "throw an error";
922 return NULL;
923 }
924 }
925 return Array::Alloc(klass, component_count);
926}
927
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700928template<typename T>
929PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700930 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700931 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
932 return down_cast<PrimitiveArray<T>*>(raw_array);
933}
934
935template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
936
937// Explicitly instantiate all the primitive array types.
938template class PrimitiveArray<uint8_t>; // BooleanArray
939template class PrimitiveArray<int8_t>; // ByteArray
940template class PrimitiveArray<uint16_t>; // CharArray
941template class PrimitiveArray<double>; // DoubleArray
942template class PrimitiveArray<float>; // FloatArray
943template class PrimitiveArray<int32_t>; // IntArray
944template class PrimitiveArray<int64_t>; // LongArray
945template class PrimitiveArray<int16_t>; // ShortArray
946
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700947// TODO: get global references for these
948Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700949
Brian Carlstroma663ea52011-08-19 23:33:41 -0700950void String::SetClass(Class* java_lang_String) {
951 CHECK(java_lang_String_ == NULL);
952 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700953 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700954}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700955
Brian Carlstroma663ea52011-08-19 23:33:41 -0700956void String::ResetClass() {
957 CHECK(java_lang_String_ != NULL);
958 java_lang_String_ = NULL;
959}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700960
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700961const String* String::Intern() const {
962 return Runtime::Current()->GetInternTable()->InternWeak(this);
963}
964
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700965int32_t String::GetHashCode() const {
966 int32_t result = GetField32(
967 OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
968 DCHECK(result != 0 ||
969 ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0);
970 return result;
971}
972
973int32_t String::GetLength() const {
974 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
975 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
976 return result;
977}
978
979uint16_t String::CharAt(int32_t index) const {
980 // TODO: do we need this? Equals is the only caller, and could
981 // bounds check itself.
982 if (index < 0 || index >= count_) {
983 Thread* self = Thread::Current();
984 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
985 "length=%i; index=%i", count_, index);
986 return 0;
987 }
988 return GetCharArray()->Get(index + GetOffset());
989}
990
991String* String::AllocFromUtf16(int32_t utf16_length,
992 const uint16_t* utf16_data_in,
993 int32_t hash_code) {
994 String* string = Alloc(GetJavaLangString(), utf16_length);
995 // TODO: use 16-bit wide memset variant
996 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
997 for (int i = 0; i < utf16_length; i++) {
998 array->Set(i, utf16_data_in[i]);
999 }
1000 if (hash_code != 0) {
1001 string->SetHashCode(hash_code);
1002 } else {
1003 string->ComputeHashCode();
1004 }
1005 return string;
1006}
1007
1008String* String::AllocFromModifiedUtf8(const char* utf) {
1009 size_t char_count = CountModifiedUtf8Chars(utf);
1010 return AllocFromModifiedUtf8(char_count, utf);
1011}
1012
1013String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1014 const char* utf8_data_in) {
1015 String* string = Alloc(GetJavaLangString(), utf16_length);
1016 uint16_t* utf16_data_out =
1017 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1018 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1019 string->ComputeHashCode();
1020 return string;
1021}
1022
1023String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
1024 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1025}
1026
1027String* String::Alloc(Class* java_lang_String, CharArray* array) {
1028 String* string = down_cast<String*>(java_lang_String->AllocObject());
1029 string->SetArray(array);
1030 string->SetCount(array->GetLength());
1031 return string;
1032}
1033
1034bool String::Equals(const String* that) const {
1035 if (this == that) {
1036 // Quick reference equality test
1037 return true;
1038 } else if (that == NULL) {
1039 // Null isn't an instanceof anything
1040 return false;
1041 } else if (this->GetLength() != that->GetLength()) {
1042 // Quick length inequality test
1043 return false;
1044 } else {
1045 // NB don't short circuit on hash code as we're presumably here as the
1046 // hash code was already equal
1047 for (int32_t i = 0; i < that->GetLength(); ++i) {
1048 if (this->CharAt(i) != that->CharAt(i)) {
1049 return false;
1050 }
1051 }
1052 return true;
1053 }
1054}
1055
1056bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1057 int32_t that_length) const {
1058 if (this->GetLength() != that_length) {
1059 return false;
1060 } else {
1061 for (int32_t i = 0; i < that_length; ++i) {
1062 if (this->CharAt(i) != that_chars[that_offset + i]) {
1063 return false;
1064 }
1065 }
1066 return true;
1067 }
1068}
1069
1070bool String::Equals(const char* modified_utf8) const {
1071 for (int32_t i = 0; i < GetLength(); ++i) {
1072 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1073 if (ch == '\0' || ch != CharAt(i)) {
1074 return false;
1075 }
1076 }
1077 return *modified_utf8 == '\0';
1078}
1079
1080bool String::Equals(const StringPiece& modified_utf8) const {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001081 // TODO: do not assume C-string representation. For now DCHECK.
1082 DCHECK_EQ(modified_utf8.data()[modified_utf8.size()], 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001083 return Equals(modified_utf8.data());
1084}
1085
1086// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1087std::string String::ToModifiedUtf8() const {
1088 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1089 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1090 std::string result(byte_count, char(0));
1091 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1092 return result;
1093}
1094
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001095Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1096
1097void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1098 CHECK(java_lang_StackTraceElement_ == NULL);
1099 CHECK(java_lang_StackTraceElement != NULL);
1100 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1101}
1102
1103void StackTraceElement::ResetClass() {
1104 CHECK(java_lang_StackTraceElement_ != NULL);
1105 java_lang_StackTraceElement_ = NULL;
1106}
1107
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001108StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1109 const String* method_name,
1110 const String* file_name,
1111 int32_t line_number) {
1112 StackTraceElement* trace =
1113 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1114 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1115 const_cast<String*>(declaring_class), false);
1116 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1117 const_cast<String*>(method_name), false);
1118 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1119 const_cast<String*>(file_name), false);
1120 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1121 line_number, false);
1122 return trace;
1123}
1124
Elliott Hughes1f359b02011-07-17 14:27:17 -07001125static const char* kClassStatusNames[] = {
1126 "Error",
1127 "NotReady",
1128 "Idx",
1129 "Loaded",
1130 "Resolved",
1131 "Verifying",
1132 "Verified",
1133 "Initializing",
1134 "Initialized"
1135};
1136std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1137 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001138 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001139 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001140 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001141 }
1142 return os;
1143}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001144
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001145} // namespace art