blob: c91a371142e8c50238159f1d4e1d81c726b2814e [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.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800484 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800485 code = runtime->GetClassLinker()->GetOatCodeFor(m);
486 }
487 return code;
488}
489
Ian Rogersbdb03912011-09-14 00:55:44 -0700490uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700491 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700492 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800493 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700494 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700495 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700496 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800497 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700498 uint32_t best_offset = 0;
499 uint32_t best_dex_offset = 0;
500 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700501 uint32_t map_offset = mapping_table[i];
502 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700503 if (map_offset == sought_offset) {
504 best_offset = map_offset;
505 best_dex_offset = map_dex_offset;
506 break;
507 }
508 if (map_offset < sought_offset && map_offset > best_offset) {
509 best_offset = map_offset;
510 best_dex_offset = map_dex_offset;
511 }
512 }
513 return best_dex_offset;
514}
515
516uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700517 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700518 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700519 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700520 return 0; // Special no mapping/pc == 0 case
521 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700522 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700523 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700524 uint32_t map_offset = mapping_table[i];
525 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700526 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800527 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700528 }
529 }
530 LOG(FATAL) << "Looking up Dex PC not contained in method";
531 return 0;
532}
533
534uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800535 MethodHelper mh(this);
536 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700537 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700538 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
539 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700540 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700541 if (iter_type_idx == DexFile::kDexNoIndex16) {
542 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700543 }
544 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800545 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700546 if (iter_exception_type == NULL) {
547 // The verifier should take care of resolving all exception classes early
548 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800549 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700550 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700551 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700552 }
553 }
554 // Handler not found
555 return DexFile::kDexNoIndex;
556}
557
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700558void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
559 // Push a transition back into managed code onto the linked list in thread.
560 CHECK_EQ(Thread::kRunnable, self->GetState());
561 NativeToManagedRecord record;
562 self->PushNativeToManagedRecord(&record);
563
564 // Call the invoke stub associated with the method.
565 // Pass everything as arguments.
566 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700567
568 bool have_executable_code = (GetCode() != NULL);
569#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700570 // Currently we can only compile non-native methods for ARM.
571 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700572#endif
573
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500574 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700575 bool log = false;
576 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800577 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
578 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700579 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700580 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700581 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800582 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
583 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700584 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700585 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800586 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
587 PrettyMethod(this).c_str(), GetCode(), stub,
588 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700589 if (result != NULL) {
590 result->j = 0;
591 }
592 }
593
594 // Pop transition.
595 self->PopNativeToManagedRecord(record);
596}
597
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700598bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700599 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800600 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800601 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700602 return native_method != jni_stub;
603}
604
Ian Rogers60db5ab2012-02-20 17:02:00 -0800605void Method::RegisterNative(Thread* self, const void* native_method) {
606 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700607 CHECK(IsNative()) << PrettyMethod(this);
608 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers60db5ab2012-02-20 17:02:00 -0800609 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
610 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
611 native_method, false);
612 } else {
613 // We've been asked to associate this method with the given native method but are working
614 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
615 // the native method to runtime support and store the target somewhere runtime support will
616 // find it.
617#if defined(__arm__)
618 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
619 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
620#else
621 UNIMPLEMENTED(FATAL);
622#endif
623 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
624 reinterpret_cast<const uint8_t*>(native_method), false);
625 }
Brian Carlstrom16192862011-09-12 17:50:06 -0700626}
627
Ian Rogers19846512012-02-24 11:42:47 -0800628void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700629 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700630 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800631 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700632}
633
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700634void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700635 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
636 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700637 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800638 if (new_status == kStatusError) {
639 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
640
641 // stash current exception
642 Thread* self = Thread::Current();
643 SirtRef<Throwable> exception(self->GetException());
644 CHECK(exception.get() != NULL);
645
646 // clear exception to call FindSystemClass
647 self->ClearException();
648 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
649 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
650 CHECK(!self->IsExceptionPending());
651
652 // only verification errors, not initialization problems, should set a verify error.
653 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
654 Class* exception_class = exception->GetClass();
655 if (!eiie_class->IsAssignableFrom(exception_class)) {
656 SetVerifyErrorClass(exception_class);
657 }
658
659 // restore exception
660 self->SetException(exception.get());
661 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700662 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700663}
664
665DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700666 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700667}
668
669void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700670 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700671}
672
Brian Carlstrom1f870082011-08-23 16:02:11 -0700673Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700674 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700675 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500676 // TODO: decide whether we want this check. It currently fails during bootstrap.
677 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700678 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700679 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700680}
681
Ian Rogers0571d352011-11-03 19:51:38 -0700682void Class::SetClassSize(size_t new_class_size) {
683 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
684 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
685}
686
Ian Rogersd418eda2012-01-30 12:14:28 -0800687// Return the class' name. The exact format is bizarre, but it's the specified behavior for
688// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
689// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
690// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
691String* Class::ComputeName() {
692 String* name = GetName();
693 if (name != NULL) {
694 return name;
695 }
696 std::string descriptor(ClassHelper(this).GetDescriptor());
697 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
698 // The descriptor indicates that this is the class for
699 // a primitive type; special-case the return value.
700 const char* c_name = NULL;
701 switch (descriptor[0]) {
702 case 'Z': c_name = "boolean"; break;
703 case 'B': c_name = "byte"; break;
704 case 'C': c_name = "char"; break;
705 case 'S': c_name = "short"; break;
706 case 'I': c_name = "int"; break;
707 case 'J': c_name = "long"; break;
708 case 'F': c_name = "float"; break;
709 case 'D': c_name = "double"; break;
710 case 'V': c_name = "void"; break;
711 default:
712 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
713 }
714 name = String::AllocFromModifiedUtf8(c_name);
715 } else {
716 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
717 // components.
718 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
719 descriptor.erase(0, 1);
720 descriptor.erase(descriptor.size() - 1);
721 }
722 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
723 name = String::AllocFromModifiedUtf8(descriptor.c_str());
724 }
725 SetName(name);
726 return name;
727}
728
Elliott Hughes4681c802011-09-25 18:04:37 -0700729void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700730 if ((flags & kDumpClassFullDetail) == 0) {
731 os << PrettyClass(this);
732 if ((flags & kDumpClassClassLoader) != 0) {
733 os << ' ' << GetClassLoader();
734 }
735 if ((flags & kDumpClassInitialized) != 0) {
736 os << ' ' << GetStatus();
737 }
Elliott Hughese0918552011-10-28 17:18:29 -0700738 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700739 return;
740 }
741
742 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800743 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700744 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800745 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700746 os << " objectSize=" << SizeOf() << " "
747 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
748 os << StringPrintf(" access=0x%04x.%04x\n",
749 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
750 if (super != NULL) {
751 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
752 }
753 if (IsArrayClass()) {
754 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
755 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800756 if (kh.NumInterfaces() > 0) {
757 os << " interfaces (" << kh.NumInterfaces() << "):\n";
758 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
759 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700760 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800761 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700762 }
763 }
764 os << " vtable (" << NumVirtualMethods() << " entries, "
765 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
766 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800767 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700768 }
769 os << " direct methods (" << NumDirectMethods() << " entries):\n";
770 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800771 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700772 }
773 if (NumStaticFields() > 0) {
774 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700775 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700776 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800777 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700778 }
779 } else {
780 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700781 }
782 }
783 if (NumInstanceFields() > 0) {
784 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700785 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700786 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800787 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700788 }
789 } else {
790 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700791 }
792 }
793}
794
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700795void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
796 if (new_reference_offsets != CLASS_WALK_SUPER) {
797 // Sanity check that the number of bits set in the reference offset bitmap
798 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800799 size_t count = 0;
800 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
801 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700802 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800803 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700804 }
805 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
806 new_reference_offsets, false);
807}
808
809void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
810 if (new_reference_offsets != CLASS_WALK_SUPER) {
811 // Sanity check that the number of bits set in the reference offset bitmap
812 // agrees with the number of references
813 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
814 NumReferenceStaticFieldsDuringLinking());
815 }
816 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
817 new_reference_offsets, false);
818}
819
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700820bool Class::Implements(const Class* klass) const {
821 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700822 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700823 // All interfaces implemented directly and by our superclass, and
824 // recursively all super-interfaces of those interfaces, are listed
825 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700826 int32_t iftable_count = GetIfTableCount();
827 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
828 for (int32_t i = 0; i < iftable_count; i++) {
829 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700830 return true;
831 }
832 }
833 return false;
834}
835
836// Determine whether "this" is assignable from "klazz", where both of these
837// are array classes.
838//
839// Consider an array class, e.g. Y[][], where Y is a subclass of X.
840// Y[][] = Y[][] --> true (identity)
841// X[][] = Y[][] --> true (element superclass)
842// Y = Y[][] --> false
843// Y[] = Y[][] --> false
844// Object = Y[][] --> true (everything is an object)
845// Object[] = Y[][] --> true
846// Object[][] = Y[][] --> true
847// Object[][][] = Y[][] --> false (too many []s)
848// Serializable = Y[][] --> true (all arrays are Serializable)
849// Serializable[] = Y[][] --> true
850// Serializable[][] = Y[][] --> false (unless Y is Serializable)
851//
852// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700853// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700854//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700855bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700856 DCHECK(IsArrayClass()) << PrettyClass(this);
857 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700858 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700859}
860
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700861bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700862 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
863 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700864 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700865 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700866 // src's super should be java_lang_Object, since it is an array.
867 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700868 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
869 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700870 return this == java_lang_Object;
871 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700872 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700873}
874
875bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700876 DCHECK(!IsInterface()) << PrettyClass(this);
877 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700878 const Class* current = this;
879 do {
880 if (current == klass) {
881 return true;
882 }
883 current = current->GetSuperClass();
884 } while (current != NULL);
885 return false;
886}
887
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800888bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700889 size_t i = 0;
890 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
891 ++i;
892 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700893 if (descriptor1.find('/', i) != StringPiece::npos ||
894 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700895 return false;
896 } else {
897 return true;
898 }
899}
900
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700901#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700902bool Class::IsInSamePackage(const StringPiece& descriptor1,
903 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700904 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700905 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700906 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
907 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700908 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
909}
910#endif
911
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700912bool Class::IsInSamePackage(const Class* that) const {
913 const Class* klass1 = this;
914 const Class* klass2 = that;
915 if (klass1 == klass2) {
916 return true;
917 }
918 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700919 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700920 return false;
921 }
922 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700923 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700924 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700925 }
jeffhao4a801a42011-09-23 13:53:40 -0700926 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700927 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700928 }
929 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800930 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800931 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800932 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800933 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800934 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700935}
936
Elliott Hughesdbb40792011-11-18 17:05:22 -0800937bool Class::IsClassClass() const {
938 Class* java_lang_Class = GetClass()->GetClass();
939 return this == java_lang_Class;
940}
941
942bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800943 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800944}
945
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800946bool Class::IsThrowableClass() const {
947 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
948 return throwable->IsAssignableFrom(this);
949}
950
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800951ClassLoader* Class::GetClassLoader() const {
952 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700953}
954
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700955void Class::SetClassLoader(const ClassLoader* new_cl) {
956 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700958}
959
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800960Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700961 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700962 DCHECK(declaring_class != NULL) << PrettyClass(this);
963 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700964 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700965 int32_t iftable_count = GetIfTableCount();
966 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
967 for (int32_t i = 0; i < iftable_count; i++) {
968 InterfaceEntry* interface_entry = iftable->Get(i);
969 if (interface_entry->GetInterface() == declaring_class) {
970 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700971 }
972 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700973 return NULL;
974}
975
Ian Rogers466bb252011-10-14 03:29:56 -0700976Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700977 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800978 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700979 if (method != NULL) {
980 return method;
981 }
982
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700983 int32_t iftable_count = GetIfTableCount();
984 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
985 for (int32_t i = 0; i < iftable_count; i++) {
986 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700987 if (method != NULL) {
988 return method;
989 }
990 }
991 return NULL;
992}
993
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800994Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
995 // Check the current class before checking the interfaces.
996 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
997 if (method != NULL) {
998 return method;
999 }
1000
1001 int32_t iftable_count = GetIfTableCount();
1002 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1003 for (int32_t i = 0; i < iftable_count; i++) {
1004 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
1005 if (method != NULL) {
1006 return method;
1007 }
1008 }
1009 return NULL;
1010}
1011
1012
1013Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001014 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001015 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001016 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001017 mh.ChangeMethod(method);
1018 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001019 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001020 }
1021 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001022 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001023}
1024
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001025Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1026 if (GetDexCache() == dex_cache) {
1027 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1028 Method* method = GetDirectMethod(i);
1029 if (method->GetDexMethodIndex() == dex_method_idx) {
1030 return method;
1031 }
1032 }
1033 }
1034 return NULL;
1035}
1036
1037Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1038 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001039 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001040 if (method != NULL) {
1041 return method;
1042 }
1043 }
1044 return NULL;
1045}
1046
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001047Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1048 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1049 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1050 if (method != NULL) {
1051 return method;
1052 }
1053 }
1054 return NULL;
1055}
1056
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001057Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001058 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001059 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001060 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001061 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001062 mh.ChangeMethod(method);
1063 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001064 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001065 }
1066 }
1067 return NULL;
1068}
1069
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001070Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1071 if (GetDexCache() == dex_cache) {
1072 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1073 Method* method = GetVirtualMethod(i);
1074 if (method->GetDexMethodIndex() == dex_method_idx) {
1075 return method;
1076 }
1077 }
1078 }
1079 return NULL;
1080}
1081
Ian Rogers466bb252011-10-14 03:29:56 -07001082Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1083 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1084 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1085 if (method != NULL) {
1086 return method;
1087 }
1088 }
1089 return NULL;
1090}
1091
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001092Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1093 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1094 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1095 if (method != NULL) {
1096 return method;
1097 }
1098 }
1099 return NULL;
1100}
1101
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001102Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 // Is the field in this class?
1104 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001105 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1107 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001108 fh.ChangeField(f);
1109 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001110 return f;
1111 }
1112 }
1113 return NULL;
1114}
1115
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001116Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1117 if (GetDexCache() == dex_cache) {
1118 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1119 Field* f = GetInstanceField(i);
1120 if (f->GetDexFieldIndex() == dex_field_idx) {
1121 return f;
1122 }
1123 }
1124 }
1125 return NULL;
1126}
1127
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001128Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 // Is the field in this class, or any of its superclasses?
1130 // Interfaces are not relevant because they can't contain instance fields.
1131 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001132 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 if (f != NULL) {
1134 return f;
1135 }
1136 }
1137 return NULL;
1138}
1139
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001140Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1141 // Is the field in this class, or any of its superclasses?
1142 // Interfaces are not relevant because they can't contain instance fields.
1143 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1144 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1145 if (f != NULL) {
1146 return f;
1147 }
1148 }
1149 return NULL;
1150}
1151
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001152Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1153 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001154 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001155 for (size_t i = 0; i < NumStaticFields(); ++i) {
1156 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001157 fh.ChangeField(f);
1158 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001159 return f;
1160 }
1161 }
1162 return NULL;
1163}
1164
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001165Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1166 if (dex_cache == GetDexCache()) {
1167 for (size_t i = 0; i < NumStaticFields(); ++i) {
1168 Field* f = GetStaticField(i);
1169 if (f->GetDexFieldIndex() == dex_field_idx) {
1170 return f;
1171 }
1172 }
1173 }
1174 return NULL;
1175}
1176
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001177Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1178 // Is the field in this class (or its interfaces), or any of its
1179 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001180 ClassHelper kh;
1181 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001182 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001183 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001184 if (f != NULL) {
1185 return f;
1186 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001187 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001188 kh.ChangeClass(k);
1189 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1190 Class* interface = kh.GetInterface(i);
1191 f = interface->FindDeclaredStaticField(name, type);
1192 if (f != NULL) {
1193 return f;
1194 }
1195 }
1196 }
1197 return NULL;
1198}
1199
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001200Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1201 ClassHelper kh;
1202 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1203 // Is the field in this class?
1204 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1205 if (f != NULL) {
1206 return f;
1207 }
1208 // Is this field in any of this class' interfaces?
1209 kh.ChangeClass(k);
1210 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1211 Class* interface = kh.GetInterface(i);
1212 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1213 if (f != NULL) {
1214 return f;
1215 }
1216 }
1217 }
1218 return NULL;
1219}
1220
Ian Rogersb067ac22011-12-13 18:05:09 -08001221Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1222 // Find a field using the JLS field resolution order
1223 ClassHelper kh;
1224 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1225 // Is the field in this class?
1226 Field* f = k->FindDeclaredInstanceField(name, type);
1227 if (f != NULL) {
1228 return f;
1229 }
1230 f = k->FindDeclaredStaticField(name, type);
1231 if (f != NULL) {
1232 return f;
1233 }
1234 // Is this field in any of this class' interfaces?
1235 kh.ChangeClass(k);
1236 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1237 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001238 f = interface->FindDeclaredStaticField(name, type);
1239 if (f != NULL) {
1240 return f;
1241 }
1242 }
1243 }
1244 return NULL;
1245}
1246
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001247Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001248 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001249 DCHECK_GE(component_count, 0);
1250 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001251
Ian Rogersa15e67d2012-02-28 13:51:55 -08001252 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001253 size_t data_size = component_count * component_size;
1254 size_t size = header_size + data_size;
1255
1256 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1257 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1258 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1259 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001260 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001261 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001262 return NULL;
1263 }
1264
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001265 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1266 if (array != NULL) {
1267 DCHECK(array->IsArrayInstance());
1268 array->SetLength(component_count);
1269 }
1270 return array;
1271}
1272
1273Array* Array::Alloc(Class* array_class, int32_t component_count) {
1274 return Alloc(array_class, component_count, array_class->GetComponentSize());
1275}
1276
Elliott Hughes80609252011-09-23 17:24:51 -07001277bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001278 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001279 "length=%i; index=%i", length_, index);
1280 return false;
1281}
1282
1283bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001284 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001285 "Can't store an element of type %s into an array of type %s",
1286 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1287 return false;
1288}
1289
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001290template<typename T>
1291PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001292 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001293 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1294 return down_cast<PrimitiveArray<T>*>(raw_array);
1295}
1296
1297template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1298
1299// Explicitly instantiate all the primitive array types.
1300template class PrimitiveArray<uint8_t>; // BooleanArray
1301template class PrimitiveArray<int8_t>; // ByteArray
1302template class PrimitiveArray<uint16_t>; // CharArray
1303template class PrimitiveArray<double>; // DoubleArray
1304template class PrimitiveArray<float>; // FloatArray
1305template class PrimitiveArray<int32_t>; // IntArray
1306template class PrimitiveArray<int64_t>; // LongArray
1307template class PrimitiveArray<int16_t>; // ShortArray
1308
Ian Rogers466bb252011-10-14 03:29:56 -07001309// Explicitly instantiate Class[][]
1310template class ObjectArray<ObjectArray<Class> >;
1311
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001312// TODO: get global references for these
1313Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001314
Brian Carlstroma663ea52011-08-19 23:33:41 -07001315void String::SetClass(Class* java_lang_String) {
1316 CHECK(java_lang_String_ == NULL);
1317 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001318 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001319}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001320
Brian Carlstroma663ea52011-08-19 23:33:41 -07001321void String::ResetClass() {
1322 CHECK(java_lang_String_ != NULL);
1323 java_lang_String_ = NULL;
1324}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001325
Brian Carlstromc74255f2011-09-11 22:47:39 -07001326String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001327 return Runtime::Current()->GetInternTable()->InternWeak(this);
1328}
1329
Brian Carlstrom395520e2011-09-25 19:35:00 -07001330int32_t String::GetHashCode() {
1331 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1332 if (result == 0) {
1333 ComputeHashCode();
1334 }
1335 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1336 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1337 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001338 return result;
1339}
1340
1341int32_t String::GetLength() const {
1342 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1343 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1344 return result;
1345}
1346
1347uint16_t String::CharAt(int32_t index) const {
1348 // TODO: do we need this? Equals is the only caller, and could
1349 // bounds check itself.
1350 if (index < 0 || index >= count_) {
1351 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001352 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001353 "length=%i; index=%i", count_, index);
1354 return 0;
1355 }
1356 return GetCharArray()->Get(index + GetOffset());
1357}
1358
1359String* String::AllocFromUtf16(int32_t utf16_length,
1360 const uint16_t* utf16_data_in,
1361 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001362 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001364 if (string == NULL) {
1365 return NULL;
1366 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367 // TODO: use 16-bit wide memset variant
1368 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001369 if (array == NULL) {
1370 return NULL;
1371 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001372 for (int i = 0; i < utf16_length; i++) {
1373 array->Set(i, utf16_data_in[i]);
1374 }
1375 if (hash_code != 0) {
1376 string->SetHashCode(hash_code);
1377 } else {
1378 string->ComputeHashCode();
1379 }
1380 return string;
1381}
1382
1383String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001384 if (utf == NULL) {
1385 return NULL;
1386 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001387 size_t char_count = CountModifiedUtf8Chars(utf);
1388 return AllocFromModifiedUtf8(char_count, utf);
1389}
1390
1391String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1392 const char* utf8_data_in) {
1393 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001394 if (string == NULL) {
1395 return NULL;
1396 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001397 uint16_t* utf16_data_out =
1398 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1399 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1400 string->ComputeHashCode();
1401 return string;
1402}
1403
1404String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001405 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1406 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001407 return NULL;
1408 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001409 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001410}
1411
1412String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001413 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001414 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001415 if (string == NULL) {
1416 return NULL;
1417 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001418 string->SetArray(array);
1419 string->SetCount(array->GetLength());
1420 return string;
1421}
1422
1423bool String::Equals(const String* that) const {
1424 if (this == that) {
1425 // Quick reference equality test
1426 return true;
1427 } else if (that == NULL) {
1428 // Null isn't an instanceof anything
1429 return false;
1430 } else if (this->GetLength() != that->GetLength()) {
1431 // Quick length inequality test
1432 return false;
1433 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001434 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001435 // hash code was already equal
1436 for (int32_t i = 0; i < that->GetLength(); ++i) {
1437 if (this->CharAt(i) != that->CharAt(i)) {
1438 return false;
1439 }
1440 }
1441 return true;
1442 }
1443}
1444
Elliott Hughes5d78d392011-12-13 16:53:05 -08001445bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001446 if (this->GetLength() != that_length) {
1447 return false;
1448 } else {
1449 for (int32_t i = 0; i < that_length; ++i) {
1450 if (this->CharAt(i) != that_chars[that_offset + i]) {
1451 return false;
1452 }
1453 }
1454 return true;
1455 }
1456}
1457
1458bool String::Equals(const char* modified_utf8) const {
1459 for (int32_t i = 0; i < GetLength(); ++i) {
1460 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1461 if (ch == '\0' || ch != CharAt(i)) {
1462 return false;
1463 }
1464 }
1465 return *modified_utf8 == '\0';
1466}
1467
1468bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001469 if (modified_utf8.size() != GetLength()) {
1470 return false;
1471 }
1472 const char* p = modified_utf8.data();
1473 for (int32_t i = 0; i < GetLength(); ++i) {
1474 uint16_t ch = GetUtf16FromUtf8(&p);
1475 if (ch != CharAt(i)) {
1476 return false;
1477 }
1478 }
1479 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001480}
1481
1482// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1483std::string String::ToModifiedUtf8() const {
1484 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1485 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1486 std::string result(byte_count, char(0));
1487 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1488 return result;
1489}
1490
Ian Rogers1c5eb702012-02-01 09:18:34 -08001491void Throwable::SetCause(Throwable* cause) {
1492 CHECK(cause != NULL);
1493 CHECK(cause != this);
1494 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1495 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1496}
1497
Ian Rogers466bb252011-10-14 03:29:56 -07001498bool Throwable::IsCheckedException() const {
1499 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1500 if (InstanceOf(error)) {
1501 return false;
1502 }
1503 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1504 return !InstanceOf(jlre);
1505}
1506
Ian Rogers9074b992011-10-26 17:41:55 -07001507std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001508 std::string result(PrettyTypeOf(this));
1509 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001510 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001511 if (msg != NULL) {
1512 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001513 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001514 result += "\n";
1515 Object* stack_state = GetStackState();
1516 // check stack state isn't missing or corrupt
1517 if (stack_state != NULL && stack_state->IsObjectArray()) {
1518 // Decode the internal stack trace into the depth and method trace
1519 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1520 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001521 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1522 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001523 for (int32_t i = 0; i < depth; ++i) {
1524 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001525 mh.ChangeMethod(method);
1526 uint32_t native_pc = pc_trace->Get(i);
1527 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1528 const char* source_file = mh.GetDeclaringClassSourceFile();
1529 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1530 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001531 }
Ian Rogers9074b992011-10-26 17:41:55 -07001532 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001533 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001534 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001535 result += "Caused by: ";
1536 result += cause->Dump();
1537 }
Ian Rogers9074b992011-10-26 17:41:55 -07001538 return result;
1539}
1540
Ian Rogers5167c972012-02-03 10:41:20 -08001541
1542Class* Throwable::java_lang_Throwable_ = NULL;
1543
1544void Throwable::SetClass(Class* java_lang_Throwable) {
1545 CHECK(java_lang_Throwable_ == NULL);
1546 CHECK(java_lang_Throwable != NULL);
1547 java_lang_Throwable_ = java_lang_Throwable;
1548}
1549
1550void Throwable::ResetClass() {
1551 CHECK(java_lang_Throwable_ != NULL);
1552 java_lang_Throwable_ = NULL;
1553}
1554
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001555Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1556
1557void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1558 CHECK(java_lang_StackTraceElement_ == NULL);
1559 CHECK(java_lang_StackTraceElement != NULL);
1560 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1561}
1562
1563void StackTraceElement::ResetClass() {
1564 CHECK(java_lang_StackTraceElement_ != NULL);
1565 java_lang_StackTraceElement_ = NULL;
1566}
1567
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001568StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1569 String* method_name,
1570 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001571 int32_t line_number) {
1572 StackTraceElement* trace =
1573 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1574 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1575 const_cast<String*>(declaring_class), false);
1576 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1577 const_cast<String*>(method_name), false);
1578 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1579 const_cast<String*>(file_name), false);
1580 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1581 line_number, false);
1582 return trace;
1583}
1584
Elliott Hughes1f359b02011-07-17 14:27:17 -07001585static const char* kClassStatusNames[] = {
1586 "Error",
1587 "NotReady",
1588 "Idx",
1589 "Loaded",
1590 "Resolved",
1591 "Verifying",
1592 "Verified",
1593 "Initializing",
1594 "Initialized"
1595};
1596std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1597 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001598 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001599 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001600 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001601 }
1602 return os;
1603}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001604
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001605} // namespace art