blob: 8b0c976f63f0cfcba0bc12622d9bb12cc303d573 [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 {
53 DCHECK(Runtime::Current() != NULL)
54 << "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 ||
501 Runtime::Current() == NULL); // no runtime implies we're not initialized
502 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
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700777Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700778 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700779 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700780 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700781 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700782 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700783 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700784 }
785 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700786 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700787}
788
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700789Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700790 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700791 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700792 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700793 if (method != NULL) {
794 return method;
795 }
796 }
797 return NULL;
798}
799
800Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700801 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700802 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700803 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700804 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700805 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700806 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700807 }
808 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700809 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700810}
811
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700812Method* Class::FindVirtualMethod(const StringPiece& name,
813 const StringPiece& descriptor) {
814 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
815 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
816 if (method != NULL) {
817 return method;
818 }
819 }
820 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700821}
822
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700823Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 // Is the field in this class?
825 // Interfaces are not relevant because they can't contain instance fields.
826 for (size_t i = 0; i < NumInstanceFields(); ++i) {
827 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700828 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 return f;
830 }
831 }
832 return NULL;
833}
834
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700835Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 // Is the field in this class, or any of its superclasses?
837 // Interfaces are not relevant because they can't contain instance fields.
838 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700839 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 if (f != NULL) {
841 return f;
842 }
843 }
844 return NULL;
845}
846
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700847Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
848 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 for (size_t i = 0; i < NumStaticFields(); ++i) {
850 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700851 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 return f;
853 }
854 }
855 return NULL;
856}
857
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700858Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700859 // Is the field in this class (or its interfaces), or any of its
860 // superclasses (or their interfaces)?
861 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
862 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700863 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 if (f != NULL) {
865 return f;
866 }
867
868 // Is this field in any of this class' interfaces?
869 for (size_t i = 0; i < c->NumInterfaces(); ++i) {
870 Class* interface = c->GetInterface(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700871 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 if (f != NULL) {
873 return f;
874 }
875 }
876 }
877 return NULL;
878}
879
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700880Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700881 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700882 DCHECK_GE(component_count, 0);
883 DCHECK(array_class->IsArrayClass());
884 size_t size = SizeOf(component_count, component_size);
885 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
886 if (array != NULL) {
887 DCHECK(array->IsArrayInstance());
888 array->SetLength(component_count);
889 }
890 return array;
891}
892
893Array* Array::Alloc(Class* array_class, int32_t component_count) {
894 return Alloc(array_class, component_count, array_class->GetComponentSize());
895}
896
897Array* Array::AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
898 // TODO: throw on negative component_count
899 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
900 if (klass == NULL) {
901 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
902 if (klass == NULL || !klass->IsArrayClass()) {
903 UNIMPLEMENTED(FATAL) << "throw an error";
904 return NULL;
905 }
906 }
907 return Array::Alloc(klass, component_count);
908}
909
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700910template<typename T>
911PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700912 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700913 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
914 return down_cast<PrimitiveArray<T>*>(raw_array);
915}
916
917template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
918
919// Explicitly instantiate all the primitive array types.
920template class PrimitiveArray<uint8_t>; // BooleanArray
921template class PrimitiveArray<int8_t>; // ByteArray
922template class PrimitiveArray<uint16_t>; // CharArray
923template class PrimitiveArray<double>; // DoubleArray
924template class PrimitiveArray<float>; // FloatArray
925template class PrimitiveArray<int32_t>; // IntArray
926template class PrimitiveArray<int64_t>; // LongArray
927template class PrimitiveArray<int16_t>; // ShortArray
928
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700929// TODO: get global references for these
930Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700931
Brian Carlstroma663ea52011-08-19 23:33:41 -0700932void String::SetClass(Class* java_lang_String) {
933 CHECK(java_lang_String_ == NULL);
934 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700935 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700936}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700937
Brian Carlstroma663ea52011-08-19 23:33:41 -0700938void String::ResetClass() {
939 CHECK(java_lang_String_ != NULL);
940 java_lang_String_ = NULL;
941}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700942
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700943const String* String::Intern() const {
944 return Runtime::Current()->GetInternTable()->InternWeak(this);
945}
946
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700947int32_t String::GetHashCode() const {
948 int32_t result = GetField32(
949 OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
950 DCHECK(result != 0 ||
951 ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0);
952 return result;
953}
954
955int32_t String::GetLength() const {
956 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
957 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
958 return result;
959}
960
961uint16_t String::CharAt(int32_t index) const {
962 // TODO: do we need this? Equals is the only caller, and could
963 // bounds check itself.
964 if (index < 0 || index >= count_) {
965 Thread* self = Thread::Current();
966 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
967 "length=%i; index=%i", count_, index);
968 return 0;
969 }
970 return GetCharArray()->Get(index + GetOffset());
971}
972
973String* String::AllocFromUtf16(int32_t utf16_length,
974 const uint16_t* utf16_data_in,
975 int32_t hash_code) {
976 String* string = Alloc(GetJavaLangString(), utf16_length);
977 // TODO: use 16-bit wide memset variant
978 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
979 for (int i = 0; i < utf16_length; i++) {
980 array->Set(i, utf16_data_in[i]);
981 }
982 if (hash_code != 0) {
983 string->SetHashCode(hash_code);
984 } else {
985 string->ComputeHashCode();
986 }
987 return string;
988}
989
990String* String::AllocFromModifiedUtf8(const char* utf) {
991 size_t char_count = CountModifiedUtf8Chars(utf);
992 return AllocFromModifiedUtf8(char_count, utf);
993}
994
995String* String::AllocFromModifiedUtf8(int32_t utf16_length,
996 const char* utf8_data_in) {
997 String* string = Alloc(GetJavaLangString(), utf16_length);
998 uint16_t* utf16_data_out =
999 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1000 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1001 string->ComputeHashCode();
1002 return string;
1003}
1004
1005String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
1006 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1007}
1008
1009String* String::Alloc(Class* java_lang_String, CharArray* array) {
1010 String* string = down_cast<String*>(java_lang_String->AllocObject());
1011 string->SetArray(array);
1012 string->SetCount(array->GetLength());
1013 return string;
1014}
1015
1016bool String::Equals(const String* that) const {
1017 if (this == that) {
1018 // Quick reference equality test
1019 return true;
1020 } else if (that == NULL) {
1021 // Null isn't an instanceof anything
1022 return false;
1023 } else if (this->GetLength() != that->GetLength()) {
1024 // Quick length inequality test
1025 return false;
1026 } else {
1027 // NB don't short circuit on hash code as we're presumably here as the
1028 // hash code was already equal
1029 for (int32_t i = 0; i < that->GetLength(); ++i) {
1030 if (this->CharAt(i) != that->CharAt(i)) {
1031 return false;
1032 }
1033 }
1034 return true;
1035 }
1036}
1037
1038bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1039 int32_t that_length) const {
1040 if (this->GetLength() != that_length) {
1041 return false;
1042 } else {
1043 for (int32_t i = 0; i < that_length; ++i) {
1044 if (this->CharAt(i) != that_chars[that_offset + i]) {
1045 return false;
1046 }
1047 }
1048 return true;
1049 }
1050}
1051
1052bool String::Equals(const char* modified_utf8) const {
1053 for (int32_t i = 0; i < GetLength(); ++i) {
1054 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1055 if (ch == '\0' || ch != CharAt(i)) {
1056 return false;
1057 }
1058 }
1059 return *modified_utf8 == '\0';
1060}
1061
1062bool String::Equals(const StringPiece& modified_utf8) const {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001063 // TODO: do not assume C-string representation. For now DCHECK.
1064 DCHECK_EQ(modified_utf8.data()[modified_utf8.size()], 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001065 return Equals(modified_utf8.data());
1066}
1067
1068// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1069std::string String::ToModifiedUtf8() const {
1070 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1071 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1072 std::string result(byte_count, char(0));
1073 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1074 return result;
1075}
1076
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001077Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1078
1079void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1080 CHECK(java_lang_StackTraceElement_ == NULL);
1081 CHECK(java_lang_StackTraceElement != NULL);
1082 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1083}
1084
1085void StackTraceElement::ResetClass() {
1086 CHECK(java_lang_StackTraceElement_ != NULL);
1087 java_lang_StackTraceElement_ = NULL;
1088}
1089
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001090StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1091 const String* method_name,
1092 const String* file_name,
1093 int32_t line_number) {
1094 StackTraceElement* trace =
1095 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1096 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1097 const_cast<String*>(declaring_class), false);
1098 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1099 const_cast<String*>(method_name), false);
1100 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1101 const_cast<String*>(file_name), false);
1102 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1103 line_number, false);
1104 return trace;
1105}
1106
Elliott Hughes1f359b02011-07-17 14:27:17 -07001107static const char* kClassStatusNames[] = {
1108 "Error",
1109 "NotReady",
1110 "Idx",
1111 "Loaded",
1112 "Resolved",
1113 "Verifying",
1114 "Verified",
1115 "Initializing",
1116 "Initialized"
1117};
1118std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1119 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001120 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001121 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001122 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001123 }
1124 return os;
1125}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001126
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001127} // namespace art