blob: 2e98f4f1f19fe0229955c2448aad8d2366b507a7 [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"
Ian Rogers60db5ab2012-02-20 17:02:00 -080037#include "runtime_support.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070038#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070039#include "utils.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070040
Logan Chienfca7e872011-12-20 20:08:22 +080041#if defined(ART_USE_LLVM_COMPILER)
42#include "compiler_llvm/inferred_reg_category_map.h"
43using art::compiler_llvm::InferredRegCategoryMap;
44#endif
45
Carl Shapiro3ee755d2011-06-28 12:11:04 -070046namespace art {
47
Elliott Hughesdbb40792011-11-18 17:05:22 -080048String* Object::AsString() {
49 DCHECK(GetClass()->IsStringClass());
50 return down_cast<String*>(this);
51}
52
Elliott Hughes081be7f2011-09-18 16:50:26 -070053Object* Object::Clone() {
54 Class* c = GetClass();
55 DCHECK(!c->IsClassClass());
56
57 // Object::SizeOf gets the right size even if we're an array.
58 // Using c->AllocObject() here would be wrong.
59 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080060 Heap* heap = Runtime::Current()->GetHeap();
61 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070062 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070063 return NULL;
64 }
65
66 // Copy instance data. We assume memcpy copies by words.
67 // TODO: expose and use move32.
68 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070069 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070070 size_t offset = sizeof(Object);
71 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
72
Elliott Hughes20cde902011-10-04 17:37:27 -070073 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080074 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070075 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070076
Brian Carlstrom40381fb2011-10-19 14:13:40 -070077 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070078}
79
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070080uint32_t Object::GetThinLockId() {
81 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070082}
83
84void Object::MonitorEnter(Thread* thread) {
85 Monitor::MonitorEnter(thread, this);
86}
87
Ian Rogersff1ed472011-09-20 13:46:24 -070088bool Object::MonitorExit(Thread* thread) {
89 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070090}
91
92void Object::Notify() {
93 Monitor::Notify(Thread::Current(), this);
94}
95
96void Object::NotifyAll() {
97 Monitor::NotifyAll(Thread::Current(), this);
98}
99
100void Object::Wait(int64_t ms, int32_t ns) {
101 Monitor::Wait(Thread::Current(), this, ms, ns, true);
102}
103
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700104// TODO: get global references for these
105Class* Field::java_lang_reflect_Field_ = NULL;
106
107void Field::SetClass(Class* java_lang_reflect_Field) {
108 CHECK(java_lang_reflect_Field_ == NULL);
109 CHECK(java_lang_reflect_Field != NULL);
110 java_lang_reflect_Field_ = java_lang_reflect_Field;
111}
112
113void Field::ResetClass() {
114 CHECK(java_lang_reflect_Field_ != NULL);
115 java_lang_reflect_Field_ = NULL;
116}
117
Ian Rogers0571d352011-11-03 19:51:38 -0700118void Field::SetOffset(MemberOffset num_bytes) {
119 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800120#if 0 // TODO enable later in boot and under !NDEBUG
121 FieldHelper fh(this);
122 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700123 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
124 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
125 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700127 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
128}
129
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700131 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700132 if (IsStatic()) {
133 object = declaring_class_;
134 }
135 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700136}
137
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700138void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700139 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700140 if (IsStatic()) {
141 object = declaring_class_;
142 }
143 object->SetField32(GetOffset(), new_value, IsVolatile());
144}
145
146uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700147 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700148 if (IsStatic()) {
149 object = declaring_class_;
150 }
151 return object->GetField64(GetOffset(), IsVolatile());
152}
153
154void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700155 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700156 if (IsStatic()) {
157 object = declaring_class_;
158 }
159 object->SetField64(GetOffset(), new_value, IsVolatile());
160}
161
162Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700163 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700164 if (IsStatic()) {
165 object = declaring_class_;
166 }
167 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
168}
169
170void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700171 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700172 if (IsStatic()) {
173 object = declaring_class_;
174 }
175 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
176}
177
178bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800179 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
180 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700181 return Get32(object);
182}
183
184void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
186 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 Set32(object, z);
188}
189
190int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800191 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
192 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193 return Get32(object);
194}
195
196void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800197 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
198 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199 Set32(object, b);
200}
201
202uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
204 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 return Get32(object);
206}
207
208void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800209 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
210 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 Set32(object, c);
212}
213
Ian Rogers466bb252011-10-14 03:29:56 -0700214int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800215 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
216 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 return Get32(object);
218}
219
Ian Rogers466bb252011-10-14 03:29:56 -0700220void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800221 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
222 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 Set32(object, s);
224}
225
226int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
228 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700229 return Get32(object);
230}
231
232void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800233 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
234 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700235 Set32(object, i);
236}
237
238int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800239 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
240 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700241 return Get64(object);
242}
243
244void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800245 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
246 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700247 Set64(object, j);
248}
249
250float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800251 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
252 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700253 JValue float_bits;
254 float_bits.i = Get32(object);
255 return float_bits.f;
256}
257
258void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
260 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 JValue float_bits;
262 float_bits.f = f;
263 Set32(object, float_bits.i);
264}
265
266double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
268 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700269 JValue double_bits;
270 double_bits.j = Get64(object);
271 return double_bits.d;
272}
273
274void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
276 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 JValue double_bits;
278 double_bits.d = d;
279 Set64(object, double_bits.j);
280}
281
282Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
284 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 return GetObj(object);
286}
287
288void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
290 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 SetObj(object, l);
292}
293
294// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700295Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700296Class* Method::java_lang_reflect_Method_ = NULL;
297
Elliott Hughes80609252011-09-23 17:24:51 -0700298void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
299 CHECK(java_lang_reflect_Constructor_ == NULL);
300 CHECK(java_lang_reflect_Constructor != NULL);
301 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
302
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 CHECK(java_lang_reflect_Method_ == NULL);
304 CHECK(java_lang_reflect_Method != NULL);
305 java_lang_reflect_Method_ = java_lang_reflect_Method;
306}
307
Elliott Hughes80609252011-09-23 17:24:51 -0700308void Method::ResetClasses() {
309 CHECK(java_lang_reflect_Constructor_ != NULL);
310 java_lang_reflect_Constructor_ = NULL;
311
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 CHECK(java_lang_reflect_Method_ != NULL);
313 java_lang_reflect_Method_ = NULL;
314}
315
Elliott Hughes418d20f2011-09-22 14:00:39 -0700316Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
317 if (*p == '[') {
318 // Something like "[[[Ljava/lang/String;".
319 const char* start = p;
320 while (*p == '[') {
321 ++p;
322 }
323 if (*p == 'L') {
324 while (*p != ';') {
325 ++p;
326 }
327 }
328 ++p; // Either the ';' or the primitive type.
329
Brian Carlstromaded5f72011-10-07 17:15:04 -0700330 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800331 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700332 } else if (*p == 'L') {
333 const char* start = p;
334 while (*p != ';') {
335 ++p;
336 }
337 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800338 std::string descriptor(start, (p - start));
339 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 } else {
341 return class_linker->FindPrimitiveClass(*p++);
342 }
343}
344
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345ObjectArray<String>* Method::GetDexCacheStrings() const {
346 return GetFieldObject<ObjectArray<String>*>(
347 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
348}
349
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
351 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
352 new_dex_cache_strings, false);
353}
354
Ian Rogers19846512012-02-24 11:42:47 -0800355ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
356 return GetFieldObject<ObjectArray<Method>*>(
357 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
358}
359
360void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
361 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
362 new_dex_cache_methods, false);
363}
364
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700365ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
366 return GetFieldObject<ObjectArray<Class>*>(
367 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
368}
369
370void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
371 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
372 new_dex_cache_classes, false);
373}
374
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700375ObjectArray<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()) {
Ian Rogers19846512012-02-24 11:42:47 -0800449 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
450 CHECK_EQ(result,
451 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800452 } else {
453 MethodHelper mh(this);
454 MethodHelper interface_mh;
455 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
456 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
457 InterfaceEntry* entry = iftable->Get(i);
458 Class* interface = entry->GetInterface();
459 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
460 Method* interface_method = interface->GetVirtualMethod(j);
461 interface_mh.ChangeMethod(interface_method);
462 if (mh.HasSameNameAndSignature(&interface_mh)) {
463 result = interface_method;
464 break;
465 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800466 }
467 }
Ian Rogers466bb252011-10-14 03:29:56 -0700468 }
469 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800470#ifndef NDEBUG
471 MethodHelper result_mh(result);
472 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
473#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700474 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700475}
476
Elliott Hughes168670b2012-02-29 16:43:26 -0800477static const void* GetOatCode(const Method* m) {
478 Runtime* runtime = Runtime::Current();
479 const void* code = m->GetCode();
480 // Peel off any method tracing trampoline.
481 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
482 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
483 }
484 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800485 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800486 code = runtime->GetClassLinker()->GetOatCodeFor(m);
487 }
488 return code;
489}
490
Ian Rogersbdb03912011-09-14 00:55:44 -0700491uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700492 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700493 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800494 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700495 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700496 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800498 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700499 uint32_t best_offset = 0;
500 uint32_t best_dex_offset = 0;
501 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 uint32_t map_offset = mapping_table[i];
503 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700504 if (map_offset == sought_offset) {
505 best_offset = map_offset;
506 best_dex_offset = map_dex_offset;
507 break;
508 }
509 if (map_offset < sought_offset && map_offset > best_offset) {
510 best_offset = map_offset;
511 best_dex_offset = map_dex_offset;
512 }
513 }
514 return best_dex_offset;
515}
516
517uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700518 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700519 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700520 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700521 return 0; // Special no mapping/pc == 0 case
522 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700524 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700525 uint32_t map_offset = mapping_table[i];
526 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700527 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800528 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700529 }
530 }
531 LOG(FATAL) << "Looking up Dex PC not contained in method";
532 return 0;
533}
534
535uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800536 MethodHelper mh(this);
537 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700538 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700539 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
540 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700541 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700542 if (iter_type_idx == DexFile::kDexNoIndex16) {
543 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700544 }
545 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800546 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700547 if (iter_exception_type == NULL) {
548 // The verifier should take care of resolving all exception classes early
549 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800550 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700551 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700552 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700553 }
554 }
555 // Handler not found
556 return DexFile::kDexNoIndex;
557}
558
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700559void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
560 // Push a transition back into managed code onto the linked list in thread.
561 CHECK_EQ(Thread::kRunnable, self->GetState());
562 NativeToManagedRecord record;
563 self->PushNativeToManagedRecord(&record);
564
565 // Call the invoke stub associated with the method.
566 // Pass everything as arguments.
567 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700568
569 bool have_executable_code = (GetCode() != NULL);
570#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700571 // Currently we can only compile non-native methods for ARM.
572 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700573#endif
574
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500575 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700576 bool log = false;
577 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800578 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
579 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700580 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700581 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700582 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800583 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
584 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700585 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700586 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800587 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
588 PrettyMethod(this).c_str(), GetCode(), stub,
589 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700590 if (result != NULL) {
591 result->j = 0;
592 }
593 }
594
595 // Pop transition.
596 self->PopNativeToManagedRecord(record);
597}
598
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700599bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700600 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800601 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800602 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700603 return native_method != jni_stub;
604}
605
Ian Rogers60db5ab2012-02-20 17:02:00 -0800606void Method::RegisterNative(Thread* self, const void* native_method) {
607 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700608 CHECK(IsNative()) << PrettyMethod(this);
609 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers60db5ab2012-02-20 17:02:00 -0800610 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
611 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
612 native_method, false);
613 } else {
614 // We've been asked to associate this method with the given native method but are working
615 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
616 // the native method to runtime support and store the target somewhere runtime support will
617 // find it.
618#if defined(__arm__)
619 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
620 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
621#else
622 UNIMPLEMENTED(FATAL);
623#endif
624 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
625 reinterpret_cast<const uint8_t*>(native_method), false);
626 }
Brian Carlstrom16192862011-09-12 17:50:06 -0700627}
628
Ian Rogers19846512012-02-24 11:42:47 -0800629void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700630 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700631 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800632 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700633}
634
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700635void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700636 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
637 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700638 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800639 if (new_status == kStatusError) {
640 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
641
642 // stash current exception
643 Thread* self = Thread::Current();
644 SirtRef<Throwable> exception(self->GetException());
645 CHECK(exception.get() != NULL);
646
647 // clear exception to call FindSystemClass
648 self->ClearException();
649 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
650 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
651 CHECK(!self->IsExceptionPending());
652
653 // only verification errors, not initialization problems, should set a verify error.
654 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
655 Class* exception_class = exception->GetClass();
656 if (!eiie_class->IsAssignableFrom(exception_class)) {
657 SetVerifyErrorClass(exception_class);
658 }
659
660 // restore exception
661 self->SetException(exception.get());
662 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700663 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700664}
665
666DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700667 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700668}
669
670void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700671 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700672}
673
Brian Carlstrom1f870082011-08-23 16:02:11 -0700674Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700675 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700676 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500677 // TODO: decide whether we want this check. It currently fails during bootstrap.
678 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700679 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800680 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700681}
682
Ian Rogers0571d352011-11-03 19:51:38 -0700683void Class::SetClassSize(size_t new_class_size) {
684 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
685 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
686}
687
Ian Rogersd418eda2012-01-30 12:14:28 -0800688// Return the class' name. The exact format is bizarre, but it's the specified behavior for
689// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
690// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
691// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
692String* Class::ComputeName() {
693 String* name = GetName();
694 if (name != NULL) {
695 return name;
696 }
697 std::string descriptor(ClassHelper(this).GetDescriptor());
698 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
699 // The descriptor indicates that this is the class for
700 // a primitive type; special-case the return value.
701 const char* c_name = NULL;
702 switch (descriptor[0]) {
703 case 'Z': c_name = "boolean"; break;
704 case 'B': c_name = "byte"; break;
705 case 'C': c_name = "char"; break;
706 case 'S': c_name = "short"; break;
707 case 'I': c_name = "int"; break;
708 case 'J': c_name = "long"; break;
709 case 'F': c_name = "float"; break;
710 case 'D': c_name = "double"; break;
711 case 'V': c_name = "void"; break;
712 default:
713 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
714 }
715 name = String::AllocFromModifiedUtf8(c_name);
716 } else {
717 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
718 // components.
719 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
720 descriptor.erase(0, 1);
721 descriptor.erase(descriptor.size() - 1);
722 }
723 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
724 name = String::AllocFromModifiedUtf8(descriptor.c_str());
725 }
726 SetName(name);
727 return name;
728}
729
Elliott Hughes4681c802011-09-25 18:04:37 -0700730void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700731 if ((flags & kDumpClassFullDetail) == 0) {
732 os << PrettyClass(this);
733 if ((flags & kDumpClassClassLoader) != 0) {
734 os << ' ' << GetClassLoader();
735 }
736 if ((flags & kDumpClassInitialized) != 0) {
737 os << ' ' << GetStatus();
738 }
Elliott Hughese0918552011-10-28 17:18:29 -0700739 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700740 return;
741 }
742
743 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800744 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700745 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800746 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700747 os << " objectSize=" << SizeOf() << " "
748 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
749 os << StringPrintf(" access=0x%04x.%04x\n",
750 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
751 if (super != NULL) {
752 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
753 }
754 if (IsArrayClass()) {
755 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
756 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800757 if (kh.NumInterfaces() > 0) {
758 os << " interfaces (" << kh.NumInterfaces() << "):\n";
759 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
760 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700761 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800762 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700763 }
764 }
765 os << " vtable (" << NumVirtualMethods() << " entries, "
766 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
767 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800768 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700769 }
770 os << " direct methods (" << NumDirectMethods() << " entries):\n";
771 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800772 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700773 }
774 if (NumStaticFields() > 0) {
775 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700776 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700777 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800778 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700779 }
780 } else {
781 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700782 }
783 }
784 if (NumInstanceFields() > 0) {
785 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700786 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700787 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800788 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700789 }
790 } else {
791 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700792 }
793 }
794}
795
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700796void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
797 if (new_reference_offsets != CLASS_WALK_SUPER) {
798 // Sanity check that the number of bits set in the reference offset bitmap
799 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800800 size_t count = 0;
801 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
802 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700803 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800804 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700805 }
806 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
807 new_reference_offsets, false);
808}
809
810void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
811 if (new_reference_offsets != CLASS_WALK_SUPER) {
812 // Sanity check that the number of bits set in the reference offset bitmap
813 // agrees with the number of references
814 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
815 NumReferenceStaticFieldsDuringLinking());
816 }
817 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
818 new_reference_offsets, false);
819}
820
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700821bool Class::Implements(const Class* klass) const {
822 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700823 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700824 // All interfaces implemented directly and by our superclass, and
825 // recursively all super-interfaces of those interfaces, are listed
826 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700827 int32_t iftable_count = GetIfTableCount();
828 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
829 for (int32_t i = 0; i < iftable_count; i++) {
830 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700831 return true;
832 }
833 }
834 return false;
835}
836
837// Determine whether "this" is assignable from "klazz", where both of these
838// are array classes.
839//
840// Consider an array class, e.g. Y[][], where Y is a subclass of X.
841// Y[][] = Y[][] --> true (identity)
842// X[][] = Y[][] --> true (element superclass)
843// Y = Y[][] --> false
844// Y[] = Y[][] --> false
845// Object = Y[][] --> true (everything is an object)
846// Object[] = Y[][] --> true
847// Object[][] = Y[][] --> true
848// Object[][][] = Y[][] --> false (too many []s)
849// Serializable = Y[][] --> true (all arrays are Serializable)
850// Serializable[] = Y[][] --> true
851// Serializable[][] = Y[][] --> false (unless Y is Serializable)
852//
853// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700854// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700855//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700856bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700857 DCHECK(IsArrayClass()) << PrettyClass(this);
858 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700859 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700860}
861
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700862bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700863 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
864 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700865 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700866 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700867 // src's super should be java_lang_Object, since it is an array.
868 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700869 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
870 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700871 return this == java_lang_Object;
872 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700873 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700874}
875
876bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700877 DCHECK(!IsInterface()) << PrettyClass(this);
878 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700879 const Class* current = this;
880 do {
881 if (current == klass) {
882 return true;
883 }
884 current = current->GetSuperClass();
885 } while (current != NULL);
886 return false;
887}
888
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800889bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700890 size_t i = 0;
891 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
892 ++i;
893 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700894 if (descriptor1.find('/', i) != StringPiece::npos ||
895 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700896 return false;
897 } else {
898 return true;
899 }
900}
901
902bool Class::IsInSamePackage(const Class* that) const {
903 const Class* klass1 = this;
904 const Class* klass2 = that;
905 if (klass1 == klass2) {
906 return true;
907 }
908 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700909 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700910 return false;
911 }
912 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700913 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700914 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700915 }
jeffhao4a801a42011-09-23 13:53:40 -0700916 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700917 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700918 }
919 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800920 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800921 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800922 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800923 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800924 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700925}
926
Elliott Hughesdbb40792011-11-18 17:05:22 -0800927bool Class::IsClassClass() const {
928 Class* java_lang_Class = GetClass()->GetClass();
929 return this == java_lang_Class;
930}
931
932bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800933 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800934}
935
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800936bool Class::IsThrowableClass() const {
937 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
938 return throwable->IsAssignableFrom(this);
939}
940
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800941ClassLoader* Class::GetClassLoader() const {
942 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700943}
944
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700945void Class::SetClassLoader(const ClassLoader* new_cl) {
946 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700948}
949
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800950Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700951 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700952 DCHECK(declaring_class != NULL) << PrettyClass(this);
953 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700954 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700955 int32_t iftable_count = GetIfTableCount();
956 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
957 for (int32_t i = 0; i < iftable_count; i++) {
958 InterfaceEntry* interface_entry = iftable->Get(i);
959 if (interface_entry->GetInterface() == declaring_class) {
960 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700961 }
962 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700963 return NULL;
964}
965
Ian Rogers466bb252011-10-14 03:29:56 -0700966Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700967 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800968 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700969 if (method != NULL) {
970 return method;
971 }
972
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700973 int32_t iftable_count = GetIfTableCount();
974 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
975 for (int32_t i = 0; i < iftable_count; i++) {
976 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700977 if (method != NULL) {
978 return method;
979 }
980 }
981 return NULL;
982}
983
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800984Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
985 // Check the current class before checking the interfaces.
986 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
987 if (method != NULL) {
988 return method;
989 }
990
991 int32_t iftable_count = GetIfTableCount();
992 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
993 for (int32_t i = 0; i < iftable_count; i++) {
994 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
995 if (method != NULL) {
996 return method;
997 }
998 }
999 return NULL;
1000}
1001
1002
1003Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001004 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001005 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001006 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001007 mh.ChangeMethod(method);
1008 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001009 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001010 }
1011 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001012 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001013}
1014
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001015Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1016 if (GetDexCache() == dex_cache) {
1017 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1018 Method* method = GetDirectMethod(i);
1019 if (method->GetDexMethodIndex() == dex_method_idx) {
1020 return method;
1021 }
1022 }
1023 }
1024 return NULL;
1025}
1026
1027Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1028 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001029 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001030 if (method != NULL) {
1031 return method;
1032 }
1033 }
1034 return NULL;
1035}
1036
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001037Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1038 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1039 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1040 if (method != NULL) {
1041 return method;
1042 }
1043 }
1044 return NULL;
1045}
1046
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001047Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001048 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001049 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001050 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001051 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001052 mh.ChangeMethod(method);
1053 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001054 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001055 }
1056 }
1057 return NULL;
1058}
1059
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001060Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1061 if (GetDexCache() == dex_cache) {
1062 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1063 Method* method = GetVirtualMethod(i);
1064 if (method->GetDexMethodIndex() == dex_method_idx) {
1065 return method;
1066 }
1067 }
1068 }
1069 return NULL;
1070}
1071
Ian Rogers466bb252011-10-14 03:29:56 -07001072Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1073 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1074 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1075 if (method != NULL) {
1076 return method;
1077 }
1078 }
1079 return NULL;
1080}
1081
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001082Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1083 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1084 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1085 if (method != NULL) {
1086 return method;
1087 }
1088 }
1089 return NULL;
1090}
1091
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001092Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 // Is the field in this class?
1094 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001095 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1097 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001098 fh.ChangeField(f);
1099 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001100 return f;
1101 }
1102 }
1103 return NULL;
1104}
1105
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001106Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1107 if (GetDexCache() == dex_cache) {
1108 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1109 Field* f = GetInstanceField(i);
1110 if (f->GetDexFieldIndex() == dex_field_idx) {
1111 return f;
1112 }
1113 }
1114 }
1115 return NULL;
1116}
1117
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001118Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 // Is the field in this class, or any of its superclasses?
1120 // Interfaces are not relevant because they can't contain instance fields.
1121 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001122 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 if (f != NULL) {
1124 return f;
1125 }
1126 }
1127 return NULL;
1128}
1129
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001130Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1131 // Is the field in this class, or any of its superclasses?
1132 // Interfaces are not relevant because they can't contain instance fields.
1133 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1134 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1135 if (f != NULL) {
1136 return f;
1137 }
1138 }
1139 return NULL;
1140}
1141
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001142Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1143 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001144 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001145 for (size_t i = 0; i < NumStaticFields(); ++i) {
1146 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001147 fh.ChangeField(f);
1148 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001149 return f;
1150 }
1151 }
1152 return NULL;
1153}
1154
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001155Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1156 if (dex_cache == GetDexCache()) {
1157 for (size_t i = 0; i < NumStaticFields(); ++i) {
1158 Field* f = GetStaticField(i);
1159 if (f->GetDexFieldIndex() == dex_field_idx) {
1160 return f;
1161 }
1162 }
1163 }
1164 return NULL;
1165}
1166
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001167Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1168 // Is the field in this class (or its interfaces), or any of its
1169 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001170 ClassHelper kh;
1171 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001172 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001173 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001174 if (f != NULL) {
1175 return f;
1176 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001177 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001178 kh.ChangeClass(k);
1179 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1180 Class* interface = kh.GetInterface(i);
1181 f = interface->FindDeclaredStaticField(name, type);
1182 if (f != NULL) {
1183 return f;
1184 }
1185 }
1186 }
1187 return NULL;
1188}
1189
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001190Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1191 ClassHelper kh;
1192 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1193 // Is the field in this class?
1194 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1195 if (f != NULL) {
1196 return f;
1197 }
1198 // Is this field in any of this class' interfaces?
1199 kh.ChangeClass(k);
1200 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1201 Class* interface = kh.GetInterface(i);
1202 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1203 if (f != NULL) {
1204 return f;
1205 }
1206 }
1207 }
1208 return NULL;
1209}
1210
Ian Rogersb067ac22011-12-13 18:05:09 -08001211Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1212 // Find a field using the JLS field resolution order
1213 ClassHelper kh;
1214 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1215 // Is the field in this class?
1216 Field* f = k->FindDeclaredInstanceField(name, type);
1217 if (f != NULL) {
1218 return f;
1219 }
1220 f = k->FindDeclaredStaticField(name, type);
1221 if (f != NULL) {
1222 return f;
1223 }
1224 // Is this field in any of this class' interfaces?
1225 kh.ChangeClass(k);
1226 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1227 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001228 f = interface->FindDeclaredStaticField(name, type);
1229 if (f != NULL) {
1230 return f;
1231 }
1232 }
1233 }
1234 return NULL;
1235}
1236
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001237Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001238 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001239 DCHECK_GE(component_count, 0);
1240 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001241
Ian Rogersa15e67d2012-02-28 13:51:55 -08001242 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001243 size_t data_size = component_count * component_size;
1244 size_t size = header_size + data_size;
1245
1246 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1247 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1248 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1249 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001250 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001251 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001252 return NULL;
1253 }
1254
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001255 Heap* heap = Runtime::Current()->GetHeap();
1256 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001257 if (array != NULL) {
1258 DCHECK(array->IsArrayInstance());
1259 array->SetLength(component_count);
1260 }
1261 return array;
1262}
1263
1264Array* Array::Alloc(Class* array_class, int32_t component_count) {
1265 return Alloc(array_class, component_count, array_class->GetComponentSize());
1266}
1267
Elliott Hughes80609252011-09-23 17:24:51 -07001268bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001269 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001270 "length=%i; index=%i", length_, index);
1271 return false;
1272}
1273
1274bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001275 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001276 "Can't store an element of type %s into an array of type %s",
1277 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1278 return false;
1279}
1280
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001281template<typename T>
1282PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001283 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001284 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1285 return down_cast<PrimitiveArray<T>*>(raw_array);
1286}
1287
1288template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1289
1290// Explicitly instantiate all the primitive array types.
1291template class PrimitiveArray<uint8_t>; // BooleanArray
1292template class PrimitiveArray<int8_t>; // ByteArray
1293template class PrimitiveArray<uint16_t>; // CharArray
1294template class PrimitiveArray<double>; // DoubleArray
1295template class PrimitiveArray<float>; // FloatArray
1296template class PrimitiveArray<int32_t>; // IntArray
1297template class PrimitiveArray<int64_t>; // LongArray
1298template class PrimitiveArray<int16_t>; // ShortArray
1299
Ian Rogers466bb252011-10-14 03:29:56 -07001300// Explicitly instantiate Class[][]
1301template class ObjectArray<ObjectArray<Class> >;
1302
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001303// TODO: get global references for these
1304Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001305
Brian Carlstroma663ea52011-08-19 23:33:41 -07001306void String::SetClass(Class* java_lang_String) {
1307 CHECK(java_lang_String_ == NULL);
1308 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001309 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001310}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001311
Brian Carlstroma663ea52011-08-19 23:33:41 -07001312void String::ResetClass() {
1313 CHECK(java_lang_String_ != NULL);
1314 java_lang_String_ = NULL;
1315}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001316
Brian Carlstromc74255f2011-09-11 22:47:39 -07001317String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001318 return Runtime::Current()->GetInternTable()->InternWeak(this);
1319}
1320
Brian Carlstrom395520e2011-09-25 19:35:00 -07001321int32_t String::GetHashCode() {
1322 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1323 if (result == 0) {
1324 ComputeHashCode();
1325 }
1326 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1327 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1328 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001329 return result;
1330}
1331
1332int32_t String::GetLength() const {
1333 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1334 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1335 return result;
1336}
1337
1338uint16_t String::CharAt(int32_t index) const {
1339 // TODO: do we need this? Equals is the only caller, and could
1340 // bounds check itself.
1341 if (index < 0 || index >= count_) {
1342 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001343 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001344 "length=%i; index=%i", count_, index);
1345 return 0;
1346 }
1347 return GetCharArray()->Get(index + GetOffset());
1348}
1349
1350String* String::AllocFromUtf16(int32_t utf16_length,
1351 const uint16_t* utf16_data_in,
1352 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001353 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001354 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001355 if (string == NULL) {
1356 return NULL;
1357 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001358 // TODO: use 16-bit wide memset variant
1359 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001360 if (array == NULL) {
1361 return NULL;
1362 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363 for (int i = 0; i < utf16_length; i++) {
1364 array->Set(i, utf16_data_in[i]);
1365 }
1366 if (hash_code != 0) {
1367 string->SetHashCode(hash_code);
1368 } else {
1369 string->ComputeHashCode();
1370 }
1371 return string;
1372}
1373
1374String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001375 if (utf == NULL) {
1376 return NULL;
1377 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001378 size_t char_count = CountModifiedUtf8Chars(utf);
1379 return AllocFromModifiedUtf8(char_count, utf);
1380}
1381
1382String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1383 const char* utf8_data_in) {
1384 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001385 if (string == NULL) {
1386 return NULL;
1387 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001388 uint16_t* utf16_data_out =
1389 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1390 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1391 string->ComputeHashCode();
1392 return string;
1393}
1394
1395String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001396 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1397 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001398 return NULL;
1399 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001400 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001401}
1402
1403String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001404 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001405 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001406 if (string == NULL) {
1407 return NULL;
1408 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001409 string->SetArray(array);
1410 string->SetCount(array->GetLength());
1411 return string;
1412}
1413
1414bool String::Equals(const String* that) const {
1415 if (this == that) {
1416 // Quick reference equality test
1417 return true;
1418 } else if (that == NULL) {
1419 // Null isn't an instanceof anything
1420 return false;
1421 } else if (this->GetLength() != that->GetLength()) {
1422 // Quick length inequality test
1423 return false;
1424 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001425 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001426 // hash code was already equal
1427 for (int32_t i = 0; i < that->GetLength(); ++i) {
1428 if (this->CharAt(i) != that->CharAt(i)) {
1429 return false;
1430 }
1431 }
1432 return true;
1433 }
1434}
1435
Elliott Hughes5d78d392011-12-13 16:53:05 -08001436bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001437 if (this->GetLength() != that_length) {
1438 return false;
1439 } else {
1440 for (int32_t i = 0; i < that_length; ++i) {
1441 if (this->CharAt(i) != that_chars[that_offset + i]) {
1442 return false;
1443 }
1444 }
1445 return true;
1446 }
1447}
1448
1449bool String::Equals(const char* modified_utf8) const {
1450 for (int32_t i = 0; i < GetLength(); ++i) {
1451 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1452 if (ch == '\0' || ch != CharAt(i)) {
1453 return false;
1454 }
1455 }
1456 return *modified_utf8 == '\0';
1457}
1458
1459bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001460 if (modified_utf8.size() != GetLength()) {
1461 return false;
1462 }
1463 const char* p = modified_utf8.data();
1464 for (int32_t i = 0; i < GetLength(); ++i) {
1465 uint16_t ch = GetUtf16FromUtf8(&p);
1466 if (ch != CharAt(i)) {
1467 return false;
1468 }
1469 }
1470 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001471}
1472
1473// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1474std::string String::ToModifiedUtf8() const {
1475 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1476 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1477 std::string result(byte_count, char(0));
1478 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1479 return result;
1480}
1481
Ian Rogers1c5eb702012-02-01 09:18:34 -08001482void Throwable::SetCause(Throwable* cause) {
1483 CHECK(cause != NULL);
1484 CHECK(cause != this);
1485 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1486 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1487}
1488
Ian Rogers466bb252011-10-14 03:29:56 -07001489bool Throwable::IsCheckedException() const {
1490 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1491 if (InstanceOf(error)) {
1492 return false;
1493 }
1494 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1495 return !InstanceOf(jlre);
1496}
1497
Ian Rogers9074b992011-10-26 17:41:55 -07001498std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001499 std::string result(PrettyTypeOf(this));
1500 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001501 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001502 if (msg != NULL) {
1503 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001504 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001505 result += "\n";
1506 Object* stack_state = GetStackState();
1507 // check stack state isn't missing or corrupt
1508 if (stack_state != NULL && stack_state->IsObjectArray()) {
1509 // Decode the internal stack trace into the depth and method trace
1510 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1511 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001512 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1513 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001514 for (int32_t i = 0; i < depth; ++i) {
1515 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001516 mh.ChangeMethod(method);
1517 uint32_t native_pc = pc_trace->Get(i);
1518 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1519 const char* source_file = mh.GetDeclaringClassSourceFile();
1520 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1521 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001522 }
Ian Rogers9074b992011-10-26 17:41:55 -07001523 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001524 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001525 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001526 result += "Caused by: ";
1527 result += cause->Dump();
1528 }
Ian Rogers9074b992011-10-26 17:41:55 -07001529 return result;
1530}
1531
Ian Rogers5167c972012-02-03 10:41:20 -08001532
1533Class* Throwable::java_lang_Throwable_ = NULL;
1534
1535void Throwable::SetClass(Class* java_lang_Throwable) {
1536 CHECK(java_lang_Throwable_ == NULL);
1537 CHECK(java_lang_Throwable != NULL);
1538 java_lang_Throwable_ = java_lang_Throwable;
1539}
1540
1541void Throwable::ResetClass() {
1542 CHECK(java_lang_Throwable_ != NULL);
1543 java_lang_Throwable_ = NULL;
1544}
1545
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001546Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1547
1548void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1549 CHECK(java_lang_StackTraceElement_ == NULL);
1550 CHECK(java_lang_StackTraceElement != NULL);
1551 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1552}
1553
1554void StackTraceElement::ResetClass() {
1555 CHECK(java_lang_StackTraceElement_ != NULL);
1556 java_lang_StackTraceElement_ = NULL;
1557}
1558
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001559StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1560 String* method_name,
1561 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001562 int32_t line_number) {
1563 StackTraceElement* trace =
1564 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1565 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1566 const_cast<String*>(declaring_class), false);
1567 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1568 const_cast<String*>(method_name), false);
1569 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1570 const_cast<String*>(file_name), false);
1571 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1572 line_number, false);
1573 return trace;
1574}
1575
Elliott Hughes1f359b02011-07-17 14:27:17 -07001576static const char* kClassStatusNames[] = {
1577 "Error",
1578 "NotReady",
1579 "Idx",
1580 "Loaded",
1581 "Resolved",
1582 "Verifying",
1583 "Verified",
1584 "Initializing",
1585 "Initialized"
1586};
1587std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1588 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001589 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001590 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001591 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001592 }
1593 return os;
1594}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001595
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001596} // namespace art