blob: 7c27ec798d181ffee96f2fcd1d1b76e5c3663be4 [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 {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700199 DCHECK(GetType()->IsPrimitiveInt()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 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 {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700276 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 // 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
buzbee4ef76522011-09-08 10:00:32 -0700456void Method::SetCode(ByteArray* code_array, InstructionSet instruction_set,
457 ByteArray* mapping_table) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700458 CHECK(!HasCode() || IsNative());
459 SetFieldPtr<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), code_array, false);
buzbee4ef76522011-09-08 10:00:32 -0700460 SetFieldPtr<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
461 mapping_table, false);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700462 int8_t* code = code_array->GetData();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700463 uintptr_t address = reinterpret_cast<uintptr_t>(code);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700464 if (instruction_set == kThumb2) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700465 // Set the low-order bit so a BLX will switch to Thumb mode
466 address |= 0x1;
467 }
468 SetFieldPtr<uintptr_t>(OFFSET_OF_OBJECT_MEMBER(Method, code_), address, false);
469}
470
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700471void Method::SetInvokeStub(const ByteArray* invoke_stub_array) {
472 const InvokeStub* invoke_stub = reinterpret_cast<InvokeStub*>(invoke_stub_array->GetData());
473 SetFieldPtr<const ByteArray*>(
474 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), invoke_stub_array, false);
475 SetFieldPtr<const InvokeStub*>(
476 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), invoke_stub, false);
477}
478
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700479void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
480 // Push a transition back into managed code onto the linked list in thread.
481 CHECK_EQ(Thread::kRunnable, self->GetState());
482 NativeToManagedRecord record;
483 self->PushNativeToManagedRecord(&record);
484
485 // Call the invoke stub associated with the method.
486 // Pass everything as arguments.
487 const Method::InvokeStub* stub = GetInvokeStub();
488 if (HasCode() && stub != NULL) {
489 (*stub)(this, receiver, self, args, result);
490 } else {
491 LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
492 if (result != NULL) {
493 result->j = 0;
494 }
495 }
496
497 // Pop transition.
498 self->PopNativeToManagedRecord(record);
499}
500
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700501void Class::SetStatus(Status new_status) {
502 CHECK(new_status > GetStatus() || new_status == kStatusError ||
Elliott Hughesdcc24742011-09-07 14:02:44 -0700503 !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700504 CHECK(sizeof(Status) == sizeof(uint32_t));
505 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_),
506 new_status, false);
507}
508
509DexCache* Class::GetDexCache() const {
510 return GetFieldObject<DexCache*>(
511 OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
512}
513
514void Class::SetDexCache(DexCache* new_dex_cache) {
515 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_),
516 new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700517}
518
Brian Carlstrom1f870082011-08-23 16:02:11 -0700519Object* Class::AllocObjectFromCode(uint32_t type_idx, Method* method) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700520 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700521 if (klass == NULL) {
522 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
523 if (klass == NULL) {
524 UNIMPLEMENTED(FATAL) << "throw an error";
525 return NULL;
526 }
527 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700528 return klass->AllocObject();
529}
530
531Object* Class::AllocObject() {
532 DCHECK(!IsAbstract());
533 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700534}
535
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700536void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
537 if (new_reference_offsets != CLASS_WALK_SUPER) {
538 // Sanity check that the number of bits set in the reference offset bitmap
539 // agrees with the number of references
540 Class* cur = this;
541 size_t cnt = 0;
542 while (cur) {
543 cnt += cur->NumReferenceInstanceFieldsDuringLinking();
544 cur = cur->GetSuperClass();
545 }
546 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt);
547 }
548 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
549 new_reference_offsets, false);
550}
551
552void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
553 if (new_reference_offsets != CLASS_WALK_SUPER) {
554 // Sanity check that the number of bits set in the reference offset bitmap
555 // agrees with the number of references
556 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
557 NumReferenceStaticFieldsDuringLinking());
558 }
559 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
560 new_reference_offsets, false);
561}
562
563size_t Class::PrimitiveSize() const {
564 switch (GetPrimitiveType()) {
565 case kPrimBoolean:
566 case kPrimByte:
567 case kPrimChar:
568 case kPrimShort:
569 case kPrimInt:
570 case kPrimFloat:
571 return sizeof(int32_t);
572 case kPrimLong:
573 case kPrimDouble:
574 return sizeof(int64_t);
575 default:
576 LOG(FATAL) << "Primitive type size calculation on invalid type " << this;
577 return 0;
578 }
579}
580
581size_t Class::GetTypeSize(const String* descriptor) {
582 switch (descriptor->CharAt(0)) {
583 case 'B': return 1; // byte
584 case 'C': return 2; // char
585 case 'D': return 8; // double
586 case 'F': return 4; // float
587 case 'I': return 4; // int
588 case 'J': return 8; // long
589 case 'S': return 2; // short
590 case 'Z': return 1; // boolean
591 case 'L': return sizeof(Object*);
592 case '[': return sizeof(Array*);
593 default:
594 LOG(ERROR) << "Unknown type " << descriptor;
595 return 0;
596 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700597}
598
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700599bool Class::Implements(const Class* klass) const {
600 DCHECK(klass != NULL);
601 DCHECK(klass->IsInterface());
602 // All interfaces implemented directly and by our superclass, and
603 // recursively all super-interfaces of those interfaces, are listed
604 // in iftable_, so we can just do a linear scan through that.
605 for (size_t i = 0; i < iftable_count_; i++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700606 if (iftable_[i].GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700607 return true;
608 }
609 }
610 return false;
611}
612
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700613bool Class::CanPutArrayElement(const Class* object_class, const Class* array_class) {
614 if (object_class->IsArrayClass()) {
615 return array_class->IsArrayAssignableFromArray(object_class);
616 } else {
617 return array_class->GetComponentType()->IsAssignableFrom(object_class);
618 }
619}
620
621void Class::CanPutArrayElementFromCode(const Class* object_class, const Class* array_class) {
622 if (!CanPutArrayElement(object_class, array_class)) {
623 LOG(ERROR) << "Can't put a " << PrettyDescriptor(object_class->GetDescriptor())
624 << " into a " << PrettyDescriptor(array_class->GetDescriptor());
625 UNIMPLEMENTED(FATAL) << "need to throw ArrayStoreException and unwind stack";
626 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700627}
628
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700629// Determine whether "this" is assignable from "klazz", where both of these
630// are array classes.
631//
632// Consider an array class, e.g. Y[][], where Y is a subclass of X.
633// Y[][] = Y[][] --> true (identity)
634// X[][] = Y[][] --> true (element superclass)
635// Y = Y[][] --> false
636// Y[] = Y[][] --> false
637// Object = Y[][] --> true (everything is an object)
638// Object[] = Y[][] --> true
639// Object[][] = Y[][] --> true
640// Object[][][] = Y[][] --> false (too many []s)
641// Serializable = Y[][] --> true (all arrays are Serializable)
642// Serializable[] = Y[][] --> true
643// Serializable[][] = Y[][] --> false (unless Y is Serializable)
644//
645// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700646// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700647//
648bool Class::IsArrayAssignableFromArray(const Class* klass) const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700649 DCHECK(IsArrayClass());
650 DCHECK(klass->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651 DCHECK_GT(GetArrayRank(), 0);
652 DCHECK_GT(klass->GetArrayRank(), 0);
653 DCHECK(GetComponentType() != NULL);
654 DCHECK(klass->GetComponentType() != NULL);
655 if (GetArrayRank() > klass->GetArrayRank()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700656 // Too many []s.
657 return false;
658 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700659 if (GetArrayRank() == klass->GetArrayRank()) {
660 return GetComponentType()->IsAssignableFrom(klass->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700661 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700662 DCHECK_LT(GetArrayRank(), klass->GetArrayRank());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700663 // The thing we might be assignable from has more dimensions. We
664 // must be an Object or array of Object, or a standard array
665 // interface or array of standard array interfaces (the standard
666 // interfaces being java/lang/Cloneable and java/io/Serializable).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700667 if (GetComponentType()->IsInterface()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700668 // See if we implement our component type. We know the
669 // base element is an interface; if the array class implements
670 // it, we know it's a standard array interface.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700671 return Implements(GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700672 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700673 // See if this is an array of Object, Object[], etc.
674 return GetComponentType()->IsObjectClass();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700675}
676
677bool Class::IsAssignableFromArray(const Class* klass) const {
678 DCHECK(!IsInterface()); // handled first in IsAssignableFrom
Brian Carlstromb63ec392011-08-27 17:38:27 -0700679 DCHECK(klass->IsArrayClass());
680 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700681 // If "this" is not also an array, it must be Object.
682 // klass's super should be java_lang_Object, since it is an array.
683 Class* java_lang_Object = klass->GetSuperClass();
684 DCHECK(java_lang_Object != NULL);
685 DCHECK(java_lang_Object->GetSuperClass() == NULL);
686 return this == java_lang_Object;
687 }
688 return IsArrayAssignableFromArray(klass);
689}
690
691bool Class::IsSubClass(const Class* klass) const {
692 DCHECK(!IsInterface());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700693 DCHECK(!klass->IsArrayClass());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700694 const Class* current = this;
695 do {
696 if (current == klass) {
697 return true;
698 }
699 current = current->GetSuperClass();
700 } while (current != NULL);
701 return false;
702}
703
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700704bool Class::IsInSamePackage(const String* descriptor_string_1,
705 const String* descriptor_string_2) {
706 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
707 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
708
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700709 size_t i = 0;
710 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
711 ++i;
712 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700713 if (descriptor1.find('/', i) != StringPiece::npos ||
714 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700715 return false;
716 } else {
717 return true;
718 }
719}
720
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700721#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700722bool Class::IsInSamePackage(const StringPiece& descriptor1,
723 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700724 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700725 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700726 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
727 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700728 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
729}
730#endif
731
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700732bool Class::IsInSamePackage(const Class* that) const {
733 const Class* klass1 = this;
734 const Class* klass2 = that;
735 if (klass1 == klass2) {
736 return true;
737 }
738 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700739 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700740 return false;
741 }
742 // Arrays are in the same package when their element classes are.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700743 if (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700744 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700745 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700746 if (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700747 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700748 }
749 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700750 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700751}
752
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700753const ClassLoader* Class::GetClassLoader() const {
754 return GetFieldObject<const ClassLoader*>(
755 OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700756}
757
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700758void Class::SetClassLoader(const ClassLoader* new_cl) {
759 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
760 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_),
761 new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700762}
763
Brian Carlstrom30b94452011-08-25 21:35:26 -0700764Method* Class::FindVirtualMethodForInterface(Method* method) {
765 Class* declaring_class = method->GetDeclaringClass();
766 DCHECK(declaring_class->IsInterface());
767 // TODO cache to improve lookup speed
768 for (size_t i = 0; i < iftable_count_; i++) {
769 InterfaceEntry& interface_entry = iftable_[i];
770 if (interface_entry.GetInterface() == declaring_class) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700771 return GetVTable()->Get(
772 interface_entry.GetMethodIndexArray()[method->GetMethodIndex()]);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700773 }
774 }
775 UNIMPLEMENTED(FATAL) << "Need to throw an error of some kind";
776 return NULL;
777}
778
jeffhaobdb76512011-09-07 11:43:16 -0700779Method* Class::FindInterfaceMethod(const StringPiece& name,
780 const StringPiece& signature) {
781 // Check the current class before checking the interfaces.
782 Method* method = FindVirtualMethod(name, signature);
783 if (method != NULL) {
784 return method;
785 }
786
787 InterfaceEntry* iftable = GetIFTable();
788 for (size_t i = 0; i < GetIFTableCount(); i++) {
789 method = iftable[i].GetInterface()->FindVirtualMethod(name, signature);
790 if (method != NULL) {
791 return method;
792 }
793 }
794 return NULL;
795}
796
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700797Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700798 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700799 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700800 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700801 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700802 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700803 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700804 }
805 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700806 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700807}
808
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700809Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700810 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700811 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700812 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700813 if (method != NULL) {
814 return method;
815 }
816 }
817 return NULL;
818}
819
820Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700821 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700822 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700823 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700824 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700825 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700826 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700827 }
828 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700829 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700830}
831
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700832Method* Class::FindVirtualMethod(const StringPiece& name,
833 const StringPiece& descriptor) {
834 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
835 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
836 if (method != NULL) {
837 return method;
838 }
839 }
840 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700841}
842
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700843Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 // Is the field in this class?
845 // Interfaces are not relevant because they can't contain instance fields.
846 for (size_t i = 0; i < NumInstanceFields(); ++i) {
847 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700848 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 return f;
850 }
851 }
852 return NULL;
853}
854
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700855Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 // Is the field in this class, or any of its superclasses?
857 // Interfaces are not relevant because they can't contain instance fields.
858 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700859 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 if (f != NULL) {
861 return f;
862 }
863 }
864 return NULL;
865}
866
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700867Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
868 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 for (size_t i = 0; i < NumStaticFields(); ++i) {
870 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700871 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 return f;
873 }
874 }
875 return NULL;
876}
877
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700878Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 // Is the field in this class (or its interfaces), or any of its
880 // superclasses (or their interfaces)?
881 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
882 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700883 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 if (f != NULL) {
885 return f;
886 }
887
888 // Is this field in any of this class' interfaces?
889 for (size_t i = 0; i < c->NumInterfaces(); ++i) {
890 Class* interface = c->GetInterface(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700891 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 if (f != NULL) {
893 return f;
894 }
895 }
896 }
897 return NULL;
898}
899
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700900Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700901 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700902 DCHECK_GE(component_count, 0);
903 DCHECK(array_class->IsArrayClass());
904 size_t size = SizeOf(component_count, component_size);
905 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
906 if (array != NULL) {
907 DCHECK(array->IsArrayInstance());
908 array->SetLength(component_count);
909 }
910 return array;
911}
912
913Array* Array::Alloc(Class* array_class, int32_t component_count) {
914 return Alloc(array_class, component_count, array_class->GetComponentSize());
915}
916
917Array* Array::AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
918 // TODO: throw on negative component_count
919 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
920 if (klass == NULL) {
921 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
922 if (klass == NULL || !klass->IsArrayClass()) {
923 UNIMPLEMENTED(FATAL) << "throw an error";
924 return NULL;
925 }
926 }
927 return Array::Alloc(klass, component_count);
928}
929
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700930template<typename T>
931PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700932 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700933 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
934 return down_cast<PrimitiveArray<T>*>(raw_array);
935}
936
937template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
938
939// Explicitly instantiate all the primitive array types.
940template class PrimitiveArray<uint8_t>; // BooleanArray
941template class PrimitiveArray<int8_t>; // ByteArray
942template class PrimitiveArray<uint16_t>; // CharArray
943template class PrimitiveArray<double>; // DoubleArray
944template class PrimitiveArray<float>; // FloatArray
945template class PrimitiveArray<int32_t>; // IntArray
946template class PrimitiveArray<int64_t>; // LongArray
947template class PrimitiveArray<int16_t>; // ShortArray
948
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700949// TODO: get global references for these
950Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700951
Brian Carlstroma663ea52011-08-19 23:33:41 -0700952void String::SetClass(Class* java_lang_String) {
953 CHECK(java_lang_String_ == NULL);
954 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700955 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700956}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700957
Brian Carlstroma663ea52011-08-19 23:33:41 -0700958void String::ResetClass() {
959 CHECK(java_lang_String_ != NULL);
960 java_lang_String_ = NULL;
961}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700962
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700963const String* String::Intern() const {
964 return Runtime::Current()->GetInternTable()->InternWeak(this);
965}
966
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700967int32_t String::GetHashCode() const {
968 int32_t result = GetField32(
969 OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
970 DCHECK(result != 0 ||
971 ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0);
972 return result;
973}
974
975int32_t String::GetLength() const {
976 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
977 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
978 return result;
979}
980
981uint16_t String::CharAt(int32_t index) const {
982 // TODO: do we need this? Equals is the only caller, and could
983 // bounds check itself.
984 if (index < 0 || index >= count_) {
985 Thread* self = Thread::Current();
986 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
987 "length=%i; index=%i", count_, index);
988 return 0;
989 }
990 return GetCharArray()->Get(index + GetOffset());
991}
992
993String* String::AllocFromUtf16(int32_t utf16_length,
994 const uint16_t* utf16_data_in,
995 int32_t hash_code) {
996 String* string = Alloc(GetJavaLangString(), utf16_length);
997 // TODO: use 16-bit wide memset variant
998 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
999 for (int i = 0; i < utf16_length; i++) {
1000 array->Set(i, utf16_data_in[i]);
1001 }
1002 if (hash_code != 0) {
1003 string->SetHashCode(hash_code);
1004 } else {
1005 string->ComputeHashCode();
1006 }
1007 return string;
1008}
1009
1010String* String::AllocFromModifiedUtf8(const char* utf) {
1011 size_t char_count = CountModifiedUtf8Chars(utf);
1012 return AllocFromModifiedUtf8(char_count, utf);
1013}
1014
1015String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1016 const char* utf8_data_in) {
1017 String* string = Alloc(GetJavaLangString(), utf16_length);
1018 uint16_t* utf16_data_out =
1019 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1020 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1021 string->ComputeHashCode();
1022 return string;
1023}
1024
1025String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
1026 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1027}
1028
1029String* String::Alloc(Class* java_lang_String, CharArray* array) {
1030 String* string = down_cast<String*>(java_lang_String->AllocObject());
1031 string->SetArray(array);
1032 string->SetCount(array->GetLength());
1033 return string;
1034}
1035
1036bool String::Equals(const String* that) const {
1037 if (this == that) {
1038 // Quick reference equality test
1039 return true;
1040 } else if (that == NULL) {
1041 // Null isn't an instanceof anything
1042 return false;
1043 } else if (this->GetLength() != that->GetLength()) {
1044 // Quick length inequality test
1045 return false;
1046 } else {
1047 // NB don't short circuit on hash code as we're presumably here as the
1048 // hash code was already equal
1049 for (int32_t i = 0; i < that->GetLength(); ++i) {
1050 if (this->CharAt(i) != that->CharAt(i)) {
1051 return false;
1052 }
1053 }
1054 return true;
1055 }
1056}
1057
1058bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1059 int32_t that_length) const {
1060 if (this->GetLength() != that_length) {
1061 return false;
1062 } else {
1063 for (int32_t i = 0; i < that_length; ++i) {
1064 if (this->CharAt(i) != that_chars[that_offset + i]) {
1065 return false;
1066 }
1067 }
1068 return true;
1069 }
1070}
1071
1072bool String::Equals(const char* modified_utf8) const {
1073 for (int32_t i = 0; i < GetLength(); ++i) {
1074 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1075 if (ch == '\0' || ch != CharAt(i)) {
1076 return false;
1077 }
1078 }
1079 return *modified_utf8 == '\0';
1080}
1081
1082bool String::Equals(const StringPiece& modified_utf8) const {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001083 // TODO: do not assume C-string representation. For now DCHECK.
1084 DCHECK_EQ(modified_utf8.data()[modified_utf8.size()], 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001085 return Equals(modified_utf8.data());
1086}
1087
1088// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1089std::string String::ToModifiedUtf8() const {
1090 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1091 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1092 std::string result(byte_count, char(0));
1093 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1094 return result;
1095}
1096
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001097Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1098
1099void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1100 CHECK(java_lang_StackTraceElement_ == NULL);
1101 CHECK(java_lang_StackTraceElement != NULL);
1102 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1103}
1104
1105void StackTraceElement::ResetClass() {
1106 CHECK(java_lang_StackTraceElement_ != NULL);
1107 java_lang_StackTraceElement_ = NULL;
1108}
1109
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001110StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1111 const String* method_name,
1112 const String* file_name,
1113 int32_t line_number) {
1114 StackTraceElement* trace =
1115 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1116 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1117 const_cast<String*>(declaring_class), false);
1118 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1119 const_cast<String*>(method_name), false);
1120 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1121 const_cast<String*>(file_name), false);
1122 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1123 line_number, false);
1124 return trace;
1125}
1126
Elliott Hughes1f359b02011-07-17 14:27:17 -07001127static const char* kClassStatusNames[] = {
1128 "Error",
1129 "NotReady",
1130 "Idx",
1131 "Loaded",
1132 "Resolved",
1133 "Verifying",
1134 "Verified",
1135 "Initializing",
1136 "Initialized"
1137};
1138std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1139 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001140 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001141 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001142 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001143 }
1144 return os;
1145}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001146
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001147} // namespace art