blob: c9e09299b81c8a258beeed7ff333277fde33c0d7 [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();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070060 SirtRef<Object> copy(Heap::AllocObject(c, num_bytes));
61 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070062 return NULL;
63 }
64
65 // Copy instance data. We assume memcpy copies by words.
66 // TODO: expose and use move32.
67 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070068 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070069 size_t offset = sizeof(Object);
70 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
71
Elliott Hughes20cde902011-10-04 17:37:27 -070072 if (c->IsFinalizable()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -070073 Heap::AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070074 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070075
Brian Carlstrom40381fb2011-10-19 14:13:40 -070076 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070077}
78
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070079uint32_t Object::GetThinLockId() {
80 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070081}
82
83void Object::MonitorEnter(Thread* thread) {
84 Monitor::MonitorEnter(thread, this);
85}
86
Ian Rogersff1ed472011-09-20 13:46:24 -070087bool Object::MonitorExit(Thread* thread) {
88 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070089}
90
91void Object::Notify() {
92 Monitor::Notify(Thread::Current(), this);
93}
94
95void Object::NotifyAll() {
96 Monitor::NotifyAll(Thread::Current(), this);
97}
98
99void Object::Wait(int64_t ms, int32_t ns) {
100 Monitor::Wait(Thread::Current(), this, ms, ns, true);
101}
102
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700103// TODO: get global references for these
104Class* Field::java_lang_reflect_Field_ = NULL;
105
106void Field::SetClass(Class* java_lang_reflect_Field) {
107 CHECK(java_lang_reflect_Field_ == NULL);
108 CHECK(java_lang_reflect_Field != NULL);
109 java_lang_reflect_Field_ = java_lang_reflect_Field;
110}
111
112void Field::ResetClass() {
113 CHECK(java_lang_reflect_Field_ != NULL);
114 java_lang_reflect_Field_ = NULL;
115}
116
Ian Rogers0571d352011-11-03 19:51:38 -0700117void Field::SetOffset(MemberOffset num_bytes) {
118 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800119#if 0 // TODO enable later in boot and under !NDEBUG
120 FieldHelper fh(this);
121 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700122 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
123 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
124 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800125#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700126 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
127}
128
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700129uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700130 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700131 if (IsStatic()) {
132 object = declaring_class_;
133 }
134 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700135}
136
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700137void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700138 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700139 if (IsStatic()) {
140 object = declaring_class_;
141 }
142 object->SetField32(GetOffset(), new_value, IsVolatile());
143}
144
145uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700146 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700147 if (IsStatic()) {
148 object = declaring_class_;
149 }
150 return object->GetField64(GetOffset(), IsVolatile());
151}
152
153void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700154 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 if (IsStatic()) {
156 object = declaring_class_;
157 }
158 object->SetField64(GetOffset(), new_value, IsVolatile());
159}
160
161Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700162 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700163 if (IsStatic()) {
164 object = declaring_class_;
165 }
166 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
167}
168
169void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700170 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700171 if (IsStatic()) {
172 object = declaring_class_;
173 }
174 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
175}
176
177bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800178 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
179 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700180 return Get32(object);
181}
182
183void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800184 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
185 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186 Set32(object, z);
187}
188
189int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800190 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
191 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 return Get32(object);
193}
194
195void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800196 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
197 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 Set32(object, b);
199}
200
201uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800202 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
203 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 return Get32(object);
205}
206
207void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800208 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
209 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 Set32(object, c);
211}
212
Ian Rogers466bb252011-10-14 03:29:56 -0700213int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800214 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
215 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 return Get32(object);
217}
218
Ian Rogers466bb252011-10-14 03:29:56 -0700219void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800220 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
221 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700222 Set32(object, s);
223}
224
225int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800226 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
227 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 return Get32(object);
229}
230
231void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800232 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
233 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234 Set32(object, i);
235}
236
237int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800238 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
239 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 return Get64(object);
241}
242
243void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
245 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700246 Set64(object, j);
247}
248
249float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800250 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
251 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700252 JValue float_bits;
253 float_bits.i = Get32(object);
254 return float_bits.f;
255}
256
257void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
259 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700260 JValue float_bits;
261 float_bits.f = f;
262 Set32(object, float_bits.i);
263}
264
265double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800266 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
267 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700268 JValue double_bits;
269 double_bits.j = Get64(object);
270 return double_bits.d;
271}
272
273void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
275 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700276 JValue double_bits;
277 double_bits.d = d;
278 Set64(object, double_bits.j);
279}
280
281Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800282 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
283 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700284 return GetObj(object);
285}
286
287void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800288 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
289 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700290 SetObj(object, l);
291}
292
293// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700294Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700295Class* Method::java_lang_reflect_Method_ = NULL;
296
Elliott Hughes80609252011-09-23 17:24:51 -0700297void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
298 CHECK(java_lang_reflect_Constructor_ == NULL);
299 CHECK(java_lang_reflect_Constructor != NULL);
300 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
301
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 CHECK(java_lang_reflect_Method_ == NULL);
303 CHECK(java_lang_reflect_Method != NULL);
304 java_lang_reflect_Method_ = java_lang_reflect_Method;
305}
306
Elliott Hughes80609252011-09-23 17:24:51 -0700307void Method::ResetClasses() {
308 CHECK(java_lang_reflect_Constructor_ != NULL);
309 java_lang_reflect_Constructor_ = NULL;
310
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 CHECK(java_lang_reflect_Method_ != NULL);
312 java_lang_reflect_Method_ = NULL;
313}
314
Elliott Hughes418d20f2011-09-22 14:00:39 -0700315Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
316 if (*p == '[') {
317 // Something like "[[[Ljava/lang/String;".
318 const char* start = p;
319 while (*p == '[') {
320 ++p;
321 }
322 if (*p == 'L') {
323 while (*p != ';') {
324 ++p;
325 }
326 }
327 ++p; // Either the ';' or the primitive type.
328
Brian Carlstromaded5f72011-10-07 17:15:04 -0700329 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800330 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700331 } else if (*p == 'L') {
332 const char* start = p;
333 while (*p != ';') {
334 ++p;
335 }
336 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800337 std::string descriptor(start, (p - start));
338 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700339 } else {
340 return class_linker->FindPrimitiveClass(*p++);
341 }
342}
343
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700344ObjectArray<String>* Method::GetDexCacheStrings() const {
345 return GetFieldObject<ObjectArray<String>*>(
346 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
347}
348
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
350 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
351 new_dex_cache_strings, false);
352}
353
Ian Rogers19846512012-02-24 11:42:47 -0800354ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
355 return GetFieldObject<ObjectArray<Method>*>(
356 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
357}
358
359void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
360 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
361 new_dex_cache_methods, false);
362}
363
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700364ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
365 return GetFieldObject<ObjectArray<Class>*>(
366 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
367}
368
369void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
370 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
371 new_dex_cache_classes, false);
372}
373
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
375 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
376 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
377 false);
378}
379
380void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700381 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383}
384
Logan Chienfca7e872011-12-20 20:08:22 +0800385#if defined(ART_USE_LLVM_COMPILER)
386
387const InferredRegCategoryMap* Method::GetInferredRegCategoryMap() const {
388 const InferredRegCategoryMap* map = GetFieldPtr<const InferredRegCategoryMap*>(
389 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
390 DCHECK(map != NULL) << PrettyMethod(this);
391 return map;
392}
393
394void Method::SetInferredRegCategoryMap(const InferredRegCategoryMap* map) {
Logan Chiena1854662012-02-17 09:52:18 +0800395 const InferredRegCategoryMap* existing_map = GetFieldPtr<const InferredRegCategoryMap*>(
396 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
Logan Chienfca7e872011-12-20 20:08:22 +0800397
398 DCHECK(existing_map == NULL) << PrettyMethod(this);
399 DCHECK(map != NULL) << PrettyMethod(this);
400
401 // TODO: Remove if we won't find any use of InferredRegCategoryMap at runtime.
402 SetFieldPtr<const InferredRegCategoryMap*>(
403 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), map, false);
404}
405
406void Method::ResetInferredRegCategoryMap() {
407 delete GetInferredRegCategoryMap();
408 SetFieldPtr<const InferredRegCategoryMap*>(
Logan Chiena1854662012-02-17 09:52:18 +0800409 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), NULL, false);
Logan Chienfca7e872011-12-20 20:08:22 +0800410}
411
412#endif
413
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700414size_t Method::NumArgRegisters(const StringPiece& shorty) {
415 CHECK_LE(1, shorty.length());
416 uint32_t num_registers = 0;
417 for (int i = 1; i < shorty.length(); ++i) {
418 char ch = shorty[i];
419 if (ch == 'D' || ch == 'J') {
420 num_registers += 2;
421 } else {
422 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700423 }
424 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 return num_registers;
426}
427
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800428bool Method::IsProxyMethod() const {
429 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430}
431
Ian Rogers466bb252011-10-14 03:29:56 -0700432Method* Method::FindOverriddenMethod() const {
433 if (IsStatic()) {
434 return NULL;
435 }
436 Class* declaring_class = GetDeclaringClass();
437 Class* super_class = declaring_class->GetSuperClass();
438 uint16_t method_index = GetMethodIndex();
439 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
440 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800441 // Did this method override a super class method? If so load the result from the super class'
442 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700443 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
444 result = super_class_vtable->Get(method_index);
445 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800447 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800448 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
449 CHECK_EQ(result,
450 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800451 } else {
452 MethodHelper mh(this);
453 MethodHelper interface_mh;
454 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
455 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
456 InterfaceEntry* entry = iftable->Get(i);
457 Class* interface = entry->GetInterface();
458 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
459 Method* interface_method = interface->GetVirtualMethod(j);
460 interface_mh.ChangeMethod(interface_method);
461 if (mh.HasSameNameAndSignature(&interface_mh)) {
462 result = interface_method;
463 break;
464 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800465 }
466 }
Ian Rogers466bb252011-10-14 03:29:56 -0700467 }
468 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800469#ifndef NDEBUG
470 MethodHelper result_mh(result);
471 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
472#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700473 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474}
475
Ian Rogersbdb03912011-09-14 00:55:44 -0700476uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700478 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800479 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700480 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700481 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 size_t mapping_table_length = GetMappingTableLength();
jeffhao26c0a1a2012-01-17 16:28:33 -0800483 const void* code_offset;
Ian Rogers19846512012-02-24 11:42:47 -0800484 Runtime* runtime = Runtime::Current();
485 if (runtime->IsMethodTracingActive() &&
486 runtime->GetTracer()->GetSavedCodeFromMap(this) != NULL) {
487 code_offset = runtime->GetTracer()->GetSavedCodeFromMap(this);
jeffhao26c0a1a2012-01-17 16:28:33 -0800488 } else {
489 code_offset = GetCode();
490 }
Ian Rogers19846512012-02-24 11:42:47 -0800491 if (code_offset == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData() ||
492 code_offset == runtime->GetResolutionStubArray(Runtime::kInstanceMethod)->GetData()) {
493 code_offset = runtime->GetClassLinker()->GetOatCodeFor(this);
494 }
jeffhaoe343b762011-12-05 16:36:44 -0800495 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code_offset);
Ian Rogersbdb03912011-09-14 00:55:44 -0700496 uint32_t best_offset = 0;
497 uint32_t best_dex_offset = 0;
498 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700499 uint32_t map_offset = mapping_table[i];
500 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700501 if (map_offset == sought_offset) {
502 best_offset = map_offset;
503 best_dex_offset = map_dex_offset;
504 break;
505 }
506 if (map_offset < sought_offset && map_offset > best_offset) {
507 best_offset = map_offset;
508 best_dex_offset = map_dex_offset;
509 }
510 }
511 return best_dex_offset;
512}
513
514uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700515 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700516 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700517 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700518 return 0; // Special no mapping/pc == 0 case
519 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700520 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700521 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700522 uint32_t map_offset = mapping_table[i];
523 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700524 if (map_dex_offset == dex_pc) {
jeffhao2692b572011-12-16 15:42:28 -0800525 if (Runtime::Current()->IsMethodTracingActive()) {
526 Trace* tracer = Runtime::Current()->GetTracer();
527 return reinterpret_cast<uintptr_t>(tracer->GetSavedCodeFromMap(this)) + map_offset;
jeffhaoe343b762011-12-05 16:36:44 -0800528 } else {
529 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
530 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700531 }
532 }
533 LOG(FATAL) << "Looking up Dex PC not contained in method";
534 return 0;
535}
536
537uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800538 MethodHelper mh(this);
539 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700540 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700541 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
542 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700543 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700544 if (iter_type_idx == DexFile::kDexNoIndex16) {
545 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700546 }
547 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800548 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700549 if (iter_exception_type == NULL) {
550 // The verifier should take care of resolving all exception classes early
551 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800552 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700553 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700554 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700555 }
556 }
557 // Handler not found
558 return DexFile::kDexNoIndex;
559}
560
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700561void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
562 // Push a transition back into managed code onto the linked list in thread.
563 CHECK_EQ(Thread::kRunnable, self->GetState());
564 NativeToManagedRecord record;
565 self->PushNativeToManagedRecord(&record);
566
567 // Call the invoke stub associated with the method.
568 // Pass everything as arguments.
569 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700570
571 bool have_executable_code = (GetCode() != NULL);
572#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700573 // Currently we can only compile non-native methods for ARM.
574 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700575#endif
576
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500577 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700578 bool log = false;
579 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800580 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
581 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700582 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700583 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700584 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800585 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
586 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700587 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700588 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800589 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
590 PrettyMethod(this).c_str(), GetCode(), stub,
591 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700592 if (result != NULL) {
593 result->j = 0;
594 }
595 }
596
597 // Pop transition.
598 self->PopNativeToManagedRecord(record);
599}
600
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700601bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700602 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800603 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800604 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700605 return native_method != jni_stub;
606}
607
Ian Rogers60db5ab2012-02-20 17:02:00 -0800608void Method::RegisterNative(Thread* self, const void* native_method) {
609 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700610 CHECK(IsNative()) << PrettyMethod(this);
611 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers60db5ab2012-02-20 17:02:00 -0800612 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
613 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
614 native_method, false);
615 } else {
616 // We've been asked to associate this method with the given native method but are working
617 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
618 // the native method to runtime support and store the target somewhere runtime support will
619 // find it.
620#if defined(__arm__)
621 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
622 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
623#else
624 UNIMPLEMENTED(FATAL);
625#endif
626 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
627 reinterpret_cast<const uint8_t*>(native_method), false);
628 }
Brian Carlstrom16192862011-09-12 17:50:06 -0700629}
630
Ian Rogers19846512012-02-24 11:42:47 -0800631void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700632 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700633 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800634 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700635}
636
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700637void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700638 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
639 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700640 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800641 if (new_status == kStatusError) {
642 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
643
644 // stash current exception
645 Thread* self = Thread::Current();
646 SirtRef<Throwable> exception(self->GetException());
647 CHECK(exception.get() != NULL);
648
649 // clear exception to call FindSystemClass
650 self->ClearException();
651 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
652 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
653 CHECK(!self->IsExceptionPending());
654
655 // only verification errors, not initialization problems, should set a verify error.
656 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
657 Class* exception_class = exception->GetClass();
658 if (!eiie_class->IsAssignableFrom(exception_class)) {
659 SetVerifyErrorClass(exception_class);
660 }
661
662 // restore exception
663 self->SetException(exception.get());
664 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700665 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700666}
667
668DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700669 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700670}
671
672void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700673 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700674}
675
Brian Carlstrom1f870082011-08-23 16:02:11 -0700676Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700677 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700678 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500679 // TODO: decide whether we want this check. It currently fails during bootstrap.
680 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700681 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700682 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700683}
684
Ian Rogers0571d352011-11-03 19:51:38 -0700685void Class::SetClassSize(size_t new_class_size) {
686 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
687 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
688}
689
Ian Rogersd418eda2012-01-30 12:14:28 -0800690// Return the class' name. The exact format is bizarre, but it's the specified behavior for
691// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
692// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
693// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
694String* Class::ComputeName() {
695 String* name = GetName();
696 if (name != NULL) {
697 return name;
698 }
699 std::string descriptor(ClassHelper(this).GetDescriptor());
700 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
701 // The descriptor indicates that this is the class for
702 // a primitive type; special-case the return value.
703 const char* c_name = NULL;
704 switch (descriptor[0]) {
705 case 'Z': c_name = "boolean"; break;
706 case 'B': c_name = "byte"; break;
707 case 'C': c_name = "char"; break;
708 case 'S': c_name = "short"; break;
709 case 'I': c_name = "int"; break;
710 case 'J': c_name = "long"; break;
711 case 'F': c_name = "float"; break;
712 case 'D': c_name = "double"; break;
713 case 'V': c_name = "void"; break;
714 default:
715 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
716 }
717 name = String::AllocFromModifiedUtf8(c_name);
718 } else {
719 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
720 // components.
721 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
722 descriptor.erase(0, 1);
723 descriptor.erase(descriptor.size() - 1);
724 }
725 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
726 name = String::AllocFromModifiedUtf8(descriptor.c_str());
727 }
728 SetName(name);
729 return name;
730}
731
Elliott Hughes4681c802011-09-25 18:04:37 -0700732void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700733 if ((flags & kDumpClassFullDetail) == 0) {
734 os << PrettyClass(this);
735 if ((flags & kDumpClassClassLoader) != 0) {
736 os << ' ' << GetClassLoader();
737 }
738 if ((flags & kDumpClassInitialized) != 0) {
739 os << ' ' << GetStatus();
740 }
Elliott Hughese0918552011-10-28 17:18:29 -0700741 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700742 return;
743 }
744
745 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800746 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700747 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800748 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700749 os << " objectSize=" << SizeOf() << " "
750 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
751 os << StringPrintf(" access=0x%04x.%04x\n",
752 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
753 if (super != NULL) {
754 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
755 }
756 if (IsArrayClass()) {
757 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
758 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800759 if (kh.NumInterfaces() > 0) {
760 os << " interfaces (" << kh.NumInterfaces() << "):\n";
761 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
762 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700763 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800764 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700765 }
766 }
767 os << " vtable (" << NumVirtualMethods() << " entries, "
768 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
769 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800770 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700771 }
772 os << " direct methods (" << NumDirectMethods() << " entries):\n";
773 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800774 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700775 }
776 if (NumStaticFields() > 0) {
777 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700778 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700779 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800780 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700781 }
782 } else {
783 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700784 }
785 }
786 if (NumInstanceFields() > 0) {
787 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700788 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700789 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800790 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700791 }
792 } else {
793 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700794 }
795 }
796}
797
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700798void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
799 if (new_reference_offsets != CLASS_WALK_SUPER) {
800 // Sanity check that the number of bits set in the reference offset bitmap
801 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800802 size_t count = 0;
803 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
804 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700805 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800806 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700807 }
808 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
809 new_reference_offsets, false);
810}
811
812void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
813 if (new_reference_offsets != CLASS_WALK_SUPER) {
814 // Sanity check that the number of bits set in the reference offset bitmap
815 // agrees with the number of references
816 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
817 NumReferenceStaticFieldsDuringLinking());
818 }
819 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
820 new_reference_offsets, false);
821}
822
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700823bool Class::Implements(const Class* klass) const {
824 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700825 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700826 // All interfaces implemented directly and by our superclass, and
827 // recursively all super-interfaces of those interfaces, are listed
828 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700829 int32_t iftable_count = GetIfTableCount();
830 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
831 for (int32_t i = 0; i < iftable_count; i++) {
832 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700833 return true;
834 }
835 }
836 return false;
837}
838
839// Determine whether "this" is assignable from "klazz", where both of these
840// are array classes.
841//
842// Consider an array class, e.g. Y[][], where Y is a subclass of X.
843// Y[][] = Y[][] --> true (identity)
844// X[][] = Y[][] --> true (element superclass)
845// Y = Y[][] --> false
846// Y[] = Y[][] --> false
847// Object = Y[][] --> true (everything is an object)
848// Object[] = Y[][] --> true
849// Object[][] = Y[][] --> true
850// Object[][][] = Y[][] --> false (too many []s)
851// Serializable = Y[][] --> true (all arrays are Serializable)
852// Serializable[] = Y[][] --> true
853// Serializable[][] = Y[][] --> false (unless Y is Serializable)
854//
855// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700856// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700857//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700858bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700859 DCHECK(IsArrayClass()) << PrettyClass(this);
860 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700861 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700862}
863
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700864bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700865 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
866 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700867 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700868 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700869 // src's super should be java_lang_Object, since it is an array.
870 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700871 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
872 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700873 return this == java_lang_Object;
874 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700875 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700876}
877
878bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700879 DCHECK(!IsInterface()) << PrettyClass(this);
880 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700881 const Class* current = this;
882 do {
883 if (current == klass) {
884 return true;
885 }
886 current = current->GetSuperClass();
887 } while (current != NULL);
888 return false;
889}
890
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800891bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700892 size_t i = 0;
893 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
894 ++i;
895 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700896 if (descriptor1.find('/', i) != StringPiece::npos ||
897 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700898 return false;
899 } else {
900 return true;
901 }
902}
903
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700904#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700905bool Class::IsInSamePackage(const StringPiece& descriptor1,
906 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700907 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700908 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700909 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
910 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700911 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
912}
913#endif
914
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700915bool Class::IsInSamePackage(const Class* that) const {
916 const Class* klass1 = this;
917 const Class* klass2 = that;
918 if (klass1 == klass2) {
919 return true;
920 }
921 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700922 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700923 return false;
924 }
925 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700926 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700927 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700928 }
jeffhao4a801a42011-09-23 13:53:40 -0700929 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700930 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700931 }
932 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800933 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800934 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800935 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800936 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800937 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700938}
939
Elliott Hughesdbb40792011-11-18 17:05:22 -0800940bool Class::IsClassClass() const {
941 Class* java_lang_Class = GetClass()->GetClass();
942 return this == java_lang_Class;
943}
944
945bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800946 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800947}
948
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800949bool Class::IsThrowableClass() const {
950 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
951 return throwable->IsAssignableFrom(this);
952}
953
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800954ClassLoader* Class::GetClassLoader() const {
955 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700956}
957
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700958void Class::SetClassLoader(const ClassLoader* new_cl) {
959 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700961}
962
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800963Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700964 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700965 DCHECK(declaring_class != NULL) << PrettyClass(this);
966 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700967 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700968 int32_t iftable_count = GetIfTableCount();
969 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
970 for (int32_t i = 0; i < iftable_count; i++) {
971 InterfaceEntry* interface_entry = iftable->Get(i);
972 if (interface_entry->GetInterface() == declaring_class) {
973 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700974 }
975 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700976 return NULL;
977}
978
Ian Rogers466bb252011-10-14 03:29:56 -0700979Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700980 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800981 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700982 if (method != NULL) {
983 return method;
984 }
985
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700986 int32_t iftable_count = GetIfTableCount();
987 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
988 for (int32_t i = 0; i < iftable_count; i++) {
989 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700990 if (method != NULL) {
991 return method;
992 }
993 }
994 return NULL;
995}
996
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800997Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
998 // Check the current class before checking the interfaces.
999 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1000 if (method != NULL) {
1001 return method;
1002 }
1003
1004 int32_t iftable_count = GetIfTableCount();
1005 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1006 for (int32_t i = 0; i < iftable_count; i++) {
1007 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
1008 if (method != NULL) {
1009 return method;
1010 }
1011 }
1012 return NULL;
1013}
1014
1015
1016Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001017 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001018 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001019 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001020 mh.ChangeMethod(method);
1021 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001022 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001023 }
1024 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001025 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001026}
1027
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001028Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1029 if (GetDexCache() == dex_cache) {
1030 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1031 Method* method = GetDirectMethod(i);
1032 if (method->GetDexMethodIndex() == dex_method_idx) {
1033 return method;
1034 }
1035 }
1036 }
1037 return NULL;
1038}
1039
1040Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1041 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001042 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001043 if (method != NULL) {
1044 return method;
1045 }
1046 }
1047 return NULL;
1048}
1049
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001050Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1051 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1052 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1053 if (method != NULL) {
1054 return method;
1055 }
1056 }
1057 return NULL;
1058}
1059
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001060Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001061 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001062 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001063 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001064 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001065 mh.ChangeMethod(method);
1066 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001067 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001068 }
1069 }
1070 return NULL;
1071}
1072
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001073Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1074 if (GetDexCache() == dex_cache) {
1075 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1076 Method* method = GetVirtualMethod(i);
1077 if (method->GetDexMethodIndex() == dex_method_idx) {
1078 return method;
1079 }
1080 }
1081 }
1082 return NULL;
1083}
1084
Ian Rogers466bb252011-10-14 03:29:56 -07001085Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1086 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1087 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1088 if (method != NULL) {
1089 return method;
1090 }
1091 }
1092 return NULL;
1093}
1094
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001095Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1096 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1097 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1098 if (method != NULL) {
1099 return method;
1100 }
1101 }
1102 return NULL;
1103}
1104
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001105Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 // Is the field in this class?
1107 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001108 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1110 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001111 fh.ChangeField(f);
1112 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001113 return f;
1114 }
1115 }
1116 return NULL;
1117}
1118
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001119Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1120 if (GetDexCache() == dex_cache) {
1121 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1122 Field* f = GetInstanceField(i);
1123 if (f->GetDexFieldIndex() == dex_field_idx) {
1124 return f;
1125 }
1126 }
1127 }
1128 return NULL;
1129}
1130
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001131Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 // Is the field in this class, or any of its superclasses?
1133 // Interfaces are not relevant because they can't contain instance fields.
1134 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001135 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 if (f != NULL) {
1137 return f;
1138 }
1139 }
1140 return NULL;
1141}
1142
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001143Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1144 // Is the field in this class, or any of its superclasses?
1145 // Interfaces are not relevant because they can't contain instance fields.
1146 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1147 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1148 if (f != NULL) {
1149 return f;
1150 }
1151 }
1152 return NULL;
1153}
1154
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001155Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1156 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001157 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001158 for (size_t i = 0; i < NumStaticFields(); ++i) {
1159 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001160 fh.ChangeField(f);
1161 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001162 return f;
1163 }
1164 }
1165 return NULL;
1166}
1167
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001168Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1169 if (dex_cache == GetDexCache()) {
1170 for (size_t i = 0; i < NumStaticFields(); ++i) {
1171 Field* f = GetStaticField(i);
1172 if (f->GetDexFieldIndex() == dex_field_idx) {
1173 return f;
1174 }
1175 }
1176 }
1177 return NULL;
1178}
1179
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001180Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1181 // Is the field in this class (or its interfaces), or any of its
1182 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001183 ClassHelper kh;
1184 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001185 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001186 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001187 if (f != NULL) {
1188 return f;
1189 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001190 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001191 kh.ChangeClass(k);
1192 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1193 Class* interface = kh.GetInterface(i);
1194 f = interface->FindDeclaredStaticField(name, type);
1195 if (f != NULL) {
1196 return f;
1197 }
1198 }
1199 }
1200 return NULL;
1201}
1202
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001203Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1204 ClassHelper kh;
1205 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1206 // Is the field in this class?
1207 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1208 if (f != NULL) {
1209 return f;
1210 }
1211 // Is this field in any of this class' interfaces?
1212 kh.ChangeClass(k);
1213 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1214 Class* interface = kh.GetInterface(i);
1215 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1216 if (f != NULL) {
1217 return f;
1218 }
1219 }
1220 }
1221 return NULL;
1222}
1223
Ian Rogersb067ac22011-12-13 18:05:09 -08001224Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1225 // Find a field using the JLS field resolution order
1226 ClassHelper kh;
1227 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1228 // Is the field in this class?
1229 Field* f = k->FindDeclaredInstanceField(name, type);
1230 if (f != NULL) {
1231 return f;
1232 }
1233 f = k->FindDeclaredStaticField(name, type);
1234 if (f != NULL) {
1235 return f;
1236 }
1237 // Is this field in any of this class' interfaces?
1238 kh.ChangeClass(k);
1239 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1240 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001241 f = interface->FindDeclaredStaticField(name, type);
1242 if (f != NULL) {
1243 return f;
1244 }
1245 }
1246 }
1247 return NULL;
1248}
1249
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001250Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001251 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001252 DCHECK_GE(component_count, 0);
1253 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001254
Ian Rogersa15e67d2012-02-28 13:51:55 -08001255 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001256 size_t data_size = component_count * component_size;
1257 size_t size = header_size + data_size;
1258
1259 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1260 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1261 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1262 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001263 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001264 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001265 return NULL;
1266 }
1267
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001268 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1269 if (array != NULL) {
1270 DCHECK(array->IsArrayInstance());
1271 array->SetLength(component_count);
1272 }
1273 return array;
1274}
1275
1276Array* Array::Alloc(Class* array_class, int32_t component_count) {
1277 return Alloc(array_class, component_count, array_class->GetComponentSize());
1278}
1279
Elliott Hughes80609252011-09-23 17:24:51 -07001280bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001281 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001282 "length=%i; index=%i", length_, index);
1283 return false;
1284}
1285
1286bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001287 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001288 "Can't store an element of type %s into an array of type %s",
1289 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1290 return false;
1291}
1292
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001293template<typename T>
1294PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001295 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001296 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1297 return down_cast<PrimitiveArray<T>*>(raw_array);
1298}
1299
1300template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1301
1302// Explicitly instantiate all the primitive array types.
1303template class PrimitiveArray<uint8_t>; // BooleanArray
1304template class PrimitiveArray<int8_t>; // ByteArray
1305template class PrimitiveArray<uint16_t>; // CharArray
1306template class PrimitiveArray<double>; // DoubleArray
1307template class PrimitiveArray<float>; // FloatArray
1308template class PrimitiveArray<int32_t>; // IntArray
1309template class PrimitiveArray<int64_t>; // LongArray
1310template class PrimitiveArray<int16_t>; // ShortArray
1311
Ian Rogers466bb252011-10-14 03:29:56 -07001312// Explicitly instantiate Class[][]
1313template class ObjectArray<ObjectArray<Class> >;
1314
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001315// TODO: get global references for these
1316Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001317
Brian Carlstroma663ea52011-08-19 23:33:41 -07001318void String::SetClass(Class* java_lang_String) {
1319 CHECK(java_lang_String_ == NULL);
1320 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001321 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001322}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001323
Brian Carlstroma663ea52011-08-19 23:33:41 -07001324void String::ResetClass() {
1325 CHECK(java_lang_String_ != NULL);
1326 java_lang_String_ = NULL;
1327}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001328
Brian Carlstromc74255f2011-09-11 22:47:39 -07001329String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001330 return Runtime::Current()->GetInternTable()->InternWeak(this);
1331}
1332
Brian Carlstrom395520e2011-09-25 19:35:00 -07001333int32_t String::GetHashCode() {
1334 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1335 if (result == 0) {
1336 ComputeHashCode();
1337 }
1338 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1339 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1340 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001341 return result;
1342}
1343
1344int32_t String::GetLength() const {
1345 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1346 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1347 return result;
1348}
1349
1350uint16_t String::CharAt(int32_t index) const {
1351 // TODO: do we need this? Equals is the only caller, and could
1352 // bounds check itself.
1353 if (index < 0 || index >= count_) {
1354 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001355 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001356 "length=%i; index=%i", count_, index);
1357 return 0;
1358 }
1359 return GetCharArray()->Get(index + GetOffset());
1360}
1361
1362String* String::AllocFromUtf16(int32_t utf16_length,
1363 const uint16_t* utf16_data_in,
1364 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001365 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001366 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001367 if (string == NULL) {
1368 return NULL;
1369 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001370 // TODO: use 16-bit wide memset variant
1371 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001372 if (array == NULL) {
1373 return NULL;
1374 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001375 for (int i = 0; i < utf16_length; i++) {
1376 array->Set(i, utf16_data_in[i]);
1377 }
1378 if (hash_code != 0) {
1379 string->SetHashCode(hash_code);
1380 } else {
1381 string->ComputeHashCode();
1382 }
1383 return string;
1384}
1385
1386String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001387 if (utf == NULL) {
1388 return NULL;
1389 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001390 size_t char_count = CountModifiedUtf8Chars(utf);
1391 return AllocFromModifiedUtf8(char_count, utf);
1392}
1393
1394String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1395 const char* utf8_data_in) {
1396 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001397 if (string == NULL) {
1398 return NULL;
1399 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001400 uint16_t* utf16_data_out =
1401 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1402 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1403 string->ComputeHashCode();
1404 return string;
1405}
1406
1407String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001408 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1409 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001410 return NULL;
1411 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001412 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001413}
1414
1415String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001416 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001417 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001418 if (string == NULL) {
1419 return NULL;
1420 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001421 string->SetArray(array);
1422 string->SetCount(array->GetLength());
1423 return string;
1424}
1425
1426bool String::Equals(const String* that) const {
1427 if (this == that) {
1428 // Quick reference equality test
1429 return true;
1430 } else if (that == NULL) {
1431 // Null isn't an instanceof anything
1432 return false;
1433 } else if (this->GetLength() != that->GetLength()) {
1434 // Quick length inequality test
1435 return false;
1436 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001437 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001438 // hash code was already equal
1439 for (int32_t i = 0; i < that->GetLength(); ++i) {
1440 if (this->CharAt(i) != that->CharAt(i)) {
1441 return false;
1442 }
1443 }
1444 return true;
1445 }
1446}
1447
Elliott Hughes5d78d392011-12-13 16:53:05 -08001448bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001449 if (this->GetLength() != that_length) {
1450 return false;
1451 } else {
1452 for (int32_t i = 0; i < that_length; ++i) {
1453 if (this->CharAt(i) != that_chars[that_offset + i]) {
1454 return false;
1455 }
1456 }
1457 return true;
1458 }
1459}
1460
1461bool String::Equals(const char* modified_utf8) const {
1462 for (int32_t i = 0; i < GetLength(); ++i) {
1463 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1464 if (ch == '\0' || ch != CharAt(i)) {
1465 return false;
1466 }
1467 }
1468 return *modified_utf8 == '\0';
1469}
1470
1471bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001472 if (modified_utf8.size() != GetLength()) {
1473 return false;
1474 }
1475 const char* p = modified_utf8.data();
1476 for (int32_t i = 0; i < GetLength(); ++i) {
1477 uint16_t ch = GetUtf16FromUtf8(&p);
1478 if (ch != CharAt(i)) {
1479 return false;
1480 }
1481 }
1482 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001483}
1484
1485// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1486std::string String::ToModifiedUtf8() const {
1487 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1488 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1489 std::string result(byte_count, char(0));
1490 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1491 return result;
1492}
1493
Ian Rogers1c5eb702012-02-01 09:18:34 -08001494void Throwable::SetCause(Throwable* cause) {
1495 CHECK(cause != NULL);
1496 CHECK(cause != this);
1497 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1498 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1499}
1500
Ian Rogers466bb252011-10-14 03:29:56 -07001501bool Throwable::IsCheckedException() const {
1502 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1503 if (InstanceOf(error)) {
1504 return false;
1505 }
1506 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1507 return !InstanceOf(jlre);
1508}
1509
Ian Rogers9074b992011-10-26 17:41:55 -07001510std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001511 std::string result(PrettyTypeOf(this));
1512 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001513 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001514 if (msg != NULL) {
1515 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001516 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001517 result += "\n";
1518 Object* stack_state = GetStackState();
1519 // check stack state isn't missing or corrupt
1520 if (stack_state != NULL && stack_state->IsObjectArray()) {
1521 // Decode the internal stack trace into the depth and method trace
1522 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1523 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001524 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1525 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001526 for (int32_t i = 0; i < depth; ++i) {
1527 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001528 mh.ChangeMethod(method);
1529 uint32_t native_pc = pc_trace->Get(i);
1530 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1531 const char* source_file = mh.GetDeclaringClassSourceFile();
1532 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1533 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001534 }
Ian Rogers9074b992011-10-26 17:41:55 -07001535 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001536 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001537 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001538 result += "Caused by: ";
1539 result += cause->Dump();
1540 }
Ian Rogers9074b992011-10-26 17:41:55 -07001541 return result;
1542}
1543
Ian Rogers5167c972012-02-03 10:41:20 -08001544
1545Class* Throwable::java_lang_Throwable_ = NULL;
1546
1547void Throwable::SetClass(Class* java_lang_Throwable) {
1548 CHECK(java_lang_Throwable_ == NULL);
1549 CHECK(java_lang_Throwable != NULL);
1550 java_lang_Throwable_ = java_lang_Throwable;
1551}
1552
1553void Throwable::ResetClass() {
1554 CHECK(java_lang_Throwable_ != NULL);
1555 java_lang_Throwable_ = NULL;
1556}
1557
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001558Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1559
1560void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1561 CHECK(java_lang_StackTraceElement_ == NULL);
1562 CHECK(java_lang_StackTraceElement != NULL);
1563 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1564}
1565
1566void StackTraceElement::ResetClass() {
1567 CHECK(java_lang_StackTraceElement_ != NULL);
1568 java_lang_StackTraceElement_ = NULL;
1569}
1570
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001571StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1572 String* method_name,
1573 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001574 int32_t line_number) {
1575 StackTraceElement* trace =
1576 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1577 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1578 const_cast<String*>(declaring_class), false);
1579 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1580 const_cast<String*>(method_name), false);
1581 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1582 const_cast<String*>(file_name), false);
1583 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1584 line_number, false);
1585 return trace;
1586}
1587
Elliott Hughes1f359b02011-07-17 14:27:17 -07001588static const char* kClassStatusNames[] = {
1589 "Error",
1590 "NotReady",
1591 "Idx",
1592 "Loaded",
1593 "Resolved",
1594 "Verifying",
1595 "Verified",
1596 "Initializing",
1597 "Initialized"
1598};
1599std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1600 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001601 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001602 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001603 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001604 }
1605 return os;
1606}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001607
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001608} // namespace art