blob: 85a2d0a645e734bdba25a2620e205deefba5895d [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
Elliott Hughes168670b2012-02-29 16:43:26 -0800476static const void* GetOatCode(const Method* m) {
477 Runtime* runtime = Runtime::Current();
478 const void* code = m->GetCode();
479 // Peel off any method tracing trampoline.
480 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
481 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
482 }
483 // Peel off any resolution stub.
484 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData() ||
485 code == runtime->GetResolutionStubArray(Runtime::kInstanceMethod)->GetData()) {
486 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));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700680 return Heap::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
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700902#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700903bool Class::IsInSamePackage(const StringPiece& descriptor1,
904 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700905 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700906 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700907 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
908 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700909 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
910}
911#endif
912
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700913bool Class::IsInSamePackage(const Class* that) const {
914 const Class* klass1 = this;
915 const Class* klass2 = that;
916 if (klass1 == klass2) {
917 return true;
918 }
919 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700920 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700921 return false;
922 }
923 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700924 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700925 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700926 }
jeffhao4a801a42011-09-23 13:53:40 -0700927 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700928 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700929 }
930 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800931 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800932 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800933 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800934 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800935 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700936}
937
Elliott Hughesdbb40792011-11-18 17:05:22 -0800938bool Class::IsClassClass() const {
939 Class* java_lang_Class = GetClass()->GetClass();
940 return this == java_lang_Class;
941}
942
943bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800944 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800945}
946
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800947bool Class::IsThrowableClass() const {
948 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
949 return throwable->IsAssignableFrom(this);
950}
951
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800952ClassLoader* Class::GetClassLoader() const {
953 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700954}
955
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700956void Class::SetClassLoader(const ClassLoader* new_cl) {
957 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700959}
960
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800961Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700962 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700963 DCHECK(declaring_class != NULL) << PrettyClass(this);
964 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700965 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700966 int32_t iftable_count = GetIfTableCount();
967 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
968 for (int32_t i = 0; i < iftable_count; i++) {
969 InterfaceEntry* interface_entry = iftable->Get(i);
970 if (interface_entry->GetInterface() == declaring_class) {
971 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700972 }
973 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700974 return NULL;
975}
976
Ian Rogers466bb252011-10-14 03:29:56 -0700977Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700978 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800979 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700980 if (method != NULL) {
981 return method;
982 }
983
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700984 int32_t iftable_count = GetIfTableCount();
985 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
986 for (int32_t i = 0; i < iftable_count; i++) {
987 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700988 if (method != NULL) {
989 return method;
990 }
991 }
992 return NULL;
993}
994
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800995Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
996 // Check the current class before checking the interfaces.
997 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
998 if (method != NULL) {
999 return method;
1000 }
1001
1002 int32_t iftable_count = GetIfTableCount();
1003 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1004 for (int32_t i = 0; i < iftable_count; i++) {
1005 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
1006 if (method != NULL) {
1007 return method;
1008 }
1009 }
1010 return NULL;
1011}
1012
1013
1014Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001015 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001016 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001017 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001018 mh.ChangeMethod(method);
1019 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001020 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001021 }
1022 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001023 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001024}
1025
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001026Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1027 if (GetDexCache() == dex_cache) {
1028 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1029 Method* method = GetDirectMethod(i);
1030 if (method->GetDexMethodIndex() == dex_method_idx) {
1031 return method;
1032 }
1033 }
1034 }
1035 return NULL;
1036}
1037
1038Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1039 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001040 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001041 if (method != NULL) {
1042 return method;
1043 }
1044 }
1045 return NULL;
1046}
1047
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001048Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1049 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1050 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1051 if (method != NULL) {
1052 return method;
1053 }
1054 }
1055 return NULL;
1056}
1057
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001058Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001059 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001060 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001061 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001062 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001063 mh.ChangeMethod(method);
1064 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001065 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001066 }
1067 }
1068 return NULL;
1069}
1070
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001071Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1072 if (GetDexCache() == dex_cache) {
1073 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1074 Method* method = GetVirtualMethod(i);
1075 if (method->GetDexMethodIndex() == dex_method_idx) {
1076 return method;
1077 }
1078 }
1079 }
1080 return NULL;
1081}
1082
Ian Rogers466bb252011-10-14 03:29:56 -07001083Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1084 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1085 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1086 if (method != NULL) {
1087 return method;
1088 }
1089 }
1090 return NULL;
1091}
1092
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001093Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1094 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1095 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1096 if (method != NULL) {
1097 return method;
1098 }
1099 }
1100 return NULL;
1101}
1102
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001103Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 // Is the field in this class?
1105 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001106 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1108 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001109 fh.ChangeField(f);
1110 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001111 return f;
1112 }
1113 }
1114 return NULL;
1115}
1116
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001117Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1118 if (GetDexCache() == dex_cache) {
1119 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1120 Field* f = GetInstanceField(i);
1121 if (f->GetDexFieldIndex() == dex_field_idx) {
1122 return f;
1123 }
1124 }
1125 }
1126 return NULL;
1127}
1128
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001129Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 // Is the field in this class, or any of its superclasses?
1131 // Interfaces are not relevant because they can't contain instance fields.
1132 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001133 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 if (f != NULL) {
1135 return f;
1136 }
1137 }
1138 return NULL;
1139}
1140
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001141Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1142 // Is the field in this class, or any of its superclasses?
1143 // Interfaces are not relevant because they can't contain instance fields.
1144 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1145 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1146 if (f != NULL) {
1147 return f;
1148 }
1149 }
1150 return NULL;
1151}
1152
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001153Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1154 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001155 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001156 for (size_t i = 0; i < NumStaticFields(); ++i) {
1157 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001158 fh.ChangeField(f);
1159 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001160 return f;
1161 }
1162 }
1163 return NULL;
1164}
1165
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001166Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1167 if (dex_cache == GetDexCache()) {
1168 for (size_t i = 0; i < NumStaticFields(); ++i) {
1169 Field* f = GetStaticField(i);
1170 if (f->GetDexFieldIndex() == dex_field_idx) {
1171 return f;
1172 }
1173 }
1174 }
1175 return NULL;
1176}
1177
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001178Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1179 // Is the field in this class (or its interfaces), or any of its
1180 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001181 ClassHelper kh;
1182 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001183 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001184 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001185 if (f != NULL) {
1186 return f;
1187 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001188 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001189 kh.ChangeClass(k);
1190 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1191 Class* interface = kh.GetInterface(i);
1192 f = interface->FindDeclaredStaticField(name, type);
1193 if (f != NULL) {
1194 return f;
1195 }
1196 }
1197 }
1198 return NULL;
1199}
1200
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001201Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1202 ClassHelper kh;
1203 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1204 // Is the field in this class?
1205 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1206 if (f != NULL) {
1207 return f;
1208 }
1209 // Is this field in any of this class' interfaces?
1210 kh.ChangeClass(k);
1211 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1212 Class* interface = kh.GetInterface(i);
1213 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1214 if (f != NULL) {
1215 return f;
1216 }
1217 }
1218 }
1219 return NULL;
1220}
1221
Ian Rogersb067ac22011-12-13 18:05:09 -08001222Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1223 // Find a field using the JLS field resolution order
1224 ClassHelper kh;
1225 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1226 // Is the field in this class?
1227 Field* f = k->FindDeclaredInstanceField(name, type);
1228 if (f != NULL) {
1229 return f;
1230 }
1231 f = k->FindDeclaredStaticField(name, type);
1232 if (f != NULL) {
1233 return f;
1234 }
1235 // Is this field in any of this class' interfaces?
1236 kh.ChangeClass(k);
1237 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1238 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001239 f = interface->FindDeclaredStaticField(name, type);
1240 if (f != NULL) {
1241 return f;
1242 }
1243 }
1244 }
1245 return NULL;
1246}
1247
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001248Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001249 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001250 DCHECK_GE(component_count, 0);
1251 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001252
Ian Rogersa15e67d2012-02-28 13:51:55 -08001253 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001254 size_t data_size = component_count * component_size;
1255 size_t size = header_size + data_size;
1256
1257 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1258 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1259 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1260 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001261 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001262 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001263 return NULL;
1264 }
1265
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001266 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1267 if (array != NULL) {
1268 DCHECK(array->IsArrayInstance());
1269 array->SetLength(component_count);
1270 }
1271 return array;
1272}
1273
1274Array* Array::Alloc(Class* array_class, int32_t component_count) {
1275 return Alloc(array_class, component_count, array_class->GetComponentSize());
1276}
1277
Elliott Hughes80609252011-09-23 17:24:51 -07001278bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001279 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001280 "length=%i; index=%i", length_, index);
1281 return false;
1282}
1283
1284bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001285 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001286 "Can't store an element of type %s into an array of type %s",
1287 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1288 return false;
1289}
1290
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001291template<typename T>
1292PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001293 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001294 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1295 return down_cast<PrimitiveArray<T>*>(raw_array);
1296}
1297
1298template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1299
1300// Explicitly instantiate all the primitive array types.
1301template class PrimitiveArray<uint8_t>; // BooleanArray
1302template class PrimitiveArray<int8_t>; // ByteArray
1303template class PrimitiveArray<uint16_t>; // CharArray
1304template class PrimitiveArray<double>; // DoubleArray
1305template class PrimitiveArray<float>; // FloatArray
1306template class PrimitiveArray<int32_t>; // IntArray
1307template class PrimitiveArray<int64_t>; // LongArray
1308template class PrimitiveArray<int16_t>; // ShortArray
1309
Ian Rogers466bb252011-10-14 03:29:56 -07001310// Explicitly instantiate Class[][]
1311template class ObjectArray<ObjectArray<Class> >;
1312
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001313// TODO: get global references for these
1314Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001315
Brian Carlstroma663ea52011-08-19 23:33:41 -07001316void String::SetClass(Class* java_lang_String) {
1317 CHECK(java_lang_String_ == NULL);
1318 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001319 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001320}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001321
Brian Carlstroma663ea52011-08-19 23:33:41 -07001322void String::ResetClass() {
1323 CHECK(java_lang_String_ != NULL);
1324 java_lang_String_ = NULL;
1325}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001326
Brian Carlstromc74255f2011-09-11 22:47:39 -07001327String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001328 return Runtime::Current()->GetInternTable()->InternWeak(this);
1329}
1330
Brian Carlstrom395520e2011-09-25 19:35:00 -07001331int32_t String::GetHashCode() {
1332 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1333 if (result == 0) {
1334 ComputeHashCode();
1335 }
1336 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1337 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1338 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001339 return result;
1340}
1341
1342int32_t String::GetLength() const {
1343 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1344 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1345 return result;
1346}
1347
1348uint16_t String::CharAt(int32_t index) const {
1349 // TODO: do we need this? Equals is the only caller, and could
1350 // bounds check itself.
1351 if (index < 0 || index >= count_) {
1352 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001353 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001354 "length=%i; index=%i", count_, index);
1355 return 0;
1356 }
1357 return GetCharArray()->Get(index + GetOffset());
1358}
1359
1360String* String::AllocFromUtf16(int32_t utf16_length,
1361 const uint16_t* utf16_data_in,
1362 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001363 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001364 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001365 if (string == NULL) {
1366 return NULL;
1367 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001368 // TODO: use 16-bit wide memset variant
1369 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001370 if (array == NULL) {
1371 return NULL;
1372 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001373 for (int i = 0; i < utf16_length; i++) {
1374 array->Set(i, utf16_data_in[i]);
1375 }
1376 if (hash_code != 0) {
1377 string->SetHashCode(hash_code);
1378 } else {
1379 string->ComputeHashCode();
1380 }
1381 return string;
1382}
1383
1384String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001385 if (utf == NULL) {
1386 return NULL;
1387 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001388 size_t char_count = CountModifiedUtf8Chars(utf);
1389 return AllocFromModifiedUtf8(char_count, utf);
1390}
1391
1392String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1393 const char* utf8_data_in) {
1394 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001395 if (string == NULL) {
1396 return NULL;
1397 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001398 uint16_t* utf16_data_out =
1399 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1400 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1401 string->ComputeHashCode();
1402 return string;
1403}
1404
1405String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001406 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1407 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001408 return NULL;
1409 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001410 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001411}
1412
1413String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001414 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001415 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001416 if (string == NULL) {
1417 return NULL;
1418 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001419 string->SetArray(array);
1420 string->SetCount(array->GetLength());
1421 return string;
1422}
1423
1424bool String::Equals(const String* that) const {
1425 if (this == that) {
1426 // Quick reference equality test
1427 return true;
1428 } else if (that == NULL) {
1429 // Null isn't an instanceof anything
1430 return false;
1431 } else if (this->GetLength() != that->GetLength()) {
1432 // Quick length inequality test
1433 return false;
1434 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001435 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001436 // hash code was already equal
1437 for (int32_t i = 0; i < that->GetLength(); ++i) {
1438 if (this->CharAt(i) != that->CharAt(i)) {
1439 return false;
1440 }
1441 }
1442 return true;
1443 }
1444}
1445
Elliott Hughes5d78d392011-12-13 16:53:05 -08001446bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001447 if (this->GetLength() != that_length) {
1448 return false;
1449 } else {
1450 for (int32_t i = 0; i < that_length; ++i) {
1451 if (this->CharAt(i) != that_chars[that_offset + i]) {
1452 return false;
1453 }
1454 }
1455 return true;
1456 }
1457}
1458
1459bool String::Equals(const char* modified_utf8) const {
1460 for (int32_t i = 0; i < GetLength(); ++i) {
1461 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1462 if (ch == '\0' || ch != CharAt(i)) {
1463 return false;
1464 }
1465 }
1466 return *modified_utf8 == '\0';
1467}
1468
1469bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001470 if (modified_utf8.size() != GetLength()) {
1471 return false;
1472 }
1473 const char* p = modified_utf8.data();
1474 for (int32_t i = 0; i < GetLength(); ++i) {
1475 uint16_t ch = GetUtf16FromUtf8(&p);
1476 if (ch != CharAt(i)) {
1477 return false;
1478 }
1479 }
1480 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001481}
1482
1483// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1484std::string String::ToModifiedUtf8() const {
1485 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1486 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1487 std::string result(byte_count, char(0));
1488 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1489 return result;
1490}
1491
Ian Rogers1c5eb702012-02-01 09:18:34 -08001492void Throwable::SetCause(Throwable* cause) {
1493 CHECK(cause != NULL);
1494 CHECK(cause != this);
1495 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1496 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1497}
1498
Ian Rogers466bb252011-10-14 03:29:56 -07001499bool Throwable::IsCheckedException() const {
1500 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1501 if (InstanceOf(error)) {
1502 return false;
1503 }
1504 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1505 return !InstanceOf(jlre);
1506}
1507
Ian Rogers9074b992011-10-26 17:41:55 -07001508std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001509 std::string result(PrettyTypeOf(this));
1510 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001511 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001512 if (msg != NULL) {
1513 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001514 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001515 result += "\n";
1516 Object* stack_state = GetStackState();
1517 // check stack state isn't missing or corrupt
1518 if (stack_state != NULL && stack_state->IsObjectArray()) {
1519 // Decode the internal stack trace into the depth and method trace
1520 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1521 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001522 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1523 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001524 for (int32_t i = 0; i < depth; ++i) {
1525 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001526 mh.ChangeMethod(method);
1527 uint32_t native_pc = pc_trace->Get(i);
1528 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1529 const char* source_file = mh.GetDeclaringClassSourceFile();
1530 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1531 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001532 }
Ian Rogers9074b992011-10-26 17:41:55 -07001533 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001534 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001535 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001536 result += "Caused by: ";
1537 result += cause->Dump();
1538 }
Ian Rogers9074b992011-10-26 17:41:55 -07001539 return result;
1540}
1541
Ian Rogers5167c972012-02-03 10:41:20 -08001542
1543Class* Throwable::java_lang_Throwable_ = NULL;
1544
1545void Throwable::SetClass(Class* java_lang_Throwable) {
1546 CHECK(java_lang_Throwable_ == NULL);
1547 CHECK(java_lang_Throwable != NULL);
1548 java_lang_Throwable_ = java_lang_Throwable;
1549}
1550
1551void Throwable::ResetClass() {
1552 CHECK(java_lang_Throwable_ != NULL);
1553 java_lang_Throwable_ = NULL;
1554}
1555
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001556Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1557
1558void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1559 CHECK(java_lang_StackTraceElement_ == NULL);
1560 CHECK(java_lang_StackTraceElement != NULL);
1561 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1562}
1563
1564void StackTraceElement::ResetClass() {
1565 CHECK(java_lang_StackTraceElement_ != NULL);
1566 java_lang_StackTraceElement_ = NULL;
1567}
1568
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001569StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1570 String* method_name,
1571 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001572 int32_t line_number) {
1573 StackTraceElement* trace =
1574 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1575 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1576 const_cast<String*>(declaring_class), false);
1577 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1578 const_cast<String*>(method_name), false);
1579 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1580 const_cast<String*>(file_name), false);
1581 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1582 line_number, false);
1583 return trace;
1584}
1585
Elliott Hughes1f359b02011-07-17 14:27:17 -07001586static const char* kClassStatusNames[] = {
1587 "Error",
1588 "NotReady",
1589 "Idx",
1590 "Loaded",
1591 "Resolved",
1592 "Verifying",
1593 "Verified",
1594 "Initializing",
1595 "Initialized"
1596};
1597std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1598 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001599 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001600 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001601 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001602 }
1603 return os;
1604}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001605
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001606} // namespace art