blob: bac3ec131012851bc57a64f6193542fad10ce1a1 [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
42namespace art {
43
Elliott Hughesdbb40792011-11-18 17:05:22 -080044String* Object::AsString() {
45 DCHECK(GetClass()->IsStringClass());
46 return down_cast<String*>(this);
47}
48
Elliott Hughes081be7f2011-09-18 16:50:26 -070049Object* Object::Clone() {
50 Class* c = GetClass();
51 DCHECK(!c->IsClassClass());
52
53 // Object::SizeOf gets the right size even if we're an array.
54 // Using c->AllocObject() here would be wrong.
55 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080056 Heap* heap = Runtime::Current()->GetHeap();
57 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070058 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070059 return NULL;
60 }
61
62 // Copy instance data. We assume memcpy copies by words.
63 // TODO: expose and use move32.
64 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070065 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070066 size_t offset = sizeof(Object);
67 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
68
Mathieu Chartier88c95be2012-09-11 14:06:41 -070069 // Perform write barriers on copied object references.
70 if (c->IsArrayClass()) {
71 if (!c->GetComponentType()->IsPrimitive()) {
72 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
73 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
74 }
75 } else {
76 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
77 size_t num_reference_fields = klass->NumReferenceInstanceFields();
78 for (size_t i = 0; i < num_reference_fields; ++i) {
79 Field* field = klass->GetInstanceField(i);
80 MemberOffset field_offset = field->GetOffset();
81 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
82 heap->WriteBarrierField(copy.get(), field_offset, ref);
83 }
84 }
85 }
86
Elliott Hughes20cde902011-10-04 17:37:27 -070087 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080088 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070089 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070090
Brian Carlstrom40381fb2011-10-19 14:13:40 -070091 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070092}
93
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070094uint32_t Object::GetThinLockId() {
95 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070096}
97
98void Object::MonitorEnter(Thread* thread) {
99 Monitor::MonitorEnter(thread, this);
100}
101
Ian Rogersff1ed472011-09-20 13:46:24 -0700102bool Object::MonitorExit(Thread* thread) {
103 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700104}
105
106void Object::Notify() {
107 Monitor::Notify(Thread::Current(), this);
108}
109
110void Object::NotifyAll() {
111 Monitor::NotifyAll(Thread::Current(), this);
112}
113
114void Object::Wait(int64_t ms, int32_t ns) {
115 Monitor::Wait(Thread::Current(), this, ms, ns, true);
116}
117
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700118// TODO: get global references for these
119Class* Field::java_lang_reflect_Field_ = NULL;
120
121void Field::SetClass(Class* java_lang_reflect_Field) {
122 CHECK(java_lang_reflect_Field_ == NULL);
123 CHECK(java_lang_reflect_Field != NULL);
124 java_lang_reflect_Field_ = java_lang_reflect_Field;
125}
126
127void Field::ResetClass() {
128 CHECK(java_lang_reflect_Field_ != NULL);
129 java_lang_reflect_Field_ = NULL;
130}
131
Ian Rogers0571d352011-11-03 19:51:38 -0700132void Field::SetOffset(MemberOffset num_bytes) {
133 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134#if 0 // TODO enable later in boot and under !NDEBUG
135 FieldHelper fh(this);
136 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700137 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
138 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
139 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700141 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
142}
143
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700144uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700146 if (IsStatic()) {
147 object = declaring_class_;
148 }
149 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700150}
151
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700152void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700153 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154 if (IsStatic()) {
155 object = declaring_class_;
156 }
157 object->SetField32(GetOffset(), new_value, IsVolatile());
158}
159
160uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700162 if (IsStatic()) {
163 object = declaring_class_;
164 }
165 return object->GetField64(GetOffset(), IsVolatile());
166}
167
168void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700169 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700170 if (IsStatic()) {
171 object = declaring_class_;
172 }
173 object->SetField64(GetOffset(), new_value, IsVolatile());
174}
175
176Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700177 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700178 if (IsStatic()) {
179 object = declaring_class_;
180 }
181 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
182}
183
184void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700185 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186 if (IsStatic()) {
187 object = declaring_class_;
188 }
189 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
190}
191
192bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
194 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 return Get32(object);
196}
197
198void Field::SetBoolean(Object* object, bool z) 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 Set32(object, z);
202}
203
204int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
206 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 return Get32(object);
208}
209
210void Field::SetByte(Object* object, int8_t b) 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 Set32(object, b);
214}
215
216uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
218 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 return Get32(object);
220}
221
222void Field::SetChar(Object* object, uint16_t c) 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 Set32(object, c);
226}
227
Ian Rogers466bb252011-10-14 03:29:56 -0700228int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800229 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
230 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 return Get32(object);
232}
233
Ian Rogers466bb252011-10-14 03:29:56 -0700234void Field::SetShort(Object* object, int16_t s) 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 Set32(object, s);
238}
239
240int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800241 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
242 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 return Get32(object);
244}
245
246void Field::SetInt(Object* object, int32_t i) 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 Set32(object, i);
250}
251
252int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800253 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
254 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700255 return Get64(object);
256}
257
258void Field::SetLong(Object* object, int64_t j) 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 Set64(object, j);
262}
263
Elliott Hughes1d878f32012-04-11 15:17:54 -0700264union Bits {
265 jdouble d;
266 jfloat f;
267 jint i;
268 jlong j;
269};
270
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800272 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
273 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700274 Bits bits;
275 bits.i = Get32(object);
276 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277}
278
279void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
281 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700282 Bits bits;
283 bits.f = f;
284 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285}
286
287double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800288 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
289 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700290 Bits bits;
291 bits.j = Get64(object);
292 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700293}
294
295void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
297 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700298 Bits bits;
299 bits.d = d;
300 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301}
302
303Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
305 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 return GetObj(object);
307}
308
309void Field::SetObject(Object* object, const Object* l) 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 SetObj(object, l);
313}
314
315// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700316Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700317Class* Method::java_lang_reflect_Method_ = NULL;
318
Ian Rogers08f753d2012-08-24 14:35:25 -0700319InvokeType Method::GetInvokeType() const {
320 // TODO: kSuper?
321 if (GetDeclaringClass()->IsInterface()) {
322 return kInterface;
323 } else if (IsStatic()) {
324 return kStatic;
325 } else if (IsDirect()) {
326 return kDirect;
327 } else {
328 return kVirtual;
329 }
330}
331
Elliott Hughes80609252011-09-23 17:24:51 -0700332void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
333 CHECK(java_lang_reflect_Constructor_ == NULL);
334 CHECK(java_lang_reflect_Constructor != NULL);
335 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
336
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 CHECK(java_lang_reflect_Method_ == NULL);
338 CHECK(java_lang_reflect_Method != NULL);
339 java_lang_reflect_Method_ = java_lang_reflect_Method;
340}
341
Elliott Hughes80609252011-09-23 17:24:51 -0700342void Method::ResetClasses() {
343 CHECK(java_lang_reflect_Constructor_ != NULL);
344 java_lang_reflect_Constructor_ = NULL;
345
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346 CHECK(java_lang_reflect_Method_ != NULL);
347 java_lang_reflect_Method_ = NULL;
348}
349
350ObjectArray<String>* Method::GetDexCacheStrings() const {
351 return GetFieldObject<ObjectArray<String>*>(
352 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
353}
354
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
356 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
357 new_dex_cache_strings, false);
358}
359
Ian Rogers19846512012-02-24 11:42:47 -0800360ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
361 return GetFieldObject<ObjectArray<Method>*>(
362 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
363}
364
365void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
366 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
367 new_dex_cache_methods, false);
368}
369
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700370ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
371 return GetFieldObject<ObjectArray<Class>*>(
372 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
373}
374
375void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
376 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
377 new_dex_cache_classes, false);
378}
379
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
381 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
382 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
383 false);
384}
385
386void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700387 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389}
390
391size_t Method::NumArgRegisters(const StringPiece& shorty) {
392 CHECK_LE(1, shorty.length());
393 uint32_t num_registers = 0;
394 for (int i = 1; i < shorty.length(); ++i) {
395 char ch = shorty[i];
396 if (ch == 'D' || ch == 'J') {
397 num_registers += 2;
398 } else {
399 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700400 }
401 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700402 return num_registers;
403}
404
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800405bool Method::IsProxyMethod() const {
406 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700407}
408
Ian Rogers466bb252011-10-14 03:29:56 -0700409Method* Method::FindOverriddenMethod() const {
410 if (IsStatic()) {
411 return NULL;
412 }
413 Class* declaring_class = GetDeclaringClass();
414 Class* super_class = declaring_class->GetSuperClass();
415 uint16_t method_index = GetMethodIndex();
416 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
417 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 // Did this method override a super class method? If so load the result from the super class'
419 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700420 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
421 result = super_class_vtable->Get(method_index);
422 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800423 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800424 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800425 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
426 CHECK_EQ(result,
427 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800428 } else {
429 MethodHelper mh(this);
430 MethodHelper interface_mh;
431 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
432 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
433 InterfaceEntry* entry = iftable->Get(i);
434 Class* interface = entry->GetInterface();
435 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
436 Method* interface_method = interface->GetVirtualMethod(j);
437 interface_mh.ChangeMethod(interface_method);
438 if (mh.HasSameNameAndSignature(&interface_mh)) {
439 result = interface_method;
440 break;
441 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 }
443 }
Ian Rogers466bb252011-10-14 03:29:56 -0700444 }
445 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446#ifndef NDEBUG
447 MethodHelper result_mh(result);
448 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
449#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700450 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700451}
452
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700453static const void* GetOatCode(const Method* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800455 Runtime* runtime = Runtime::Current();
456 const void* code = m->GetCode();
457 // Peel off any method tracing trampoline.
458 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
459 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
460 }
461 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800462 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800463 code = runtime->GetClassLinker()->GetOatCodeFor(m);
464 }
465 return code;
466}
467
Ian Rogersbdb03912011-09-14 00:55:44 -0700468uint32_t Method::ToDexPC(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700469#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700470 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700471 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800472 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700473 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700474 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800476 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700477 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700478 if (mapping_table[i] == sought_offset) {
479 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700480 }
481 }
buzbee8320f382012-09-11 16:29:42 -0700482 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
483 << " in " << PrettyMethod(this);
484 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700485#else
486 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
487 return static_cast<uint32_t>(pc);
488#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700489}
490
491uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700492 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700493 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700494 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700495 return 0; // Special no mapping/pc == 0 case
496 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700498 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700499 uint32_t map_offset = mapping_table[i];
500 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700501 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800502 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700503 }
504 }
505 LOG(FATAL) << "Looking up Dex PC not contained in method";
506 return 0;
507}
508
509uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800510 MethodHelper mh(this);
511 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700512 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700513 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
514 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700515 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700516 if (iter_type_idx == DexFile::kDexNoIndex16) {
517 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700518 }
519 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700521 if (iter_exception_type == NULL) {
522 // The verifier should take care of resolving all exception classes early
523 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800524 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700525 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700526 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700527 }
528 }
529 // Handler not found
530 return DexFile::kDexNoIndex;
531}
532
Elliott Hughes77405792012-03-15 15:22:12 -0700533void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700534 if (kIsDebugBuild) {
535 self->AssertThreadSuspensionIsAllowable();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700536 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 CHECK_EQ(kRunnable, self->GetState());
538 }
TDYa12785321912012-04-01 15:24:56 -0700539
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700540 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700541 ManagedStack fragment;
542 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700543
544 // Call the invoke stub associated with the method.
545 // Pass everything as arguments.
Ian Rogers1b09b092012-08-20 15:35:52 -0700546 Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700547
548 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700549
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500550 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700551 bool log = false;
552 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800553 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
554 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700555 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700556 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700557 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800558 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
559 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700560 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700561 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800562 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
563 PrettyMethod(this).c_str(), GetCode(), stub,
564 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700565 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700566 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700567 }
568 }
569
570 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700571 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700572}
573
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700574bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700575 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800576 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800577 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700578 return native_method != jni_stub;
579}
580
Ian Rogers60db5ab2012-02-20 17:02:00 -0800581void Method::RegisterNative(Thread* self, const void* native_method) {
582 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700583 CHECK(IsNative()) << PrettyMethod(this);
584 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700585#if defined(ART_USE_LLVM_COMPILER)
586 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
587 native_method, false);
588#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800589 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
590 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
591 native_method, false);
592 } else {
593 // We've been asked to associate this method with the given native method but are working
594 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
595 // the native method to runtime support and store the target somewhere runtime support will
596 // find it.
597#if defined(__arm__)
598 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
599 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
600#else
601 UNIMPLEMENTED(FATAL);
602#endif
603 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
604 reinterpret_cast<const uint8_t*>(native_method), false);
605 }
TDYa12726467572012-04-17 20:51:22 -0700606#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700607}
608
Ian Rogers19846512012-02-24 11:42:47 -0800609void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700610 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700611 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800612 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700613}
614
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700615void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700616 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
617 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700618 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700619 if (new_status > kStatusResolved) {
620 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
621 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800622 if (new_status == kStatusError) {
623 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
624
625 // stash current exception
626 Thread* self = Thread::Current();
627 SirtRef<Throwable> exception(self->GetException());
628 CHECK(exception.get() != NULL);
629
630 // clear exception to call FindSystemClass
631 self->ClearException();
632 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
633 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
634 CHECK(!self->IsExceptionPending());
635
636 // only verification errors, not initialization problems, should set a verify error.
637 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
638 Class* exception_class = exception->GetClass();
639 if (!eiie_class->IsAssignableFrom(exception_class)) {
640 SetVerifyErrorClass(exception_class);
641 }
642
643 // restore exception
644 self->SetException(exception.get());
645 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700646 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647}
648
649DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700650 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651}
652
653void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700654 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700655}
656
Brian Carlstrom1f870082011-08-23 16:02:11 -0700657Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700658 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700659 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500660 // TODO: decide whether we want this check. It currently fails during bootstrap.
661 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700662 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800663 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700664}
665
Ian Rogers0571d352011-11-03 19:51:38 -0700666void Class::SetClassSize(size_t new_class_size) {
667 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
668 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
669}
670
Ian Rogersd418eda2012-01-30 12:14:28 -0800671// Return the class' name. The exact format is bizarre, but it's the specified behavior for
672// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
673// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
674// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
675String* Class::ComputeName() {
676 String* name = GetName();
677 if (name != NULL) {
678 return name;
679 }
680 std::string descriptor(ClassHelper(this).GetDescriptor());
681 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
682 // The descriptor indicates that this is the class for
683 // a primitive type; special-case the return value.
684 const char* c_name = NULL;
685 switch (descriptor[0]) {
686 case 'Z': c_name = "boolean"; break;
687 case 'B': c_name = "byte"; break;
688 case 'C': c_name = "char"; break;
689 case 'S': c_name = "short"; break;
690 case 'I': c_name = "int"; break;
691 case 'J': c_name = "long"; break;
692 case 'F': c_name = "float"; break;
693 case 'D': c_name = "double"; break;
694 case 'V': c_name = "void"; break;
695 default:
696 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
697 }
698 name = String::AllocFromModifiedUtf8(c_name);
699 } else {
700 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
701 // components.
702 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
703 descriptor.erase(0, 1);
704 descriptor.erase(descriptor.size() - 1);
705 }
706 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
707 name = String::AllocFromModifiedUtf8(descriptor.c_str());
708 }
709 SetName(name);
710 return name;
711}
712
Elliott Hughes4681c802011-09-25 18:04:37 -0700713void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700714 if ((flags & kDumpClassFullDetail) == 0) {
715 os << PrettyClass(this);
716 if ((flags & kDumpClassClassLoader) != 0) {
717 os << ' ' << GetClassLoader();
718 }
719 if ((flags & kDumpClassInitialized) != 0) {
720 os << ' ' << GetStatus();
721 }
Elliott Hughese0918552011-10-28 17:18:29 -0700722 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700723 return;
724 }
725
726 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800727 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700728 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800729 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700730 os << " objectSize=" << SizeOf() << " "
731 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
732 os << StringPrintf(" access=0x%04x.%04x\n",
733 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
734 if (super != NULL) {
735 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
736 }
737 if (IsArrayClass()) {
738 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
739 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700740 if (kh.NumDirectInterfaces() > 0) {
741 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
742 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
743 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700744 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800745 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700746 }
747 }
748 os << " vtable (" << NumVirtualMethods() << " entries, "
749 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
750 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800751 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700752 }
753 os << " direct methods (" << NumDirectMethods() << " entries):\n";
754 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800755 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700756 }
757 if (NumStaticFields() > 0) {
758 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700759 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700760 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800761 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700762 }
763 } else {
764 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700765 }
766 }
767 if (NumInstanceFields() > 0) {
768 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700769 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700770 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800771 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700772 }
773 } else {
774 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700775 }
776 }
777}
778
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700779void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
780 if (new_reference_offsets != CLASS_WALK_SUPER) {
781 // Sanity check that the number of bits set in the reference offset bitmap
782 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800783 size_t count = 0;
784 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
785 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700786 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800787 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700788 }
789 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
790 new_reference_offsets, false);
791}
792
793void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
794 if (new_reference_offsets != CLASS_WALK_SUPER) {
795 // Sanity check that the number of bits set in the reference offset bitmap
796 // agrees with the number of references
797 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
798 NumReferenceStaticFieldsDuringLinking());
799 }
800 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
801 new_reference_offsets, false);
802}
803
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700804bool Class::Implements(const Class* klass) const {
805 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700806 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700807 // All interfaces implemented directly and by our superclass, and
808 // recursively all super-interfaces of those interfaces, are listed
809 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700810 int32_t iftable_count = GetIfTableCount();
811 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
812 for (int32_t i = 0; i < iftable_count; i++) {
813 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700814 return true;
815 }
816 }
817 return false;
818}
819
Elliott Hughese84278b2012-03-22 10:06:53 -0700820// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700821// are array classes.
822//
823// Consider an array class, e.g. Y[][], where Y is a subclass of X.
824// Y[][] = Y[][] --> true (identity)
825// X[][] = Y[][] --> true (element superclass)
826// Y = Y[][] --> false
827// Y[] = Y[][] --> false
828// Object = Y[][] --> true (everything is an object)
829// Object[] = Y[][] --> true
830// Object[][] = Y[][] --> true
831// Object[][][] = Y[][] --> false (too many []s)
832// Serializable = Y[][] --> true (all arrays are Serializable)
833// Serializable[] = Y[][] --> true
834// Serializable[][] = Y[][] --> false (unless Y is Serializable)
835//
836// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700837// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700838//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700839bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700840 DCHECK(IsArrayClass()) << PrettyClass(this);
841 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700842 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700843}
844
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700846 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
847 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700848 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700849 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700850 // src's super should be java_lang_Object, since it is an array.
851 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700852 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
853 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700854 return this == java_lang_Object;
855 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700856 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700857}
858
859bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700860 DCHECK(!IsInterface()) << PrettyClass(this);
861 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700862 const Class* current = this;
863 do {
864 if (current == klass) {
865 return true;
866 }
867 current = current->GetSuperClass();
868 } while (current != NULL);
869 return false;
870}
871
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800872bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700873 size_t i = 0;
874 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
875 ++i;
876 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700877 if (descriptor1.find('/', i) != StringPiece::npos ||
878 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700879 return false;
880 } else {
881 return true;
882 }
883}
884
885bool Class::IsInSamePackage(const Class* that) const {
886 const Class* klass1 = this;
887 const Class* klass2 = that;
888 if (klass1 == klass2) {
889 return true;
890 }
891 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700892 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700893 return false;
894 }
895 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700896 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700897 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700898 }
jeffhao4a801a42011-09-23 13:53:40 -0700899 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700900 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700901 }
902 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800903 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800904 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800905 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800906 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800907 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700908}
909
Elliott Hughesdbb40792011-11-18 17:05:22 -0800910bool Class::IsClassClass() const {
911 Class* java_lang_Class = GetClass()->GetClass();
912 return this == java_lang_Class;
913}
914
915bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800916 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800917}
918
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800919bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700920 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800921}
922
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800923ClassLoader* Class::GetClassLoader() const {
924 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700925}
926
Ian Rogers365c1022012-06-22 15:05:28 -0700927void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700929}
930
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800931Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700932 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700933 DCHECK(declaring_class != NULL) << PrettyClass(this);
934 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700935 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700936 int32_t iftable_count = GetIfTableCount();
937 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
938 for (int32_t i = 0; i < iftable_count; i++) {
939 InterfaceEntry* interface_entry = iftable->Get(i);
940 if (interface_entry->GetInterface() == declaring_class) {
941 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700942 }
943 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700944 return NULL;
945}
946
Ian Rogers466bb252011-10-14 03:29:56 -0700947Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700948 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800949 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700950 if (method != NULL) {
951 return method;
952 }
953
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700954 int32_t iftable_count = GetIfTableCount();
955 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
956 for (int32_t i = 0; i < iftable_count; i++) {
957 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700958 if (method != NULL) {
959 return method;
960 }
961 }
962 return NULL;
963}
964
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800965Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
966 // Check the current class before checking the interfaces.
967 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
968 if (method != NULL) {
969 return method;
970 }
971
972 int32_t iftable_count = GetIfTableCount();
973 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
974 for (int32_t i = 0; i < iftable_count; i++) {
975 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
976 if (method != NULL) {
977 return method;
978 }
979 }
980 return NULL;
981}
982
983
984Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800985 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700986 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700987 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800988 mh.ChangeMethod(method);
989 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700990 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700991 }
992 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700993 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700994}
995
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800996Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
997 if (GetDexCache() == dex_cache) {
998 for (size_t i = 0; i < NumDirectMethods(); ++i) {
999 Method* method = GetDirectMethod(i);
1000 if (method->GetDexMethodIndex() == dex_method_idx) {
1001 return method;
1002 }
1003 }
1004 }
1005 return NULL;
1006}
1007
1008Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1009 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001010 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001011 if (method != NULL) {
1012 return method;
1013 }
1014 }
1015 return NULL;
1016}
1017
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001018Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1019 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1020 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1021 if (method != NULL) {
1022 return method;
1023 }
1024 }
1025 return NULL;
1026}
1027
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001028Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001029 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001030 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001031 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001032 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001033 mh.ChangeMethod(method);
1034 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001035 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001036 }
1037 }
1038 return NULL;
1039}
1040
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001041Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1042 if (GetDexCache() == dex_cache) {
1043 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1044 Method* method = GetVirtualMethod(i);
1045 if (method->GetDexMethodIndex() == dex_method_idx) {
1046 return method;
1047 }
1048 }
1049 }
1050 return NULL;
1051}
1052
Ian Rogers466bb252011-10-14 03:29:56 -07001053Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1054 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1055 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1056 if (method != NULL) {
1057 return method;
1058 }
1059 }
1060 return NULL;
1061}
1062
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001063Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1064 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1065 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1066 if (method != NULL) {
1067 return method;
1068 }
1069 }
1070 return NULL;
1071}
1072
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001073Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 // Is the field in this class?
1075 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001076 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1078 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001079 fh.ChangeField(f);
1080 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001081 return f;
1082 }
1083 }
1084 return NULL;
1085}
1086
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001087Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1088 if (GetDexCache() == dex_cache) {
1089 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1090 Field* f = GetInstanceField(i);
1091 if (f->GetDexFieldIndex() == dex_field_idx) {
1092 return f;
1093 }
1094 }
1095 }
1096 return NULL;
1097}
1098
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001099Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 // Is the field in this class, or any of its superclasses?
1101 // Interfaces are not relevant because they can't contain instance fields.
1102 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001103 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 if (f != NULL) {
1105 return f;
1106 }
1107 }
1108 return NULL;
1109}
1110
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001111Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1112 // Is the field in this class, or any of its superclasses?
1113 // Interfaces are not relevant because they can't contain instance fields.
1114 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1115 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1116 if (f != NULL) {
1117 return f;
1118 }
1119 }
1120 return NULL;
1121}
1122
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001123Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1124 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001125 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001126 for (size_t i = 0; i < NumStaticFields(); ++i) {
1127 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001128 fh.ChangeField(f);
1129 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001130 return f;
1131 }
1132 }
1133 return NULL;
1134}
1135
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001136Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1137 if (dex_cache == GetDexCache()) {
1138 for (size_t i = 0; i < NumStaticFields(); ++i) {
1139 Field* f = GetStaticField(i);
1140 if (f->GetDexFieldIndex() == dex_field_idx) {
1141 return f;
1142 }
1143 }
1144 }
1145 return NULL;
1146}
1147
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001148Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1149 // Is the field in this class (or its interfaces), or any of its
1150 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001151 ClassHelper kh;
1152 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001153 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001154 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001155 if (f != NULL) {
1156 return f;
1157 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001158 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001159 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001160 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1161 Class* interface = kh.GetDirectInterface(i);
1162 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001163 if (f != NULL) {
1164 return f;
1165 }
1166 }
1167 }
1168 return NULL;
1169}
1170
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001171Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1172 ClassHelper kh;
1173 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1174 // Is the field in this class?
1175 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1176 if (f != NULL) {
1177 return f;
1178 }
1179 // Is this field in any of this class' interfaces?
1180 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001181 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1182 Class* interface = kh.GetDirectInterface(i);
1183 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001184 if (f != NULL) {
1185 return f;
1186 }
1187 }
1188 }
1189 return NULL;
1190}
1191
Ian Rogersb067ac22011-12-13 18:05:09 -08001192Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1193 // Find a field using the JLS field resolution order
1194 ClassHelper kh;
1195 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1196 // Is the field in this class?
1197 Field* f = k->FindDeclaredInstanceField(name, type);
1198 if (f != NULL) {
1199 return f;
1200 }
1201 f = k->FindDeclaredStaticField(name, type);
1202 if (f != NULL) {
1203 return f;
1204 }
1205 // Is this field in any of this class' interfaces?
1206 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001207 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1208 Class* interface = kh.GetDirectInterface(i);
1209 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001210 if (f != NULL) {
1211 return f;
1212 }
1213 }
1214 }
1215 return NULL;
1216}
1217
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001218Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001219 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001220 DCHECK_GE(component_count, 0);
1221 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001222
Ian Rogersa15e67d2012-02-28 13:51:55 -08001223 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001224 size_t data_size = component_count * component_size;
1225 size_t size = header_size + data_size;
1226
1227 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1228 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1229 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1230 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001231 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001232 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001233 return NULL;
1234 }
1235
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001236 Heap* heap = Runtime::Current()->GetHeap();
1237 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001238 if (array != NULL) {
1239 DCHECK(array->IsArrayInstance());
1240 array->SetLength(component_count);
1241 }
1242 return array;
1243}
1244
1245Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001247 return Alloc(array_class, component_count, array_class->GetComponentSize());
1248}
1249
Elliott Hughes80609252011-09-23 17:24:51 -07001250bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001251 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001252 "length=%i; index=%i", length_, index);
1253 return false;
1254}
1255
1256bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001257 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001258 "Can't store an element of type %s into an array of type %s",
1259 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1260 return false;
1261}
1262
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001263template<typename T>
1264PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001265 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001266 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1267 return down_cast<PrimitiveArray<T>*>(raw_array);
1268}
1269
1270template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1271
1272// Explicitly instantiate all the primitive array types.
1273template class PrimitiveArray<uint8_t>; // BooleanArray
1274template class PrimitiveArray<int8_t>; // ByteArray
1275template class PrimitiveArray<uint16_t>; // CharArray
1276template class PrimitiveArray<double>; // DoubleArray
1277template class PrimitiveArray<float>; // FloatArray
1278template class PrimitiveArray<int32_t>; // IntArray
1279template class PrimitiveArray<int64_t>; // LongArray
1280template class PrimitiveArray<int16_t>; // ShortArray
1281
Ian Rogers466bb252011-10-14 03:29:56 -07001282// Explicitly instantiate Class[][]
1283template class ObjectArray<ObjectArray<Class> >;
1284
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001285// TODO: get global references for these
1286Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001287
Brian Carlstroma663ea52011-08-19 23:33:41 -07001288void String::SetClass(Class* java_lang_String) {
1289 CHECK(java_lang_String_ == NULL);
1290 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001291 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001292}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001293
Brian Carlstroma663ea52011-08-19 23:33:41 -07001294void String::ResetClass() {
1295 CHECK(java_lang_String_ != NULL);
1296 java_lang_String_ = NULL;
1297}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001298
Brian Carlstromc74255f2011-09-11 22:47:39 -07001299String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001300 return Runtime::Current()->GetInternTable()->InternWeak(this);
1301}
1302
Brian Carlstrom395520e2011-09-25 19:35:00 -07001303int32_t String::GetHashCode() {
1304 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1305 if (result == 0) {
1306 ComputeHashCode();
1307 }
1308 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1309 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1310 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001311 return result;
1312}
1313
1314int32_t String::GetLength() const {
1315 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1316 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1317 return result;
1318}
1319
1320uint16_t String::CharAt(int32_t index) const {
1321 // TODO: do we need this? Equals is the only caller, and could
1322 // bounds check itself.
1323 if (index < 0 || index >= count_) {
1324 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001325 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001326 "length=%i; index=%i", count_, index);
1327 return 0;
1328 }
1329 return GetCharArray()->Get(index + GetOffset());
1330}
1331
1332String* String::AllocFromUtf16(int32_t utf16_length,
1333 const uint16_t* utf16_data_in,
1334 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001335 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001336 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001337 if (string == NULL) {
1338 return NULL;
1339 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001340 // TODO: use 16-bit wide memset variant
1341 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001342 if (array == NULL) {
1343 return NULL;
1344 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001345 for (int i = 0; i < utf16_length; i++) {
1346 array->Set(i, utf16_data_in[i]);
1347 }
1348 if (hash_code != 0) {
1349 string->SetHashCode(hash_code);
1350 } else {
1351 string->ComputeHashCode();
1352 }
1353 return string;
1354}
1355
1356String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001357 if (utf == NULL) {
1358 return NULL;
1359 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001360 size_t char_count = CountModifiedUtf8Chars(utf);
1361 return AllocFromModifiedUtf8(char_count, utf);
1362}
1363
1364String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1365 const char* utf8_data_in) {
1366 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001367 if (string == NULL) {
1368 return NULL;
1369 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001370 uint16_t* utf16_data_out =
1371 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1372 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1373 string->ComputeHashCode();
1374 return string;
1375}
1376
1377String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001378 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1379 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001380 return NULL;
1381 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001382 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001383}
1384
1385String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001386 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001387 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001388 if (string == NULL) {
1389 return NULL;
1390 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001391 string->SetArray(array);
1392 string->SetCount(array->GetLength());
1393 return string;
1394}
1395
1396bool String::Equals(const String* that) const {
1397 if (this == that) {
1398 // Quick reference equality test
1399 return true;
1400 } else if (that == NULL) {
1401 // Null isn't an instanceof anything
1402 return false;
1403 } else if (this->GetLength() != that->GetLength()) {
1404 // Quick length inequality test
1405 return false;
1406 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001407 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001408 // hash code was already equal
1409 for (int32_t i = 0; i < that->GetLength(); ++i) {
1410 if (this->CharAt(i) != that->CharAt(i)) {
1411 return false;
1412 }
1413 }
1414 return true;
1415 }
1416}
1417
Elliott Hughes5d78d392011-12-13 16:53:05 -08001418bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001419 if (this->GetLength() != that_length) {
1420 return false;
1421 } else {
1422 for (int32_t i = 0; i < that_length; ++i) {
1423 if (this->CharAt(i) != that_chars[that_offset + i]) {
1424 return false;
1425 }
1426 }
1427 return true;
1428 }
1429}
1430
1431bool String::Equals(const char* modified_utf8) const {
1432 for (int32_t i = 0; i < GetLength(); ++i) {
1433 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1434 if (ch == '\0' || ch != CharAt(i)) {
1435 return false;
1436 }
1437 }
1438 return *modified_utf8 == '\0';
1439}
1440
1441bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001442 if (modified_utf8.size() != GetLength()) {
1443 return false;
1444 }
1445 const char* p = modified_utf8.data();
1446 for (int32_t i = 0; i < GetLength(); ++i) {
1447 uint16_t ch = GetUtf16FromUtf8(&p);
1448 if (ch != CharAt(i)) {
1449 return false;
1450 }
1451 }
1452 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001453}
1454
1455// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1456std::string String::ToModifiedUtf8() const {
1457 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001458 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001459 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001460 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1461 return result;
1462}
1463
Ian Rogers1c5eb702012-02-01 09:18:34 -08001464void Throwable::SetCause(Throwable* cause) {
1465 CHECK(cause != NULL);
1466 CHECK(cause != this);
1467 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1468 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1469}
1470
Ian Rogers466bb252011-10-14 03:29:56 -07001471bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001472 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001473 return false;
1474 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001475 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001476}
1477
Ian Rogers9074b992011-10-26 17:41:55 -07001478std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001479 std::string result(PrettyTypeOf(this));
1480 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001481 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001482 if (msg != NULL) {
1483 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001484 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001485 result += "\n";
1486 Object* stack_state = GetStackState();
1487 // check stack state isn't missing or corrupt
1488 if (stack_state != NULL && stack_state->IsObjectArray()) {
1489 // Decode the internal stack trace into the depth and method trace
1490 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1491 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001492 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1493 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001494 for (int32_t i = 0; i < depth; ++i) {
1495 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001496 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001497 uint32_t dex_pc = pc_trace->Get(i);
1498 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001499 const char* source_file = mh.GetDeclaringClassSourceFile();
1500 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1501 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001502 }
Ian Rogers9074b992011-10-26 17:41:55 -07001503 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001504 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001505 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001506 result += "Caused by: ";
1507 result += cause->Dump();
1508 }
Ian Rogers9074b992011-10-26 17:41:55 -07001509 return result;
1510}
1511
Ian Rogers5167c972012-02-03 10:41:20 -08001512
1513Class* Throwable::java_lang_Throwable_ = NULL;
1514
1515void Throwable::SetClass(Class* java_lang_Throwable) {
1516 CHECK(java_lang_Throwable_ == NULL);
1517 CHECK(java_lang_Throwable != NULL);
1518 java_lang_Throwable_ = java_lang_Throwable;
1519}
1520
1521void Throwable::ResetClass() {
1522 CHECK(java_lang_Throwable_ != NULL);
1523 java_lang_Throwable_ = NULL;
1524}
1525
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001526Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1527
1528void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1529 CHECK(java_lang_StackTraceElement_ == NULL);
1530 CHECK(java_lang_StackTraceElement != NULL);
1531 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1532}
1533
1534void StackTraceElement::ResetClass() {
1535 CHECK(java_lang_StackTraceElement_ != NULL);
1536 java_lang_StackTraceElement_ = NULL;
1537}
1538
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001539StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1540 String* method_name,
1541 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001542 int32_t line_number) {
1543 StackTraceElement* trace =
1544 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1545 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1546 const_cast<String*>(declaring_class), false);
1547 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1548 const_cast<String*>(method_name), false);
1549 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1550 const_cast<String*>(file_name), false);
1551 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1552 line_number, false);
1553 return trace;
1554}
1555
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001556} // namespace art