blob: 63c92374f81e297bd7871bdf2f84481e15760ec2 [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"
Elliott Hughes54e7df12011-09-16 11:47:04 -070013#include "dex_cache.h"
14#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070016#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070017#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070019#include "monitor.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include "runtime.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070021
22namespace art {
23
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070024bool Object::IsString() const {
25 // TODO use "klass_ == String::GetJavaLangString()" instead?
26 return GetClass() == GetClass()->GetDescriptor()->GetClass();
27}
28
Elliott Hughes5f791332011-09-15 17:45:30 -070029uint32_t Object::GetLockOwner() {
30 return Monitor::GetLockOwner(monitor_);
31}
32
33void Object::MonitorEnter(Thread* thread) {
34 Monitor::MonitorEnter(thread, this);
35}
36
37void Object::MonitorExit(Thread* thread) {
38 Monitor::MonitorExit(thread, this);
39}
40
41void Object::Notify() {
42 Monitor::Notify(Thread::Current(), this);
43}
44
45void Object::NotifyAll() {
46 Monitor::NotifyAll(Thread::Current(), this);
47}
48
49void Object::Wait(int64_t ms, int32_t ns) {
50 Monitor::Wait(Thread::Current(), this, ms, ns, true);
51}
52
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070053// TODO: get global references for these
54Class* Field::java_lang_reflect_Field_ = NULL;
55
56void Field::SetClass(Class* java_lang_reflect_Field) {
57 CHECK(java_lang_reflect_Field_ == NULL);
58 CHECK(java_lang_reflect_Field != NULL);
59 java_lang_reflect_Field_ = java_lang_reflect_Field;
60}
61
62void Field::ResetClass() {
63 CHECK(java_lang_reflect_Field_ != NULL);
64 java_lang_reflect_Field_ = NULL;
65}
66
67void Field::SetTypeIdx(uint32_t type_idx) {
68 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), type_idx, false);
69}
70
71Class* Field::GetTypeDuringLinking() const {
72 // We are assured that the necessary primitive types are in the dex cache
73 // early during class linking
74 return GetDeclaringClass()->GetDexCache()->GetResolvedType(GetTypeIdx());
75}
76
77Class* Field::GetType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070078 // Do full linkage (which sets dex cache value to speed next call)
79 return Runtime::Current()->GetClassLinker()->ResolveType(GetTypeIdx(), this);
80}
81
buzbee34cd9e52011-09-08 14:31:52 -070082Field* Field::FindFieldFromCode(uint32_t field_idx, const Method* referrer) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070083 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
84 Field* f = class_linker->ResolveField(field_idx, referrer);
85 if (f != NULL) {
86 Class* c = f->GetDeclaringClass();
87 // If the class is already initializing, we must be inside <clinit>, or
88 // we'd still be waiting for the lock.
89 if (c->GetStatus() == Class::kStatusInitializing || class_linker->EnsureInitialized(c)) {
90 return f;
91 }
Brian Carlstromb63ec392011-08-27 17:38:27 -070092 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -070093 UNIMPLEMENTED(FATAL) << "throw an error and unwind";
94 return NULL;
95}
96
97uint32_t Field::Get32StaticFromCode(uint32_t field_idx, const Method* referrer) {
98 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070099 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t));
100 return field->Get32(NULL);
101}
102void Field::Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700103 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700104 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t));
105 field->Set32(NULL, new_value);
106}
107uint64_t Field::Get64StaticFromCode(uint32_t field_idx, const Method* referrer) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700108 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700109 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t));
110 return field->Get64(NULL);
111}
112void Field::Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700113 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700114 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t));
115 field->Set64(NULL, new_value);
116}
117Object* Field::GetObjStaticFromCode(uint32_t field_idx, const Method* referrer) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700118 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700119 DCHECK(!field->GetType()->IsPrimitive());
120 return field->GetObj(NULL);
121}
122void Field::SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700123 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700124 DCHECK(!field->GetType()->IsPrimitive());
125 field->SetObj(NULL, new_value);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700126}
127
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700128uint32_t Field::Get32(const Object* object) const {
129 CHECK((object == NULL) == IsStatic());
130 if (IsStatic()) {
131 object = declaring_class_;
132 }
133 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700134}
135
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700136void Field::Set32(Object* object, uint32_t new_value) const {
137 CHECK((object == NULL) == IsStatic());
138 if (IsStatic()) {
139 object = declaring_class_;
140 }
141 object->SetField32(GetOffset(), new_value, IsVolatile());
142}
143
144uint64_t Field::Get64(const Object* object) const {
145 CHECK((object == NULL) == IsStatic());
146 if (IsStatic()) {
147 object = declaring_class_;
148 }
149 return object->GetField64(GetOffset(), IsVolatile());
150}
151
152void Field::Set64(Object* object, uint64_t new_value) const {
153 CHECK((object == NULL) == IsStatic());
154 if (IsStatic()) {
155 object = declaring_class_;
156 }
157 object->SetField64(GetOffset(), new_value, IsVolatile());
158}
159
160Object* Field::GetObj(const Object* object) const {
161 CHECK((object == NULL) == IsStatic());
162 if (IsStatic()) {
163 object = declaring_class_;
164 }
165 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
166}
167
168void Field::SetObj(Object* object, const Object* new_value) const {
169 CHECK((object == NULL) == IsStatic());
170 if (IsStatic()) {
171 object = declaring_class_;
172 }
173 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
174}
175
176bool Field::GetBoolean(const Object* object) const {
177 DCHECK(GetType()->IsPrimitiveBoolean());
178 return Get32(object);
179}
180
181void Field::SetBoolean(Object* object, bool z) const {
182 DCHECK(GetType()->IsPrimitiveBoolean());
183 Set32(object, z);
184}
185
186int8_t Field::GetByte(const Object* object) const {
187 DCHECK(GetType()->IsPrimitiveByte());
188 return Get32(object);
189}
190
191void Field::SetByte(Object* object, int8_t b) const {
192 DCHECK(GetType()->IsPrimitiveByte());
193 Set32(object, b);
194}
195
196uint16_t Field::GetChar(const Object* object) const {
197 DCHECK(GetType()->IsPrimitiveChar());
198 return Get32(object);
199}
200
201void Field::SetChar(Object* object, uint16_t c) const {
202 DCHECK(GetType()->IsPrimitiveChar());
203 Set32(object, c);
204}
205
206uint16_t Field::GetShort(const Object* object) const {
207 DCHECK(GetType()->IsPrimitiveShort());
208 return Get32(object);
209}
210
211void Field::SetShort(Object* object, uint16_t s) const {
212 DCHECK(GetType()->IsPrimitiveShort());
213 Set32(object, s);
214}
215
216int32_t Field::GetInt(const Object* object) const {
217 DCHECK(GetType()->IsPrimitiveInt());
218 return Get32(object);
219}
220
221void Field::SetInt(Object* object, int32_t i) const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700222 DCHECK(GetType()->IsPrimitiveInt()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 Set32(object, i);
224}
225
226int64_t Field::GetLong(const Object* object) const {
227 DCHECK(GetType()->IsPrimitiveLong());
228 return Get64(object);
229}
230
231void Field::SetLong(Object* object, int64_t j) const {
232 DCHECK(GetType()->IsPrimitiveLong());
233 Set64(object, j);
234}
235
236float Field::GetFloat(const Object* object) const {
237 DCHECK(GetType()->IsPrimitiveFloat());
238 JValue float_bits;
239 float_bits.i = Get32(object);
240 return float_bits.f;
241}
242
243void Field::SetFloat(Object* object, float f) const {
244 DCHECK(GetType()->IsPrimitiveFloat());
245 JValue float_bits;
246 float_bits.f = f;
247 Set32(object, float_bits.i);
248}
249
250double Field::GetDouble(const Object* object) const {
251 DCHECK(GetType()->IsPrimitiveDouble());
252 JValue double_bits;
253 double_bits.j = Get64(object);
254 return double_bits.d;
255}
256
257void Field::SetDouble(Object* object, double d) const {
258 DCHECK(GetType()->IsPrimitiveDouble());
259 JValue double_bits;
260 double_bits.d = d;
261 Set64(object, double_bits.j);
262}
263
264Object* Field::GetObject(const Object* object) const {
265 CHECK(!GetType()->IsPrimitive());
266 return GetObj(object);
267}
268
269void Field::SetObject(Object* object, const Object* l) const {
270 CHECK(!GetType()->IsPrimitive());
271 SetObj(object, l);
272}
273
274// TODO: get global references for these
275Class* Method::java_lang_reflect_Method_ = NULL;
276
277void Method::SetClass(Class* java_lang_reflect_Method) {
278 CHECK(java_lang_reflect_Method_ == NULL);
279 CHECK(java_lang_reflect_Method != NULL);
280 java_lang_reflect_Method_ = java_lang_reflect_Method;
281}
282
283void Method::ResetClass() {
284 CHECK(java_lang_reflect_Method_ != NULL);
285 java_lang_reflect_Method_ = NULL;
286}
287
288ObjectArray<String>* Method::GetDexCacheStrings() const {
289 return GetFieldObject<ObjectArray<String>*>(
290 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
291}
292
293void Method::SetReturnTypeIdx(uint32_t new_return_type_idx) {
294 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
295 new_return_type_idx, false);
296}
297
298Class* Method::GetReturnType() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700299 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 // Short-cut
301 Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx());
302 if (result == NULL) {
303 // Do full linkage and set cache value for next call
304 result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this);
305 }
306 CHECK(result != NULL);
307 return result;
308}
309
310void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
311 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
312 new_dex_cache_strings, false);
313}
314
315ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
316 return GetFieldObject<ObjectArray<Class>*>(
317 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
318}
319
320void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
321 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
322 new_dex_cache_classes, false);
323}
324
325ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
326 return GetFieldObject<ObjectArray<Method>*>(
327 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
328}
329
330void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
331 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
332 new_dex_cache_methods, false);
333}
334
335ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
336 return GetFieldObject<ObjectArray<Field>*>(
337 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
338}
339
340void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
341 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
342 new_dex_cache_fields, false);
343}
344
345CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
346 return GetFieldPtr<CodeAndDirectMethods*>(
347 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
348 false);
349}
350
351void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
352 SetFieldPtr<CodeAndDirectMethods*>(
353 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
354 new_value, false);
355}
356
357ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
358 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
359 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
360 false);
361}
362
363void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
364 SetFieldObject(
365 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
366 new_value, false);
367
368}
369
370size_t Method::NumArgRegisters(const StringPiece& shorty) {
371 CHECK_LE(1, shorty.length());
372 uint32_t num_registers = 0;
373 for (int i = 1; i < shorty.length(); ++i) {
374 char ch = shorty[i];
375 if (ch == 'D' || ch == 'J') {
376 num_registers += 2;
377 } else {
378 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700379 }
380 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 return num_registers;
382}
383
384size_t Method::NumArgArrayBytes() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700385 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 size_t num_bytes = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700387 for (int i = 1; i < shorty->GetLength(); ++i) {
388 char ch = shorty->CharAt(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389 if (ch == 'D' || ch == 'J') {
390 num_bytes += 8;
391 } else if (ch == 'L') {
392 // Argument is a reference or an array. The shorty descriptor
393 // does not distinguish between these types.
394 num_bytes += sizeof(Object*);
395 } else {
396 num_bytes += 4;
397 }
398 }
399 return num_bytes;
400}
401
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700402size_t Method::NumArgs() const {
403 // "1 +" because the first in Args is the receiver.
404 // "- 1" because we don't count the return type.
405 return (IsStatic() ? 0 : 1) + GetShorty()->GetLength() - 1;
406}
407
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700408// The number of reference arguments to this method including implicit this
409// pointer
410size_t Method::NumReferenceArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700411 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700412 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700413 for (int i = 1; i < shorty->GetLength(); i++) {
414 char ch = shorty->CharAt(i);
415 if ((ch == 'L') || (ch == '[')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700416 result++;
417 }
418 }
419 return result;
420}
421
422// The number of long or double arguments
423size_t Method::NumLongOrDoubleArgs() const {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700424 const String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 size_t result = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700426 for (int i = 1; i < shorty->GetLength(); i++) {
427 char ch = shorty->CharAt(i);
428 if ((ch == 'D') || (ch == 'J')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700429 result++;
430 }
431 }
432 return result;
433}
434
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700435// Is the given method parameter a reference?
436bool Method::IsParamAReference(unsigned int param) const {
437 CHECK_LT(param, NumArgs());
438 if (IsStatic()) {
439 param++; // 0th argument must skip return value at start of the shorty
440 } else if (param == 0) {
441 return true; // this argument
442 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700443 return GetShorty()->CharAt(param) == 'L';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700444}
445
446// Is the given method parameter a long or double?
447bool Method::IsParamALongOrDouble(unsigned int param) const {
448 CHECK_LT(param, NumArgs());
449 if (IsStatic()) {
450 param++; // 0th argument must skip return value at start of the shorty
451 } else if (param == 0) {
452 return false; // this argument
453 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700454 char ch = GetShorty()->CharAt(param);
455 return (ch == 'J' || ch == 'D');
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700456}
457
458static size_t ShortyCharToSize(char x) {
459 switch (x) {
460 case 'V': return 0;
461 case '[': return kPointerSize;
462 case 'L': return kPointerSize;
463 case 'D': return 8;
464 case 'J': return 8;
465 default: return 4;
466 }
467}
468
469size_t Method::ParamSize(unsigned int param) const {
470 CHECK_LT(param, NumArgs());
471 if (IsStatic()) {
472 param++; // 0th argument must skip return value at start of the shorty
473 } else if (param == 0) {
474 return kPointerSize; // this argument
475 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700476 return ShortyCharToSize(GetShorty()->CharAt(param));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700477}
478
479size_t Method::ReturnSize() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700480 return ShortyCharToSize(GetShorty()->CharAt(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700481}
482
483bool Method::HasSameNameAndDescriptor(const Method* that) const {
484 return (this->GetName()->Equals(that->GetName()) &&
485 this->GetSignature()->Equals(that->GetSignature()));
486}
487
Ian Rogersbdb03912011-09-14 00:55:44 -0700488uint32_t Method::ToDexPC(const uintptr_t pc) const {
489 IntArray* mapping_table = GetMappingTable();
490 if (mapping_table == NULL) {
Ian Rogers67375ac2011-09-14 00:55:44 -0700491 DCHECK(IsNative());
492 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700493 }
494 size_t mapping_table_length = mapping_table->GetLength();
495 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetCode());
496 CHECK_LT(sought_offset, static_cast<uint32_t>(GetCodeArray()->GetLength()));
497 uint32_t best_offset = 0;
498 uint32_t best_dex_offset = 0;
499 for (size_t i = 0; i < mapping_table_length; i += 2) {
500 uint32_t map_offset = mapping_table->Get(i);
501 uint32_t map_dex_offset = mapping_table->Get(i + 1);
502 if (map_offset == sought_offset) {
503 best_offset = map_offset;
504 best_dex_offset = map_dex_offset;
505 break;
506 }
507 if (map_offset < sought_offset && map_offset > best_offset) {
508 best_offset = map_offset;
509 best_dex_offset = map_dex_offset;
510 }
511 }
512 return best_dex_offset;
513}
514
515uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
516 IntArray* mapping_table = GetMappingTable();
517 if (mapping_table == NULL) {
518 DCHECK(dex_pc == 0);
519 return 0; // Special no mapping/pc == 0 case
520 }
521 size_t mapping_table_length = mapping_table->GetLength();
522 for (size_t i = 0; i < mapping_table_length; i += 2) {
523 uint32_t map_offset = mapping_table->Get(i);
524 uint32_t map_dex_offset = mapping_table->Get(i + 1);
525 if (map_dex_offset == dex_pc) {
526 DCHECK_LT(map_offset, static_cast<uint32_t>(GetCodeArray()->GetLength()));
527 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
528 }
529 }
530 LOG(FATAL) << "Looking up Dex PC not contained in method";
531 return 0;
532}
533
534uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
535 DexCache* dex_cache = GetDeclaringClass()->GetDexCache();
536 const ClassLoader* class_loader = GetDeclaringClass()->GetClassLoader();
537 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
538 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
539 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(GetCodeItemOffset());
540 // Iterate over the catch handlers associated with dex_pc
541 for (DexFile::CatchHandlerIterator iter = dex_file.dexFindCatchHandler(*code_item, dex_pc);
542 !iter.HasNext(); iter.Next()) {
543 uint32_t iter_type_idx = iter.Get().type_idx_;
544 // Catch all case
545 if(iter_type_idx == DexFile::kDexNoIndex) {
546 return iter.Get().address_;
547 }
548 // Does this catch exception type apply?
549 Class* iter_exception_type =
550 class_linker->ResolveType(dex_file, iter_type_idx, dex_cache, class_loader);
551 if (iter_exception_type->IsAssignableFrom(exception_type)) {
552 return iter.Get().address_;
553 }
554 }
555 // Handler not found
556 return DexFile::kDexNoIndex;
557}
558
buzbee4ef76522011-09-08 10:00:32 -0700559void Method::SetCode(ByteArray* code_array, InstructionSet instruction_set,
Ian Rogersbdb03912011-09-14 00:55:44 -0700560 IntArray* mapping_table) {
Elliott Hughes1240dad2011-09-09 16:24:50 -0700561 CHECK(GetCode() == NULL || IsNative());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700562 SetFieldPtr<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), code_array, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700563 SetFieldPtr<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
buzbee4ef76522011-09-08 10:00:32 -0700564 mapping_table, false);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700565 int8_t* code = code_array->GetData();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700566 uintptr_t address = reinterpret_cast<uintptr_t>(code);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700567 if (instruction_set == kThumb2) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700568 // Set the low-order bit so a BLX will switch to Thumb mode
569 address |= 0x1;
570 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700571 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), reinterpret_cast<const void*>(address), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700572}
573
Ian Rogersbdb03912011-09-14 00:55:44 -0700574bool Method::IsWithinCode(uintptr_t pc) const {
575 if (GetCode() == NULL) {
576 return false;
577 }
578 if (pc == 0) {
579 // assume that this is some initial value that will always lie in code
580 return true;
581 } else {
Ian Rogers93dd9662011-09-17 23:21:22 -0700582#if defined(__arm__)
583 pc &= ~0x1; // clear any possible thumb instruction mode bit
584#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700585 uint32_t rel_offset = pc - reinterpret_cast<uintptr_t>(GetCodeArray()->GetData());
Ian Rogers93dd9662011-09-17 23:21:22 -0700586 // Strictly the following test should be a less-than, however, if the last
587 // instruction is a call to an exception throw we may see return addresses
588 // that are 1 beyond the end of code.
589 return rel_offset <= static_cast<uint32_t>(GetCodeArray()->GetLength());
Ian Rogersbdb03912011-09-14 00:55:44 -0700590 }
591}
592
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700593void Method::SetInvokeStub(const ByteArray* invoke_stub_array) {
594 const InvokeStub* invoke_stub = reinterpret_cast<InvokeStub*>(invoke_stub_array->GetData());
595 SetFieldPtr<const ByteArray*>(
596 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), invoke_stub_array, false);
597 SetFieldPtr<const InvokeStub*>(
598 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), invoke_stub, false);
599}
600
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700601void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
602 // Push a transition back into managed code onto the linked list in thread.
603 CHECK_EQ(Thread::kRunnable, self->GetState());
604 NativeToManagedRecord record;
605 self->PushNativeToManagedRecord(&record);
606
607 // Call the invoke stub associated with the method.
608 // Pass everything as arguments.
609 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700610
611 bool have_executable_code = (GetCode() != NULL);
612#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700613 // Currently we can only compile non-native methods for ARM.
614 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700615#endif
616
617 if (have_executable_code && stub != NULL) {
618 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700619 (*stub)(this, receiver, self, args, result);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700620 LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700621 } else {
622 LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
623 if (result != NULL) {
624 result->j = 0;
625 }
626 }
627
628 // Pop transition.
629 self->PopNativeToManagedRecord(record);
630}
631
Brian Carlstrom16192862011-09-12 17:50:06 -0700632bool Method::IsRegistered() {
633 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
634 void* jni_stub = Runtime::Current()->GetJniStubArray()->GetData();
635 return native_method != jni_stub;
636}
637
638void Method::RegisterNative(const void* native_method) {
639 CHECK(IsNative());
640 CHECK(native_method != NULL);
641 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
642 native_method, false);
643}
644
645void Method::UnregisterNative() {
646 CHECK(IsNative());
647 // restore stub to lookup native pointer via dlsym
648 RegisterNative(Runtime::Current()->GetJniStubArray()->GetData());
649}
650
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651void Class::SetStatus(Status new_status) {
652 CHECK(new_status > GetStatus() || new_status == kStatusError ||
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700653 !Runtime::Current()->IsStarted()) << GetDescriptor()->ToModifiedUtf8();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700654 CHECK(sizeof(Status) == sizeof(uint32_t));
655 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_),
656 new_status, false);
657}
658
659DexCache* Class::GetDexCache() const {
660 return GetFieldObject<DexCache*>(
661 OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
662}
663
664void Class::SetDexCache(DexCache* new_dex_cache) {
665 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_),
666 new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700667}
668
Brian Carlstrom1f870082011-08-23 16:02:11 -0700669Object* Class::AllocObjectFromCode(uint32_t type_idx, Method* method) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700670 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700671 if (klass == NULL) {
672 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
673 if (klass == NULL) {
674 UNIMPLEMENTED(FATAL) << "throw an error";
675 return NULL;
676 }
677 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700678 return klass->AllocObject();
679}
680
681Object* Class::AllocObject() {
682 DCHECK(!IsAbstract());
683 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700684}
685
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700686void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
687 if (new_reference_offsets != CLASS_WALK_SUPER) {
688 // Sanity check that the number of bits set in the reference offset bitmap
689 // agrees with the number of references
690 Class* cur = this;
691 size_t cnt = 0;
692 while (cur) {
693 cnt += cur->NumReferenceInstanceFieldsDuringLinking();
694 cur = cur->GetSuperClass();
695 }
696 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt);
697 }
698 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
699 new_reference_offsets, false);
700}
701
702void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
703 if (new_reference_offsets != CLASS_WALK_SUPER) {
704 // Sanity check that the number of bits set in the reference offset bitmap
705 // agrees with the number of references
706 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
707 NumReferenceStaticFieldsDuringLinking());
708 }
709 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
710 new_reference_offsets, false);
711}
712
713size_t Class::PrimitiveSize() const {
714 switch (GetPrimitiveType()) {
715 case kPrimBoolean:
716 case kPrimByte:
717 case kPrimChar:
718 case kPrimShort:
719 case kPrimInt:
720 case kPrimFloat:
721 return sizeof(int32_t);
722 case kPrimLong:
723 case kPrimDouble:
724 return sizeof(int64_t);
725 default:
726 LOG(FATAL) << "Primitive type size calculation on invalid type " << this;
727 return 0;
728 }
729}
730
731size_t Class::GetTypeSize(const String* descriptor) {
732 switch (descriptor->CharAt(0)) {
733 case 'B': return 1; // byte
734 case 'C': return 2; // char
735 case 'D': return 8; // double
736 case 'F': return 4; // float
737 case 'I': return 4; // int
738 case 'J': return 8; // long
739 case 'S': return 2; // short
740 case 'Z': return 1; // boolean
741 case 'L': return sizeof(Object*);
742 case '[': return sizeof(Array*);
743 default:
744 LOG(ERROR) << "Unknown type " << descriptor;
745 return 0;
746 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700747}
748
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700749bool Class::Implements(const Class* klass) const {
750 DCHECK(klass != NULL);
751 DCHECK(klass->IsInterface());
752 // All interfaces implemented directly and by our superclass, and
753 // recursively all super-interfaces of those interfaces, are listed
754 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700755 int32_t iftable_count = GetIfTableCount();
756 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
757 for (int32_t i = 0; i < iftable_count; i++) {
758 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700759 return true;
760 }
761 }
762 return false;
763}
764
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700765bool Class::CanPutArrayElement(const Class* object_class, const Class* array_class) {
766 if (object_class->IsArrayClass()) {
767 return array_class->IsArrayAssignableFromArray(object_class);
768 } else {
769 return array_class->GetComponentType()->IsAssignableFrom(object_class);
770 }
771}
772
buzbee9a195c92011-09-16 13:26:02 -0700773void Class::CanPutArrayElementFromCode(const Object* element, const Class* array_class) {
774 if (element == NULL) {
775 return;
776 }
777 if (!CanPutArrayElement(element->GetClass(), array_class)) {
778 LOG(ERROR) << "Can't put a " << PrettyClass(element->GetClass())
Elliott Hughes54e7df12011-09-16 11:47:04 -0700779 << " into a " << PrettyClass(array_class);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700780 UNIMPLEMENTED(FATAL) << "need to throw ArrayStoreException and unwind stack";
781 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700782}
783
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700784// Determine whether "this" is assignable from "klazz", where both of these
785// are array classes.
786//
787// Consider an array class, e.g. Y[][], where Y is a subclass of X.
788// Y[][] = Y[][] --> true (identity)
789// X[][] = Y[][] --> true (element superclass)
790// Y = Y[][] --> false
791// Y[] = Y[][] --> false
792// Object = Y[][] --> true (everything is an object)
793// Object[] = Y[][] --> true
794// Object[][] = Y[][] --> true
795// Object[][][] = Y[][] --> false (too many []s)
796// Serializable = Y[][] --> true (all arrays are Serializable)
797// Serializable[] = Y[][] --> true
798// Serializable[][] = Y[][] --> false (unless Y is Serializable)
799//
800// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700801// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700802//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700803bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700804 DCHECK(IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700805 DCHECK(src->IsArrayClass());
806 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700807}
808
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700809bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700810 DCHECK(!IsInterface()); // handled first in IsAssignableFrom
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700811 DCHECK(src->IsArrayClass());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700812 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700813 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700814 // src's super should be java_lang_Object, since it is an array.
815 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700816 DCHECK(java_lang_Object != NULL);
817 DCHECK(java_lang_Object->GetSuperClass() == NULL);
818 return this == java_lang_Object;
819 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700820 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700821}
822
823bool Class::IsSubClass(const Class* klass) const {
824 DCHECK(!IsInterface());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700825 DCHECK(!IsArrayClass());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700826 const Class* current = this;
827 do {
828 if (current == klass) {
829 return true;
830 }
831 current = current->GetSuperClass();
832 } while (current != NULL);
833 return false;
834}
835
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700836bool Class::IsInSamePackage(const String* descriptor_string_1,
837 const String* descriptor_string_2) {
838 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
839 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
840
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700841 size_t i = 0;
842 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
843 ++i;
844 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700845 if (descriptor1.find('/', i) != StringPiece::npos ||
846 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700847 return false;
848 } else {
849 return true;
850 }
851}
852
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700853#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700854bool Class::IsInSamePackage(const StringPiece& descriptor1,
855 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700856 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700857 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700858 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
859 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700860 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
861}
862#endif
863
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700864bool Class::IsInSamePackage(const Class* that) const {
865 const Class* klass1 = this;
866 const Class* klass2 = that;
867 if (klass1 == klass2) {
868 return true;
869 }
870 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700871 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700872 return false;
873 }
874 // Arrays are in the same package when their element classes are.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700875 if (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700876 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700877 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700878 if (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700879 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700880 }
881 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700882 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700883}
884
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700885const ClassLoader* Class::GetClassLoader() const {
886 return GetFieldObject<const ClassLoader*>(
887 OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700888}
889
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700890void Class::SetClassLoader(const ClassLoader* new_cl) {
891 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
892 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_),
893 new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700894}
895
Brian Carlstrom30b94452011-08-25 21:35:26 -0700896Method* Class::FindVirtualMethodForInterface(Method* method) {
897 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700898 DCHECK(declaring_class != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700899 DCHECK(declaring_class->IsInterface());
900 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700901 int32_t iftable_count = GetIfTableCount();
902 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
903 for (int32_t i = 0; i < iftable_count; i++) {
904 InterfaceEntry* interface_entry = iftable->Get(i);
905 if (interface_entry->GetInterface() == declaring_class) {
906 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700907 }
908 }
Brian Carlstrom16192862011-09-12 17:50:06 -0700909 UNIMPLEMENTED(FATAL) << "Need to throw an error of some kind " << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700910 return NULL;
911}
912
jeffhaobdb76512011-09-07 11:43:16 -0700913Method* Class::FindInterfaceMethod(const StringPiece& name,
914 const StringPiece& signature) {
915 // Check the current class before checking the interfaces.
916 Method* method = FindVirtualMethod(name, signature);
917 if (method != NULL) {
918 return method;
919 }
920
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700921 int32_t iftable_count = GetIfTableCount();
922 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
923 for (int32_t i = 0; i < iftable_count; i++) {
924 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700925 if (method != NULL) {
926 return method;
927 }
928 }
929 return NULL;
930}
931
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700932Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700933 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700934 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700935 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700936 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700937 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700938 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700939 }
940 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700941 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700942}
943
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700944Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700945 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700946 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700947 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700948 if (method != NULL) {
949 return method;
950 }
951 }
952 return NULL;
953}
954
955Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700956 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700957 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700958 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700959 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700960 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700961 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700962 }
963 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700964 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700965}
966
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700967Method* Class::FindVirtualMethod(const StringPiece& name,
968 const StringPiece& descriptor) {
969 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
970 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
971 if (method != NULL) {
972 return method;
973 }
974 }
975 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700976}
977
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700978Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 // Is the field in this class?
980 // Interfaces are not relevant because they can't contain instance fields.
981 for (size_t i = 0; i < NumInstanceFields(); ++i) {
982 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700983 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 return f;
985 }
986 }
987 return NULL;
988}
989
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700990Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 // Is the field in this class, or any of its superclasses?
992 // Interfaces are not relevant because they can't contain instance fields.
993 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700994 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 if (f != NULL) {
996 return f;
997 }
998 }
999 return NULL;
1000}
1001
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001002Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
1003 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 for (size_t i = 0; i < NumStaticFields(); ++i) {
1005 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001006 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 return f;
1008 }
1009 }
1010 return NULL;
1011}
1012
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001013Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 // Is the field in this class (or its interfaces), or any of its
1015 // superclasses (or their interfaces)?
1016 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1017 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001018 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 if (f != NULL) {
1020 return f;
1021 }
1022
1023 // Is this field in any of this class' interfaces?
1024 for (size_t i = 0; i < c->NumInterfaces(); ++i) {
1025 Class* interface = c->GetInterface(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001026 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 if (f != NULL) {
1028 return f;
1029 }
1030 }
1031 }
1032 return NULL;
1033}
1034
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001035Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001036 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001037 DCHECK_GE(component_count, 0);
1038 DCHECK(array_class->IsArrayClass());
1039 size_t size = SizeOf(component_count, component_size);
1040 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1041 if (array != NULL) {
1042 DCHECK(array->IsArrayInstance());
1043 array->SetLength(component_count);
1044 }
1045 return array;
1046}
1047
1048Array* Array::Alloc(Class* array_class, int32_t component_count) {
1049 return Alloc(array_class, component_count, array_class->GetComponentSize());
1050}
1051
1052Array* Array::AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
1053 // TODO: throw on negative component_count
1054 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
1055 if (klass == NULL) {
1056 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
1057 if (klass == NULL || !klass->IsArrayClass()) {
1058 UNIMPLEMENTED(FATAL) << "throw an error";
1059 return NULL;
1060 }
1061 }
1062 return Array::Alloc(klass, component_count);
1063}
1064
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001065template<typename T>
1066PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001067 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001068 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1069 return down_cast<PrimitiveArray<T>*>(raw_array);
1070}
1071
1072template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1073
1074// Explicitly instantiate all the primitive array types.
1075template class PrimitiveArray<uint8_t>; // BooleanArray
1076template class PrimitiveArray<int8_t>; // ByteArray
1077template class PrimitiveArray<uint16_t>; // CharArray
1078template class PrimitiveArray<double>; // DoubleArray
1079template class PrimitiveArray<float>; // FloatArray
1080template class PrimitiveArray<int32_t>; // IntArray
1081template class PrimitiveArray<int64_t>; // LongArray
1082template class PrimitiveArray<int16_t>; // ShortArray
1083
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001084// TODO: get global references for these
1085Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001086
Brian Carlstroma663ea52011-08-19 23:33:41 -07001087void String::SetClass(Class* java_lang_String) {
1088 CHECK(java_lang_String_ == NULL);
1089 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001090 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001091}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001092
Brian Carlstroma663ea52011-08-19 23:33:41 -07001093void String::ResetClass() {
1094 CHECK(java_lang_String_ != NULL);
1095 java_lang_String_ = NULL;
1096}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001097
Brian Carlstromc74255f2011-09-11 22:47:39 -07001098String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001099 return Runtime::Current()->GetInternTable()->InternWeak(this);
1100}
1101
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001102int32_t String::GetHashCode() const {
1103 int32_t result = GetField32(
1104 OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1105 DCHECK(result != 0 ||
1106 ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0);
1107 return result;
1108}
1109
1110int32_t String::GetLength() const {
1111 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1112 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1113 return result;
1114}
1115
1116uint16_t String::CharAt(int32_t index) const {
1117 // TODO: do we need this? Equals is the only caller, and could
1118 // bounds check itself.
1119 if (index < 0 || index >= count_) {
1120 Thread* self = Thread::Current();
1121 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1122 "length=%i; index=%i", count_, index);
1123 return 0;
1124 }
1125 return GetCharArray()->Get(index + GetOffset());
1126}
1127
1128String* String::AllocFromUtf16(int32_t utf16_length,
1129 const uint16_t* utf16_data_in,
1130 int32_t hash_code) {
1131 String* string = Alloc(GetJavaLangString(), utf16_length);
1132 // TODO: use 16-bit wide memset variant
1133 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
1134 for (int i = 0; i < utf16_length; i++) {
1135 array->Set(i, utf16_data_in[i]);
1136 }
1137 if (hash_code != 0) {
1138 string->SetHashCode(hash_code);
1139 } else {
1140 string->ComputeHashCode();
1141 }
1142 return string;
1143}
1144
1145String* String::AllocFromModifiedUtf8(const char* utf) {
1146 size_t char_count = CountModifiedUtf8Chars(utf);
1147 return AllocFromModifiedUtf8(char_count, utf);
1148}
1149
1150String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1151 const char* utf8_data_in) {
1152 String* string = Alloc(GetJavaLangString(), utf16_length);
1153 uint16_t* utf16_data_out =
1154 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1155 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1156 string->ComputeHashCode();
1157 return string;
1158}
1159
1160String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
1161 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1162}
1163
1164String* String::Alloc(Class* java_lang_String, CharArray* array) {
1165 String* string = down_cast<String*>(java_lang_String->AllocObject());
1166 string->SetArray(array);
1167 string->SetCount(array->GetLength());
1168 return string;
1169}
1170
1171bool String::Equals(const String* that) const {
1172 if (this == that) {
1173 // Quick reference equality test
1174 return true;
1175 } else if (that == NULL) {
1176 // Null isn't an instanceof anything
1177 return false;
1178 } else if (this->GetLength() != that->GetLength()) {
1179 // Quick length inequality test
1180 return false;
1181 } else {
1182 // NB don't short circuit on hash code as we're presumably here as the
1183 // hash code was already equal
1184 for (int32_t i = 0; i < that->GetLength(); ++i) {
1185 if (this->CharAt(i) != that->CharAt(i)) {
1186 return false;
1187 }
1188 }
1189 return true;
1190 }
1191}
1192
1193bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1194 int32_t that_length) const {
1195 if (this->GetLength() != that_length) {
1196 return false;
1197 } else {
1198 for (int32_t i = 0; i < that_length; ++i) {
1199 if (this->CharAt(i) != that_chars[that_offset + i]) {
1200 return false;
1201 }
1202 }
1203 return true;
1204 }
1205}
1206
1207bool String::Equals(const char* modified_utf8) const {
1208 for (int32_t i = 0; i < GetLength(); ++i) {
1209 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1210 if (ch == '\0' || ch != CharAt(i)) {
1211 return false;
1212 }
1213 }
1214 return *modified_utf8 == '\0';
1215}
1216
1217bool String::Equals(const StringPiece& modified_utf8) const {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001218 // TODO: do not assume C-string representation. For now DCHECK.
1219 DCHECK_EQ(modified_utf8.data()[modified_utf8.size()], 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001220 return Equals(modified_utf8.data());
1221}
1222
1223// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1224std::string String::ToModifiedUtf8() const {
1225 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1226 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1227 std::string result(byte_count, char(0));
1228 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1229 return result;
1230}
1231
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001232Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1233
1234void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1235 CHECK(java_lang_StackTraceElement_ == NULL);
1236 CHECK(java_lang_StackTraceElement != NULL);
1237 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1238}
1239
1240void StackTraceElement::ResetClass() {
1241 CHECK(java_lang_StackTraceElement_ != NULL);
1242 java_lang_StackTraceElement_ = NULL;
1243}
1244
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001245StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1246 const String* method_name,
1247 const String* file_name,
1248 int32_t line_number) {
1249 StackTraceElement* trace =
1250 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1251 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1252 const_cast<String*>(declaring_class), false);
1253 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1254 const_cast<String*>(method_name), false);
1255 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1256 const_cast<String*>(file_name), false);
1257 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1258 line_number, false);
1259 return trace;
1260}
1261
Elliott Hughes1f359b02011-07-17 14:27:17 -07001262static const char* kClassStatusNames[] = {
1263 "Error",
1264 "NotReady",
1265 "Idx",
1266 "Loaded",
1267 "Resolved",
1268 "Verifying",
1269 "Verified",
1270 "Initializing",
1271 "Initialized"
1272};
1273std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1274 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001275 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001276 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001277 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001278 }
1279 return os;
1280}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001281
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001282} // namespace art