blob: dcf4ce989afab7a1d2980e11682a29fd3a6bfb0f [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)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700442 SHARED_LOCKS_REQUIRED(Locks::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();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700532 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533 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);
Ian Rogersc8982582012-09-07 16:53:25 -0700615 if (new_status > kStatusResolved) {
616 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
617 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800618 if (new_status == kStatusError) {
619 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
620
621 // stash current exception
622 Thread* self = Thread::Current();
623 SirtRef<Throwable> exception(self->GetException());
624 CHECK(exception.get() != NULL);
625
626 // clear exception to call FindSystemClass
627 self->ClearException();
628 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
629 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
630 CHECK(!self->IsExceptionPending());
631
632 // only verification errors, not initialization problems, should set a verify error.
633 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
634 Class* exception_class = exception->GetClass();
635 if (!eiie_class->IsAssignableFrom(exception_class)) {
636 SetVerifyErrorClass(exception_class);
637 }
638
639 // restore exception
640 self->SetException(exception.get());
641 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700642 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700643}
644
645DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700646 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647}
648
649void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700650 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700651}
652
Brian Carlstrom1f870082011-08-23 16:02:11 -0700653Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700654 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700655 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500656 // TODO: decide whether we want this check. It currently fails during bootstrap.
657 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700658 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800659 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700660}
661
Ian Rogers0571d352011-11-03 19:51:38 -0700662void Class::SetClassSize(size_t new_class_size) {
663 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
664 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
665}
666
Ian Rogersd418eda2012-01-30 12:14:28 -0800667// Return the class' name. The exact format is bizarre, but it's the specified behavior for
668// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
669// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
670// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
671String* Class::ComputeName() {
672 String* name = GetName();
673 if (name != NULL) {
674 return name;
675 }
676 std::string descriptor(ClassHelper(this).GetDescriptor());
677 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
678 // The descriptor indicates that this is the class for
679 // a primitive type; special-case the return value.
680 const char* c_name = NULL;
681 switch (descriptor[0]) {
682 case 'Z': c_name = "boolean"; break;
683 case 'B': c_name = "byte"; break;
684 case 'C': c_name = "char"; break;
685 case 'S': c_name = "short"; break;
686 case 'I': c_name = "int"; break;
687 case 'J': c_name = "long"; break;
688 case 'F': c_name = "float"; break;
689 case 'D': c_name = "double"; break;
690 case 'V': c_name = "void"; break;
691 default:
692 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
693 }
694 name = String::AllocFromModifiedUtf8(c_name);
695 } else {
696 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
697 // components.
698 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
699 descriptor.erase(0, 1);
700 descriptor.erase(descriptor.size() - 1);
701 }
702 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
703 name = String::AllocFromModifiedUtf8(descriptor.c_str());
704 }
705 SetName(name);
706 return name;
707}
708
Elliott Hughes4681c802011-09-25 18:04:37 -0700709void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700710 if ((flags & kDumpClassFullDetail) == 0) {
711 os << PrettyClass(this);
712 if ((flags & kDumpClassClassLoader) != 0) {
713 os << ' ' << GetClassLoader();
714 }
715 if ((flags & kDumpClassInitialized) != 0) {
716 os << ' ' << GetStatus();
717 }
Elliott Hughese0918552011-10-28 17:18:29 -0700718 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700719 return;
720 }
721
722 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800723 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700724 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800725 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700726 os << " objectSize=" << SizeOf() << " "
727 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
728 os << StringPrintf(" access=0x%04x.%04x\n",
729 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
730 if (super != NULL) {
731 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
732 }
733 if (IsArrayClass()) {
734 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
735 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700736 if (kh.NumDirectInterfaces() > 0) {
737 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
738 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
739 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700740 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800741 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700742 }
743 }
744 os << " vtable (" << NumVirtualMethods() << " entries, "
745 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
746 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800747 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700748 }
749 os << " direct methods (" << NumDirectMethods() << " entries):\n";
750 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800751 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700752 }
753 if (NumStaticFields() > 0) {
754 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700755 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700756 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800757 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700758 }
759 } else {
760 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700761 }
762 }
763 if (NumInstanceFields() > 0) {
764 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700765 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700766 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800767 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700768 }
769 } else {
770 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700771 }
772 }
773}
774
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700775void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
776 if (new_reference_offsets != CLASS_WALK_SUPER) {
777 // Sanity check that the number of bits set in the reference offset bitmap
778 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800779 size_t count = 0;
780 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
781 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700782 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800783 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700784 }
785 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
786 new_reference_offsets, false);
787}
788
789void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
790 if (new_reference_offsets != CLASS_WALK_SUPER) {
791 // Sanity check that the number of bits set in the reference offset bitmap
792 // agrees with the number of references
793 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
794 NumReferenceStaticFieldsDuringLinking());
795 }
796 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
797 new_reference_offsets, false);
798}
799
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700800bool Class::Implements(const Class* klass) const {
801 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700802 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700803 // All interfaces implemented directly and by our superclass, and
804 // recursively all super-interfaces of those interfaces, are listed
805 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700806 int32_t iftable_count = GetIfTableCount();
807 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
808 for (int32_t i = 0; i < iftable_count; i++) {
809 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700810 return true;
811 }
812 }
813 return false;
814}
815
Elliott Hughese84278b2012-03-22 10:06:53 -0700816// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700817// are array classes.
818//
819// Consider an array class, e.g. Y[][], where Y is a subclass of X.
820// Y[][] = Y[][] --> true (identity)
821// X[][] = Y[][] --> true (element superclass)
822// Y = Y[][] --> false
823// Y[] = Y[][] --> false
824// Object = Y[][] --> true (everything is an object)
825// Object[] = Y[][] --> true
826// Object[][] = Y[][] --> true
827// Object[][][] = Y[][] --> false (too many []s)
828// Serializable = Y[][] --> true (all arrays are Serializable)
829// Serializable[] = Y[][] --> true
830// Serializable[][] = Y[][] --> false (unless Y is Serializable)
831//
832// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700833// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700834//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700835bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700836 DCHECK(IsArrayClass()) << PrettyClass(this);
837 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700838 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700839}
840
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700841bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700842 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
843 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700844 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700845 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700846 // src's super should be java_lang_Object, since it is an array.
847 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700848 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
849 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700850 return this == java_lang_Object;
851 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700852 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700853}
854
855bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700856 DCHECK(!IsInterface()) << PrettyClass(this);
857 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700858 const Class* current = this;
859 do {
860 if (current == klass) {
861 return true;
862 }
863 current = current->GetSuperClass();
864 } while (current != NULL);
865 return false;
866}
867
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800868bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700869 size_t i = 0;
870 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
871 ++i;
872 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700873 if (descriptor1.find('/', i) != StringPiece::npos ||
874 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700875 return false;
876 } else {
877 return true;
878 }
879}
880
881bool Class::IsInSamePackage(const Class* that) const {
882 const Class* klass1 = this;
883 const Class* klass2 = that;
884 if (klass1 == klass2) {
885 return true;
886 }
887 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700888 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700889 return false;
890 }
891 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700892 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700893 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700894 }
jeffhao4a801a42011-09-23 13:53:40 -0700895 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700896 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700897 }
898 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800899 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800900 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800901 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800902 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800903 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700904}
905
Elliott Hughesdbb40792011-11-18 17:05:22 -0800906bool Class::IsClassClass() const {
907 Class* java_lang_Class = GetClass()->GetClass();
908 return this == java_lang_Class;
909}
910
911bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800912 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800913}
914
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800915bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700916 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800917}
918
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800919ClassLoader* Class::GetClassLoader() const {
920 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700921}
922
Ian Rogers365c1022012-06-22 15:05:28 -0700923void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700925}
926
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800927Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700928 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700929 DCHECK(declaring_class != NULL) << PrettyClass(this);
930 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700931 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700932 int32_t iftable_count = GetIfTableCount();
933 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
934 for (int32_t i = 0; i < iftable_count; i++) {
935 InterfaceEntry* interface_entry = iftable->Get(i);
936 if (interface_entry->GetInterface() == declaring_class) {
937 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700938 }
939 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700940 return NULL;
941}
942
Ian Rogers466bb252011-10-14 03:29:56 -0700943Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700944 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800945 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700946 if (method != NULL) {
947 return method;
948 }
949
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700950 int32_t iftable_count = GetIfTableCount();
951 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
952 for (int32_t i = 0; i < iftable_count; i++) {
953 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700954 if (method != NULL) {
955 return method;
956 }
957 }
958 return NULL;
959}
960
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800961Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
962 // Check the current class before checking the interfaces.
963 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
964 if (method != NULL) {
965 return method;
966 }
967
968 int32_t iftable_count = GetIfTableCount();
969 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
970 for (int32_t i = 0; i < iftable_count; i++) {
971 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
972 if (method != NULL) {
973 return method;
974 }
975 }
976 return NULL;
977}
978
979
980Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800981 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700982 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700983 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800984 mh.ChangeMethod(method);
985 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700986 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700987 }
988 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700989 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700990}
991
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800992Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
993 if (GetDexCache() == dex_cache) {
994 for (size_t i = 0; i < NumDirectMethods(); ++i) {
995 Method* method = GetDirectMethod(i);
996 if (method->GetDexMethodIndex() == dex_method_idx) {
997 return method;
998 }
999 }
1000 }
1001 return NULL;
1002}
1003
1004Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1005 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001006 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001007 if (method != NULL) {
1008 return method;
1009 }
1010 }
1011 return NULL;
1012}
1013
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001014Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1015 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1016 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1017 if (method != NULL) {
1018 return method;
1019 }
1020 }
1021 return NULL;
1022}
1023
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001024Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001025 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001026 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001027 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001028 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001029 mh.ChangeMethod(method);
1030 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001031 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001032 }
1033 }
1034 return NULL;
1035}
1036
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001037Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1038 if (GetDexCache() == dex_cache) {
1039 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1040 Method* method = GetVirtualMethod(i);
1041 if (method->GetDexMethodIndex() == dex_method_idx) {
1042 return method;
1043 }
1044 }
1045 }
1046 return NULL;
1047}
1048
Ian Rogers466bb252011-10-14 03:29:56 -07001049Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1050 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1051 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1052 if (method != NULL) {
1053 return method;
1054 }
1055 }
1056 return NULL;
1057}
1058
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001059Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1060 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1061 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1062 if (method != NULL) {
1063 return method;
1064 }
1065 }
1066 return NULL;
1067}
1068
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001069Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 // Is the field in this class?
1071 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001072 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1074 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001075 fh.ChangeField(f);
1076 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001077 return f;
1078 }
1079 }
1080 return NULL;
1081}
1082
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001083Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1084 if (GetDexCache() == dex_cache) {
1085 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1086 Field* f = GetInstanceField(i);
1087 if (f->GetDexFieldIndex() == dex_field_idx) {
1088 return f;
1089 }
1090 }
1091 }
1092 return NULL;
1093}
1094
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001095Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 // Is the field in this class, or any of its superclasses?
1097 // Interfaces are not relevant because they can't contain instance fields.
1098 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001099 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 if (f != NULL) {
1101 return f;
1102 }
1103 }
1104 return NULL;
1105}
1106
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001107Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1108 // Is the field in this class, or any of its superclasses?
1109 // Interfaces are not relevant because they can't contain instance fields.
1110 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1111 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1112 if (f != NULL) {
1113 return f;
1114 }
1115 }
1116 return NULL;
1117}
1118
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001119Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1120 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001121 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001122 for (size_t i = 0; i < NumStaticFields(); ++i) {
1123 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001124 fh.ChangeField(f);
1125 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001126 return f;
1127 }
1128 }
1129 return NULL;
1130}
1131
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001132Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1133 if (dex_cache == GetDexCache()) {
1134 for (size_t i = 0; i < NumStaticFields(); ++i) {
1135 Field* f = GetStaticField(i);
1136 if (f->GetDexFieldIndex() == dex_field_idx) {
1137 return f;
1138 }
1139 }
1140 }
1141 return NULL;
1142}
1143
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001144Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1145 // Is the field in this class (or its interfaces), or any of its
1146 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001147 ClassHelper kh;
1148 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001149 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001150 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001151 if (f != NULL) {
1152 return f;
1153 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001154 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001155 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001156 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1157 Class* interface = kh.GetDirectInterface(i);
1158 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001159 if (f != NULL) {
1160 return f;
1161 }
1162 }
1163 }
1164 return NULL;
1165}
1166
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001167Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1168 ClassHelper kh;
1169 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1170 // Is the field in this class?
1171 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1172 if (f != NULL) {
1173 return f;
1174 }
1175 // Is this field in any of this class' interfaces?
1176 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001177 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1178 Class* interface = kh.GetDirectInterface(i);
1179 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001180 if (f != NULL) {
1181 return f;
1182 }
1183 }
1184 }
1185 return NULL;
1186}
1187
Ian Rogersb067ac22011-12-13 18:05:09 -08001188Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1189 // Find a field using the JLS field resolution order
1190 ClassHelper kh;
1191 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1192 // Is the field in this class?
1193 Field* f = k->FindDeclaredInstanceField(name, type);
1194 if (f != NULL) {
1195 return f;
1196 }
1197 f = k->FindDeclaredStaticField(name, type);
1198 if (f != NULL) {
1199 return f;
1200 }
1201 // Is this field in any of this class' interfaces?
1202 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001203 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1204 Class* interface = kh.GetDirectInterface(i);
1205 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001206 if (f != NULL) {
1207 return f;
1208 }
1209 }
1210 }
1211 return NULL;
1212}
1213
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001214Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001215 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001216 DCHECK_GE(component_count, 0);
1217 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001218
Ian Rogersa15e67d2012-02-28 13:51:55 -08001219 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001220 size_t data_size = component_count * component_size;
1221 size_t size = header_size + data_size;
1222
1223 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1224 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1225 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1226 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001227 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001228 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001229 return NULL;
1230 }
1231
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001232 Heap* heap = Runtime::Current()->GetHeap();
1233 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001234 if (array != NULL) {
1235 DCHECK(array->IsArrayInstance());
1236 array->SetLength(component_count);
1237 }
1238 return array;
1239}
1240
1241Array* Array::Alloc(Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001242 DCHECK(array_class->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001243 return Alloc(array_class, component_count, array_class->GetComponentSize());
1244}
1245
Elliott Hughes80609252011-09-23 17:24:51 -07001246bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001247 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001248 "length=%i; index=%i", length_, index);
1249 return false;
1250}
1251
1252bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001253 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001254 "Can't store an element of type %s into an array of type %s",
1255 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1256 return false;
1257}
1258
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001259template<typename T>
1260PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001261 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001262 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1263 return down_cast<PrimitiveArray<T>*>(raw_array);
1264}
1265
1266template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1267
1268// Explicitly instantiate all the primitive array types.
1269template class PrimitiveArray<uint8_t>; // BooleanArray
1270template class PrimitiveArray<int8_t>; // ByteArray
1271template class PrimitiveArray<uint16_t>; // CharArray
1272template class PrimitiveArray<double>; // DoubleArray
1273template class PrimitiveArray<float>; // FloatArray
1274template class PrimitiveArray<int32_t>; // IntArray
1275template class PrimitiveArray<int64_t>; // LongArray
1276template class PrimitiveArray<int16_t>; // ShortArray
1277
Ian Rogers466bb252011-10-14 03:29:56 -07001278// Explicitly instantiate Class[][]
1279template class ObjectArray<ObjectArray<Class> >;
1280
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001281// TODO: get global references for these
1282Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001283
Brian Carlstroma663ea52011-08-19 23:33:41 -07001284void String::SetClass(Class* java_lang_String) {
1285 CHECK(java_lang_String_ == NULL);
1286 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001287 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001288}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001289
Brian Carlstroma663ea52011-08-19 23:33:41 -07001290void String::ResetClass() {
1291 CHECK(java_lang_String_ != NULL);
1292 java_lang_String_ = NULL;
1293}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001294
Brian Carlstromc74255f2011-09-11 22:47:39 -07001295String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001296 return Runtime::Current()->GetInternTable()->InternWeak(this);
1297}
1298
Brian Carlstrom395520e2011-09-25 19:35:00 -07001299int32_t String::GetHashCode() {
1300 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1301 if (result == 0) {
1302 ComputeHashCode();
1303 }
1304 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1305 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1306 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001307 return result;
1308}
1309
1310int32_t String::GetLength() const {
1311 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1312 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1313 return result;
1314}
1315
1316uint16_t String::CharAt(int32_t index) const {
1317 // TODO: do we need this? Equals is the only caller, and could
1318 // bounds check itself.
1319 if (index < 0 || index >= count_) {
1320 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001321 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001322 "length=%i; index=%i", count_, index);
1323 return 0;
1324 }
1325 return GetCharArray()->Get(index + GetOffset());
1326}
1327
1328String* String::AllocFromUtf16(int32_t utf16_length,
1329 const uint16_t* utf16_data_in,
1330 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001331 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001332 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001333 if (string == NULL) {
1334 return NULL;
1335 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001336 // TODO: use 16-bit wide memset variant
1337 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001338 if (array == NULL) {
1339 return NULL;
1340 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001341 for (int i = 0; i < utf16_length; i++) {
1342 array->Set(i, utf16_data_in[i]);
1343 }
1344 if (hash_code != 0) {
1345 string->SetHashCode(hash_code);
1346 } else {
1347 string->ComputeHashCode();
1348 }
1349 return string;
1350}
1351
1352String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001353 if (utf == NULL) {
1354 return NULL;
1355 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001356 size_t char_count = CountModifiedUtf8Chars(utf);
1357 return AllocFromModifiedUtf8(char_count, utf);
1358}
1359
1360String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1361 const char* utf8_data_in) {
1362 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001363 if (string == NULL) {
1364 return NULL;
1365 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001366 uint16_t* utf16_data_out =
1367 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1368 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1369 string->ComputeHashCode();
1370 return string;
1371}
1372
1373String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001374 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1375 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001376 return NULL;
1377 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001378 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001379}
1380
1381String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001382 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001383 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001384 if (string == NULL) {
1385 return NULL;
1386 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001387 string->SetArray(array);
1388 string->SetCount(array->GetLength());
1389 return string;
1390}
1391
1392bool String::Equals(const String* that) const {
1393 if (this == that) {
1394 // Quick reference equality test
1395 return true;
1396 } else if (that == NULL) {
1397 // Null isn't an instanceof anything
1398 return false;
1399 } else if (this->GetLength() != that->GetLength()) {
1400 // Quick length inequality test
1401 return false;
1402 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001403 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001404 // hash code was already equal
1405 for (int32_t i = 0; i < that->GetLength(); ++i) {
1406 if (this->CharAt(i) != that->CharAt(i)) {
1407 return false;
1408 }
1409 }
1410 return true;
1411 }
1412}
1413
Elliott Hughes5d78d392011-12-13 16:53:05 -08001414bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001415 if (this->GetLength() != that_length) {
1416 return false;
1417 } else {
1418 for (int32_t i = 0; i < that_length; ++i) {
1419 if (this->CharAt(i) != that_chars[that_offset + i]) {
1420 return false;
1421 }
1422 }
1423 return true;
1424 }
1425}
1426
1427bool String::Equals(const char* modified_utf8) const {
1428 for (int32_t i = 0; i < GetLength(); ++i) {
1429 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1430 if (ch == '\0' || ch != CharAt(i)) {
1431 return false;
1432 }
1433 }
1434 return *modified_utf8 == '\0';
1435}
1436
1437bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001438 if (modified_utf8.size() != GetLength()) {
1439 return false;
1440 }
1441 const char* p = modified_utf8.data();
1442 for (int32_t i = 0; i < GetLength(); ++i) {
1443 uint16_t ch = GetUtf16FromUtf8(&p);
1444 if (ch != CharAt(i)) {
1445 return false;
1446 }
1447 }
1448 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001449}
1450
1451// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1452std::string String::ToModifiedUtf8() const {
1453 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001454 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001455 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001456 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1457 return result;
1458}
1459
Ian Rogers1c5eb702012-02-01 09:18:34 -08001460void Throwable::SetCause(Throwable* cause) {
1461 CHECK(cause != NULL);
1462 CHECK(cause != this);
1463 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1464 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1465}
1466
Ian Rogers466bb252011-10-14 03:29:56 -07001467bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001468 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001469 return false;
1470 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001471 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001472}
1473
Ian Rogers9074b992011-10-26 17:41:55 -07001474std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001475 std::string result(PrettyTypeOf(this));
1476 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001477 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001478 if (msg != NULL) {
1479 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001480 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001481 result += "\n";
1482 Object* stack_state = GetStackState();
1483 // check stack state isn't missing or corrupt
1484 if (stack_state != NULL && stack_state->IsObjectArray()) {
1485 // Decode the internal stack trace into the depth and method trace
1486 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1487 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001488 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1489 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001490 for (int32_t i = 0; i < depth; ++i) {
1491 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001492 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001493 uint32_t dex_pc = pc_trace->Get(i);
1494 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001495 const char* source_file = mh.GetDeclaringClassSourceFile();
1496 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1497 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001498 }
Ian Rogers9074b992011-10-26 17:41:55 -07001499 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001500 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001501 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001502 result += "Caused by: ";
1503 result += cause->Dump();
1504 }
Ian Rogers9074b992011-10-26 17:41:55 -07001505 return result;
1506}
1507
Ian Rogers5167c972012-02-03 10:41:20 -08001508
1509Class* Throwable::java_lang_Throwable_ = NULL;
1510
1511void Throwable::SetClass(Class* java_lang_Throwable) {
1512 CHECK(java_lang_Throwable_ == NULL);
1513 CHECK(java_lang_Throwable != NULL);
1514 java_lang_Throwable_ = java_lang_Throwable;
1515}
1516
1517void Throwable::ResetClass() {
1518 CHECK(java_lang_Throwable_ != NULL);
1519 java_lang_Throwable_ = NULL;
1520}
1521
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001522Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1523
1524void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1525 CHECK(java_lang_StackTraceElement_ == NULL);
1526 CHECK(java_lang_StackTraceElement != NULL);
1527 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1528}
1529
1530void StackTraceElement::ResetClass() {
1531 CHECK(java_lang_StackTraceElement_ != NULL);
1532 java_lang_StackTraceElement_ = NULL;
1533}
1534
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001535StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1536 String* method_name,
1537 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001538 int32_t line_number) {
1539 StackTraceElement* trace =
1540 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1541 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1542 const_cast<String*>(declaring_class), false);
1543 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1544 const_cast<String*>(method_name), false);
1545 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1546 const_cast<String*>(file_name), false);
1547 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1548 line_number, false);
1549 return trace;
1550}
1551
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001552} // namespace art