blob: b131517385ad531513ac66d5e5345544a2dde3b3 [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"
Elliott Hughesa4f94742012-05-29 16:28:38 -070040#include "well_known_classes.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070041
Logan Chienfca7e872011-12-20 20:08:22 +080042#if defined(ART_USE_LLVM_COMPILER)
43#include "compiler_llvm/inferred_reg_category_map.h"
TDYa12785321912012-04-01 15:24:56 -070044#include "compiler_llvm/runtime_support_llvm.h"
Logan Chienfca7e872011-12-20 20:08:22 +080045using art::compiler_llvm::InferredRegCategoryMap;
46#endif
47
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048namespace art {
49
Elliott Hughesdbb40792011-11-18 17:05:22 -080050String* Object::AsString() {
51 DCHECK(GetClass()->IsStringClass());
52 return down_cast<String*>(this);
53}
54
Elliott Hughes081be7f2011-09-18 16:50:26 -070055Object* Object::Clone() {
56 Class* c = GetClass();
57 DCHECK(!c->IsClassClass());
58
59 // Object::SizeOf gets the right size even if we're an array.
60 // Using c->AllocObject() here would be wrong.
61 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080062 Heap* heap = Runtime::Current()->GetHeap();
63 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070064 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070065 return NULL;
66 }
67
68 // Copy instance data. We assume memcpy copies by words.
69 // TODO: expose and use move32.
70 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070071 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070072 size_t offset = sizeof(Object);
73 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
74
Mathieu Chartier88c95be2012-09-11 14:06:41 -070075 // Perform write barriers on copied object references.
76 if (c->IsArrayClass()) {
77 if (!c->GetComponentType()->IsPrimitive()) {
78 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
79 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
80 }
81 } else {
82 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
83 size_t num_reference_fields = klass->NumReferenceInstanceFields();
84 for (size_t i = 0; i < num_reference_fields; ++i) {
85 Field* field = klass->GetInstanceField(i);
86 MemberOffset field_offset = field->GetOffset();
87 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
88 heap->WriteBarrierField(copy.get(), field_offset, ref);
89 }
90 }
91 }
92
Elliott Hughes20cde902011-10-04 17:37:27 -070093 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080094 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070095 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070096
Brian Carlstrom40381fb2011-10-19 14:13:40 -070097 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070098}
99
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700100uint32_t Object::GetThinLockId() {
101 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700102}
103
104void Object::MonitorEnter(Thread* thread) {
105 Monitor::MonitorEnter(thread, this);
106}
107
Ian Rogersff1ed472011-09-20 13:46:24 -0700108bool Object::MonitorExit(Thread* thread) {
109 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700110}
111
112void Object::Notify() {
113 Monitor::Notify(Thread::Current(), this);
114}
115
116void Object::NotifyAll() {
117 Monitor::NotifyAll(Thread::Current(), this);
118}
119
120void Object::Wait(int64_t ms, int32_t ns) {
121 Monitor::Wait(Thread::Current(), this, ms, ns, true);
122}
123
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700124// TODO: get global references for these
125Class* Field::java_lang_reflect_Field_ = NULL;
126
127void Field::SetClass(Class* java_lang_reflect_Field) {
128 CHECK(java_lang_reflect_Field_ == NULL);
129 CHECK(java_lang_reflect_Field != NULL);
130 java_lang_reflect_Field_ = java_lang_reflect_Field;
131}
132
133void Field::ResetClass() {
134 CHECK(java_lang_reflect_Field_ != NULL);
135 java_lang_reflect_Field_ = NULL;
136}
137
Ian Rogers0571d352011-11-03 19:51:38 -0700138void Field::SetOffset(MemberOffset num_bytes) {
139 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140#if 0 // TODO enable later in boot and under !NDEBUG
141 FieldHelper fh(this);
142 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700143 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
144 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
145 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800146#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700147 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
148}
149
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700151 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700152 if (IsStatic()) {
153 object = declaring_class_;
154 }
155 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700156}
157
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700158void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700159 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700160 if (IsStatic()) {
161 object = declaring_class_;
162 }
163 object->SetField32(GetOffset(), new_value, IsVolatile());
164}
165
166uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700167 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700168 if (IsStatic()) {
169 object = declaring_class_;
170 }
171 return object->GetField64(GetOffset(), IsVolatile());
172}
173
174void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700175 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700176 if (IsStatic()) {
177 object = declaring_class_;
178 }
179 object->SetField64(GetOffset(), new_value, IsVolatile());
180}
181
182Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700183 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184 if (IsStatic()) {
185 object = declaring_class_;
186 }
187 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
188}
189
190void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700191 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 if (IsStatic()) {
193 object = declaring_class_;
194 }
195 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
196}
197
198bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
200 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 return Get32(object);
202}
203
204void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
206 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 Set32(object, z);
208}
209
210int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
212 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 return Get32(object);
214}
215
216void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
218 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 Set32(object, b);
220}
221
222uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
224 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 return Get32(object);
226}
227
228void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800229 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
230 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 Set32(object, c);
232}
233
Ian Rogers466bb252011-10-14 03:29:56 -0700234int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800235 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
236 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 return Get32(object);
238}
239
Ian Rogers466bb252011-10-14 03:29:56 -0700240void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800241 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
242 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 Set32(object, s);
244}
245
246int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
248 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 return Get32(object);
250}
251
252void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800253 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
254 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700255 Set32(object, i);
256}
257
258int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
260 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 return Get64(object);
262}
263
264void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
266 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 Set64(object, j);
268}
269
Elliott Hughes1d878f32012-04-11 15:17:54 -0700270union Bits {
271 jdouble d;
272 jfloat f;
273 jint i;
274 jlong j;
275};
276
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800278 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
279 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700280 Bits bits;
281 bits.i = Get32(object);
282 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283}
284
285void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800286 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
287 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700288 Bits bits;
289 bits.f = f;
290 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291}
292
293double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
295 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700296 Bits bits;
297 bits.j = Get64(object);
298 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700299}
300
301void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800302 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
303 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700304 Bits bits;
305 bits.d = d;
306 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700307}
308
309Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800310 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
311 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 return GetObj(object);
313}
314
315void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
317 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700318 SetObj(object, l);
319}
320
321// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700322Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323Class* Method::java_lang_reflect_Method_ = NULL;
324
Ian Rogers08f753d2012-08-24 14:35:25 -0700325InvokeType Method::GetInvokeType() const {
326 // TODO: kSuper?
327 if (GetDeclaringClass()->IsInterface()) {
328 return kInterface;
329 } else if (IsStatic()) {
330 return kStatic;
331 } else if (IsDirect()) {
332 return kDirect;
333 } else {
334 return kVirtual;
335 }
336}
337
Elliott Hughes80609252011-09-23 17:24:51 -0700338void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
339 CHECK(java_lang_reflect_Constructor_ == NULL);
340 CHECK(java_lang_reflect_Constructor != NULL);
341 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
342
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 CHECK(java_lang_reflect_Method_ == NULL);
344 CHECK(java_lang_reflect_Method != NULL);
345 java_lang_reflect_Method_ = java_lang_reflect_Method;
346}
347
Elliott Hughes80609252011-09-23 17:24:51 -0700348void Method::ResetClasses() {
349 CHECK(java_lang_reflect_Constructor_ != NULL);
350 java_lang_reflect_Constructor_ = NULL;
351
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352 CHECK(java_lang_reflect_Method_ != NULL);
353 java_lang_reflect_Method_ = NULL;
354}
355
356ObjectArray<String>* Method::GetDexCacheStrings() const {
357 return GetFieldObject<ObjectArray<String>*>(
358 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
359}
360
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700361void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
362 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
363 new_dex_cache_strings, false);
364}
365
Ian Rogers19846512012-02-24 11:42:47 -0800366ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
367 return GetFieldObject<ObjectArray<Method>*>(
368 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
369}
370
371void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
372 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
373 new_dex_cache_methods, false);
374}
375
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
377 return GetFieldObject<ObjectArray<Class>*>(
378 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
379}
380
381void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
382 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
383 new_dex_cache_classes, false);
384}
385
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
387 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
388 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
389 false);
390}
391
392void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700393 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395}
396
397size_t Method::NumArgRegisters(const StringPiece& shorty) {
398 CHECK_LE(1, shorty.length());
399 uint32_t num_registers = 0;
400 for (int i = 1; i < shorty.length(); ++i) {
401 char ch = shorty[i];
402 if (ch == 'D' || ch == 'J') {
403 num_registers += 2;
404 } else {
405 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700406 }
407 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700408 return num_registers;
409}
410
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800411bool Method::IsProxyMethod() const {
412 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700413}
414
Ian Rogers466bb252011-10-14 03:29:56 -0700415Method* Method::FindOverriddenMethod() const {
416 if (IsStatic()) {
417 return NULL;
418 }
419 Class* declaring_class = GetDeclaringClass();
420 Class* super_class = declaring_class->GetSuperClass();
421 uint16_t method_index = GetMethodIndex();
422 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
423 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800424 // Did this method override a super class method? If so load the result from the super class'
425 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700426 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
427 result = super_class_vtable->Get(method_index);
428 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800430 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800431 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
432 CHECK_EQ(result,
433 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800434 } else {
435 MethodHelper mh(this);
436 MethodHelper interface_mh;
437 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
438 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
439 InterfaceEntry* entry = iftable->Get(i);
440 Class* interface = entry->GetInterface();
441 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
442 Method* interface_method = interface->GetVirtualMethod(j);
443 interface_mh.ChangeMethod(interface_method);
444 if (mh.HasSameNameAndSignature(&interface_mh)) {
445 result = interface_method;
446 break;
447 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800448 }
449 }
Ian Rogers466bb252011-10-14 03:29:56 -0700450 }
451 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800452#ifndef NDEBUG
453 MethodHelper result_mh(result);
454 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
455#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700456 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457}
458
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459static const void* GetOatCode(const Method* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800461 Runtime* runtime = Runtime::Current();
462 const void* code = m->GetCode();
463 // Peel off any method tracing trampoline.
464 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
465 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
466 }
467 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800468 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800469 code = runtime->GetClassLinker()->GetOatCodeFor(m);
470 }
471 return code;
472}
473
Ian Rogersbdb03912011-09-14 00:55:44 -0700474uint32_t Method::ToDexPC(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700475#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700476 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700477 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800478 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700479 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700480 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700481 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800482 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700483 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700484 if (mapping_table[i] == sought_offset) {
485 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700486 }
487 }
buzbee8320f382012-09-11 16:29:42 -0700488 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
489 << " in " << PrettyMethod(this);
490 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700491#else
492 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
493 return static_cast<uint32_t>(pc);
494#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700495}
496
497uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700498 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700499 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700500 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700501 return 0; // Special no mapping/pc == 0 case
502 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700504 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700505 uint32_t map_offset = mapping_table[i];
506 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700507 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800508 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700509 }
510 }
511 LOG(FATAL) << "Looking up Dex PC not contained in method";
512 return 0;
513}
514
515uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800516 MethodHelper mh(this);
517 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700518 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700519 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
520 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700521 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700522 if (iter_type_idx == DexFile::kDexNoIndex16) {
523 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700524 }
525 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800526 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700527 if (iter_exception_type == NULL) {
528 // The verifier should take care of resolving all exception classes early
529 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800530 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700531 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700532 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700533 }
534 }
535 // Handler not found
536 return DexFile::kDexNoIndex;
537}
538
Elliott Hughes77405792012-03-15 15:22:12 -0700539void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700540 if (kIsDebugBuild) {
541 self->AssertThreadSuspensionIsAllowable();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700542 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543 CHECK_EQ(kRunnable, self->GetState());
544 }
TDYa12785321912012-04-01 15:24:56 -0700545
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700546 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700547 ManagedStack fragment;
548 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700549
550 // Call the invoke stub associated with the method.
551 // Pass everything as arguments.
Ian Rogers1b09b092012-08-20 15:35:52 -0700552 Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700553
554 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700555
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500556 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700557 bool log = false;
558 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800559 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
560 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700561 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700562 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700563 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800564 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
565 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700566 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700567 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800568 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
569 PrettyMethod(this).c_str(), GetCode(), stub,
570 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700571 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700572 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700573 }
574 }
575
576 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700577 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700578}
579
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700580bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700581 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800582 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800583 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700584 return native_method != jni_stub;
585}
586
Ian Rogers60db5ab2012-02-20 17:02:00 -0800587void Method::RegisterNative(Thread* self, const void* native_method) {
588 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700589 CHECK(IsNative()) << PrettyMethod(this);
590 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700591#if defined(ART_USE_LLVM_COMPILER)
592 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
593 native_method, false);
594#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800595 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
596 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
597 native_method, false);
598 } else {
599 // We've been asked to associate this method with the given native method but are working
600 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
601 // the native method to runtime support and store the target somewhere runtime support will
602 // find it.
603#if defined(__arm__)
604 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
605 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
606#else
607 UNIMPLEMENTED(FATAL);
608#endif
609 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
610 reinterpret_cast<const uint8_t*>(native_method), false);
611 }
TDYa12726467572012-04-17 20:51:22 -0700612#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700613}
614
Ian Rogers19846512012-02-24 11:42:47 -0800615void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700616 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700617 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800618 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700619}
620
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700622 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
623 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700624 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700625 if (new_status > kStatusResolved) {
626 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
627 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800628 if (new_status == kStatusError) {
629 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
630
631 // stash current exception
632 Thread* self = Thread::Current();
633 SirtRef<Throwable> exception(self->GetException());
634 CHECK(exception.get() != NULL);
635
636 // clear exception to call FindSystemClass
637 self->ClearException();
638 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
639 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
640 CHECK(!self->IsExceptionPending());
641
642 // only verification errors, not initialization problems, should set a verify error.
643 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
644 Class* exception_class = exception->GetClass();
645 if (!eiie_class->IsAssignableFrom(exception_class)) {
646 SetVerifyErrorClass(exception_class);
647 }
648
649 // restore exception
650 self->SetException(exception.get());
651 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700652 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700653}
654
655DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700656 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657}
658
659void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700660 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700661}
662
Brian Carlstrom1f870082011-08-23 16:02:11 -0700663Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700664 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700665 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500666 // TODO: decide whether we want this check. It currently fails during bootstrap.
667 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700668 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800669 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700670}
671
Ian Rogers0571d352011-11-03 19:51:38 -0700672void Class::SetClassSize(size_t new_class_size) {
673 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
674 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
675}
676
Ian Rogersd418eda2012-01-30 12:14:28 -0800677// Return the class' name. The exact format is bizarre, but it's the specified behavior for
678// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
679// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
680// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
681String* Class::ComputeName() {
682 String* name = GetName();
683 if (name != NULL) {
684 return name;
685 }
686 std::string descriptor(ClassHelper(this).GetDescriptor());
687 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
688 // The descriptor indicates that this is the class for
689 // a primitive type; special-case the return value.
690 const char* c_name = NULL;
691 switch (descriptor[0]) {
692 case 'Z': c_name = "boolean"; break;
693 case 'B': c_name = "byte"; break;
694 case 'C': c_name = "char"; break;
695 case 'S': c_name = "short"; break;
696 case 'I': c_name = "int"; break;
697 case 'J': c_name = "long"; break;
698 case 'F': c_name = "float"; break;
699 case 'D': c_name = "double"; break;
700 case 'V': c_name = "void"; break;
701 default:
702 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
703 }
704 name = String::AllocFromModifiedUtf8(c_name);
705 } else {
706 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
707 // components.
708 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
709 descriptor.erase(0, 1);
710 descriptor.erase(descriptor.size() - 1);
711 }
712 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
713 name = String::AllocFromModifiedUtf8(descriptor.c_str());
714 }
715 SetName(name);
716 return name;
717}
718
Elliott Hughes4681c802011-09-25 18:04:37 -0700719void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700720 if ((flags & kDumpClassFullDetail) == 0) {
721 os << PrettyClass(this);
722 if ((flags & kDumpClassClassLoader) != 0) {
723 os << ' ' << GetClassLoader();
724 }
725 if ((flags & kDumpClassInitialized) != 0) {
726 os << ' ' << GetStatus();
727 }
Elliott Hughese0918552011-10-28 17:18:29 -0700728 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700729 return;
730 }
731
732 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800733 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700734 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800735 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700736 os << " objectSize=" << SizeOf() << " "
737 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
738 os << StringPrintf(" access=0x%04x.%04x\n",
739 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
740 if (super != NULL) {
741 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
742 }
743 if (IsArrayClass()) {
744 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
745 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700746 if (kh.NumDirectInterfaces() > 0) {
747 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
748 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
749 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700750 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800751 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700752 }
753 }
754 os << " vtable (" << NumVirtualMethods() << " entries, "
755 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
756 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800757 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700758 }
759 os << " direct methods (" << NumDirectMethods() << " entries):\n";
760 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800761 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700762 }
763 if (NumStaticFields() > 0) {
764 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700765 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700766 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800767 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700768 }
769 } else {
770 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700771 }
772 }
773 if (NumInstanceFields() > 0) {
774 os << " instance fields (" << NumInstanceFields() << " 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 < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800777 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(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}
784
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700785void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
786 if (new_reference_offsets != CLASS_WALK_SUPER) {
787 // Sanity check that the number of bits set in the reference offset bitmap
788 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800789 size_t count = 0;
790 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
791 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700792 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800793 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700794 }
795 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
796 new_reference_offsets, false);
797}
798
799void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
800 if (new_reference_offsets != CLASS_WALK_SUPER) {
801 // Sanity check that the number of bits set in the reference offset bitmap
802 // agrees with the number of references
803 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
804 NumReferenceStaticFieldsDuringLinking());
805 }
806 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
807 new_reference_offsets, false);
808}
809
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700810bool Class::Implements(const Class* klass) const {
811 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700812 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700813 // All interfaces implemented directly and by our superclass, and
814 // recursively all super-interfaces of those interfaces, are listed
815 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700816 int32_t iftable_count = GetIfTableCount();
817 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
818 for (int32_t i = 0; i < iftable_count; i++) {
819 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700820 return true;
821 }
822 }
823 return false;
824}
825
Elliott Hughese84278b2012-03-22 10:06:53 -0700826// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700827// are array classes.
828//
829// Consider an array class, e.g. Y[][], where Y is a subclass of X.
830// Y[][] = Y[][] --> true (identity)
831// X[][] = Y[][] --> true (element superclass)
832// Y = Y[][] --> false
833// Y[] = Y[][] --> false
834// Object = Y[][] --> true (everything is an object)
835// Object[] = Y[][] --> true
836// Object[][] = Y[][] --> true
837// Object[][][] = Y[][] --> false (too many []s)
838// Serializable = Y[][] --> true (all arrays are Serializable)
839// Serializable[] = Y[][] --> true
840// Serializable[][] = Y[][] --> false (unless Y is Serializable)
841//
842// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700843// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700844//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700846 DCHECK(IsArrayClass()) << PrettyClass(this);
847 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700848 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700849}
850
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700851bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700852 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
853 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700854 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700855 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700856 // src's super should be java_lang_Object, since it is an array.
857 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700858 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
859 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700860 return this == java_lang_Object;
861 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700862 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700863}
864
865bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700866 DCHECK(!IsInterface()) << PrettyClass(this);
867 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700868 const Class* current = this;
869 do {
870 if (current == klass) {
871 return true;
872 }
873 current = current->GetSuperClass();
874 } while (current != NULL);
875 return false;
876}
877
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800878bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700879 size_t i = 0;
880 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
881 ++i;
882 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700883 if (descriptor1.find('/', i) != StringPiece::npos ||
884 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700885 return false;
886 } else {
887 return true;
888 }
889}
890
891bool Class::IsInSamePackage(const Class* that) const {
892 const Class* klass1 = this;
893 const Class* klass2 = that;
894 if (klass1 == klass2) {
895 return true;
896 }
897 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700898 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700899 return false;
900 }
901 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700902 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700903 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700904 }
jeffhao4a801a42011-09-23 13:53:40 -0700905 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700906 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700907 }
908 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800910 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800911 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800912 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800913 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700914}
915
Elliott Hughesdbb40792011-11-18 17:05:22 -0800916bool Class::IsClassClass() const {
917 Class* java_lang_Class = GetClass()->GetClass();
918 return this == java_lang_Class;
919}
920
921bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800922 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800923}
924
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800925bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700926 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800927}
928
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800929ClassLoader* Class::GetClassLoader() const {
930 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700931}
932
Ian Rogers365c1022012-06-22 15:05:28 -0700933void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700935}
936
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800937Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700938 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700939 DCHECK(declaring_class != NULL) << PrettyClass(this);
940 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700941 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700942 int32_t iftable_count = GetIfTableCount();
943 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
944 for (int32_t i = 0; i < iftable_count; i++) {
945 InterfaceEntry* interface_entry = iftable->Get(i);
946 if (interface_entry->GetInterface() == declaring_class) {
947 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700948 }
949 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700950 return NULL;
951}
952
Ian Rogers466bb252011-10-14 03:29:56 -0700953Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700954 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800955 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700956 if (method != NULL) {
957 return method;
958 }
959
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700960 int32_t iftable_count = GetIfTableCount();
961 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
962 for (int32_t i = 0; i < iftable_count; i++) {
963 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700964 if (method != NULL) {
965 return method;
966 }
967 }
968 return NULL;
969}
970
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800971Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
972 // Check the current class before checking the interfaces.
973 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
974 if (method != NULL) {
975 return method;
976 }
977
978 int32_t iftable_count = GetIfTableCount();
979 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
980 for (int32_t i = 0; i < iftable_count; i++) {
981 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
982 if (method != NULL) {
983 return method;
984 }
985 }
986 return NULL;
987}
988
989
990Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800991 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700992 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700993 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800994 mh.ChangeMethod(method);
995 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700996 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700997 }
998 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700999 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001000}
1001
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001002Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1003 if (GetDexCache() == dex_cache) {
1004 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1005 Method* method = GetDirectMethod(i);
1006 if (method->GetDexMethodIndex() == dex_method_idx) {
1007 return method;
1008 }
1009 }
1010 }
1011 return NULL;
1012}
1013
1014Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1015 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001016 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001017 if (method != NULL) {
1018 return method;
1019 }
1020 }
1021 return NULL;
1022}
1023
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001024Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1025 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1026 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1027 if (method != NULL) {
1028 return method;
1029 }
1030 }
1031 return NULL;
1032}
1033
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001034Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001035 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001036 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001037 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001038 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001039 mh.ChangeMethod(method);
1040 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001041 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001042 }
1043 }
1044 return NULL;
1045}
1046
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001047Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1048 if (GetDexCache() == dex_cache) {
1049 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1050 Method* method = GetVirtualMethod(i);
1051 if (method->GetDexMethodIndex() == dex_method_idx) {
1052 return method;
1053 }
1054 }
1055 }
1056 return NULL;
1057}
1058
Ian Rogers466bb252011-10-14 03:29:56 -07001059Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1060 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1061 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1062 if (method != NULL) {
1063 return method;
1064 }
1065 }
1066 return NULL;
1067}
1068
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001069Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1070 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1071 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1072 if (method != NULL) {
1073 return method;
1074 }
1075 }
1076 return NULL;
1077}
1078
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001079Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 // Is the field in this class?
1081 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001082 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1084 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001085 fh.ChangeField(f);
1086 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001087 return f;
1088 }
1089 }
1090 return NULL;
1091}
1092
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001093Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1094 if (GetDexCache() == dex_cache) {
1095 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1096 Field* f = GetInstanceField(i);
1097 if (f->GetDexFieldIndex() == dex_field_idx) {
1098 return f;
1099 }
1100 }
1101 }
1102 return NULL;
1103}
1104
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001105Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 // Is the field in this class, or any of its superclasses?
1107 // Interfaces are not relevant because they can't contain instance fields.
1108 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001109 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 if (f != NULL) {
1111 return f;
1112 }
1113 }
1114 return NULL;
1115}
1116
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001117Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1118 // Is the field in this class, or any of its superclasses?
1119 // Interfaces are not relevant because they can't contain instance fields.
1120 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1121 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1122 if (f != NULL) {
1123 return f;
1124 }
1125 }
1126 return NULL;
1127}
1128
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001129Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1130 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001131 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001132 for (size_t i = 0; i < NumStaticFields(); ++i) {
1133 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001134 fh.ChangeField(f);
1135 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001136 return f;
1137 }
1138 }
1139 return NULL;
1140}
1141
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001142Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1143 if (dex_cache == GetDexCache()) {
1144 for (size_t i = 0; i < NumStaticFields(); ++i) {
1145 Field* f = GetStaticField(i);
1146 if (f->GetDexFieldIndex() == dex_field_idx) {
1147 return f;
1148 }
1149 }
1150 }
1151 return NULL;
1152}
1153
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001154Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1155 // Is the field in this class (or its interfaces), or any of its
1156 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001157 ClassHelper kh;
1158 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001159 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001160 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001161 if (f != NULL) {
1162 return f;
1163 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001164 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001165 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001166 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1167 Class* interface = kh.GetDirectInterface(i);
1168 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001169 if (f != NULL) {
1170 return f;
1171 }
1172 }
1173 }
1174 return NULL;
1175}
1176
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001177Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1178 ClassHelper kh;
1179 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1180 // Is the field in this class?
1181 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1182 if (f != NULL) {
1183 return f;
1184 }
1185 // Is this field in any of this class' interfaces?
1186 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001187 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1188 Class* interface = kh.GetDirectInterface(i);
1189 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001190 if (f != NULL) {
1191 return f;
1192 }
1193 }
1194 }
1195 return NULL;
1196}
1197
Ian Rogersb067ac22011-12-13 18:05:09 -08001198Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1199 // Find a field using the JLS field resolution order
1200 ClassHelper kh;
1201 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1202 // Is the field in this class?
1203 Field* f = k->FindDeclaredInstanceField(name, type);
1204 if (f != NULL) {
1205 return f;
1206 }
1207 f = k->FindDeclaredStaticField(name, type);
1208 if (f != NULL) {
1209 return f;
1210 }
1211 // Is this field in any of this class' interfaces?
1212 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001213 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1214 Class* interface = kh.GetDirectInterface(i);
1215 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001216 if (f != NULL) {
1217 return f;
1218 }
1219 }
1220 }
1221 return NULL;
1222}
1223
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001224Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001225 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001226 DCHECK_GE(component_count, 0);
1227 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001228
Ian Rogersa15e67d2012-02-28 13:51:55 -08001229 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001230 size_t data_size = component_count * component_size;
1231 size_t size = header_size + data_size;
1232
1233 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1234 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1235 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1236 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001237 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001238 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001239 return NULL;
1240 }
1241
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001242 Heap* heap = Runtime::Current()->GetHeap();
1243 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001244 if (array != NULL) {
1245 DCHECK(array->IsArrayInstance());
1246 array->SetLength(component_count);
1247 }
1248 return array;
1249}
1250
1251Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001253 return Alloc(array_class, component_count, array_class->GetComponentSize());
1254}
1255
Elliott Hughes80609252011-09-23 17:24:51 -07001256bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001257 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001258 "length=%i; index=%i", length_, index);
1259 return false;
1260}
1261
1262bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001263 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001264 "Can't store an element of type %s into an array of type %s",
1265 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1266 return false;
1267}
1268
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001269template<typename T>
1270PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001271 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001272 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1273 return down_cast<PrimitiveArray<T>*>(raw_array);
1274}
1275
1276template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1277
1278// Explicitly instantiate all the primitive array types.
1279template class PrimitiveArray<uint8_t>; // BooleanArray
1280template class PrimitiveArray<int8_t>; // ByteArray
1281template class PrimitiveArray<uint16_t>; // CharArray
1282template class PrimitiveArray<double>; // DoubleArray
1283template class PrimitiveArray<float>; // FloatArray
1284template class PrimitiveArray<int32_t>; // IntArray
1285template class PrimitiveArray<int64_t>; // LongArray
1286template class PrimitiveArray<int16_t>; // ShortArray
1287
Ian Rogers466bb252011-10-14 03:29:56 -07001288// Explicitly instantiate Class[][]
1289template class ObjectArray<ObjectArray<Class> >;
1290
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001291// TODO: get global references for these
1292Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001293
Brian Carlstroma663ea52011-08-19 23:33:41 -07001294void String::SetClass(Class* java_lang_String) {
1295 CHECK(java_lang_String_ == NULL);
1296 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001297 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001298}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001299
Brian Carlstroma663ea52011-08-19 23:33:41 -07001300void String::ResetClass() {
1301 CHECK(java_lang_String_ != NULL);
1302 java_lang_String_ = NULL;
1303}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001304
Brian Carlstromc74255f2011-09-11 22:47:39 -07001305String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001306 return Runtime::Current()->GetInternTable()->InternWeak(this);
1307}
1308
Brian Carlstrom395520e2011-09-25 19:35:00 -07001309int32_t String::GetHashCode() {
1310 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1311 if (result == 0) {
1312 ComputeHashCode();
1313 }
1314 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1315 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1316 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001317 return result;
1318}
1319
1320int32_t String::GetLength() const {
1321 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1322 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1323 return result;
1324}
1325
1326uint16_t String::CharAt(int32_t index) const {
1327 // TODO: do we need this? Equals is the only caller, and could
1328 // bounds check itself.
1329 if (index < 0 || index >= count_) {
1330 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001331 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001332 "length=%i; index=%i", count_, index);
1333 return 0;
1334 }
1335 return GetCharArray()->Get(index + GetOffset());
1336}
1337
1338String* String::AllocFromUtf16(int32_t utf16_length,
1339 const uint16_t* utf16_data_in,
1340 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001341 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001342 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001343 if (string == NULL) {
1344 return NULL;
1345 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001346 // TODO: use 16-bit wide memset variant
1347 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001348 if (array == NULL) {
1349 return NULL;
1350 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001351 for (int i = 0; i < utf16_length; i++) {
1352 array->Set(i, utf16_data_in[i]);
1353 }
1354 if (hash_code != 0) {
1355 string->SetHashCode(hash_code);
1356 } else {
1357 string->ComputeHashCode();
1358 }
1359 return string;
1360}
1361
1362String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001363 if (utf == NULL) {
1364 return NULL;
1365 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001366 size_t char_count = CountModifiedUtf8Chars(utf);
1367 return AllocFromModifiedUtf8(char_count, utf);
1368}
1369
1370String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1371 const char* utf8_data_in) {
1372 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001373 if (string == NULL) {
1374 return NULL;
1375 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 uint16_t* utf16_data_out =
1377 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1378 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1379 string->ComputeHashCode();
1380 return string;
1381}
1382
1383String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001384 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1385 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001386 return NULL;
1387 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001388 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001389}
1390
1391String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001392 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001393 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001394 if (string == NULL) {
1395 return NULL;
1396 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001397 string->SetArray(array);
1398 string->SetCount(array->GetLength());
1399 return string;
1400}
1401
1402bool String::Equals(const String* that) const {
1403 if (this == that) {
1404 // Quick reference equality test
1405 return true;
1406 } else if (that == NULL) {
1407 // Null isn't an instanceof anything
1408 return false;
1409 } else if (this->GetLength() != that->GetLength()) {
1410 // Quick length inequality test
1411 return false;
1412 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001413 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001414 // hash code was already equal
1415 for (int32_t i = 0; i < that->GetLength(); ++i) {
1416 if (this->CharAt(i) != that->CharAt(i)) {
1417 return false;
1418 }
1419 }
1420 return true;
1421 }
1422}
1423
Elliott Hughes5d78d392011-12-13 16:53:05 -08001424bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001425 if (this->GetLength() != that_length) {
1426 return false;
1427 } else {
1428 for (int32_t i = 0; i < that_length; ++i) {
1429 if (this->CharAt(i) != that_chars[that_offset + i]) {
1430 return false;
1431 }
1432 }
1433 return true;
1434 }
1435}
1436
1437bool String::Equals(const char* modified_utf8) const {
1438 for (int32_t i = 0; i < GetLength(); ++i) {
1439 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1440 if (ch == '\0' || ch != CharAt(i)) {
1441 return false;
1442 }
1443 }
1444 return *modified_utf8 == '\0';
1445}
1446
1447bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001448 if (modified_utf8.size() != GetLength()) {
1449 return false;
1450 }
1451 const char* p = modified_utf8.data();
1452 for (int32_t i = 0; i < GetLength(); ++i) {
1453 uint16_t ch = GetUtf16FromUtf8(&p);
1454 if (ch != CharAt(i)) {
1455 return false;
1456 }
1457 }
1458 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001459}
1460
1461// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1462std::string String::ToModifiedUtf8() const {
1463 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001464 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001465 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001466 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1467 return result;
1468}
1469
Ian Rogers1c5eb702012-02-01 09:18:34 -08001470void Throwable::SetCause(Throwable* cause) {
1471 CHECK(cause != NULL);
1472 CHECK(cause != this);
1473 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1474 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1475}
1476
Ian Rogers466bb252011-10-14 03:29:56 -07001477bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001478 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001479 return false;
1480 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001481 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001482}
1483
Ian Rogers9074b992011-10-26 17:41:55 -07001484std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001485 std::string result(PrettyTypeOf(this));
1486 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001487 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001488 if (msg != NULL) {
1489 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001490 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001491 result += "\n";
1492 Object* stack_state = GetStackState();
1493 // check stack state isn't missing or corrupt
1494 if (stack_state != NULL && stack_state->IsObjectArray()) {
1495 // Decode the internal stack trace into the depth and method trace
1496 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1497 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001498 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1499 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001500 for (int32_t i = 0; i < depth; ++i) {
1501 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001502 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001503 uint32_t dex_pc = pc_trace->Get(i);
1504 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001505 const char* source_file = mh.GetDeclaringClassSourceFile();
1506 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1507 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001508 }
Ian Rogers9074b992011-10-26 17:41:55 -07001509 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001510 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001511 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001512 result += "Caused by: ";
1513 result += cause->Dump();
1514 }
Ian Rogers9074b992011-10-26 17:41:55 -07001515 return result;
1516}
1517
Ian Rogers5167c972012-02-03 10:41:20 -08001518
1519Class* Throwable::java_lang_Throwable_ = NULL;
1520
1521void Throwable::SetClass(Class* java_lang_Throwable) {
1522 CHECK(java_lang_Throwable_ == NULL);
1523 CHECK(java_lang_Throwable != NULL);
1524 java_lang_Throwable_ = java_lang_Throwable;
1525}
1526
1527void Throwable::ResetClass() {
1528 CHECK(java_lang_Throwable_ != NULL);
1529 java_lang_Throwable_ = NULL;
1530}
1531
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001532Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1533
1534void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1535 CHECK(java_lang_StackTraceElement_ == NULL);
1536 CHECK(java_lang_StackTraceElement != NULL);
1537 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1538}
1539
1540void StackTraceElement::ResetClass() {
1541 CHECK(java_lang_StackTraceElement_ != NULL);
1542 java_lang_StackTraceElement_ = NULL;
1543}
1544
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001545StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1546 String* method_name,
1547 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001548 int32_t line_number) {
1549 StackTraceElement* trace =
1550 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1551 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1552 const_cast<String*>(declaring_class), false);
1553 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1554 const_cast<String*>(method_name), false);
1555 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1556 const_cast<String*>(file_name), false);
1557 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1558 line_number, false);
1559 return trace;
1560}
1561
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001562} // namespace art