blob: 3aba5c891b5b3c90c4443b13e29557d3358540f2 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro3ee755d2011-06-28 12:11:04 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020
Ian Rogersdf20fe02011-07-20 20:34:16 -070021#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include <string>
24#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070028#include "dex_cache.h"
29#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070034#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070037#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070038#include "utils.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070039
Logan Chienfca7e872011-12-20 20:08:22 +080040#if defined(ART_USE_LLVM_COMPILER)
41#include "compiler_llvm/inferred_reg_category_map.h"
42using art::compiler_llvm::InferredRegCategoryMap;
43#endif
44
Carl Shapiro3ee755d2011-06-28 12:11:04 -070045namespace art {
46
Elliott Hughesdbb40792011-11-18 17:05:22 -080047String* Object::AsString() {
48 DCHECK(GetClass()->IsStringClass());
49 return down_cast<String*>(this);
50}
51
Elliott Hughes081be7f2011-09-18 16:50:26 -070052Object* Object::Clone() {
53 Class* c = GetClass();
54 DCHECK(!c->IsClassClass());
55
56 // Object::SizeOf gets the right size even if we're an array.
57 // Using c->AllocObject() here would be wrong.
58 size_t num_bytes = SizeOf();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070059 SirtRef<Object> copy(Heap::AllocObject(c, num_bytes));
60 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070061 return NULL;
62 }
63
64 // Copy instance data. We assume memcpy copies by words.
65 // TODO: expose and use move32.
66 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070067 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070068 size_t offset = sizeof(Object);
69 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
70
Elliott Hughes20cde902011-10-04 17:37:27 -070071 if (c->IsFinalizable()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -070072 Heap::AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070073 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070074
Brian Carlstrom40381fb2011-10-19 14:13:40 -070075 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070076}
77
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070078uint32_t Object::GetThinLockId() {
79 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070080}
81
82void Object::MonitorEnter(Thread* thread) {
83 Monitor::MonitorEnter(thread, this);
84}
85
Ian Rogersff1ed472011-09-20 13:46:24 -070086bool Object::MonitorExit(Thread* thread) {
87 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070088}
89
90void Object::Notify() {
91 Monitor::Notify(Thread::Current(), this);
92}
93
94void Object::NotifyAll() {
95 Monitor::NotifyAll(Thread::Current(), this);
96}
97
98void Object::Wait(int64_t ms, int32_t ns) {
99 Monitor::Wait(Thread::Current(), this, ms, ns, true);
100}
101
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700102// TODO: get global references for these
103Class* Field::java_lang_reflect_Field_ = NULL;
104
105void Field::SetClass(Class* java_lang_reflect_Field) {
106 CHECK(java_lang_reflect_Field_ == NULL);
107 CHECK(java_lang_reflect_Field != NULL);
108 java_lang_reflect_Field_ = java_lang_reflect_Field;
109}
110
111void Field::ResetClass() {
112 CHECK(java_lang_reflect_Field_ != NULL);
113 java_lang_reflect_Field_ = NULL;
114}
115
Ian Rogers0571d352011-11-03 19:51:38 -0700116void Field::SetOffset(MemberOffset num_bytes) {
117 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800118#if 0 // TODO enable later in boot and under !NDEBUG
119 FieldHelper fh(this);
120 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700121 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
122 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
123 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800124#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700125 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
126}
127
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700128uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700129 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130 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 {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700137 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700138 if (IsStatic()) {
139 object = declaring_class_;
140 }
141 object->SetField32(GetOffset(), new_value, IsVolatile());
142}
143
144uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700146 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 {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700153 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154 if (IsStatic()) {
155 object = declaring_class_;
156 }
157 object->SetField64(GetOffset(), new_value, IsVolatile());
158}
159
160Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700162 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 {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700169 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700170 if (IsStatic()) {
171 object = declaring_class_;
172 }
173 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
174}
175
176bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800177 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
178 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700179 return Get32(object);
180}
181
182void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800183 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
184 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700185 Set32(object, z);
186}
187
188int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800189 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
190 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191 return Get32(object);
192}
193
194void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800195 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
196 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700197 Set32(object, b);
198}
199
200uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800201 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
202 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700203 return Get32(object);
204}
205
206void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
208 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 Set32(object, c);
210}
211
Ian Rogers466bb252011-10-14 03:29:56 -0700212int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800213 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
214 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 return Get32(object);
216}
217
Ian Rogers466bb252011-10-14 03:29:56 -0700218void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800219 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
220 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700221 Set32(object, s);
222}
223
224int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
226 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 return Get32(object);
228}
229
230void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800231 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
232 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700233 Set32(object, i);
234}
235
236int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800237 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
238 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239 return Get64(object);
240}
241
242void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800243 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
244 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700245 Set64(object, j);
246}
247
248float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800249 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
250 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700251 JValue float_bits;
252 float_bits.i = Get32(object);
253 return float_bits.f;
254}
255
256void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800257 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
258 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259 JValue float_bits;
260 float_bits.f = f;
261 Set32(object, float_bits.i);
262}
263
264double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
266 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 JValue double_bits;
268 double_bits.j = Get64(object);
269 return double_bits.d;
270}
271
272void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
274 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 JValue double_bits;
276 double_bits.d = d;
277 Set64(object, double_bits.j);
278}
279
280Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
282 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 return GetObj(object);
284}
285
286void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800287 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
288 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 SetObj(object, l);
290}
291
292// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700293Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294Class* Method::java_lang_reflect_Method_ = NULL;
295
Elliott Hughes80609252011-09-23 17:24:51 -0700296void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
297 CHECK(java_lang_reflect_Constructor_ == NULL);
298 CHECK(java_lang_reflect_Constructor != NULL);
299 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
300
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 CHECK(java_lang_reflect_Method_ == NULL);
302 CHECK(java_lang_reflect_Method != NULL);
303 java_lang_reflect_Method_ = java_lang_reflect_Method;
304}
305
Elliott Hughes80609252011-09-23 17:24:51 -0700306void Method::ResetClasses() {
307 CHECK(java_lang_reflect_Constructor_ != NULL);
308 java_lang_reflect_Constructor_ = NULL;
309
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700310 CHECK(java_lang_reflect_Method_ != NULL);
311 java_lang_reflect_Method_ = NULL;
312}
313
Elliott Hughes418d20f2011-09-22 14:00:39 -0700314Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
315 if (*p == '[') {
316 // Something like "[[[Ljava/lang/String;".
317 const char* start = p;
318 while (*p == '[') {
319 ++p;
320 }
321 if (*p == 'L') {
322 while (*p != ';') {
323 ++p;
324 }
325 }
326 ++p; // Either the ';' or the primitive type.
327
Brian Carlstromaded5f72011-10-07 17:15:04 -0700328 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800329 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700330 } else if (*p == 'L') {
331 const char* start = p;
332 while (*p != ';') {
333 ++p;
334 }
335 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800336 std::string descriptor(start, (p - start));
337 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700338 } else {
339 return class_linker->FindPrimitiveClass(*p++);
340 }
341}
342
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343ObjectArray<String>* Method::GetDexCacheStrings() const {
344 return GetFieldObject<ObjectArray<String>*>(
345 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
346}
347
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
349 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
350 new_dex_cache_strings, false);
351}
352
353ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
354 return GetFieldObject<ObjectArray<Class>*>(
355 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
356}
357
358void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
359 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
360 new_dex_cache_classes, false);
361}
362
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
364 return GetFieldPtr<CodeAndDirectMethods*>(
365 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
366 false);
367}
368
369void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
370 SetFieldPtr<CodeAndDirectMethods*>(
371 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
372 new_value, false);
373}
374
375ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
376 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
377 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
378 false);
379}
380
381void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700382 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384}
385
Logan Chienfca7e872011-12-20 20:08:22 +0800386#if defined(ART_USE_LLVM_COMPILER)
387
388const InferredRegCategoryMap* Method::GetInferredRegCategoryMap() const {
389 const InferredRegCategoryMap* map = GetFieldPtr<const InferredRegCategoryMap*>(
390 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
391 DCHECK(map != NULL) << PrettyMethod(this);
392 return map;
393}
394
395void Method::SetInferredRegCategoryMap(const InferredRegCategoryMap* map) {
Logan Chiena1854662012-02-17 09:52:18 +0800396 const InferredRegCategoryMap* existing_map = GetFieldPtr<const InferredRegCategoryMap*>(
397 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
Logan Chienfca7e872011-12-20 20:08:22 +0800398
399 DCHECK(existing_map == NULL) << PrettyMethod(this);
400 DCHECK(map != NULL) << PrettyMethod(this);
401
402 // TODO: Remove if we won't find any use of InferredRegCategoryMap at runtime.
403 SetFieldPtr<const InferredRegCategoryMap*>(
404 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), map, false);
405}
406
407void Method::ResetInferredRegCategoryMap() {
408 delete GetInferredRegCategoryMap();
409 SetFieldPtr<const InferredRegCategoryMap*>(
Logan Chiena1854662012-02-17 09:52:18 +0800410 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), NULL, false);
Logan Chienfca7e872011-12-20 20:08:22 +0800411}
412
413#endif
414
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700415size_t Method::NumArgRegisters(const StringPiece& shorty) {
416 CHECK_LE(1, shorty.length());
417 uint32_t num_registers = 0;
418 for (int i = 1; i < shorty.length(); ++i) {
419 char ch = shorty[i];
420 if (ch == 'D' || ch == 'J') {
421 num_registers += 2;
422 } else {
423 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700424 }
425 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 return num_registers;
427}
428
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429bool Method::IsProxyMethod() const {
430 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700431}
432
Ian Rogers466bb252011-10-14 03:29:56 -0700433Method* Method::FindOverriddenMethod() const {
434 if (IsStatic()) {
435 return NULL;
436 }
437 Class* declaring_class = GetDeclaringClass();
438 Class* super_class = declaring_class->GetSuperClass();
439 uint16_t method_index = GetMethodIndex();
440 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
441 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 // Did this method override a super class method? If so load the result from the super class'
443 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700444 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
445 result = super_class_vtable->Get(method_index);
446 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800447 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800448 if (IsProxyMethod()) {
449 result = Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this);
450 } else {
451 MethodHelper mh(this);
452 MethodHelper interface_mh;
453 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
454 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
455 InterfaceEntry* entry = iftable->Get(i);
456 Class* interface = entry->GetInterface();
457 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
458 Method* interface_method = interface->GetVirtualMethod(j);
459 interface_mh.ChangeMethod(interface_method);
460 if (mh.HasSameNameAndSignature(&interface_mh)) {
461 result = interface_method;
462 break;
463 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800464 }
465 }
Ian Rogers466bb252011-10-14 03:29:56 -0700466 }
467 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800468#ifndef NDEBUG
469 MethodHelper result_mh(result);
470 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
471#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700472 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700473}
474
Ian Rogersbdb03912011-09-14 00:55:44 -0700475uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700476 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700477 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800478 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700479 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700480 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700481 size_t mapping_table_length = GetMappingTableLength();
jeffhao26c0a1a2012-01-17 16:28:33 -0800482 const void* code_offset;
483 if (Runtime::Current()->IsMethodTracingActive() &&
484 Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this) != NULL) {
485 code_offset = Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this);
486 } else {
487 code_offset = GetCode();
488 }
jeffhaoe343b762011-12-05 16:36:44 -0800489 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code_offset);
Ian Rogersbdb03912011-09-14 00:55:44 -0700490 uint32_t best_offset = 0;
491 uint32_t best_dex_offset = 0;
492 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700493 uint32_t map_offset = mapping_table[i];
494 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700495 if (map_offset == sought_offset) {
496 best_offset = map_offset;
497 best_dex_offset = map_dex_offset;
498 break;
499 }
500 if (map_offset < sought_offset && map_offset > best_offset) {
501 best_offset = map_offset;
502 best_dex_offset = map_dex_offset;
503 }
504 }
505 return best_dex_offset;
506}
507
508uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700509 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700510 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700511 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700512 return 0; // Special no mapping/pc == 0 case
513 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700514 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700515 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700516 uint32_t map_offset = mapping_table[i];
517 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700518 if (map_dex_offset == dex_pc) {
jeffhao2692b572011-12-16 15:42:28 -0800519 if (Runtime::Current()->IsMethodTracingActive()) {
520 Trace* tracer = Runtime::Current()->GetTracer();
521 return reinterpret_cast<uintptr_t>(tracer->GetSavedCodeFromMap(this)) + map_offset;
jeffhaoe343b762011-12-05 16:36:44 -0800522 } else {
523 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
524 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700525 }
526 }
527 LOG(FATAL) << "Looking up Dex PC not contained in method";
528 return 0;
529}
530
531uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800532 MethodHelper mh(this);
533 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700534 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700535 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
536 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700537 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700538 if (iter_type_idx == DexFile::kDexNoIndex16) {
539 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700540 }
541 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800542 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700543 if (iter_exception_type == NULL) {
544 // The verifier should take care of resolving all exception classes early
545 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800546 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700547 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700548 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700549 }
550 }
551 // Handler not found
552 return DexFile::kDexNoIndex;
553}
554
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700555void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
556 // Push a transition back into managed code onto the linked list in thread.
557 CHECK_EQ(Thread::kRunnable, self->GetState());
558 NativeToManagedRecord record;
559 self->PushNativeToManagedRecord(&record);
560
561 // Call the invoke stub associated with the method.
562 // Pass everything as arguments.
563 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700564
565 bool have_executable_code = (GetCode() != NULL);
566#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700567 // Currently we can only compile non-native methods for ARM.
568 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700569#endif
570
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500571 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700572 bool log = false;
573 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800574 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
575 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700576 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700577 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700578 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800579 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
580 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700581 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700582 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800583 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
584 PrettyMethod(this).c_str(), GetCode(), stub,
585 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700586 if (result != NULL) {
587 result->j = 0;
588 }
589 }
590
591 // Pop transition.
592 self->PopNativeToManagedRecord(record);
593}
594
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700595bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700596 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers169c9a72011-11-13 20:13:17 -0800597 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700598 return native_method != jni_stub;
599}
600
601void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700602 CHECK(IsNative()) << PrettyMethod(this);
603 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700604 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
605 native_method, false);
606}
607
608void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700609 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700610 // restore stub to lookup native pointer via dlsym
Ian Rogers169c9a72011-11-13 20:13:17 -0800611 RegisterNative(Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700612}
613
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700614void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700615 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
616 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700617 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800618 if (new_status == kStatusError) {
619 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
620
621 // stash current exception
622 Thread* self = Thread::Current();
623 SirtRef<Throwable> exception(self->GetException());
624 CHECK(exception.get() != NULL);
625
626 // clear exception to call FindSystemClass
627 self->ClearException();
628 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
629 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
630 CHECK(!self->IsExceptionPending());
631
632 // only verification errors, not initialization problems, should set a verify error.
633 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
634 Class* exception_class = exception->GetClass();
635 if (!eiie_class->IsAssignableFrom(exception_class)) {
636 SetVerifyErrorClass(exception_class);
637 }
638
639 // restore exception
640 self->SetException(exception.get());
641 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700642 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700643}
644
645DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700646 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647}
648
649void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700650 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700651}
652
Brian Carlstrom1f870082011-08-23 16:02:11 -0700653Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700654 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700655 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500656 // TODO: decide whether we want this check. It currently fails during bootstrap.
657 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700658 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700659 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700660}
661
Ian Rogers0571d352011-11-03 19:51:38 -0700662void Class::SetClassSize(size_t new_class_size) {
663 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
664 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
665}
666
Ian Rogersd418eda2012-01-30 12:14:28 -0800667// Return the class' name. The exact format is bizarre, but it's the specified behavior for
668// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
669// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
670// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
671String* Class::ComputeName() {
672 String* name = GetName();
673 if (name != NULL) {
674 return name;
675 }
676 std::string descriptor(ClassHelper(this).GetDescriptor());
677 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
678 // The descriptor indicates that this is the class for
679 // a primitive type; special-case the return value.
680 const char* c_name = NULL;
681 switch (descriptor[0]) {
682 case 'Z': c_name = "boolean"; break;
683 case 'B': c_name = "byte"; break;
684 case 'C': c_name = "char"; break;
685 case 'S': c_name = "short"; break;
686 case 'I': c_name = "int"; break;
687 case 'J': c_name = "long"; break;
688 case 'F': c_name = "float"; break;
689 case 'D': c_name = "double"; break;
690 case 'V': c_name = "void"; break;
691 default:
692 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
693 }
694 name = String::AllocFromModifiedUtf8(c_name);
695 } else {
696 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
697 // components.
698 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
699 descriptor.erase(0, 1);
700 descriptor.erase(descriptor.size() - 1);
701 }
702 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
703 name = String::AllocFromModifiedUtf8(descriptor.c_str());
704 }
705 SetName(name);
706 return name;
707}
708
Elliott Hughes4681c802011-09-25 18:04:37 -0700709void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700710 if ((flags & kDumpClassFullDetail) == 0) {
711 os << PrettyClass(this);
712 if ((flags & kDumpClassClassLoader) != 0) {
713 os << ' ' << GetClassLoader();
714 }
715 if ((flags & kDumpClassInitialized) != 0) {
716 os << ' ' << GetStatus();
717 }
Elliott Hughese0918552011-10-28 17:18:29 -0700718 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700719 return;
720 }
721
722 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800723 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700724 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800725 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700726 os << " objectSize=" << SizeOf() << " "
727 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
728 os << StringPrintf(" access=0x%04x.%04x\n",
729 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
730 if (super != NULL) {
731 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
732 }
733 if (IsArrayClass()) {
734 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
735 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800736 if (kh.NumInterfaces() > 0) {
737 os << " interfaces (" << kh.NumInterfaces() << "):\n";
738 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
739 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700740 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800741 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700742 }
743 }
744 os << " vtable (" << NumVirtualMethods() << " entries, "
745 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
746 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800747 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700748 }
749 os << " direct methods (" << NumDirectMethods() << " entries):\n";
750 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800751 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700752 }
753 if (NumStaticFields() > 0) {
754 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700755 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700756 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800757 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700758 }
759 } else {
760 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700761 }
762 }
763 if (NumInstanceFields() > 0) {
764 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700765 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700766 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800767 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700768 }
769 } else {
770 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700771 }
772 }
773}
774
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700775void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
776 if (new_reference_offsets != CLASS_WALK_SUPER) {
777 // Sanity check that the number of bits set in the reference offset bitmap
778 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800779 size_t count = 0;
780 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
781 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700782 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800783 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700784 }
785 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
786 new_reference_offsets, false);
787}
788
789void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
790 if (new_reference_offsets != CLASS_WALK_SUPER) {
791 // Sanity check that the number of bits set in the reference offset bitmap
792 // agrees with the number of references
793 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
794 NumReferenceStaticFieldsDuringLinking());
795 }
796 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
797 new_reference_offsets, false);
798}
799
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700800bool Class::Implements(const Class* klass) const {
801 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700802 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700803 // All interfaces implemented directly and by our superclass, and
804 // recursively all super-interfaces of those interfaces, are listed
805 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700806 int32_t iftable_count = GetIfTableCount();
807 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
808 for (int32_t i = 0; i < iftable_count; i++) {
809 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700810 return true;
811 }
812 }
813 return false;
814}
815
816// Determine whether "this" is assignable from "klazz", where both of these
817// are array classes.
818//
819// Consider an array class, e.g. Y[][], where Y is a subclass of X.
820// Y[][] = Y[][] --> true (identity)
821// X[][] = Y[][] --> true (element superclass)
822// Y = Y[][] --> false
823// Y[] = Y[][] --> false
824// Object = Y[][] --> true (everything is an object)
825// Object[] = Y[][] --> true
826// Object[][] = Y[][] --> true
827// Object[][][] = Y[][] --> false (too many []s)
828// Serializable = Y[][] --> true (all arrays are Serializable)
829// Serializable[] = Y[][] --> true
830// Serializable[][] = Y[][] --> false (unless Y is Serializable)
831//
832// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700833// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700834//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700835bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700836 DCHECK(IsArrayClass()) << PrettyClass(this);
837 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700838 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700839}
840
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700841bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700842 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
843 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700844 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700845 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700846 // src's super should be java_lang_Object, since it is an array.
847 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700848 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
849 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700850 return this == java_lang_Object;
851 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700852 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700853}
854
855bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700856 DCHECK(!IsInterface()) << PrettyClass(this);
857 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700858 const Class* current = this;
859 do {
860 if (current == klass) {
861 return true;
862 }
863 current = current->GetSuperClass();
864 } while (current != NULL);
865 return false;
866}
867
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800868bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700869 size_t i = 0;
870 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
871 ++i;
872 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700873 if (descriptor1.find('/', i) != StringPiece::npos ||
874 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700875 return false;
876 } else {
877 return true;
878 }
879}
880
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700881#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700882bool Class::IsInSamePackage(const StringPiece& descriptor1,
883 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700884 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700885 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700886 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
887 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700888 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
889}
890#endif
891
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700892bool Class::IsInSamePackage(const Class* that) const {
893 const Class* klass1 = this;
894 const Class* klass2 = that;
895 if (klass1 == klass2) {
896 return true;
897 }
898 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700899 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700900 return false;
901 }
902 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700903 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700904 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700905 }
jeffhao4a801a42011-09-23 13:53:40 -0700906 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700907 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700908 }
909 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800910 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800911 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800912 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800913 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800914 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700915}
916
Elliott Hughesdbb40792011-11-18 17:05:22 -0800917bool Class::IsClassClass() const {
918 Class* java_lang_Class = GetClass()->GetClass();
919 return this == java_lang_Class;
920}
921
922bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800923 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800924}
925
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800926bool Class::IsThrowableClass() const {
927 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
928 return throwable->IsAssignableFrom(this);
929}
930
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800931ClassLoader* Class::GetClassLoader() const {
932 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700933}
934
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700935void Class::SetClassLoader(const ClassLoader* new_cl) {
936 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700938}
939
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800940Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700941 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700942 DCHECK(declaring_class != NULL) << PrettyClass(this);
943 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700944 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700945 int32_t iftable_count = GetIfTableCount();
946 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
947 for (int32_t i = 0; i < iftable_count; i++) {
948 InterfaceEntry* interface_entry = iftable->Get(i);
949 if (interface_entry->GetInterface() == declaring_class) {
950 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700951 }
952 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700953 return NULL;
954}
955
Ian Rogers466bb252011-10-14 03:29:56 -0700956Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700957 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800958 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700959 if (method != NULL) {
960 return method;
961 }
962
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700963 int32_t iftable_count = GetIfTableCount();
964 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
965 for (int32_t i = 0; i < iftable_count; i++) {
966 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700967 if (method != NULL) {
968 return method;
969 }
970 }
971 return NULL;
972}
973
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800974Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
975 // Check the current class before checking the interfaces.
976 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
977 if (method != NULL) {
978 return method;
979 }
980
981 int32_t iftable_count = GetIfTableCount();
982 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
983 for (int32_t i = 0; i < iftable_count; i++) {
984 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
985 if (method != NULL) {
986 return method;
987 }
988 }
989 return NULL;
990}
991
992
993Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800994 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700995 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700996 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800997 mh.ChangeMethod(method);
998 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700999 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001000 }
1001 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001002 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001003}
1004
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001005Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1006 if (GetDexCache() == dex_cache) {
1007 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1008 Method* method = GetDirectMethod(i);
1009 if (method->GetDexMethodIndex() == dex_method_idx) {
1010 return method;
1011 }
1012 }
1013 }
1014 return NULL;
1015}
1016
1017Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1018 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001019 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001020 if (method != NULL) {
1021 return method;
1022 }
1023 }
1024 return NULL;
1025}
1026
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001027Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1028 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1029 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1030 if (method != NULL) {
1031 return method;
1032 }
1033 }
1034 return NULL;
1035}
1036
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001037Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001038 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001039 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001040 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001041 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001042 mh.ChangeMethod(method);
1043 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001044 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001045 }
1046 }
1047 return NULL;
1048}
1049
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001050Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1051 if (GetDexCache() == dex_cache) {
1052 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1053 Method* method = GetVirtualMethod(i);
1054 if (method->GetDexMethodIndex() == dex_method_idx) {
1055 return method;
1056 }
1057 }
1058 }
1059 return NULL;
1060}
1061
Ian Rogers466bb252011-10-14 03:29:56 -07001062Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1063 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1064 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1065 if (method != NULL) {
1066 return method;
1067 }
1068 }
1069 return NULL;
1070}
1071
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001072Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1073 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1074 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1075 if (method != NULL) {
1076 return method;
1077 }
1078 }
1079 return NULL;
1080}
1081
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001082Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 // Is the field in this class?
1084 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001085 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1087 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001088 fh.ChangeField(f);
1089 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001090 return f;
1091 }
1092 }
1093 return NULL;
1094}
1095
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001096Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1097 if (GetDexCache() == dex_cache) {
1098 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1099 Field* f = GetInstanceField(i);
1100 if (f->GetDexFieldIndex() == dex_field_idx) {
1101 return f;
1102 }
1103 }
1104 }
1105 return NULL;
1106}
1107
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001108Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 // Is the field in this class, or any of its superclasses?
1110 // Interfaces are not relevant because they can't contain instance fields.
1111 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001112 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 if (f != NULL) {
1114 return f;
1115 }
1116 }
1117 return NULL;
1118}
1119
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001120Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1121 // Is the field in this class, or any of its superclasses?
1122 // Interfaces are not relevant because they can't contain instance fields.
1123 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1124 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1125 if (f != NULL) {
1126 return f;
1127 }
1128 }
1129 return NULL;
1130}
1131
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001132Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1133 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001134 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001135 for (size_t i = 0; i < NumStaticFields(); ++i) {
1136 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001137 fh.ChangeField(f);
1138 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001139 return f;
1140 }
1141 }
1142 return NULL;
1143}
1144
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001145Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1146 if (dex_cache == GetDexCache()) {
1147 for (size_t i = 0; i < NumStaticFields(); ++i) {
1148 Field* f = GetStaticField(i);
1149 if (f->GetDexFieldIndex() == dex_field_idx) {
1150 return f;
1151 }
1152 }
1153 }
1154 return NULL;
1155}
1156
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001157Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1158 // Is the field in this class (or its interfaces), or any of its
1159 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001160 ClassHelper kh;
1161 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001162 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001163 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001164 if (f != NULL) {
1165 return f;
1166 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001167 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001168 kh.ChangeClass(k);
1169 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1170 Class* interface = kh.GetInterface(i);
1171 f = interface->FindDeclaredStaticField(name, type);
1172 if (f != NULL) {
1173 return f;
1174 }
1175 }
1176 }
1177 return NULL;
1178}
1179
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001180Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1181 ClassHelper kh;
1182 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1183 // Is the field in this class?
1184 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1185 if (f != NULL) {
1186 return f;
1187 }
1188 // Is this field in any of this class' interfaces?
1189 kh.ChangeClass(k);
1190 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1191 Class* interface = kh.GetInterface(i);
1192 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1193 if (f != NULL) {
1194 return f;
1195 }
1196 }
1197 }
1198 return NULL;
1199}
1200
Ian Rogersb067ac22011-12-13 18:05:09 -08001201Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1202 // Find a field using the JLS field resolution order
1203 ClassHelper kh;
1204 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1205 // Is the field in this class?
1206 Field* f = k->FindDeclaredInstanceField(name, type);
1207 if (f != NULL) {
1208 return f;
1209 }
1210 f = k->FindDeclaredStaticField(name, type);
1211 if (f != NULL) {
1212 return f;
1213 }
1214 // Is this field in any of this class' interfaces?
1215 kh.ChangeClass(k);
1216 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1217 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001218 f = interface->FindDeclaredStaticField(name, type);
1219 if (f != NULL) {
1220 return f;
1221 }
1222 }
1223 }
1224 return NULL;
1225}
1226
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001227Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001228 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001229 DCHECK_GE(component_count, 0);
1230 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001231
1232 size_t header_size = sizeof(Array);
1233 size_t data_size = component_count * component_size;
1234 size_t size = header_size + data_size;
1235
1236 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1237 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1238 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1239 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001240 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001241 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001242 return NULL;
1243 }
1244
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001245 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1246 if (array != NULL) {
1247 DCHECK(array->IsArrayInstance());
1248 array->SetLength(component_count);
1249 }
1250 return array;
1251}
1252
1253Array* Array::Alloc(Class* array_class, int32_t component_count) {
1254 return Alloc(array_class, component_count, array_class->GetComponentSize());
1255}
1256
Elliott Hughes80609252011-09-23 17:24:51 -07001257bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001258 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001259 "length=%i; index=%i", length_, index);
1260 return false;
1261}
1262
1263bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001264 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001265 "Can't store an element of type %s into an array of type %s",
1266 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1267 return false;
1268}
1269
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001270template<typename T>
1271PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001272 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001273 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1274 return down_cast<PrimitiveArray<T>*>(raw_array);
1275}
1276
1277template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1278
1279// Explicitly instantiate all the primitive array types.
1280template class PrimitiveArray<uint8_t>; // BooleanArray
1281template class PrimitiveArray<int8_t>; // ByteArray
1282template class PrimitiveArray<uint16_t>; // CharArray
1283template class PrimitiveArray<double>; // DoubleArray
1284template class PrimitiveArray<float>; // FloatArray
1285template class PrimitiveArray<int32_t>; // IntArray
1286template class PrimitiveArray<int64_t>; // LongArray
1287template class PrimitiveArray<int16_t>; // ShortArray
1288
Ian Rogers466bb252011-10-14 03:29:56 -07001289// Explicitly instantiate Class[][]
1290template class ObjectArray<ObjectArray<Class> >;
1291
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001292// TODO: get global references for these
1293Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001294
Brian Carlstroma663ea52011-08-19 23:33:41 -07001295void String::SetClass(Class* java_lang_String) {
1296 CHECK(java_lang_String_ == NULL);
1297 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001298 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001299}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001300
Brian Carlstroma663ea52011-08-19 23:33:41 -07001301void String::ResetClass() {
1302 CHECK(java_lang_String_ != NULL);
1303 java_lang_String_ = NULL;
1304}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001305
Brian Carlstromc74255f2011-09-11 22:47:39 -07001306String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001307 return Runtime::Current()->GetInternTable()->InternWeak(this);
1308}
1309
Brian Carlstrom395520e2011-09-25 19:35:00 -07001310int32_t String::GetHashCode() {
1311 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1312 if (result == 0) {
1313 ComputeHashCode();
1314 }
1315 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1316 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1317 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001318 return result;
1319}
1320
1321int32_t String::GetLength() const {
1322 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1323 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1324 return result;
1325}
1326
1327uint16_t String::CharAt(int32_t index) const {
1328 // TODO: do we need this? Equals is the only caller, and could
1329 // bounds check itself.
1330 if (index < 0 || index >= count_) {
1331 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001332 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001333 "length=%i; index=%i", count_, index);
1334 return 0;
1335 }
1336 return GetCharArray()->Get(index + GetOffset());
1337}
1338
1339String* String::AllocFromUtf16(int32_t utf16_length,
1340 const uint16_t* utf16_data_in,
1341 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001342 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001343 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001344 if (string == NULL) {
1345 return NULL;
1346 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001347 // TODO: use 16-bit wide memset variant
1348 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001349 if (array == NULL) {
1350 return NULL;
1351 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001352 for (int i = 0; i < utf16_length; i++) {
1353 array->Set(i, utf16_data_in[i]);
1354 }
1355 if (hash_code != 0) {
1356 string->SetHashCode(hash_code);
1357 } else {
1358 string->ComputeHashCode();
1359 }
1360 return string;
1361}
1362
1363String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001364 if (utf == NULL) {
1365 return NULL;
1366 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367 size_t char_count = CountModifiedUtf8Chars(utf);
1368 return AllocFromModifiedUtf8(char_count, utf);
1369}
1370
1371String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1372 const char* utf8_data_in) {
1373 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001374 if (string == NULL) {
1375 return NULL;
1376 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001377 uint16_t* utf16_data_out =
1378 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1379 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1380 string->ComputeHashCode();
1381 return string;
1382}
1383
1384String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001385 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1386 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001387 return NULL;
1388 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001389 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001390}
1391
1392String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001393 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001394 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001395 if (string == NULL) {
1396 return NULL;
1397 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001398 string->SetArray(array);
1399 string->SetCount(array->GetLength());
1400 return string;
1401}
1402
1403bool String::Equals(const String* that) const {
1404 if (this == that) {
1405 // Quick reference equality test
1406 return true;
1407 } else if (that == NULL) {
1408 // Null isn't an instanceof anything
1409 return false;
1410 } else if (this->GetLength() != that->GetLength()) {
1411 // Quick length inequality test
1412 return false;
1413 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001414 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001415 // hash code was already equal
1416 for (int32_t i = 0; i < that->GetLength(); ++i) {
1417 if (this->CharAt(i) != that->CharAt(i)) {
1418 return false;
1419 }
1420 }
1421 return true;
1422 }
1423}
1424
Elliott Hughes5d78d392011-12-13 16:53:05 -08001425bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001426 if (this->GetLength() != that_length) {
1427 return false;
1428 } else {
1429 for (int32_t i = 0; i < that_length; ++i) {
1430 if (this->CharAt(i) != that_chars[that_offset + i]) {
1431 return false;
1432 }
1433 }
1434 return true;
1435 }
1436}
1437
1438bool String::Equals(const char* modified_utf8) const {
1439 for (int32_t i = 0; i < GetLength(); ++i) {
1440 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1441 if (ch == '\0' || ch != CharAt(i)) {
1442 return false;
1443 }
1444 }
1445 return *modified_utf8 == '\0';
1446}
1447
1448bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001449 if (modified_utf8.size() != GetLength()) {
1450 return false;
1451 }
1452 const char* p = modified_utf8.data();
1453 for (int32_t i = 0; i < GetLength(); ++i) {
1454 uint16_t ch = GetUtf16FromUtf8(&p);
1455 if (ch != CharAt(i)) {
1456 return false;
1457 }
1458 }
1459 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001460}
1461
1462// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1463std::string String::ToModifiedUtf8() const {
1464 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1465 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1466 std::string result(byte_count, char(0));
1467 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1468 return result;
1469}
1470
Ian Rogers1c5eb702012-02-01 09:18:34 -08001471void Throwable::SetCause(Throwable* cause) {
1472 CHECK(cause != NULL);
1473 CHECK(cause != this);
1474 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1475 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1476}
1477
Ian Rogers466bb252011-10-14 03:29:56 -07001478bool Throwable::IsCheckedException() const {
1479 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1480 if (InstanceOf(error)) {
1481 return false;
1482 }
1483 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1484 return !InstanceOf(jlre);
1485}
1486
Ian Rogers9074b992011-10-26 17:41:55 -07001487std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001488 std::string result(PrettyTypeOf(this));
1489 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001490 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001491 if (msg != NULL) {
1492 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001493 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001494 result += "\n";
1495 Object* stack_state = GetStackState();
1496 // check stack state isn't missing or corrupt
1497 if (stack_state != NULL && stack_state->IsObjectArray()) {
1498 // Decode the internal stack trace into the depth and method trace
1499 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1500 int32_t depth = method_trace->GetLength() - 1;
1501 for (int32_t i = 0; i < depth; ++i) {
1502 Method* method = down_cast<Method*>(method_trace->Get(i));
1503 result += " at ";
1504 result += PrettyMethod(method, true);
1505 result += "\n";
1506 }
Ian Rogers9074b992011-10-26 17:41:55 -07001507 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001508 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001509 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001510 result += "Caused by: ";
1511 result += cause->Dump();
1512 }
Ian Rogers9074b992011-10-26 17:41:55 -07001513 return result;
1514}
1515
Ian Rogers5167c972012-02-03 10:41:20 -08001516
1517Class* Throwable::java_lang_Throwable_ = NULL;
1518
1519void Throwable::SetClass(Class* java_lang_Throwable) {
1520 CHECK(java_lang_Throwable_ == NULL);
1521 CHECK(java_lang_Throwable != NULL);
1522 java_lang_Throwable_ = java_lang_Throwable;
1523}
1524
1525void Throwable::ResetClass() {
1526 CHECK(java_lang_Throwable_ != NULL);
1527 java_lang_Throwable_ = NULL;
1528}
1529
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001530Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1531
1532void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1533 CHECK(java_lang_StackTraceElement_ == NULL);
1534 CHECK(java_lang_StackTraceElement != NULL);
1535 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1536}
1537
1538void StackTraceElement::ResetClass() {
1539 CHECK(java_lang_StackTraceElement_ != NULL);
1540 java_lang_StackTraceElement_ = NULL;
1541}
1542
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001543StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1544 String* method_name,
1545 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001546 int32_t line_number) {
1547 StackTraceElement* trace =
1548 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1549 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1550 const_cast<String*>(declaring_class), false);
1551 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1552 const_cast<String*>(method_name), false);
1553 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1554 const_cast<String*>(file_name), false);
1555 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1556 line_number, false);
1557 return trace;
1558}
1559
Elliott Hughes1f359b02011-07-17 14:27:17 -07001560static const char* kClassStatusNames[] = {
1561 "Error",
1562 "NotReady",
1563 "Idx",
1564 "Loaded",
1565 "Resolved",
1566 "Verifying",
1567 "Verified",
1568 "Initializing",
1569 "Initialized"
1570};
1571std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1572 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001573 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001574 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001575 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001576 }
1577 return os;
1578}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001579
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001580} // namespace art