blob: 83994ffd592485a65561a7c7c685494c6803de23 [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
Ian Rogers08f753d2012-08-24 14:35:25 -0700307InvokeType Method::GetInvokeType() const {
308 // TODO: kSuper?
309 if (GetDeclaringClass()->IsInterface()) {
310 return kInterface;
311 } else if (IsStatic()) {
312 return kStatic;
313 } else if (IsDirect()) {
314 return kDirect;
315 } else {
316 return kVirtual;
317 }
318}
319
Elliott Hughes80609252011-09-23 17:24:51 -0700320void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
321 CHECK(java_lang_reflect_Constructor_ == NULL);
322 CHECK(java_lang_reflect_Constructor != NULL);
323 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
324
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700325 CHECK(java_lang_reflect_Method_ == NULL);
326 CHECK(java_lang_reflect_Method != NULL);
327 java_lang_reflect_Method_ = java_lang_reflect_Method;
328}
329
Elliott Hughes80609252011-09-23 17:24:51 -0700330void Method::ResetClasses() {
331 CHECK(java_lang_reflect_Constructor_ != NULL);
332 java_lang_reflect_Constructor_ = NULL;
333
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700334 CHECK(java_lang_reflect_Method_ != NULL);
335 java_lang_reflect_Method_ = NULL;
336}
337
338ObjectArray<String>* Method::GetDexCacheStrings() const {
339 return GetFieldObject<ObjectArray<String>*>(
340 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
341}
342
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
344 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
345 new_dex_cache_strings, false);
346}
347
Ian Rogers19846512012-02-24 11:42:47 -0800348ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
349 return GetFieldObject<ObjectArray<Method>*>(
350 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
351}
352
353void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
354 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
355 new_dex_cache_methods, false);
356}
357
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
359 return GetFieldObject<ObjectArray<Class>*>(
360 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
361}
362
363void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
364 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
365 new_dex_cache_classes, false);
366}
367
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700368ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
369 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
370 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
371 false);
372}
373
374void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700375 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377}
378
379size_t Method::NumArgRegisters(const StringPiece& shorty) {
380 CHECK_LE(1, shorty.length());
381 uint32_t num_registers = 0;
382 for (int i = 1; i < shorty.length(); ++i) {
383 char ch = shorty[i];
384 if (ch == 'D' || ch == 'J') {
385 num_registers += 2;
386 } else {
387 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700388 }
389 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 return num_registers;
391}
392
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800393bool Method::IsProxyMethod() const {
394 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395}
396
Ian Rogers466bb252011-10-14 03:29:56 -0700397Method* Method::FindOverriddenMethod() const {
398 if (IsStatic()) {
399 return NULL;
400 }
401 Class* declaring_class = GetDeclaringClass();
402 Class* super_class = declaring_class->GetSuperClass();
403 uint16_t method_index = GetMethodIndex();
404 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
405 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800406 // Did this method override a super class method? If so load the result from the super class'
407 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700408 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
409 result = super_class_vtable->Get(method_index);
410 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800411 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800412 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800413 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
414 CHECK_EQ(result,
415 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800416 } else {
417 MethodHelper mh(this);
418 MethodHelper interface_mh;
419 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
420 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
421 InterfaceEntry* entry = iftable->Get(i);
422 Class* interface = entry->GetInterface();
423 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
424 Method* interface_method = interface->GetVirtualMethod(j);
425 interface_mh.ChangeMethod(interface_method);
426 if (mh.HasSameNameAndSignature(&interface_mh)) {
427 result = interface_method;
428 break;
429 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800430 }
431 }
Ian Rogers466bb252011-10-14 03:29:56 -0700432 }
433 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800434#ifndef NDEBUG
435 MethodHelper result_mh(result);
436 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
437#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700438 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700439}
440
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700441static const void* GetOatCode(const Method* m)
442 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800443 Runtime* runtime = Runtime::Current();
444 const void* code = m->GetCode();
445 // Peel off any method tracing trampoline.
446 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
447 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
448 }
449 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800450 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800451 code = runtime->GetClassLinker()->GetOatCodeFor(m);
452 }
453 return code;
454}
455
Ian Rogersbdb03912011-09-14 00:55:44 -0700456uint32_t Method::ToDexPC(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700457#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700458 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700459 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800460 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700461 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700462 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700463 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800464 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700465 uint32_t best_offset = 0;
466 uint32_t best_dex_offset = 0;
467 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700468 uint32_t map_offset = mapping_table[i];
469 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700470 if (map_offset == sought_offset) {
471 best_offset = map_offset;
472 best_dex_offset = map_dex_offset;
473 break;
474 }
475 if (map_offset < sought_offset && map_offset > best_offset) {
476 best_offset = map_offset;
477 best_dex_offset = map_dex_offset;
478 }
479 }
480 return best_dex_offset;
TDYa127c8dc1012012-04-19 07:03:33 -0700481#else
482 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
483 return static_cast<uint32_t>(pc);
484#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700485}
486
487uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700488 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700489 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700490 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700491 return 0; // Special no mapping/pc == 0 case
492 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700493 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700494 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700495 uint32_t map_offset = mapping_table[i];
496 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700497 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800498 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700499 }
500 }
501 LOG(FATAL) << "Looking up Dex PC not contained in method";
502 return 0;
503}
504
505uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800506 MethodHelper mh(this);
507 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700508 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700509 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
510 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700511 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700512 if (iter_type_idx == DexFile::kDexNoIndex16) {
513 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700514 }
515 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800516 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700517 if (iter_exception_type == NULL) {
518 // The verifier should take care of resolving all exception classes early
519 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700521 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700522 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700523 }
524 }
525 // Handler not found
526 return DexFile::kDexNoIndex;
527}
528
Elliott Hughes77405792012-03-15 15:22:12 -0700529void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 if (kIsDebugBuild) {
531 self->AssertThreadSuspensionIsAllowable();
532 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
533 CHECK_EQ(kRunnable, self->GetState());
534 }
TDYa12785321912012-04-01 15:24:56 -0700535
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700536 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700537 ManagedStack fragment;
538 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700539
540 // Call the invoke stub associated with the method.
541 // Pass everything as arguments.
Ian Rogers1b09b092012-08-20 15:35:52 -0700542 Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700543
544 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700545
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500546 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700547 bool log = false;
548 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800549 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
550 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700551 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700552 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700553 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800554 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
555 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700556 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700557 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800558 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
559 PrettyMethod(this).c_str(), GetCode(), stub,
560 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700561 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700562 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700563 }
564 }
565
566 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700567 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700568}
569
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700570bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700571 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800572 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800573 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700574 return native_method != jni_stub;
575}
576
Ian Rogers60db5ab2012-02-20 17:02:00 -0800577void Method::RegisterNative(Thread* self, const void* native_method) {
578 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700579 CHECK(IsNative()) << PrettyMethod(this);
580 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700581#if defined(ART_USE_LLVM_COMPILER)
582 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
583 native_method, false);
584#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800585 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
586 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
587 native_method, false);
588 } else {
589 // We've been asked to associate this method with the given native method but are working
590 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
591 // the native method to runtime support and store the target somewhere runtime support will
592 // find it.
593#if defined(__arm__)
594 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
595 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
596#else
597 UNIMPLEMENTED(FATAL);
598#endif
599 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
600 reinterpret_cast<const uint8_t*>(native_method), false);
601 }
TDYa12726467572012-04-17 20:51:22 -0700602#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700603}
604
Ian Rogers19846512012-02-24 11:42:47 -0800605void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700606 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700607 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800608 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700609}
610
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700611void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700612 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
613 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700614 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800615 if (new_status == kStatusError) {
616 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
617
618 // stash current exception
619 Thread* self = Thread::Current();
620 SirtRef<Throwable> exception(self->GetException());
621 CHECK(exception.get() != NULL);
622
623 // clear exception to call FindSystemClass
624 self->ClearException();
625 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
626 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
627 CHECK(!self->IsExceptionPending());
628
629 // only verification errors, not initialization problems, should set a verify error.
630 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
631 Class* exception_class = exception->GetClass();
632 if (!eiie_class->IsAssignableFrom(exception_class)) {
633 SetVerifyErrorClass(exception_class);
634 }
635
636 // restore exception
637 self->SetException(exception.get());
638 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700639 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700640}
641
642DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700643 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700644}
645
646void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700647 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700648}
649
Brian Carlstrom1f870082011-08-23 16:02:11 -0700650Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700651 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700652 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500653 // TODO: decide whether we want this check. It currently fails during bootstrap.
654 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700655 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800656 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700657}
658
Ian Rogers0571d352011-11-03 19:51:38 -0700659void Class::SetClassSize(size_t new_class_size) {
660 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
661 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
662}
663
Ian Rogersd418eda2012-01-30 12:14:28 -0800664// Return the class' name. The exact format is bizarre, but it's the specified behavior for
665// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
666// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
667// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
668String* Class::ComputeName() {
669 String* name = GetName();
670 if (name != NULL) {
671 return name;
672 }
673 std::string descriptor(ClassHelper(this).GetDescriptor());
674 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
675 // The descriptor indicates that this is the class for
676 // a primitive type; special-case the return value.
677 const char* c_name = NULL;
678 switch (descriptor[0]) {
679 case 'Z': c_name = "boolean"; break;
680 case 'B': c_name = "byte"; break;
681 case 'C': c_name = "char"; break;
682 case 'S': c_name = "short"; break;
683 case 'I': c_name = "int"; break;
684 case 'J': c_name = "long"; break;
685 case 'F': c_name = "float"; break;
686 case 'D': c_name = "double"; break;
687 case 'V': c_name = "void"; break;
688 default:
689 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
690 }
691 name = String::AllocFromModifiedUtf8(c_name);
692 } else {
693 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
694 // components.
695 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
696 descriptor.erase(0, 1);
697 descriptor.erase(descriptor.size() - 1);
698 }
699 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
700 name = String::AllocFromModifiedUtf8(descriptor.c_str());
701 }
702 SetName(name);
703 return name;
704}
705
Elliott Hughes4681c802011-09-25 18:04:37 -0700706void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700707 if ((flags & kDumpClassFullDetail) == 0) {
708 os << PrettyClass(this);
709 if ((flags & kDumpClassClassLoader) != 0) {
710 os << ' ' << GetClassLoader();
711 }
712 if ((flags & kDumpClassInitialized) != 0) {
713 os << ' ' << GetStatus();
714 }
Elliott Hughese0918552011-10-28 17:18:29 -0700715 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700716 return;
717 }
718
719 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800720 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700721 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800722 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700723 os << " objectSize=" << SizeOf() << " "
724 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
725 os << StringPrintf(" access=0x%04x.%04x\n",
726 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
727 if (super != NULL) {
728 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
729 }
730 if (IsArrayClass()) {
731 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
732 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700733 if (kh.NumDirectInterfaces() > 0) {
734 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
735 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
736 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700737 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800738 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700739 }
740 }
741 os << " vtable (" << NumVirtualMethods() << " entries, "
742 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
743 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800744 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700745 }
746 os << " direct methods (" << NumDirectMethods() << " entries):\n";
747 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800748 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700749 }
750 if (NumStaticFields() > 0) {
751 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700752 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700753 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800754 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700755 }
756 } else {
757 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700758 }
759 }
760 if (NumInstanceFields() > 0) {
761 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700762 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700763 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800764 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700765 }
766 } else {
767 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700768 }
769 }
770}
771
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700772void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
773 if (new_reference_offsets != CLASS_WALK_SUPER) {
774 // Sanity check that the number of bits set in the reference offset bitmap
775 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800776 size_t count = 0;
777 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
778 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700779 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800780 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700781 }
782 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
783 new_reference_offsets, false);
784}
785
786void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
787 if (new_reference_offsets != CLASS_WALK_SUPER) {
788 // Sanity check that the number of bits set in the reference offset bitmap
789 // agrees with the number of references
790 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
791 NumReferenceStaticFieldsDuringLinking());
792 }
793 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
794 new_reference_offsets, false);
795}
796
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700797bool Class::Implements(const Class* klass) const {
798 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700799 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700800 // All interfaces implemented directly and by our superclass, and
801 // recursively all super-interfaces of those interfaces, are listed
802 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700803 int32_t iftable_count = GetIfTableCount();
804 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
805 for (int32_t i = 0; i < iftable_count; i++) {
806 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700807 return true;
808 }
809 }
810 return false;
811}
812
Elliott Hughese84278b2012-03-22 10:06:53 -0700813// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700814// are array classes.
815//
816// Consider an array class, e.g. Y[][], where Y is a subclass of X.
817// Y[][] = Y[][] --> true (identity)
818// X[][] = Y[][] --> true (element superclass)
819// Y = Y[][] --> false
820// Y[] = Y[][] --> false
821// Object = Y[][] --> true (everything is an object)
822// Object[] = Y[][] --> true
823// Object[][] = Y[][] --> true
824// Object[][][] = Y[][] --> false (too many []s)
825// Serializable = Y[][] --> true (all arrays are Serializable)
826// Serializable[] = Y[][] --> true
827// Serializable[][] = Y[][] --> false (unless Y is Serializable)
828//
829// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700830// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700831//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700832bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700833 DCHECK(IsArrayClass()) << PrettyClass(this);
834 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700835 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700836}
837
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700838bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700839 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
840 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700841 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700842 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700843 // src's super should be java_lang_Object, since it is an array.
844 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700845 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
846 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700847 return this == java_lang_Object;
848 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700849 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700850}
851
852bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700853 DCHECK(!IsInterface()) << PrettyClass(this);
854 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700855 const Class* current = this;
856 do {
857 if (current == klass) {
858 return true;
859 }
860 current = current->GetSuperClass();
861 } while (current != NULL);
862 return false;
863}
864
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800865bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700866 size_t i = 0;
867 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
868 ++i;
869 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700870 if (descriptor1.find('/', i) != StringPiece::npos ||
871 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700872 return false;
873 } else {
874 return true;
875 }
876}
877
878bool Class::IsInSamePackage(const Class* that) const {
879 const Class* klass1 = this;
880 const Class* klass2 = that;
881 if (klass1 == klass2) {
882 return true;
883 }
884 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700885 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700886 return false;
887 }
888 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700889 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700890 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700891 }
jeffhao4a801a42011-09-23 13:53:40 -0700892 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700893 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700894 }
895 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800896 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800897 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800898 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800899 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700901}
902
Elliott Hughesdbb40792011-11-18 17:05:22 -0800903bool Class::IsClassClass() const {
904 Class* java_lang_Class = GetClass()->GetClass();
905 return this == java_lang_Class;
906}
907
908bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800910}
911
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800912bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700913 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800914}
915
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800916ClassLoader* Class::GetClassLoader() const {
917 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700918}
919
Ian Rogers365c1022012-06-22 15:05:28 -0700920void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700922}
923
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800924Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700925 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700926 DCHECK(declaring_class != NULL) << PrettyClass(this);
927 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700928 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700929 int32_t iftable_count = GetIfTableCount();
930 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
931 for (int32_t i = 0; i < iftable_count; i++) {
932 InterfaceEntry* interface_entry = iftable->Get(i);
933 if (interface_entry->GetInterface() == declaring_class) {
934 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700935 }
936 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700937 return NULL;
938}
939
Ian Rogers466bb252011-10-14 03:29:56 -0700940Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700941 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800942 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700943 if (method != NULL) {
944 return method;
945 }
946
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700947 int32_t iftable_count = GetIfTableCount();
948 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
949 for (int32_t i = 0; i < iftable_count; i++) {
950 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700951 if (method != NULL) {
952 return method;
953 }
954 }
955 return NULL;
956}
957
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800958Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
959 // Check the current class before checking the interfaces.
960 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
961 if (method != NULL) {
962 return method;
963 }
964
965 int32_t iftable_count = GetIfTableCount();
966 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
967 for (int32_t i = 0; i < iftable_count; i++) {
968 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
969 if (method != NULL) {
970 return method;
971 }
972 }
973 return NULL;
974}
975
976
977Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800978 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700979 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700980 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800981 mh.ChangeMethod(method);
982 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700983 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700984 }
985 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700986 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700987}
988
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800989Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
990 if (GetDexCache() == dex_cache) {
991 for (size_t i = 0; i < NumDirectMethods(); ++i) {
992 Method* method = GetDirectMethod(i);
993 if (method->GetDexMethodIndex() == dex_method_idx) {
994 return method;
995 }
996 }
997 }
998 return NULL;
999}
1000
1001Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1002 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001003 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001004 if (method != NULL) {
1005 return method;
1006 }
1007 }
1008 return NULL;
1009}
1010
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001011Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1012 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1013 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1014 if (method != NULL) {
1015 return method;
1016 }
1017 }
1018 return NULL;
1019}
1020
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001021Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001022 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001023 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001024 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001025 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001026 mh.ChangeMethod(method);
1027 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001028 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001029 }
1030 }
1031 return NULL;
1032}
1033
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001034Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1035 if (GetDexCache() == dex_cache) {
1036 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1037 Method* method = GetVirtualMethod(i);
1038 if (method->GetDexMethodIndex() == dex_method_idx) {
1039 return method;
1040 }
1041 }
1042 }
1043 return NULL;
1044}
1045
Ian Rogers466bb252011-10-14 03:29:56 -07001046Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1047 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1048 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1049 if (method != NULL) {
1050 return method;
1051 }
1052 }
1053 return NULL;
1054}
1055
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001056Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1057 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1058 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1059 if (method != NULL) {
1060 return method;
1061 }
1062 }
1063 return NULL;
1064}
1065
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001066Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 // Is the field in this class?
1068 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001069 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1071 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001072 fh.ChangeField(f);
1073 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001074 return f;
1075 }
1076 }
1077 return NULL;
1078}
1079
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001080Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1081 if (GetDexCache() == dex_cache) {
1082 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1083 Field* f = GetInstanceField(i);
1084 if (f->GetDexFieldIndex() == dex_field_idx) {
1085 return f;
1086 }
1087 }
1088 }
1089 return NULL;
1090}
1091
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001092Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 // Is the field in this class, or any of its superclasses?
1094 // Interfaces are not relevant because they can't contain instance fields.
1095 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001096 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 if (f != NULL) {
1098 return f;
1099 }
1100 }
1101 return NULL;
1102}
1103
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001104Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1105 // Is the field in this class, or any of its superclasses?
1106 // Interfaces are not relevant because they can't contain instance fields.
1107 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1108 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1109 if (f != NULL) {
1110 return f;
1111 }
1112 }
1113 return NULL;
1114}
1115
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001116Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1117 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001118 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001119 for (size_t i = 0; i < NumStaticFields(); ++i) {
1120 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001121 fh.ChangeField(f);
1122 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001123 return f;
1124 }
1125 }
1126 return NULL;
1127}
1128
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001129Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1130 if (dex_cache == GetDexCache()) {
1131 for (size_t i = 0; i < NumStaticFields(); ++i) {
1132 Field* f = GetStaticField(i);
1133 if (f->GetDexFieldIndex() == dex_field_idx) {
1134 return f;
1135 }
1136 }
1137 }
1138 return NULL;
1139}
1140
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001141Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1142 // Is the field in this class (or its interfaces), or any of its
1143 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001144 ClassHelper kh;
1145 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001146 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001147 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001148 if (f != NULL) {
1149 return f;
1150 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001151 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001152 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001153 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1154 Class* interface = kh.GetDirectInterface(i);
1155 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001156 if (f != NULL) {
1157 return f;
1158 }
1159 }
1160 }
1161 return NULL;
1162}
1163
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001164Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1165 ClassHelper kh;
1166 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1167 // Is the field in this class?
1168 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1169 if (f != NULL) {
1170 return f;
1171 }
1172 // Is this field in any of this class' interfaces?
1173 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001174 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1175 Class* interface = kh.GetDirectInterface(i);
1176 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001177 if (f != NULL) {
1178 return f;
1179 }
1180 }
1181 }
1182 return NULL;
1183}
1184
Ian Rogersb067ac22011-12-13 18:05:09 -08001185Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1186 // Find a field using the JLS field resolution order
1187 ClassHelper kh;
1188 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1189 // Is the field in this class?
1190 Field* f = k->FindDeclaredInstanceField(name, type);
1191 if (f != NULL) {
1192 return f;
1193 }
1194 f = k->FindDeclaredStaticField(name, type);
1195 if (f != NULL) {
1196 return f;
1197 }
1198 // Is this field in any of this class' interfaces?
1199 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001200 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1201 Class* interface = kh.GetDirectInterface(i);
1202 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001203 if (f != NULL) {
1204 return f;
1205 }
1206 }
1207 }
1208 return NULL;
1209}
1210
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001211Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001212 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001213 DCHECK_GE(component_count, 0);
1214 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001215
Ian Rogersa15e67d2012-02-28 13:51:55 -08001216 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001217 size_t data_size = component_count * component_size;
1218 size_t size = header_size + data_size;
1219
1220 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1221 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1222 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1223 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001224 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001225 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001226 return NULL;
1227 }
1228
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001229 Heap* heap = Runtime::Current()->GetHeap();
1230 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001231 if (array != NULL) {
1232 DCHECK(array->IsArrayInstance());
1233 array->SetLength(component_count);
1234 }
1235 return array;
1236}
1237
1238Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001240 return Alloc(array_class, component_count, array_class->GetComponentSize());
1241}
1242
Elliott Hughes80609252011-09-23 17:24:51 -07001243bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001244 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001245 "length=%i; index=%i", length_, index);
1246 return false;
1247}
1248
1249bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001250 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001251 "Can't store an element of type %s into an array of type %s",
1252 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1253 return false;
1254}
1255
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001256template<typename T>
1257PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001258 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001259 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1260 return down_cast<PrimitiveArray<T>*>(raw_array);
1261}
1262
1263template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1264
1265// Explicitly instantiate all the primitive array types.
1266template class PrimitiveArray<uint8_t>; // BooleanArray
1267template class PrimitiveArray<int8_t>; // ByteArray
1268template class PrimitiveArray<uint16_t>; // CharArray
1269template class PrimitiveArray<double>; // DoubleArray
1270template class PrimitiveArray<float>; // FloatArray
1271template class PrimitiveArray<int32_t>; // IntArray
1272template class PrimitiveArray<int64_t>; // LongArray
1273template class PrimitiveArray<int16_t>; // ShortArray
1274
Ian Rogers466bb252011-10-14 03:29:56 -07001275// Explicitly instantiate Class[][]
1276template class ObjectArray<ObjectArray<Class> >;
1277
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001278// TODO: get global references for these
1279Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001280
Brian Carlstroma663ea52011-08-19 23:33:41 -07001281void String::SetClass(Class* java_lang_String) {
1282 CHECK(java_lang_String_ == NULL);
1283 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001284 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001285}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001286
Brian Carlstroma663ea52011-08-19 23:33:41 -07001287void String::ResetClass() {
1288 CHECK(java_lang_String_ != NULL);
1289 java_lang_String_ = NULL;
1290}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001291
Brian Carlstromc74255f2011-09-11 22:47:39 -07001292String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001293 return Runtime::Current()->GetInternTable()->InternWeak(this);
1294}
1295
Brian Carlstrom395520e2011-09-25 19:35:00 -07001296int32_t String::GetHashCode() {
1297 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1298 if (result == 0) {
1299 ComputeHashCode();
1300 }
1301 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1302 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1303 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001304 return result;
1305}
1306
1307int32_t String::GetLength() const {
1308 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1309 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1310 return result;
1311}
1312
1313uint16_t String::CharAt(int32_t index) const {
1314 // TODO: do we need this? Equals is the only caller, and could
1315 // bounds check itself.
1316 if (index < 0 || index >= count_) {
1317 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001318 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001319 "length=%i; index=%i", count_, index);
1320 return 0;
1321 }
1322 return GetCharArray()->Get(index + GetOffset());
1323}
1324
1325String* String::AllocFromUtf16(int32_t utf16_length,
1326 const uint16_t* utf16_data_in,
1327 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001328 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001329 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001330 if (string == NULL) {
1331 return NULL;
1332 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001333 // TODO: use 16-bit wide memset variant
1334 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001335 if (array == NULL) {
1336 return NULL;
1337 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001338 for (int i = 0; i < utf16_length; i++) {
1339 array->Set(i, utf16_data_in[i]);
1340 }
1341 if (hash_code != 0) {
1342 string->SetHashCode(hash_code);
1343 } else {
1344 string->ComputeHashCode();
1345 }
1346 return string;
1347}
1348
1349String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001350 if (utf == NULL) {
1351 return NULL;
1352 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001353 size_t char_count = CountModifiedUtf8Chars(utf);
1354 return AllocFromModifiedUtf8(char_count, utf);
1355}
1356
1357String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1358 const char* utf8_data_in) {
1359 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001360 if (string == NULL) {
1361 return NULL;
1362 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363 uint16_t* utf16_data_out =
1364 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1365 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1366 string->ComputeHashCode();
1367 return string;
1368}
1369
1370String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001371 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1372 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001373 return NULL;
1374 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001375 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376}
1377
1378String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001379 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001380 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001381 if (string == NULL) {
1382 return NULL;
1383 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001384 string->SetArray(array);
1385 string->SetCount(array->GetLength());
1386 return string;
1387}
1388
1389bool String::Equals(const String* that) const {
1390 if (this == that) {
1391 // Quick reference equality test
1392 return true;
1393 } else if (that == NULL) {
1394 // Null isn't an instanceof anything
1395 return false;
1396 } else if (this->GetLength() != that->GetLength()) {
1397 // Quick length inequality test
1398 return false;
1399 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001400 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001401 // hash code was already equal
1402 for (int32_t i = 0; i < that->GetLength(); ++i) {
1403 if (this->CharAt(i) != that->CharAt(i)) {
1404 return false;
1405 }
1406 }
1407 return true;
1408 }
1409}
1410
Elliott Hughes5d78d392011-12-13 16:53:05 -08001411bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001412 if (this->GetLength() != that_length) {
1413 return false;
1414 } else {
1415 for (int32_t i = 0; i < that_length; ++i) {
1416 if (this->CharAt(i) != that_chars[that_offset + i]) {
1417 return false;
1418 }
1419 }
1420 return true;
1421 }
1422}
1423
1424bool String::Equals(const char* modified_utf8) const {
1425 for (int32_t i = 0; i < GetLength(); ++i) {
1426 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1427 if (ch == '\0' || ch != CharAt(i)) {
1428 return false;
1429 }
1430 }
1431 return *modified_utf8 == '\0';
1432}
1433
1434bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001435 if (modified_utf8.size() != GetLength()) {
1436 return false;
1437 }
1438 const char* p = modified_utf8.data();
1439 for (int32_t i = 0; i < GetLength(); ++i) {
1440 uint16_t ch = GetUtf16FromUtf8(&p);
1441 if (ch != CharAt(i)) {
1442 return false;
1443 }
1444 }
1445 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001446}
1447
1448// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1449std::string String::ToModifiedUtf8() const {
1450 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001451 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001452 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001453 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1454 return result;
1455}
1456
Ian Rogers1c5eb702012-02-01 09:18:34 -08001457void Throwable::SetCause(Throwable* cause) {
1458 CHECK(cause != NULL);
1459 CHECK(cause != this);
1460 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1461 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1462}
1463
Ian Rogers466bb252011-10-14 03:29:56 -07001464bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001465 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001466 return false;
1467 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001468 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001469}
1470
Ian Rogers9074b992011-10-26 17:41:55 -07001471std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001472 std::string result(PrettyTypeOf(this));
1473 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001474 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001475 if (msg != NULL) {
1476 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001477 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001478 result += "\n";
1479 Object* stack_state = GetStackState();
1480 // check stack state isn't missing or corrupt
1481 if (stack_state != NULL && stack_state->IsObjectArray()) {
1482 // Decode the internal stack trace into the depth and method trace
1483 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1484 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001485 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1486 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001487 for (int32_t i = 0; i < depth; ++i) {
1488 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001489 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001490 uint32_t dex_pc = pc_trace->Get(i);
1491 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001492 const char* source_file = mh.GetDeclaringClassSourceFile();
1493 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1494 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001495 }
Ian Rogers9074b992011-10-26 17:41:55 -07001496 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001497 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001498 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001499 result += "Caused by: ";
1500 result += cause->Dump();
1501 }
Ian Rogers9074b992011-10-26 17:41:55 -07001502 return result;
1503}
1504
Ian Rogers5167c972012-02-03 10:41:20 -08001505
1506Class* Throwable::java_lang_Throwable_ = NULL;
1507
1508void Throwable::SetClass(Class* java_lang_Throwable) {
1509 CHECK(java_lang_Throwable_ == NULL);
1510 CHECK(java_lang_Throwable != NULL);
1511 java_lang_Throwable_ = java_lang_Throwable;
1512}
1513
1514void Throwable::ResetClass() {
1515 CHECK(java_lang_Throwable_ != NULL);
1516 java_lang_Throwable_ = NULL;
1517}
1518
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001519Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1520
1521void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1522 CHECK(java_lang_StackTraceElement_ == NULL);
1523 CHECK(java_lang_StackTraceElement != NULL);
1524 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1525}
1526
1527void StackTraceElement::ResetClass() {
1528 CHECK(java_lang_StackTraceElement_ != NULL);
1529 java_lang_StackTraceElement_ = NULL;
1530}
1531
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001532StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1533 String* method_name,
1534 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001535 int32_t line_number) {
1536 StackTraceElement* trace =
1537 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1538 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1539 const_cast<String*>(declaring_class), false);
1540 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1541 const_cast<String*>(method_name), false);
1542 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1543 const_cast<String*>(file_name), false);
1544 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1545 line_number, false);
1546 return trace;
1547}
1548
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001549} // namespace art