blob: 8c101ba259d14bbcb2a6a354ba7df62a4fb72d05 [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"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070040
Logan Chienfca7e872011-12-20 20:08:22 +080041#if defined(ART_USE_LLVM_COMPILER)
42#include "compiler_llvm/inferred_reg_category_map.h"
TDYa12785321912012-04-01 15:24:56 -070043#include "compiler_llvm/runtime_support_llvm.h"
Logan Chienfca7e872011-12-20 20:08:22 +080044using art::compiler_llvm::InferredRegCategoryMap;
45#endif
46
Carl Shapiro3ee755d2011-06-28 12:11:04 -070047namespace art {
48
Elliott Hughesdbb40792011-11-18 17:05:22 -080049String* Object::AsString() {
50 DCHECK(GetClass()->IsStringClass());
51 return down_cast<String*>(this);
52}
53
Elliott Hughes081be7f2011-09-18 16:50:26 -070054Object* Object::Clone() {
55 Class* c = GetClass();
56 DCHECK(!c->IsClassClass());
57
58 // Object::SizeOf gets the right size even if we're an array.
59 // Using c->AllocObject() here would be wrong.
60 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080061 Heap* heap = Runtime::Current()->GetHeap();
62 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070063 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070064 return NULL;
65 }
66
67 // Copy instance data. We assume memcpy copies by words.
68 // TODO: expose and use move32.
69 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070070 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070071 size_t offset = sizeof(Object);
72 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
73
Elliott Hughes20cde902011-10-04 17:37:27 -070074 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080075 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070076 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070077
Brian Carlstrom40381fb2011-10-19 14:13:40 -070078 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070079}
80
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070081uint32_t Object::GetThinLockId() {
82 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070083}
84
85void Object::MonitorEnter(Thread* thread) {
86 Monitor::MonitorEnter(thread, this);
87}
88
Ian Rogersff1ed472011-09-20 13:46:24 -070089bool Object::MonitorExit(Thread* thread) {
90 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070091}
92
93void Object::Notify() {
94 Monitor::Notify(Thread::Current(), this);
95}
96
97void Object::NotifyAll() {
98 Monitor::NotifyAll(Thread::Current(), this);
99}
100
101void Object::Wait(int64_t ms, int32_t ns) {
102 Monitor::Wait(Thread::Current(), this, ms, ns, true);
103}
104
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105// TODO: get global references for these
106Class* Field::java_lang_reflect_Field_ = NULL;
107
108void Field::SetClass(Class* java_lang_reflect_Field) {
109 CHECK(java_lang_reflect_Field_ == NULL);
110 CHECK(java_lang_reflect_Field != NULL);
111 java_lang_reflect_Field_ = java_lang_reflect_Field;
112}
113
114void Field::ResetClass() {
115 CHECK(java_lang_reflect_Field_ != NULL);
116 java_lang_reflect_Field_ = NULL;
117}
118
Ian Rogers0571d352011-11-03 19:51:38 -0700119void Field::SetOffset(MemberOffset num_bytes) {
120 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800121#if 0 // TODO enable later in boot and under !NDEBUG
122 FieldHelper fh(this);
123 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700124 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
125 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
126 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800127#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700128 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
129}
130
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700131uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700132 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700133 if (IsStatic()) {
134 object = declaring_class_;
135 }
136 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700137}
138
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700139void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700140 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700141 if (IsStatic()) {
142 object = declaring_class_;
143 }
144 object->SetField32(GetOffset(), new_value, IsVolatile());
145}
146
147uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700148 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700149 if (IsStatic()) {
150 object = declaring_class_;
151 }
152 return object->GetField64(GetOffset(), IsVolatile());
153}
154
155void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700156 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157 if (IsStatic()) {
158 object = declaring_class_;
159 }
160 object->SetField64(GetOffset(), new_value, IsVolatile());
161}
162
163Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700164 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700165 if (IsStatic()) {
166 object = declaring_class_;
167 }
168 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
169}
170
171void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700172 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700173 if (IsStatic()) {
174 object = declaring_class_;
175 }
176 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
177}
178
179bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
181 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700182 return Get32(object);
183}
184
185void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800186 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
187 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188 Set32(object, z);
189}
190
191int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800192 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
193 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 return Get32(object);
195}
196
197void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800198 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
199 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 Set32(object, b);
201}
202
203uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800204 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
205 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 return Get32(object);
207}
208
209void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800210 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
211 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 Set32(object, c);
213}
214
Ian Rogers466bb252011-10-14 03:29:56 -0700215int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
217 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 return Get32(object);
219}
220
Ian Rogers466bb252011-10-14 03:29:56 -0700221void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
223 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224 Set32(object, s);
225}
226
227int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800228 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
229 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230 return Get32(object);
231}
232
233void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800234 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
235 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700236 Set32(object, i);
237}
238
239int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800240 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
241 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700242 return Get64(object);
243}
244
245void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800246 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
247 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700248 Set64(object, j);
249}
250
Elliott Hughes1d878f32012-04-11 15:17:54 -0700251union Bits {
252 jdouble d;
253 jfloat f;
254 jint i;
255 jlong j;
256};
257
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700258float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
260 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700261 Bits bits;
262 bits.i = Get32(object);
263 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700264}
265
266void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
268 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700269 Bits bits;
270 bits.f = f;
271 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700272}
273
274double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
276 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700277 Bits bits;
278 bits.j = Get64(object);
279 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280}
281
282void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
284 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700285 Bits bits;
286 bits.d = d;
287 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700288}
289
290Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
292 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700293 return GetObj(object);
294}
295
296void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800297 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
298 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700299 SetObj(object, l);
300}
301
302// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700303Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304Class* Method::java_lang_reflect_Method_ = NULL;
305
Elliott Hughes80609252011-09-23 17:24:51 -0700306void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
307 CHECK(java_lang_reflect_Constructor_ == NULL);
308 CHECK(java_lang_reflect_Constructor != NULL);
309 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
310
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 CHECK(java_lang_reflect_Method_ == NULL);
312 CHECK(java_lang_reflect_Method != NULL);
313 java_lang_reflect_Method_ = java_lang_reflect_Method;
314}
315
Elliott Hughes80609252011-09-23 17:24:51 -0700316void Method::ResetClasses() {
317 CHECK(java_lang_reflect_Constructor_ != NULL);
318 java_lang_reflect_Constructor_ = NULL;
319
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 CHECK(java_lang_reflect_Method_ != NULL);
321 java_lang_reflect_Method_ = NULL;
322}
323
Elliott Hughes418d20f2011-09-22 14:00:39 -0700324Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
325 if (*p == '[') {
326 // Something like "[[[Ljava/lang/String;".
327 const char* start = p;
328 while (*p == '[') {
329 ++p;
330 }
331 if (*p == 'L') {
332 while (*p != ';') {
333 ++p;
334 }
335 }
336 ++p; // Either the ';' or the primitive type.
337
Brian Carlstromaded5f72011-10-07 17:15:04 -0700338 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800339 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 } else if (*p == 'L') {
341 const char* start = p;
342 while (*p != ';') {
343 ++p;
344 }
345 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800346 std::string descriptor(start, (p - start));
347 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700348 } else {
349 return class_linker->FindPrimitiveClass(*p++);
350 }
351}
352
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353ObjectArray<String>* Method::GetDexCacheStrings() const {
354 return GetFieldObject<ObjectArray<String>*>(
355 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
356}
357
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
359 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
360 new_dex_cache_strings, false);
361}
362
Ian Rogers19846512012-02-24 11:42:47 -0800363ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
364 return GetFieldObject<ObjectArray<Method>*>(
365 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
366}
367
368void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
369 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
370 new_dex_cache_methods, false);
371}
372
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
374 return GetFieldObject<ObjectArray<Class>*>(
375 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
376}
377
378void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
379 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
380 new_dex_cache_classes, false);
381}
382
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
384 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
385 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
386 false);
387}
388
389void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700390 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700391 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392}
393
394size_t Method::NumArgRegisters(const StringPiece& shorty) {
395 CHECK_LE(1, shorty.length());
396 uint32_t num_registers = 0;
397 for (int i = 1; i < shorty.length(); ++i) {
398 char ch = shorty[i];
399 if (ch == 'D' || ch == 'J') {
400 num_registers += 2;
401 } else {
402 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700403 }
404 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700405 return num_registers;
406}
407
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408bool Method::IsProxyMethod() const {
409 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700410}
411
Ian Rogers466bb252011-10-14 03:29:56 -0700412Method* Method::FindOverriddenMethod() const {
413 if (IsStatic()) {
414 return NULL;
415 }
416 Class* declaring_class = GetDeclaringClass();
417 Class* super_class = declaring_class->GetSuperClass();
418 uint16_t method_index = GetMethodIndex();
419 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
420 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800421 // Did this method override a super class method? If so load the result from the super class'
422 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700423 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
424 result = super_class_vtable->Get(method_index);
425 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800426 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800427 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800428 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
429 CHECK_EQ(result,
430 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800431 } else {
432 MethodHelper mh(this);
433 MethodHelper interface_mh;
434 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
435 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
436 InterfaceEntry* entry = iftable->Get(i);
437 Class* interface = entry->GetInterface();
438 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
439 Method* interface_method = interface->GetVirtualMethod(j);
440 interface_mh.ChangeMethod(interface_method);
441 if (mh.HasSameNameAndSignature(&interface_mh)) {
442 result = interface_method;
443 break;
444 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800445 }
446 }
Ian Rogers466bb252011-10-14 03:29:56 -0700447 }
448 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800449#ifndef NDEBUG
450 MethodHelper result_mh(result);
451 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
452#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700453 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700454}
455
Elliott Hughes168670b2012-02-29 16:43:26 -0800456static const void* GetOatCode(const Method* m) {
457 Runtime* runtime = Runtime::Current();
458 const void* code = m->GetCode();
459 // Peel off any method tracing trampoline.
460 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
461 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
462 }
463 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800464 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800465 code = runtime->GetClassLinker()->GetOatCodeFor(m);
466 }
467 return code;
468}
469
Ian Rogersbdb03912011-09-14 00:55:44 -0700470uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700471 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700472 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800473 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700474 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700475 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700476 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800477 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700478 uint32_t best_offset = 0;
479 uint32_t best_dex_offset = 0;
480 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700481 uint32_t map_offset = mapping_table[i];
482 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700483 if (map_offset == sought_offset) {
484 best_offset = map_offset;
485 best_dex_offset = map_dex_offset;
486 break;
487 }
488 if (map_offset < sought_offset && map_offset > best_offset) {
489 best_offset = map_offset;
490 best_dex_offset = map_dex_offset;
491 }
492 }
493 return best_dex_offset;
494}
495
496uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700498 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700499 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700500 return 0; // Special no mapping/pc == 0 case
501 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700503 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700504 uint32_t map_offset = mapping_table[i];
505 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700506 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800507 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700508 }
509 }
510 LOG(FATAL) << "Looking up Dex PC not contained in method";
511 return 0;
512}
513
514uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800515 MethodHelper mh(this);
516 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700517 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700518 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
519 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700520 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700521 if (iter_type_idx == DexFile::kDexNoIndex16) {
522 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700523 }
524 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800525 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700526 if (iter_exception_type == NULL) {
527 // The verifier should take care of resolving all exception classes early
528 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800529 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700530 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700531 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700532 }
533 }
534 // Handler not found
535 return DexFile::kDexNoIndex;
536}
537
Elliott Hughes77405792012-03-15 15:22:12 -0700538void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700539 // Push a transition back into managed code onto the linked list in thread.
Elliott Hughes34e06962012-04-09 13:55:55 -0700540 CHECK_EQ(kRunnable, self->GetState());
TDYa12785321912012-04-01 15:24:56 -0700541
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700542#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700543 NativeToManagedRecord record;
544 self->PushNativeToManagedRecord(&record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700545#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700546
TDYa1270b686e52012-04-09 22:43:35 -0700547#if defined(ART_USE_LLVM_COMPILER)
548 art_fix_stub_from_code(const_cast<Method*>(this));
549#endif
550
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700551 // Call the invoke stub associated with the method.
552 // Pass everything as arguments.
553 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700554
555 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700556
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500557 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700558 bool log = false;
559 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800560 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
561 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700562 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700563 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700564 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800565 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
566 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700567 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700568 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800569 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
570 PrettyMethod(this).c_str(), GetCode(), stub,
571 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700572 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700573 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700574 }
575 }
576
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700577#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700578 // Pop transition.
579 self->PopNativeToManagedRecord(record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700580#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700581}
582
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700583bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700584 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800585 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800586 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700587 return native_method != jni_stub;
588}
589
Ian Rogers60db5ab2012-02-20 17:02:00 -0800590void Method::RegisterNative(Thread* self, const void* native_method) {
591 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700592 CHECK(IsNative()) << PrettyMethod(this);
593 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700594#if defined(ART_USE_LLVM_COMPILER)
595 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
596 native_method, false);
597#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800598 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
599 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
600 native_method, false);
601 } else {
602 // We've been asked to associate this method with the given native method but are working
603 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
604 // the native method to runtime support and store the target somewhere runtime support will
605 // find it.
606#if defined(__arm__)
607 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
608 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
609#else
610 UNIMPLEMENTED(FATAL);
611#endif
612 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
613 reinterpret_cast<const uint8_t*>(native_method), false);
614 }
TDYa12726467572012-04-17 20:51:22 -0700615#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700616}
617
Ian Rogers19846512012-02-24 11:42:47 -0800618void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700619 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700620 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800621 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700622}
623
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700624void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700625 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
626 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700627 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800628 if (new_status == kStatusError) {
629 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
630
631 // stash current exception
632 Thread* self = Thread::Current();
633 SirtRef<Throwable> exception(self->GetException());
634 CHECK(exception.get() != NULL);
635
636 // clear exception to call FindSystemClass
637 self->ClearException();
638 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
639 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
640 CHECK(!self->IsExceptionPending());
641
642 // only verification errors, not initialization problems, should set a verify error.
643 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
644 Class* exception_class = exception->GetClass();
645 if (!eiie_class->IsAssignableFrom(exception_class)) {
646 SetVerifyErrorClass(exception_class);
647 }
648
649 // restore exception
650 self->SetException(exception.get());
651 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700652 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700653}
654
655DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700656 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657}
658
659void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700660 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700661}
662
Brian Carlstrom1f870082011-08-23 16:02:11 -0700663Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700664 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700665 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500666 // TODO: decide whether we want this check. It currently fails during bootstrap.
667 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700668 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800669 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700670}
671
Ian Rogers0571d352011-11-03 19:51:38 -0700672void Class::SetClassSize(size_t new_class_size) {
673 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
674 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
675}
676
Ian Rogersd418eda2012-01-30 12:14:28 -0800677// Return the class' name. The exact format is bizarre, but it's the specified behavior for
678// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
679// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
680// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
681String* Class::ComputeName() {
682 String* name = GetName();
683 if (name != NULL) {
684 return name;
685 }
686 std::string descriptor(ClassHelper(this).GetDescriptor());
687 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
688 // The descriptor indicates that this is the class for
689 // a primitive type; special-case the return value.
690 const char* c_name = NULL;
691 switch (descriptor[0]) {
692 case 'Z': c_name = "boolean"; break;
693 case 'B': c_name = "byte"; break;
694 case 'C': c_name = "char"; break;
695 case 'S': c_name = "short"; break;
696 case 'I': c_name = "int"; break;
697 case 'J': c_name = "long"; break;
698 case 'F': c_name = "float"; break;
699 case 'D': c_name = "double"; break;
700 case 'V': c_name = "void"; break;
701 default:
702 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
703 }
704 name = String::AllocFromModifiedUtf8(c_name);
705 } else {
706 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
707 // components.
708 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
709 descriptor.erase(0, 1);
710 descriptor.erase(descriptor.size() - 1);
711 }
712 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
713 name = String::AllocFromModifiedUtf8(descriptor.c_str());
714 }
715 SetName(name);
716 return name;
717}
718
Elliott Hughes4681c802011-09-25 18:04:37 -0700719void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700720 if ((flags & kDumpClassFullDetail) == 0) {
721 os << PrettyClass(this);
722 if ((flags & kDumpClassClassLoader) != 0) {
723 os << ' ' << GetClassLoader();
724 }
725 if ((flags & kDumpClassInitialized) != 0) {
726 os << ' ' << GetStatus();
727 }
Elliott Hughese0918552011-10-28 17:18:29 -0700728 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700729 return;
730 }
731
732 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800733 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700734 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800735 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700736 os << " objectSize=" << SizeOf() << " "
737 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
738 os << StringPrintf(" access=0x%04x.%04x\n",
739 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
740 if (super != NULL) {
741 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
742 }
743 if (IsArrayClass()) {
744 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
745 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800746 if (kh.NumInterfaces() > 0) {
747 os << " interfaces (" << kh.NumInterfaces() << "):\n";
748 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
749 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700750 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800751 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700752 }
753 }
754 os << " vtable (" << NumVirtualMethods() << " entries, "
755 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
756 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800757 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700758 }
759 os << " direct methods (" << NumDirectMethods() << " entries):\n";
760 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800761 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700762 }
763 if (NumStaticFields() > 0) {
764 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700765 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700766 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800767 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700768 }
769 } else {
770 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700771 }
772 }
773 if (NumInstanceFields() > 0) {
774 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700775 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700776 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800777 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700778 }
779 } else {
780 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700781 }
782 }
783}
784
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700785void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
786 if (new_reference_offsets != CLASS_WALK_SUPER) {
787 // Sanity check that the number of bits set in the reference offset bitmap
788 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800789 size_t count = 0;
790 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
791 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700792 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800793 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700794 }
795 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
796 new_reference_offsets, false);
797}
798
799void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
800 if (new_reference_offsets != CLASS_WALK_SUPER) {
801 // Sanity check that the number of bits set in the reference offset bitmap
802 // agrees with the number of references
803 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
804 NumReferenceStaticFieldsDuringLinking());
805 }
806 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
807 new_reference_offsets, false);
808}
809
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700810bool Class::Implements(const Class* klass) const {
811 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700812 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700813 // All interfaces implemented directly and by our superclass, and
814 // recursively all super-interfaces of those interfaces, are listed
815 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700816 int32_t iftable_count = GetIfTableCount();
817 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
818 for (int32_t i = 0; i < iftable_count; i++) {
819 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700820 return true;
821 }
822 }
823 return false;
824}
825
Elliott Hughese84278b2012-03-22 10:06:53 -0700826// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700827// are array classes.
828//
829// Consider an array class, e.g. Y[][], where Y is a subclass of X.
830// Y[][] = Y[][] --> true (identity)
831// X[][] = Y[][] --> true (element superclass)
832// Y = Y[][] --> false
833// Y[] = Y[][] --> false
834// Object = Y[][] --> true (everything is an object)
835// Object[] = Y[][] --> true
836// Object[][] = Y[][] --> true
837// Object[][][] = Y[][] --> false (too many []s)
838// Serializable = Y[][] --> true (all arrays are Serializable)
839// Serializable[] = Y[][] --> true
840// Serializable[][] = Y[][] --> false (unless Y is Serializable)
841//
842// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700843// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700844//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700846 DCHECK(IsArrayClass()) << PrettyClass(this);
847 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700848 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700849}
850
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700851bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700852 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
853 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700854 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700855 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700856 // src's super should be java_lang_Object, since it is an array.
857 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700858 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
859 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700860 return this == java_lang_Object;
861 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700862 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700863}
864
865bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700866 DCHECK(!IsInterface()) << PrettyClass(this);
867 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700868 const Class* current = this;
869 do {
870 if (current == klass) {
871 return true;
872 }
873 current = current->GetSuperClass();
874 } while (current != NULL);
875 return false;
876}
877
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800878bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700879 size_t i = 0;
880 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
881 ++i;
882 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700883 if (descriptor1.find('/', i) != StringPiece::npos ||
884 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700885 return false;
886 } else {
887 return true;
888 }
889}
890
891bool Class::IsInSamePackage(const Class* that) const {
892 const Class* klass1 = this;
893 const Class* klass2 = that;
894 if (klass1 == klass2) {
895 return true;
896 }
897 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700898 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700899 return false;
900 }
901 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700902 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700903 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700904 }
jeffhao4a801a42011-09-23 13:53:40 -0700905 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700906 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700907 }
908 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800910 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800911 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800912 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800913 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700914}
915
Elliott Hughesdbb40792011-11-18 17:05:22 -0800916bool Class::IsClassClass() const {
917 Class* java_lang_Class = GetClass()->GetClass();
918 return this == java_lang_Class;
919}
920
921bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800922 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800923}
924
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800925bool Class::IsThrowableClass() const {
926 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
927 return throwable->IsAssignableFrom(this);
928}
929
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800930ClassLoader* Class::GetClassLoader() const {
931 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700932}
933
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700934void Class::SetClassLoader(const ClassLoader* new_cl) {
935 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700937}
938
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800939Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700940 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700941 DCHECK(declaring_class != NULL) << PrettyClass(this);
942 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700943 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700944 int32_t iftable_count = GetIfTableCount();
945 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
946 for (int32_t i = 0; i < iftable_count; i++) {
947 InterfaceEntry* interface_entry = iftable->Get(i);
948 if (interface_entry->GetInterface() == declaring_class) {
949 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700950 }
951 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700952 return NULL;
953}
954
Ian Rogers466bb252011-10-14 03:29:56 -0700955Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700956 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800957 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700958 if (method != NULL) {
959 return method;
960 }
961
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700962 int32_t iftable_count = GetIfTableCount();
963 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
964 for (int32_t i = 0; i < iftable_count; i++) {
965 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700966 if (method != NULL) {
967 return method;
968 }
969 }
970 return NULL;
971}
972
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800973Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
974 // Check the current class before checking the interfaces.
975 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
976 if (method != NULL) {
977 return method;
978 }
979
980 int32_t iftable_count = GetIfTableCount();
981 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
982 for (int32_t i = 0; i < iftable_count; i++) {
983 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
984 if (method != NULL) {
985 return method;
986 }
987 }
988 return NULL;
989}
990
991
992Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800993 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700994 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700995 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800996 mh.ChangeMethod(method);
997 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700998 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700999 }
1000 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001001 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001002}
1003
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001004Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1005 if (GetDexCache() == dex_cache) {
1006 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1007 Method* method = GetDirectMethod(i);
1008 if (method->GetDexMethodIndex() == dex_method_idx) {
1009 return method;
1010 }
1011 }
1012 }
1013 return NULL;
1014}
1015
1016Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1017 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001018 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001019 if (method != NULL) {
1020 return method;
1021 }
1022 }
1023 return NULL;
1024}
1025
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001026Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1027 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1028 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1029 if (method != NULL) {
1030 return method;
1031 }
1032 }
1033 return NULL;
1034}
1035
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001036Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001037 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001038 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001039 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001040 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001041 mh.ChangeMethod(method);
1042 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001043 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001044 }
1045 }
1046 return NULL;
1047}
1048
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001049Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1050 if (GetDexCache() == dex_cache) {
1051 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1052 Method* method = GetVirtualMethod(i);
1053 if (method->GetDexMethodIndex() == dex_method_idx) {
1054 return method;
1055 }
1056 }
1057 }
1058 return NULL;
1059}
1060
Ian Rogers466bb252011-10-14 03:29:56 -07001061Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1062 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1063 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1064 if (method != NULL) {
1065 return method;
1066 }
1067 }
1068 return NULL;
1069}
1070
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001071Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1072 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1073 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1074 if (method != NULL) {
1075 return method;
1076 }
1077 }
1078 return NULL;
1079}
1080
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001081Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 // Is the field in this class?
1083 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001084 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1086 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001087 fh.ChangeField(f);
1088 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001089 return f;
1090 }
1091 }
1092 return NULL;
1093}
1094
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001095Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1096 if (GetDexCache() == dex_cache) {
1097 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1098 Field* f = GetInstanceField(i);
1099 if (f->GetDexFieldIndex() == dex_field_idx) {
1100 return f;
1101 }
1102 }
1103 }
1104 return NULL;
1105}
1106
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001107Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 // 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()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001111 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 if (f != NULL) {
1113 return f;
1114 }
1115 }
1116 return NULL;
1117}
1118
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001119Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1120 // Is the field in this class, or any of its superclasses?
1121 // Interfaces are not relevant because they can't contain instance fields.
1122 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1123 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1124 if (f != NULL) {
1125 return f;
1126 }
1127 }
1128 return NULL;
1129}
1130
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001131Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1132 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001133 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001134 for (size_t i = 0; i < NumStaticFields(); ++i) {
1135 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001136 fh.ChangeField(f);
1137 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001138 return f;
1139 }
1140 }
1141 return NULL;
1142}
1143
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001144Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1145 if (dex_cache == GetDexCache()) {
1146 for (size_t i = 0; i < NumStaticFields(); ++i) {
1147 Field* f = GetStaticField(i);
1148 if (f->GetDexFieldIndex() == dex_field_idx) {
1149 return f;
1150 }
1151 }
1152 }
1153 return NULL;
1154}
1155
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001156Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1157 // Is the field in this class (or its interfaces), or any of its
1158 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001159 ClassHelper kh;
1160 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001161 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001162 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001163 if (f != NULL) {
1164 return f;
1165 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001166 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001167 kh.ChangeClass(k);
1168 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1169 Class* interface = kh.GetInterface(i);
1170 f = interface->FindDeclaredStaticField(name, type);
1171 if (f != NULL) {
1172 return f;
1173 }
1174 }
1175 }
1176 return NULL;
1177}
1178
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001179Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1180 ClassHelper kh;
1181 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1182 // Is the field in this class?
1183 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1184 if (f != NULL) {
1185 return f;
1186 }
1187 // Is this field in any of this class' interfaces?
1188 kh.ChangeClass(k);
1189 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1190 Class* interface = kh.GetInterface(i);
1191 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1192 if (f != NULL) {
1193 return f;
1194 }
1195 }
1196 }
1197 return NULL;
1198}
1199
Ian Rogersb067ac22011-12-13 18:05:09 -08001200Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1201 // Find a field using the JLS field resolution order
1202 ClassHelper kh;
1203 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1204 // Is the field in this class?
1205 Field* f = k->FindDeclaredInstanceField(name, type);
1206 if (f != NULL) {
1207 return f;
1208 }
1209 f = k->FindDeclaredStaticField(name, type);
1210 if (f != NULL) {
1211 return f;
1212 }
1213 // Is this field in any of this class' interfaces?
1214 kh.ChangeClass(k);
1215 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1216 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001217 f = interface->FindDeclaredStaticField(name, type);
1218 if (f != NULL) {
1219 return f;
1220 }
1221 }
1222 }
1223 return NULL;
1224}
1225
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001226Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001227 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001228 DCHECK_GE(component_count, 0);
1229 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001230
Ian Rogersa15e67d2012-02-28 13:51:55 -08001231 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001232 size_t data_size = component_count * component_size;
1233 size_t size = header_size + data_size;
1234
1235 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1236 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1237 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1238 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001239 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001240 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001241 return NULL;
1242 }
1243
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001244 Heap* heap = Runtime::Current()->GetHeap();
1245 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001246 if (array != NULL) {
1247 DCHECK(array->IsArrayInstance());
1248 array->SetLength(component_count);
1249 }
1250 return array;
1251}
1252
1253Array* Array::Alloc(Class* array_class, int32_t component_count) {
1254 return Alloc(array_class, component_count, array_class->GetComponentSize());
1255}
1256
Elliott Hughes80609252011-09-23 17:24:51 -07001257bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001258 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001259 "length=%i; index=%i", length_, index);
1260 return false;
1261}
1262
1263bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001264 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001265 "Can't store an element of type %s into an array of type %s",
1266 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1267 return false;
1268}
1269
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001270template<typename T>
1271PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001272 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001273 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1274 return down_cast<PrimitiveArray<T>*>(raw_array);
1275}
1276
1277template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1278
1279// Explicitly instantiate all the primitive array types.
1280template class PrimitiveArray<uint8_t>; // BooleanArray
1281template class PrimitiveArray<int8_t>; // ByteArray
1282template class PrimitiveArray<uint16_t>; // CharArray
1283template class PrimitiveArray<double>; // DoubleArray
1284template class PrimitiveArray<float>; // FloatArray
1285template class PrimitiveArray<int32_t>; // IntArray
1286template class PrimitiveArray<int64_t>; // LongArray
1287template class PrimitiveArray<int16_t>; // ShortArray
1288
Ian Rogers466bb252011-10-14 03:29:56 -07001289// Explicitly instantiate Class[][]
1290template class ObjectArray<ObjectArray<Class> >;
1291
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001292// TODO: get global references for these
1293Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001294
Brian Carlstroma663ea52011-08-19 23:33:41 -07001295void String::SetClass(Class* java_lang_String) {
1296 CHECK(java_lang_String_ == NULL);
1297 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001298 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001299}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001300
Brian Carlstroma663ea52011-08-19 23:33:41 -07001301void String::ResetClass() {
1302 CHECK(java_lang_String_ != NULL);
1303 java_lang_String_ = NULL;
1304}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001305
Brian Carlstromc74255f2011-09-11 22:47:39 -07001306String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001307 return Runtime::Current()->GetInternTable()->InternWeak(this);
1308}
1309
Brian Carlstrom395520e2011-09-25 19:35:00 -07001310int32_t String::GetHashCode() {
1311 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1312 if (result == 0) {
1313 ComputeHashCode();
1314 }
1315 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1316 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1317 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001318 return result;
1319}
1320
1321int32_t String::GetLength() const {
1322 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1323 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1324 return result;
1325}
1326
1327uint16_t String::CharAt(int32_t index) const {
1328 // TODO: do we need this? Equals is the only caller, and could
1329 // bounds check itself.
1330 if (index < 0 || index >= count_) {
1331 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001332 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001333 "length=%i; index=%i", count_, index);
1334 return 0;
1335 }
1336 return GetCharArray()->Get(index + GetOffset());
1337}
1338
1339String* String::AllocFromUtf16(int32_t utf16_length,
1340 const uint16_t* utf16_data_in,
1341 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001342 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001343 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001344 if (string == NULL) {
1345 return NULL;
1346 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001347 // TODO: use 16-bit wide memset variant
1348 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001349 if (array == NULL) {
1350 return NULL;
1351 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001352 for (int i = 0; i < utf16_length; i++) {
1353 array->Set(i, utf16_data_in[i]);
1354 }
1355 if (hash_code != 0) {
1356 string->SetHashCode(hash_code);
1357 } else {
1358 string->ComputeHashCode();
1359 }
1360 return string;
1361}
1362
1363String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001364 if (utf == NULL) {
1365 return NULL;
1366 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367 size_t char_count = CountModifiedUtf8Chars(utf);
1368 return AllocFromModifiedUtf8(char_count, utf);
1369}
1370
1371String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1372 const char* utf8_data_in) {
1373 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001374 if (string == NULL) {
1375 return NULL;
1376 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001377 uint16_t* utf16_data_out =
1378 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1379 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1380 string->ComputeHashCode();
1381 return string;
1382}
1383
1384String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001385 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1386 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001387 return NULL;
1388 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001389 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001390}
1391
1392String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001393 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001394 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001395 if (string == NULL) {
1396 return NULL;
1397 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001398 string->SetArray(array);
1399 string->SetCount(array->GetLength());
1400 return string;
1401}
1402
1403bool String::Equals(const String* that) const {
1404 if (this == that) {
1405 // Quick reference equality test
1406 return true;
1407 } else if (that == NULL) {
1408 // Null isn't an instanceof anything
1409 return false;
1410 } else if (this->GetLength() != that->GetLength()) {
1411 // Quick length inequality test
1412 return false;
1413 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001414 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001415 // hash code was already equal
1416 for (int32_t i = 0; i < that->GetLength(); ++i) {
1417 if (this->CharAt(i) != that->CharAt(i)) {
1418 return false;
1419 }
1420 }
1421 return true;
1422 }
1423}
1424
Elliott Hughes5d78d392011-12-13 16:53:05 -08001425bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001426 if (this->GetLength() != that_length) {
1427 return false;
1428 } else {
1429 for (int32_t i = 0; i < that_length; ++i) {
1430 if (this->CharAt(i) != that_chars[that_offset + i]) {
1431 return false;
1432 }
1433 }
1434 return true;
1435 }
1436}
1437
1438bool String::Equals(const char* modified_utf8) const {
1439 for (int32_t i = 0; i < GetLength(); ++i) {
1440 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1441 if (ch == '\0' || ch != CharAt(i)) {
1442 return false;
1443 }
1444 }
1445 return *modified_utf8 == '\0';
1446}
1447
1448bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001449 if (modified_utf8.size() != GetLength()) {
1450 return false;
1451 }
1452 const char* p = modified_utf8.data();
1453 for (int32_t i = 0; i < GetLength(); ++i) {
1454 uint16_t ch = GetUtf16FromUtf8(&p);
1455 if (ch != CharAt(i)) {
1456 return false;
1457 }
1458 }
1459 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001460}
1461
1462// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1463std::string String::ToModifiedUtf8() const {
1464 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001465 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001466 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001467 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1468 return result;
1469}
1470
Ian Rogers1c5eb702012-02-01 09:18:34 -08001471void Throwable::SetCause(Throwable* cause) {
1472 CHECK(cause != NULL);
1473 CHECK(cause != this);
1474 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1475 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1476}
1477
Ian Rogers466bb252011-10-14 03:29:56 -07001478bool Throwable::IsCheckedException() const {
1479 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1480 if (InstanceOf(error)) {
1481 return false;
1482 }
1483 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1484 return !InstanceOf(jlre);
1485}
1486
Ian Rogers9074b992011-10-26 17:41:55 -07001487std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001488 std::string result(PrettyTypeOf(this));
1489 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001490 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001491 if (msg != NULL) {
1492 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001493 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001494 result += "\n";
1495 Object* stack_state = GetStackState();
1496 // check stack state isn't missing or corrupt
1497 if (stack_state != NULL && stack_state->IsObjectArray()) {
1498 // Decode the internal stack trace into the depth and method trace
1499 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1500 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001501 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1502 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001503 for (int32_t i = 0; i < depth; ++i) {
1504 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001505 mh.ChangeMethod(method);
1506 uint32_t native_pc = pc_trace->Get(i);
1507 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1508 const char* source_file = mh.GetDeclaringClassSourceFile();
1509 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1510 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001511 }
Ian Rogers9074b992011-10-26 17:41:55 -07001512 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001513 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001514 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001515 result += "Caused by: ";
1516 result += cause->Dump();
1517 }
Ian Rogers9074b992011-10-26 17:41:55 -07001518 return result;
1519}
1520
Ian Rogers5167c972012-02-03 10:41:20 -08001521
1522Class* Throwable::java_lang_Throwable_ = NULL;
1523
1524void Throwable::SetClass(Class* java_lang_Throwable) {
1525 CHECK(java_lang_Throwable_ == NULL);
1526 CHECK(java_lang_Throwable != NULL);
1527 java_lang_Throwable_ = java_lang_Throwable;
1528}
1529
1530void Throwable::ResetClass() {
1531 CHECK(java_lang_Throwable_ != NULL);
1532 java_lang_Throwable_ = NULL;
1533}
1534
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001535Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1536
1537void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1538 CHECK(java_lang_StackTraceElement_ == NULL);
1539 CHECK(java_lang_StackTraceElement != NULL);
1540 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1541}
1542
1543void StackTraceElement::ResetClass() {
1544 CHECK(java_lang_StackTraceElement_ != NULL);
1545 java_lang_StackTraceElement_ = NULL;
1546}
1547
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001548StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1549 String* method_name,
1550 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001551 int32_t line_number) {
1552 StackTraceElement* trace =
1553 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1554 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1555 const_cast<String*>(declaring_class), false);
1556 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1557 const_cast<String*>(method_name), false);
1558 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1559 const_cast<String*>(file_name), false);
1560 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1561 line_number, false);
1562 return trace;
1563}
1564
Elliott Hughes1f359b02011-07-17 14:27:17 -07001565static const char* kClassStatusNames[] = {
1566 "Error",
1567 "NotReady",
1568 "Idx",
1569 "Loaded",
1570 "Resolved",
1571 "Verifying",
1572 "Verified",
1573 "Initializing",
1574 "Initialized"
1575};
1576std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1577 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001578 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001579 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001580 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001581 }
1582 return os;
1583}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001584
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001585} // namespace art