blob: c8da0fa6b57bc7ac523996572bcde682f493f4bb [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
Elliott Hughes20cde902011-10-04 17:37:27 -070075 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080076 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070077 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070078
Brian Carlstrom40381fb2011-10-19 14:13:40 -070079 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070080}
81
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070082uint32_t Object::GetThinLockId() {
83 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070084}
85
86void Object::MonitorEnter(Thread* thread) {
87 Monitor::MonitorEnter(thread, this);
88}
89
Ian Rogersff1ed472011-09-20 13:46:24 -070090bool Object::MonitorExit(Thread* thread) {
91 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070092}
93
94void Object::Notify() {
95 Monitor::Notify(Thread::Current(), this);
96}
97
98void Object::NotifyAll() {
99 Monitor::NotifyAll(Thread::Current(), this);
100}
101
102void Object::Wait(int64_t ms, int32_t ns) {
103 Monitor::Wait(Thread::Current(), this, ms, ns, true);
104}
105
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700106// TODO: get global references for these
107Class* Field::java_lang_reflect_Field_ = NULL;
108
109void Field::SetClass(Class* java_lang_reflect_Field) {
110 CHECK(java_lang_reflect_Field_ == NULL);
111 CHECK(java_lang_reflect_Field != NULL);
112 java_lang_reflect_Field_ = java_lang_reflect_Field;
113}
114
115void Field::ResetClass() {
116 CHECK(java_lang_reflect_Field_ != NULL);
117 java_lang_reflect_Field_ = NULL;
118}
119
Ian Rogers0571d352011-11-03 19:51:38 -0700120void Field::SetOffset(MemberOffset num_bytes) {
121 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800122#if 0 // TODO enable later in boot and under !NDEBUG
123 FieldHelper fh(this);
124 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700125 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
126 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
127 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700129 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
130}
131
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700132uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700133 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700134 if (IsStatic()) {
135 object = declaring_class_;
136 }
137 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700138}
139
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700140void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700141 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700142 if (IsStatic()) {
143 object = declaring_class_;
144 }
145 object->SetField32(GetOffset(), new_value, IsVolatile());
146}
147
148uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700149 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150 if (IsStatic()) {
151 object = declaring_class_;
152 }
153 return object->GetField64(GetOffset(), IsVolatile());
154}
155
156void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700157 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700158 if (IsStatic()) {
159 object = declaring_class_;
160 }
161 object->SetField64(GetOffset(), new_value, IsVolatile());
162}
163
164Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700165 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 if (IsStatic()) {
167 object = declaring_class_;
168 }
169 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
170}
171
172void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700173 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700174 if (IsStatic()) {
175 object = declaring_class_;
176 }
177 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
178}
179
180bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800181 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
182 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700183 return Get32(object);
184}
185
186void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800187 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
188 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189 Set32(object, z);
190}
191
192int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
194 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 return Get32(object);
196}
197
198void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
200 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 Set32(object, b);
202}
203
204uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
206 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 return Get32(object);
208}
209
210void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
212 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 Set32(object, c);
214}
215
Ian Rogers466bb252011-10-14 03:29:56 -0700216int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
218 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 return Get32(object);
220}
221
Ian Rogers466bb252011-10-14 03:29:56 -0700222void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
224 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 Set32(object, s);
226}
227
228int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800229 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
230 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 return Get32(object);
232}
233
234void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800235 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
236 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 Set32(object, i);
238}
239
240int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800241 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
242 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 return Get64(object);
244}
245
246void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
248 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 Set64(object, j);
250}
251
Elliott Hughes1d878f32012-04-11 15:17:54 -0700252union Bits {
253 jdouble d;
254 jfloat f;
255 jint i;
256 jlong j;
257};
258
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
261 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700262 Bits bits;
263 bits.i = Get32(object);
264 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265}
266
267void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
269 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700270 Bits bits;
271 bits.f = f;
272 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273}
274
275double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
277 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700278 Bits bits;
279 bits.j = Get64(object);
280 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281}
282
283void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800284 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
285 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700286 Bits bits;
287 bits.d = d;
288 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289}
290
291Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
293 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 return GetObj(object);
295}
296
297void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
299 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 SetObj(object, l);
301}
302
303// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700304Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305Class* Method::java_lang_reflect_Method_ = NULL;
306
Elliott Hughes80609252011-09-23 17:24:51 -0700307void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
308 CHECK(java_lang_reflect_Constructor_ == NULL);
309 CHECK(java_lang_reflect_Constructor != NULL);
310 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
311
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 CHECK(java_lang_reflect_Method_ == NULL);
313 CHECK(java_lang_reflect_Method != NULL);
314 java_lang_reflect_Method_ = java_lang_reflect_Method;
315}
316
Elliott Hughes80609252011-09-23 17:24:51 -0700317void Method::ResetClasses() {
318 CHECK(java_lang_reflect_Constructor_ != NULL);
319 java_lang_reflect_Constructor_ = NULL;
320
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 CHECK(java_lang_reflect_Method_ != NULL);
322 java_lang_reflect_Method_ = NULL;
323}
324
325ObjectArray<String>* Method::GetDexCacheStrings() const {
326 return GetFieldObject<ObjectArray<String>*>(
327 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
328}
329
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
331 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
332 new_dex_cache_strings, false);
333}
334
Ian Rogers19846512012-02-24 11:42:47 -0800335ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
336 return GetFieldObject<ObjectArray<Method>*>(
337 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
338}
339
340void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
341 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
342 new_dex_cache_methods, false);
343}
344
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
346 return GetFieldObject<ObjectArray<Class>*>(
347 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
348}
349
350void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
351 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
352 new_dex_cache_classes, false);
353}
354
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
356 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
357 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
358 false);
359}
360
361void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700362 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700364}
365
366size_t Method::NumArgRegisters(const StringPiece& shorty) {
367 CHECK_LE(1, shorty.length());
368 uint32_t num_registers = 0;
369 for (int i = 1; i < shorty.length(); ++i) {
370 char ch = shorty[i];
371 if (ch == 'D' || ch == 'J') {
372 num_registers += 2;
373 } else {
374 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700375 }
376 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377 return num_registers;
378}
379
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800380bool Method::IsProxyMethod() const {
381 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382}
383
Ian Rogers466bb252011-10-14 03:29:56 -0700384Method* Method::FindOverriddenMethod() const {
385 if (IsStatic()) {
386 return NULL;
387 }
388 Class* declaring_class = GetDeclaringClass();
389 Class* super_class = declaring_class->GetSuperClass();
390 uint16_t method_index = GetMethodIndex();
391 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
392 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800393 // Did this method override a super class method? If so load the result from the super class'
394 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700395 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
396 result = super_class_vtable->Get(method_index);
397 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800398 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800399 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800400 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
401 CHECK_EQ(result,
402 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800403 } else {
404 MethodHelper mh(this);
405 MethodHelper interface_mh;
406 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
407 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
408 InterfaceEntry* entry = iftable->Get(i);
409 Class* interface = entry->GetInterface();
410 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
411 Method* interface_method = interface->GetVirtualMethod(j);
412 interface_mh.ChangeMethod(interface_method);
413 if (mh.HasSameNameAndSignature(&interface_mh)) {
414 result = interface_method;
415 break;
416 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800417 }
418 }
Ian Rogers466bb252011-10-14 03:29:56 -0700419 }
420 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800421#ifndef NDEBUG
422 MethodHelper result_mh(result);
423 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
424#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700425 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426}
427
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700428static const void* GetOatCode(const Method* m)
429 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800430 Runtime* runtime = Runtime::Current();
431 const void* code = m->GetCode();
432 // Peel off any method tracing trampoline.
433 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
434 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
435 }
436 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800437 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800438 code = runtime->GetClassLinker()->GetOatCodeFor(m);
439 }
440 return code;
441}
442
Ian Rogersbdb03912011-09-14 00:55:44 -0700443uint32_t Method::ToDexPC(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700444#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700445 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700446 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800447 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700448 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700449 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700450 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800451 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700452 uint32_t best_offset = 0;
453 uint32_t best_dex_offset = 0;
454 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700455 uint32_t map_offset = mapping_table[i];
456 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700457 if (map_offset == sought_offset) {
458 best_offset = map_offset;
459 best_dex_offset = map_dex_offset;
460 break;
461 }
462 if (map_offset < sought_offset && map_offset > best_offset) {
463 best_offset = map_offset;
464 best_dex_offset = map_dex_offset;
465 }
466 }
467 return best_dex_offset;
TDYa127c8dc1012012-04-19 07:03:33 -0700468#else
469 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
470 return static_cast<uint32_t>(pc);
471#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700472}
473
474uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700476 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700477 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700478 return 0; // Special no mapping/pc == 0 case
479 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700480 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700481 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 uint32_t map_offset = mapping_table[i];
483 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700484 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800485 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700486 }
487 }
488 LOG(FATAL) << "Looking up Dex PC not contained in method";
489 return 0;
490}
491
492uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800493 MethodHelper mh(this);
494 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700495 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700496 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
497 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700498 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700499 if (iter_type_idx == DexFile::kDexNoIndex16) {
500 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700501 }
502 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800503 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700504 if (iter_exception_type == NULL) {
505 // The verifier should take care of resolving all exception classes early
506 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800507 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700508 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700509 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700510 }
511 }
512 // Handler not found
513 return DexFile::kDexNoIndex;
514}
515
Elliott Hughes77405792012-03-15 15:22:12 -0700516void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517 if (kIsDebugBuild) {
518 self->AssertThreadSuspensionIsAllowable();
519 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
520 CHECK_EQ(kRunnable, self->GetState());
521 }
TDYa12785321912012-04-01 15:24:56 -0700522
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700523 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700524 ManagedStack fragment;
525 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700526
527 // Call the invoke stub associated with the method.
528 // Pass everything as arguments.
Ian Rogers1b09b092012-08-20 15:35:52 -0700529 Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700530
531 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700532
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500533 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700534 bool log = false;
535 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800536 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
537 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700538 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700539 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700540 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800541 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
542 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700543 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700544 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800545 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
546 PrettyMethod(this).c_str(), GetCode(), stub,
547 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700548 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700549 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700550 }
551 }
552
553 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700554 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700555}
556
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700557bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700558 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800559 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800560 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700561 return native_method != jni_stub;
562}
563
Ian Rogers60db5ab2012-02-20 17:02:00 -0800564void Method::RegisterNative(Thread* self, const void* native_method) {
565 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700566 CHECK(IsNative()) << PrettyMethod(this);
567 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700568#if defined(ART_USE_LLVM_COMPILER)
569 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
570 native_method, false);
571#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800572 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
573 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
574 native_method, false);
575 } else {
576 // We've been asked to associate this method with the given native method but are working
577 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
578 // the native method to runtime support and store the target somewhere runtime support will
579 // find it.
580#if defined(__arm__)
581 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
582 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
583#else
584 UNIMPLEMENTED(FATAL);
585#endif
586 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
587 reinterpret_cast<const uint8_t*>(native_method), false);
588 }
TDYa12726467572012-04-17 20:51:22 -0700589#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700590}
591
Ian Rogers19846512012-02-24 11:42:47 -0800592void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700593 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700594 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800595 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700596}
597
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700598void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700599 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
600 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700601 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800602 if (new_status == kStatusError) {
603 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
604
605 // stash current exception
606 Thread* self = Thread::Current();
607 SirtRef<Throwable> exception(self->GetException());
608 CHECK(exception.get() != NULL);
609
610 // clear exception to call FindSystemClass
611 self->ClearException();
612 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
613 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
614 CHECK(!self->IsExceptionPending());
615
616 // only verification errors, not initialization problems, should set a verify error.
617 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
618 Class* exception_class = exception->GetClass();
619 if (!eiie_class->IsAssignableFrom(exception_class)) {
620 SetVerifyErrorClass(exception_class);
621 }
622
623 // restore exception
624 self->SetException(exception.get());
625 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700626 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700627}
628
629DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700630 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700631}
632
633void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700634 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700635}
636
Brian Carlstrom1f870082011-08-23 16:02:11 -0700637Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700638 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700639 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500640 // TODO: decide whether we want this check. It currently fails during bootstrap.
641 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700642 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800643 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700644}
645
Ian Rogers0571d352011-11-03 19:51:38 -0700646void Class::SetClassSize(size_t new_class_size) {
647 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
648 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
649}
650
Ian Rogersd418eda2012-01-30 12:14:28 -0800651// Return the class' name. The exact format is bizarre, but it's the specified behavior for
652// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
653// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
654// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
655String* Class::ComputeName() {
656 String* name = GetName();
657 if (name != NULL) {
658 return name;
659 }
660 std::string descriptor(ClassHelper(this).GetDescriptor());
661 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
662 // The descriptor indicates that this is the class for
663 // a primitive type; special-case the return value.
664 const char* c_name = NULL;
665 switch (descriptor[0]) {
666 case 'Z': c_name = "boolean"; break;
667 case 'B': c_name = "byte"; break;
668 case 'C': c_name = "char"; break;
669 case 'S': c_name = "short"; break;
670 case 'I': c_name = "int"; break;
671 case 'J': c_name = "long"; break;
672 case 'F': c_name = "float"; break;
673 case 'D': c_name = "double"; break;
674 case 'V': c_name = "void"; break;
675 default:
676 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
677 }
678 name = String::AllocFromModifiedUtf8(c_name);
679 } else {
680 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
681 // components.
682 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
683 descriptor.erase(0, 1);
684 descriptor.erase(descriptor.size() - 1);
685 }
686 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
687 name = String::AllocFromModifiedUtf8(descriptor.c_str());
688 }
689 SetName(name);
690 return name;
691}
692
Elliott Hughes4681c802011-09-25 18:04:37 -0700693void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700694 if ((flags & kDumpClassFullDetail) == 0) {
695 os << PrettyClass(this);
696 if ((flags & kDumpClassClassLoader) != 0) {
697 os << ' ' << GetClassLoader();
698 }
699 if ((flags & kDumpClassInitialized) != 0) {
700 os << ' ' << GetStatus();
701 }
Elliott Hughese0918552011-10-28 17:18:29 -0700702 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700703 return;
704 }
705
706 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800707 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700708 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800709 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700710 os << " objectSize=" << SizeOf() << " "
711 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
712 os << StringPrintf(" access=0x%04x.%04x\n",
713 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
714 if (super != NULL) {
715 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
716 }
717 if (IsArrayClass()) {
718 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
719 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700720 if (kh.NumDirectInterfaces() > 0) {
721 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
722 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
723 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700724 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800725 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700726 }
727 }
728 os << " vtable (" << NumVirtualMethods() << " entries, "
729 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
730 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800731 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700732 }
733 os << " direct methods (" << NumDirectMethods() << " entries):\n";
734 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800735 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700736 }
737 if (NumStaticFields() > 0) {
738 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700739 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700740 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800741 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700742 }
743 } else {
744 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700745 }
746 }
747 if (NumInstanceFields() > 0) {
748 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700749 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700750 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800751 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700752 }
753 } else {
754 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700755 }
756 }
757}
758
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700759void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
760 if (new_reference_offsets != CLASS_WALK_SUPER) {
761 // Sanity check that the number of bits set in the reference offset bitmap
762 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800763 size_t count = 0;
764 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
765 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700766 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800767 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700768 }
769 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
770 new_reference_offsets, false);
771}
772
773void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
774 if (new_reference_offsets != CLASS_WALK_SUPER) {
775 // Sanity check that the number of bits set in the reference offset bitmap
776 // agrees with the number of references
777 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
778 NumReferenceStaticFieldsDuringLinking());
779 }
780 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
781 new_reference_offsets, false);
782}
783
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700784bool Class::Implements(const Class* klass) const {
785 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700786 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700787 // All interfaces implemented directly and by our superclass, and
788 // recursively all super-interfaces of those interfaces, are listed
789 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700790 int32_t iftable_count = GetIfTableCount();
791 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
792 for (int32_t i = 0; i < iftable_count; i++) {
793 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700794 return true;
795 }
796 }
797 return false;
798}
799
Elliott Hughese84278b2012-03-22 10:06:53 -0700800// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700801// are array classes.
802//
803// Consider an array class, e.g. Y[][], where Y is a subclass of X.
804// Y[][] = Y[][] --> true (identity)
805// X[][] = Y[][] --> true (element superclass)
806// Y = Y[][] --> false
807// Y[] = Y[][] --> false
808// Object = Y[][] --> true (everything is an object)
809// Object[] = Y[][] --> true
810// Object[][] = Y[][] --> true
811// Object[][][] = Y[][] --> false (too many []s)
812// Serializable = Y[][] --> true (all arrays are Serializable)
813// Serializable[] = Y[][] --> true
814// Serializable[][] = Y[][] --> false (unless Y is Serializable)
815//
816// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700817// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700818//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700819bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700820 DCHECK(IsArrayClass()) << PrettyClass(this);
821 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700822 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700823}
824
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700825bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700826 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
827 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700828 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700829 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700830 // src's super should be java_lang_Object, since it is an array.
831 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700832 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
833 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700834 return this == java_lang_Object;
835 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700836 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700837}
838
839bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700840 DCHECK(!IsInterface()) << PrettyClass(this);
841 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700842 const Class* current = this;
843 do {
844 if (current == klass) {
845 return true;
846 }
847 current = current->GetSuperClass();
848 } while (current != NULL);
849 return false;
850}
851
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800852bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700853 size_t i = 0;
854 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
855 ++i;
856 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700857 if (descriptor1.find('/', i) != StringPiece::npos ||
858 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700859 return false;
860 } else {
861 return true;
862 }
863}
864
865bool Class::IsInSamePackage(const Class* that) const {
866 const Class* klass1 = this;
867 const Class* klass2 = that;
868 if (klass1 == klass2) {
869 return true;
870 }
871 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700872 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700873 return false;
874 }
875 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700876 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700877 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700878 }
jeffhao4a801a42011-09-23 13:53:40 -0700879 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700880 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700881 }
882 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800883 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800884 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800885 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800886 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800887 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700888}
889
Elliott Hughesdbb40792011-11-18 17:05:22 -0800890bool Class::IsClassClass() const {
891 Class* java_lang_Class = GetClass()->GetClass();
892 return this == java_lang_Class;
893}
894
895bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800896 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800897}
898
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800899bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700900 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800901}
902
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800903ClassLoader* Class::GetClassLoader() const {
904 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700905}
906
Ian Rogers365c1022012-06-22 15:05:28 -0700907void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700909}
910
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800911Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700912 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700913 DCHECK(declaring_class != NULL) << PrettyClass(this);
914 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700915 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700916 int32_t iftable_count = GetIfTableCount();
917 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
918 for (int32_t i = 0; i < iftable_count; i++) {
919 InterfaceEntry* interface_entry = iftable->Get(i);
920 if (interface_entry->GetInterface() == declaring_class) {
921 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700922 }
923 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700924 return NULL;
925}
926
Ian Rogers466bb252011-10-14 03:29:56 -0700927Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700928 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800929 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700930 if (method != NULL) {
931 return method;
932 }
933
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700934 int32_t iftable_count = GetIfTableCount();
935 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
936 for (int32_t i = 0; i < iftable_count; i++) {
937 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700938 if (method != NULL) {
939 return method;
940 }
941 }
942 return NULL;
943}
944
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800945Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
946 // Check the current class before checking the interfaces.
947 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
948 if (method != NULL) {
949 return method;
950 }
951
952 int32_t iftable_count = GetIfTableCount();
953 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
954 for (int32_t i = 0; i < iftable_count; i++) {
955 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
956 if (method != NULL) {
957 return method;
958 }
959 }
960 return NULL;
961}
962
963
964Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800965 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700966 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700967 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800968 mh.ChangeMethod(method);
969 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700970 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700971 }
972 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700973 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700974}
975
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800976Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
977 if (GetDexCache() == dex_cache) {
978 for (size_t i = 0; i < NumDirectMethods(); ++i) {
979 Method* method = GetDirectMethod(i);
980 if (method->GetDexMethodIndex() == dex_method_idx) {
981 return method;
982 }
983 }
984 }
985 return NULL;
986}
987
988Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
989 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700990 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700991 if (method != NULL) {
992 return method;
993 }
994 }
995 return NULL;
996}
997
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800998Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
999 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1000 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1001 if (method != NULL) {
1002 return method;
1003 }
1004 }
1005 return NULL;
1006}
1007
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001008Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001009 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001010 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001011 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001012 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001013 mh.ChangeMethod(method);
1014 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001015 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001016 }
1017 }
1018 return NULL;
1019}
1020
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001021Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1022 if (GetDexCache() == dex_cache) {
1023 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1024 Method* method = GetVirtualMethod(i);
1025 if (method->GetDexMethodIndex() == dex_method_idx) {
1026 return method;
1027 }
1028 }
1029 }
1030 return NULL;
1031}
1032
Ian Rogers466bb252011-10-14 03:29:56 -07001033Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1034 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1035 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1036 if (method != NULL) {
1037 return method;
1038 }
1039 }
1040 return NULL;
1041}
1042
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001043Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1044 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1045 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1046 if (method != NULL) {
1047 return method;
1048 }
1049 }
1050 return NULL;
1051}
1052
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001053Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 // Is the field in this class?
1055 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001056 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1058 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001059 fh.ChangeField(f);
1060 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001061 return f;
1062 }
1063 }
1064 return NULL;
1065}
1066
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001067Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1068 if (GetDexCache() == dex_cache) {
1069 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1070 Field* f = GetInstanceField(i);
1071 if (f->GetDexFieldIndex() == dex_field_idx) {
1072 return f;
1073 }
1074 }
1075 }
1076 return NULL;
1077}
1078
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001079Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 // Is the field in this class, or any of its superclasses?
1081 // Interfaces are not relevant because they can't contain instance fields.
1082 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001083 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 if (f != NULL) {
1085 return f;
1086 }
1087 }
1088 return NULL;
1089}
1090
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001091Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1092 // Is the field in this class, or any of its superclasses?
1093 // Interfaces are not relevant because they can't contain instance fields.
1094 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1095 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1096 if (f != NULL) {
1097 return f;
1098 }
1099 }
1100 return NULL;
1101}
1102
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001103Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1104 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001105 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001106 for (size_t i = 0; i < NumStaticFields(); ++i) {
1107 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001108 fh.ChangeField(f);
1109 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001110 return f;
1111 }
1112 }
1113 return NULL;
1114}
1115
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001116Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1117 if (dex_cache == GetDexCache()) {
1118 for (size_t i = 0; i < NumStaticFields(); ++i) {
1119 Field* f = GetStaticField(i);
1120 if (f->GetDexFieldIndex() == dex_field_idx) {
1121 return f;
1122 }
1123 }
1124 }
1125 return NULL;
1126}
1127
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001128Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1129 // Is the field in this class (or its interfaces), or any of its
1130 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001131 ClassHelper kh;
1132 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001133 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001134 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001135 if (f != NULL) {
1136 return f;
1137 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001138 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001139 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001140 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1141 Class* interface = kh.GetDirectInterface(i);
1142 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001143 if (f != NULL) {
1144 return f;
1145 }
1146 }
1147 }
1148 return NULL;
1149}
1150
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001151Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1152 ClassHelper kh;
1153 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1154 // Is the field in this class?
1155 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1156 if (f != NULL) {
1157 return f;
1158 }
1159 // Is this field in any of this class' interfaces?
1160 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001161 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1162 Class* interface = kh.GetDirectInterface(i);
1163 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001164 if (f != NULL) {
1165 return f;
1166 }
1167 }
1168 }
1169 return NULL;
1170}
1171
Ian Rogersb067ac22011-12-13 18:05:09 -08001172Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1173 // Find a field using the JLS field resolution order
1174 ClassHelper kh;
1175 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1176 // Is the field in this class?
1177 Field* f = k->FindDeclaredInstanceField(name, type);
1178 if (f != NULL) {
1179 return f;
1180 }
1181 f = k->FindDeclaredStaticField(name, type);
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(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001190 if (f != NULL) {
1191 return f;
1192 }
1193 }
1194 }
1195 return NULL;
1196}
1197
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001198Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001199 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001200 DCHECK_GE(component_count, 0);
1201 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001202
Ian Rogersa15e67d2012-02-28 13:51:55 -08001203 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001204 size_t data_size = component_count * component_size;
1205 size_t size = header_size + data_size;
1206
1207 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1208 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1209 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1210 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001211 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001212 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001213 return NULL;
1214 }
1215
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001216 Heap* heap = Runtime::Current()->GetHeap();
1217 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001218 if (array != NULL) {
1219 DCHECK(array->IsArrayInstance());
1220 array->SetLength(component_count);
1221 }
1222 return array;
1223}
1224
1225Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001227 return Alloc(array_class, component_count, array_class->GetComponentSize());
1228}
1229
Elliott Hughes80609252011-09-23 17:24:51 -07001230bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001231 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001232 "length=%i; index=%i", length_, index);
1233 return false;
1234}
1235
1236bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001237 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001238 "Can't store an element of type %s into an array of type %s",
1239 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1240 return false;
1241}
1242
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001243template<typename T>
1244PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001245 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001246 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1247 return down_cast<PrimitiveArray<T>*>(raw_array);
1248}
1249
1250template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1251
1252// Explicitly instantiate all the primitive array types.
1253template class PrimitiveArray<uint8_t>; // BooleanArray
1254template class PrimitiveArray<int8_t>; // ByteArray
1255template class PrimitiveArray<uint16_t>; // CharArray
1256template class PrimitiveArray<double>; // DoubleArray
1257template class PrimitiveArray<float>; // FloatArray
1258template class PrimitiveArray<int32_t>; // IntArray
1259template class PrimitiveArray<int64_t>; // LongArray
1260template class PrimitiveArray<int16_t>; // ShortArray
1261
Ian Rogers466bb252011-10-14 03:29:56 -07001262// Explicitly instantiate Class[][]
1263template class ObjectArray<ObjectArray<Class> >;
1264
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001265// TODO: get global references for these
1266Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001267
Brian Carlstroma663ea52011-08-19 23:33:41 -07001268void String::SetClass(Class* java_lang_String) {
1269 CHECK(java_lang_String_ == NULL);
1270 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001271 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001272}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001273
Brian Carlstroma663ea52011-08-19 23:33:41 -07001274void String::ResetClass() {
1275 CHECK(java_lang_String_ != NULL);
1276 java_lang_String_ = NULL;
1277}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001278
Brian Carlstromc74255f2011-09-11 22:47:39 -07001279String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001280 return Runtime::Current()->GetInternTable()->InternWeak(this);
1281}
1282
Brian Carlstrom395520e2011-09-25 19:35:00 -07001283int32_t String::GetHashCode() {
1284 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1285 if (result == 0) {
1286 ComputeHashCode();
1287 }
1288 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1289 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1290 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001291 return result;
1292}
1293
1294int32_t String::GetLength() const {
1295 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1296 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1297 return result;
1298}
1299
1300uint16_t String::CharAt(int32_t index) const {
1301 // TODO: do we need this? Equals is the only caller, and could
1302 // bounds check itself.
1303 if (index < 0 || index >= count_) {
1304 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001305 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001306 "length=%i; index=%i", count_, index);
1307 return 0;
1308 }
1309 return GetCharArray()->Get(index + GetOffset());
1310}
1311
1312String* String::AllocFromUtf16(int32_t utf16_length,
1313 const uint16_t* utf16_data_in,
1314 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001315 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001316 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001317 if (string == NULL) {
1318 return NULL;
1319 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001320 // TODO: use 16-bit wide memset variant
1321 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001322 if (array == NULL) {
1323 return NULL;
1324 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001325 for (int i = 0; i < utf16_length; i++) {
1326 array->Set(i, utf16_data_in[i]);
1327 }
1328 if (hash_code != 0) {
1329 string->SetHashCode(hash_code);
1330 } else {
1331 string->ComputeHashCode();
1332 }
1333 return string;
1334}
1335
1336String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001337 if (utf == NULL) {
1338 return NULL;
1339 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001340 size_t char_count = CountModifiedUtf8Chars(utf);
1341 return AllocFromModifiedUtf8(char_count, utf);
1342}
1343
1344String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1345 const char* utf8_data_in) {
1346 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001347 if (string == NULL) {
1348 return NULL;
1349 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001350 uint16_t* utf16_data_out =
1351 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1352 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1353 string->ComputeHashCode();
1354 return string;
1355}
1356
1357String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001358 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1359 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001360 return NULL;
1361 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001362 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363}
1364
1365String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001366 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001368 if (string == NULL) {
1369 return NULL;
1370 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001371 string->SetArray(array);
1372 string->SetCount(array->GetLength());
1373 return string;
1374}
1375
1376bool String::Equals(const String* that) const {
1377 if (this == that) {
1378 // Quick reference equality test
1379 return true;
1380 } else if (that == NULL) {
1381 // Null isn't an instanceof anything
1382 return false;
1383 } else if (this->GetLength() != that->GetLength()) {
1384 // Quick length inequality test
1385 return false;
1386 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001387 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001388 // hash code was already equal
1389 for (int32_t i = 0; i < that->GetLength(); ++i) {
1390 if (this->CharAt(i) != that->CharAt(i)) {
1391 return false;
1392 }
1393 }
1394 return true;
1395 }
1396}
1397
Elliott Hughes5d78d392011-12-13 16:53:05 -08001398bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001399 if (this->GetLength() != that_length) {
1400 return false;
1401 } else {
1402 for (int32_t i = 0; i < that_length; ++i) {
1403 if (this->CharAt(i) != that_chars[that_offset + i]) {
1404 return false;
1405 }
1406 }
1407 return true;
1408 }
1409}
1410
1411bool String::Equals(const char* modified_utf8) const {
1412 for (int32_t i = 0; i < GetLength(); ++i) {
1413 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1414 if (ch == '\0' || ch != CharAt(i)) {
1415 return false;
1416 }
1417 }
1418 return *modified_utf8 == '\0';
1419}
1420
1421bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001422 if (modified_utf8.size() != GetLength()) {
1423 return false;
1424 }
1425 const char* p = modified_utf8.data();
1426 for (int32_t i = 0; i < GetLength(); ++i) {
1427 uint16_t ch = GetUtf16FromUtf8(&p);
1428 if (ch != CharAt(i)) {
1429 return false;
1430 }
1431 }
1432 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001433}
1434
1435// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1436std::string String::ToModifiedUtf8() const {
1437 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001438 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001439 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001440 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1441 return result;
1442}
1443
Ian Rogers1c5eb702012-02-01 09:18:34 -08001444void Throwable::SetCause(Throwable* cause) {
1445 CHECK(cause != NULL);
1446 CHECK(cause != this);
1447 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1448 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1449}
1450
Ian Rogers466bb252011-10-14 03:29:56 -07001451bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001452 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001453 return false;
1454 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001455 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001456}
1457
Ian Rogers9074b992011-10-26 17:41:55 -07001458std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001459 std::string result(PrettyTypeOf(this));
1460 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001461 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001462 if (msg != NULL) {
1463 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001464 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001465 result += "\n";
1466 Object* stack_state = GetStackState();
1467 // check stack state isn't missing or corrupt
1468 if (stack_state != NULL && stack_state->IsObjectArray()) {
1469 // Decode the internal stack trace into the depth and method trace
1470 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1471 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001472 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1473 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001474 for (int32_t i = 0; i < depth; ++i) {
1475 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001476 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001477 uint32_t dex_pc = pc_trace->Get(i);
1478 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001479 const char* source_file = mh.GetDeclaringClassSourceFile();
1480 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1481 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001482 }
Ian Rogers9074b992011-10-26 17:41:55 -07001483 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001484 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001485 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001486 result += "Caused by: ";
1487 result += cause->Dump();
1488 }
Ian Rogers9074b992011-10-26 17:41:55 -07001489 return result;
1490}
1491
Ian Rogers5167c972012-02-03 10:41:20 -08001492
1493Class* Throwable::java_lang_Throwable_ = NULL;
1494
1495void Throwable::SetClass(Class* java_lang_Throwable) {
1496 CHECK(java_lang_Throwable_ == NULL);
1497 CHECK(java_lang_Throwable != NULL);
1498 java_lang_Throwable_ = java_lang_Throwable;
1499}
1500
1501void Throwable::ResetClass() {
1502 CHECK(java_lang_Throwable_ != NULL);
1503 java_lang_Throwable_ = NULL;
1504}
1505
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001506Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1507
1508void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1509 CHECK(java_lang_StackTraceElement_ == NULL);
1510 CHECK(java_lang_StackTraceElement != NULL);
1511 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1512}
1513
1514void StackTraceElement::ResetClass() {
1515 CHECK(java_lang_StackTraceElement_ != NULL);
1516 java_lang_StackTraceElement_ = NULL;
1517}
1518
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001519StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1520 String* method_name,
1521 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001522 int32_t line_number) {
1523 StackTraceElement* trace =
1524 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1525 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1526 const_cast<String*>(declaring_class), false);
1527 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1528 const_cast<String*>(method_name), false);
1529 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1530 const_cast<String*>(file_name), false);
1531 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1532 line_number, false);
1533 return trace;
1534}
1535
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001536} // namespace art