blob: d91e70c9dfc7d73ff502819022b82f7b405e57cb [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 {
TDYa127c8dc1012012-04-19 07:03:33 -0700471#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700472 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700473 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800474 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700475 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700476 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800478 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700479 uint32_t best_offset = 0;
480 uint32_t best_dex_offset = 0;
481 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 uint32_t map_offset = mapping_table[i];
483 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700484 if (map_offset == sought_offset) {
485 best_offset = map_offset;
486 best_dex_offset = map_dex_offset;
487 break;
488 }
489 if (map_offset < sought_offset && map_offset > best_offset) {
490 best_offset = map_offset;
491 best_dex_offset = map_dex_offset;
492 }
493 }
494 return best_dex_offset;
TDYa127c8dc1012012-04-19 07:03:33 -0700495#else
496 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
497 return static_cast<uint32_t>(pc);
498#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700499}
500
501uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700503 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700504 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700505 return 0; // Special no mapping/pc == 0 case
506 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700508 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700509 uint32_t map_offset = mapping_table[i];
510 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700511 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800512 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700513 }
514 }
515 LOG(FATAL) << "Looking up Dex PC not contained in method";
516 return 0;
517}
518
519uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 MethodHelper mh(this);
521 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700522 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700523 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
524 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700525 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700526 if (iter_type_idx == DexFile::kDexNoIndex16) {
527 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700528 }
529 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800530 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700531 if (iter_exception_type == NULL) {
532 // The verifier should take care of resolving all exception classes early
533 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800534 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700535 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700536 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700537 }
538 }
539 // Handler not found
540 return DexFile::kDexNoIndex;
541}
542
Elliott Hughes77405792012-03-15 15:22:12 -0700543void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700544 // Push a transition back into managed code onto the linked list in thread.
Elliott Hughes34e06962012-04-09 13:55:55 -0700545 CHECK_EQ(kRunnable, self->GetState());
TDYa12785321912012-04-01 15:24:56 -0700546
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700547#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700548 NativeToManagedRecord record;
549 self->PushNativeToManagedRecord(&record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700550#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700551
552 // Call the invoke stub associated with the method.
553 // Pass everything as arguments.
554 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700555
556 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700557
TDYa12705fe3b62012-04-21 00:28:54 -0700558#if defined(ART_USE_LLVM_COMPILER)
559 if (stub == NULL || !have_executable_code) {
560 Runtime::Current()->GetClassLinker()->LinkOatCodeFor(const_cast<Method*>(this));
561 stub = GetInvokeStub();
562 have_executable_code = (GetCode() != NULL);
563 }
564#endif
565
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500566 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700567 bool log = false;
568 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800569 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
570 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700571 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700572 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700573 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800574 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
575 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700576 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700577 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800578 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
579 PrettyMethod(this).c_str(), GetCode(), stub,
580 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700581 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700582 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700583 }
584 }
585
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700586#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700587 // Pop transition.
588 self->PopNativeToManagedRecord(record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700589#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700590}
591
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700592bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700593 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800594 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800595 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700596 return native_method != jni_stub;
597}
598
Ian Rogers60db5ab2012-02-20 17:02:00 -0800599void Method::RegisterNative(Thread* self, const void* native_method) {
600 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700601 CHECK(IsNative()) << PrettyMethod(this);
602 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700603#if defined(ART_USE_LLVM_COMPILER)
604 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
605 native_method, false);
606#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800607 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
608 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
609 native_method, false);
610 } else {
611 // We've been asked to associate this method with the given native method but are working
612 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
613 // the native method to runtime support and store the target somewhere runtime support will
614 // find it.
615#if defined(__arm__)
616 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
617 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
618#else
619 UNIMPLEMENTED(FATAL);
620#endif
621 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
622 reinterpret_cast<const uint8_t*>(native_method), false);
623 }
TDYa12726467572012-04-17 20:51:22 -0700624#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700625}
626
Ian Rogers19846512012-02-24 11:42:47 -0800627void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700628 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700629 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800630 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700631}
632
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700633void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700634 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
635 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700636 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800637 if (new_status == kStatusError) {
638 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
639
640 // stash current exception
641 Thread* self = Thread::Current();
642 SirtRef<Throwable> exception(self->GetException());
643 CHECK(exception.get() != NULL);
644
645 // clear exception to call FindSystemClass
646 self->ClearException();
647 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
648 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
649 CHECK(!self->IsExceptionPending());
650
651 // only verification errors, not initialization problems, should set a verify error.
652 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
653 Class* exception_class = exception->GetClass();
654 if (!eiie_class->IsAssignableFrom(exception_class)) {
655 SetVerifyErrorClass(exception_class);
656 }
657
658 // restore exception
659 self->SetException(exception.get());
660 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700661 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700662}
663
664DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700665 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700666}
667
668void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700669 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700670}
671
Brian Carlstrom1f870082011-08-23 16:02:11 -0700672Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700673 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700674 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500675 // TODO: decide whether we want this check. It currently fails during bootstrap.
676 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700677 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800678 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700679}
680
Ian Rogers0571d352011-11-03 19:51:38 -0700681void Class::SetClassSize(size_t new_class_size) {
682 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
683 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
684}
685
Ian Rogersd418eda2012-01-30 12:14:28 -0800686// Return the class' name. The exact format is bizarre, but it's the specified behavior for
687// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
688// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
689// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
690String* Class::ComputeName() {
691 String* name = GetName();
692 if (name != NULL) {
693 return name;
694 }
695 std::string descriptor(ClassHelper(this).GetDescriptor());
696 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
697 // The descriptor indicates that this is the class for
698 // a primitive type; special-case the return value.
699 const char* c_name = NULL;
700 switch (descriptor[0]) {
701 case 'Z': c_name = "boolean"; break;
702 case 'B': c_name = "byte"; break;
703 case 'C': c_name = "char"; break;
704 case 'S': c_name = "short"; break;
705 case 'I': c_name = "int"; break;
706 case 'J': c_name = "long"; break;
707 case 'F': c_name = "float"; break;
708 case 'D': c_name = "double"; break;
709 case 'V': c_name = "void"; break;
710 default:
711 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
712 }
713 name = String::AllocFromModifiedUtf8(c_name);
714 } else {
715 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
716 // components.
717 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
718 descriptor.erase(0, 1);
719 descriptor.erase(descriptor.size() - 1);
720 }
721 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
722 name = String::AllocFromModifiedUtf8(descriptor.c_str());
723 }
724 SetName(name);
725 return name;
726}
727
Elliott Hughes4681c802011-09-25 18:04:37 -0700728void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700729 if ((flags & kDumpClassFullDetail) == 0) {
730 os << PrettyClass(this);
731 if ((flags & kDumpClassClassLoader) != 0) {
732 os << ' ' << GetClassLoader();
733 }
734 if ((flags & kDumpClassInitialized) != 0) {
735 os << ' ' << GetStatus();
736 }
Elliott Hughese0918552011-10-28 17:18:29 -0700737 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700738 return;
739 }
740
741 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800742 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700743 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800744 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700745 os << " objectSize=" << SizeOf() << " "
746 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
747 os << StringPrintf(" access=0x%04x.%04x\n",
748 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
749 if (super != NULL) {
750 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
751 }
752 if (IsArrayClass()) {
753 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
754 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800755 if (kh.NumInterfaces() > 0) {
756 os << " interfaces (" << kh.NumInterfaces() << "):\n";
757 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
758 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700759 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800760 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700761 }
762 }
763 os << " vtable (" << NumVirtualMethods() << " entries, "
764 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
765 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800766 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700767 }
768 os << " direct methods (" << NumDirectMethods() << " entries):\n";
769 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800770 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700771 }
772 if (NumStaticFields() > 0) {
773 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700774 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700775 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800776 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700777 }
778 } else {
779 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700780 }
781 }
782 if (NumInstanceFields() > 0) {
783 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700784 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700785 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800786 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700787 }
788 } else {
789 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700790 }
791 }
792}
793
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700794void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
795 if (new_reference_offsets != CLASS_WALK_SUPER) {
796 // Sanity check that the number of bits set in the reference offset bitmap
797 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800798 size_t count = 0;
799 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
800 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700801 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800802 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700803 }
804 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
805 new_reference_offsets, false);
806}
807
808void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
809 if (new_reference_offsets != CLASS_WALK_SUPER) {
810 // Sanity check that the number of bits set in the reference offset bitmap
811 // agrees with the number of references
812 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
813 NumReferenceStaticFieldsDuringLinking());
814 }
815 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
816 new_reference_offsets, false);
817}
818
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700819bool Class::Implements(const Class* klass) const {
820 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700821 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700822 // All interfaces implemented directly and by our superclass, and
823 // recursively all super-interfaces of those interfaces, are listed
824 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700825 int32_t iftable_count = GetIfTableCount();
826 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
827 for (int32_t i = 0; i < iftable_count; i++) {
828 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700829 return true;
830 }
831 }
832 return false;
833}
834
Elliott Hughese84278b2012-03-22 10:06:53 -0700835// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700836// are array classes.
837//
838// Consider an array class, e.g. Y[][], where Y is a subclass of X.
839// Y[][] = Y[][] --> true (identity)
840// X[][] = Y[][] --> true (element superclass)
841// Y = Y[][] --> false
842// Y[] = Y[][] --> false
843// Object = Y[][] --> true (everything is an object)
844// Object[] = Y[][] --> true
845// Object[][] = Y[][] --> true
846// Object[][][] = Y[][] --> false (too many []s)
847// Serializable = Y[][] --> true (all arrays are Serializable)
848// Serializable[] = Y[][] --> true
849// Serializable[][] = Y[][] --> false (unless Y is Serializable)
850//
851// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700852// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700853//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700854bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700855 DCHECK(IsArrayClass()) << PrettyClass(this);
856 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700857 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700858}
859
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700860bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700861 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
862 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700863 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700864 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700865 // src's super should be java_lang_Object, since it is an array.
866 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700867 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
868 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700869 return this == java_lang_Object;
870 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700871 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700872}
873
874bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700875 DCHECK(!IsInterface()) << PrettyClass(this);
876 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700877 const Class* current = this;
878 do {
879 if (current == klass) {
880 return true;
881 }
882 current = current->GetSuperClass();
883 } while (current != NULL);
884 return false;
885}
886
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800887bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700888 size_t i = 0;
889 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
890 ++i;
891 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700892 if (descriptor1.find('/', i) != StringPiece::npos ||
893 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700894 return false;
895 } else {
896 return true;
897 }
898}
899
900bool Class::IsInSamePackage(const Class* that) const {
901 const Class* klass1 = this;
902 const Class* klass2 = that;
903 if (klass1 == klass2) {
904 return true;
905 }
906 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700907 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700908 return false;
909 }
910 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700911 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700912 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700913 }
jeffhao4a801a42011-09-23 13:53:40 -0700914 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700915 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700916 }
917 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800918 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800919 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800920 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800921 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800922 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700923}
924
Elliott Hughesdbb40792011-11-18 17:05:22 -0800925bool Class::IsClassClass() const {
926 Class* java_lang_Class = GetClass()->GetClass();
927 return this == java_lang_Class;
928}
929
930bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800931 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800932}
933
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800934bool Class::IsThrowableClass() const {
935 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
936 return throwable->IsAssignableFrom(this);
937}
938
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800939ClassLoader* Class::GetClassLoader() const {
940 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700941}
942
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700943void Class::SetClassLoader(const ClassLoader* new_cl) {
944 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700946}
947
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800948Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700949 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700950 DCHECK(declaring_class != NULL) << PrettyClass(this);
951 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700952 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700953 int32_t iftable_count = GetIfTableCount();
954 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
955 for (int32_t i = 0; i < iftable_count; i++) {
956 InterfaceEntry* interface_entry = iftable->Get(i);
957 if (interface_entry->GetInterface() == declaring_class) {
958 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700959 }
960 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700961 return NULL;
962}
963
Ian Rogers466bb252011-10-14 03:29:56 -0700964Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700965 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800966 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700967 if (method != NULL) {
968 return method;
969 }
970
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700971 int32_t iftable_count = GetIfTableCount();
972 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
973 for (int32_t i = 0; i < iftable_count; i++) {
974 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700975 if (method != NULL) {
976 return method;
977 }
978 }
979 return NULL;
980}
981
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800982Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
983 // Check the current class before checking the interfaces.
984 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
985 if (method != NULL) {
986 return method;
987 }
988
989 int32_t iftable_count = GetIfTableCount();
990 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
991 for (int32_t i = 0; i < iftable_count; i++) {
992 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
993 if (method != NULL) {
994 return method;
995 }
996 }
997 return NULL;
998}
999
1000
1001Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001002 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001003 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001004 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001005 mh.ChangeMethod(method);
1006 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001007 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001008 }
1009 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001010 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001011}
1012
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001013Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1014 if (GetDexCache() == dex_cache) {
1015 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1016 Method* method = GetDirectMethod(i);
1017 if (method->GetDexMethodIndex() == dex_method_idx) {
1018 return method;
1019 }
1020 }
1021 }
1022 return NULL;
1023}
1024
1025Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1026 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001027 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001028 if (method != NULL) {
1029 return method;
1030 }
1031 }
1032 return NULL;
1033}
1034
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001035Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1036 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1037 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1038 if (method != NULL) {
1039 return method;
1040 }
1041 }
1042 return NULL;
1043}
1044
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001045Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001046 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001047 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001048 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001049 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001050 mh.ChangeMethod(method);
1051 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001052 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001053 }
1054 }
1055 return NULL;
1056}
1057
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001058Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1059 if (GetDexCache() == dex_cache) {
1060 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1061 Method* method = GetVirtualMethod(i);
1062 if (method->GetDexMethodIndex() == dex_method_idx) {
1063 return method;
1064 }
1065 }
1066 }
1067 return NULL;
1068}
1069
Ian Rogers466bb252011-10-14 03:29:56 -07001070Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1071 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1072 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1073 if (method != NULL) {
1074 return method;
1075 }
1076 }
1077 return NULL;
1078}
1079
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001080Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1081 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1082 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1083 if (method != NULL) {
1084 return method;
1085 }
1086 }
1087 return NULL;
1088}
1089
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001090Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 // Is the field in this class?
1092 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001093 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1095 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001096 fh.ChangeField(f);
1097 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001098 return f;
1099 }
1100 }
1101 return NULL;
1102}
1103
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001104Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1105 if (GetDexCache() == dex_cache) {
1106 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1107 Field* f = GetInstanceField(i);
1108 if (f->GetDexFieldIndex() == dex_field_idx) {
1109 return f;
1110 }
1111 }
1112 }
1113 return NULL;
1114}
1115
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001116Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 // Is the field in this class, or any of its superclasses?
1118 // Interfaces are not relevant because they can't contain instance fields.
1119 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001120 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 if (f != NULL) {
1122 return f;
1123 }
1124 }
1125 return NULL;
1126}
1127
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001128Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1129 // Is the field in this class, or any of its superclasses?
1130 // Interfaces are not relevant because they can't contain instance fields.
1131 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1132 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1133 if (f != NULL) {
1134 return f;
1135 }
1136 }
1137 return NULL;
1138}
1139
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001140Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1141 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001142 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001143 for (size_t i = 0; i < NumStaticFields(); ++i) {
1144 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001145 fh.ChangeField(f);
1146 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001147 return f;
1148 }
1149 }
1150 return NULL;
1151}
1152
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001153Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1154 if (dex_cache == GetDexCache()) {
1155 for (size_t i = 0; i < NumStaticFields(); ++i) {
1156 Field* f = GetStaticField(i);
1157 if (f->GetDexFieldIndex() == dex_field_idx) {
1158 return f;
1159 }
1160 }
1161 }
1162 return NULL;
1163}
1164
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001165Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1166 // Is the field in this class (or its interfaces), or any of its
1167 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001168 ClassHelper kh;
1169 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001170 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001171 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001172 if (f != NULL) {
1173 return f;
1174 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001175 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001176 kh.ChangeClass(k);
1177 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1178 Class* interface = kh.GetInterface(i);
1179 f = interface->FindDeclaredStaticField(name, type);
1180 if (f != NULL) {
1181 return f;
1182 }
1183 }
1184 }
1185 return NULL;
1186}
1187
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001188Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1189 ClassHelper kh;
1190 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1191 // Is the field in this class?
1192 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1193 if (f != NULL) {
1194 return f;
1195 }
1196 // Is this field in any of this class' interfaces?
1197 kh.ChangeClass(k);
1198 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1199 Class* interface = kh.GetInterface(i);
1200 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1201 if (f != NULL) {
1202 return f;
1203 }
1204 }
1205 }
1206 return NULL;
1207}
1208
Ian Rogersb067ac22011-12-13 18:05:09 -08001209Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1210 // Find a field using the JLS field resolution order
1211 ClassHelper kh;
1212 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1213 // Is the field in this class?
1214 Field* f = k->FindDeclaredInstanceField(name, type);
1215 if (f != NULL) {
1216 return f;
1217 }
1218 f = k->FindDeclaredStaticField(name, type);
1219 if (f != NULL) {
1220 return f;
1221 }
1222 // Is this field in any of this class' interfaces?
1223 kh.ChangeClass(k);
1224 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1225 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001226 f = interface->FindDeclaredStaticField(name, type);
1227 if (f != NULL) {
1228 return f;
1229 }
1230 }
1231 }
1232 return NULL;
1233}
1234
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001235Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001236 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001237 DCHECK_GE(component_count, 0);
1238 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001239
Ian Rogersa15e67d2012-02-28 13:51:55 -08001240 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001241 size_t data_size = component_count * component_size;
1242 size_t size = header_size + data_size;
1243
1244 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1245 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1246 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1247 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001248 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001249 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001250 return NULL;
1251 }
1252
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001253 Heap* heap = Runtime::Current()->GetHeap();
1254 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001255 if (array != NULL) {
1256 DCHECK(array->IsArrayInstance());
1257 array->SetLength(component_count);
1258 }
1259 return array;
1260}
1261
1262Array* Array::Alloc(Class* array_class, int32_t component_count) {
1263 return Alloc(array_class, component_count, array_class->GetComponentSize());
1264}
1265
Elliott Hughes80609252011-09-23 17:24:51 -07001266bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001267 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001268 "length=%i; index=%i", length_, index);
1269 return false;
1270}
1271
1272bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001273 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001274 "Can't store an element of type %s into an array of type %s",
1275 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1276 return false;
1277}
1278
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001279template<typename T>
1280PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001281 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001282 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1283 return down_cast<PrimitiveArray<T>*>(raw_array);
1284}
1285
1286template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1287
1288// Explicitly instantiate all the primitive array types.
1289template class PrimitiveArray<uint8_t>; // BooleanArray
1290template class PrimitiveArray<int8_t>; // ByteArray
1291template class PrimitiveArray<uint16_t>; // CharArray
1292template class PrimitiveArray<double>; // DoubleArray
1293template class PrimitiveArray<float>; // FloatArray
1294template class PrimitiveArray<int32_t>; // IntArray
1295template class PrimitiveArray<int64_t>; // LongArray
1296template class PrimitiveArray<int16_t>; // ShortArray
1297
Ian Rogers466bb252011-10-14 03:29:56 -07001298// Explicitly instantiate Class[][]
1299template class ObjectArray<ObjectArray<Class> >;
1300
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001301// TODO: get global references for these
1302Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001303
Brian Carlstroma663ea52011-08-19 23:33:41 -07001304void String::SetClass(Class* java_lang_String) {
1305 CHECK(java_lang_String_ == NULL);
1306 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001307 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001308}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001309
Brian Carlstroma663ea52011-08-19 23:33:41 -07001310void String::ResetClass() {
1311 CHECK(java_lang_String_ != NULL);
1312 java_lang_String_ = NULL;
1313}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001314
Brian Carlstromc74255f2011-09-11 22:47:39 -07001315String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001316 return Runtime::Current()->GetInternTable()->InternWeak(this);
1317}
1318
Brian Carlstrom395520e2011-09-25 19:35:00 -07001319int32_t String::GetHashCode() {
1320 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1321 if (result == 0) {
1322 ComputeHashCode();
1323 }
1324 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1325 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1326 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001327 return result;
1328}
1329
1330int32_t String::GetLength() const {
1331 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1332 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1333 return result;
1334}
1335
1336uint16_t String::CharAt(int32_t index) const {
1337 // TODO: do we need this? Equals is the only caller, and could
1338 // bounds check itself.
1339 if (index < 0 || index >= count_) {
1340 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001341 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001342 "length=%i; index=%i", count_, index);
1343 return 0;
1344 }
1345 return GetCharArray()->Get(index + GetOffset());
1346}
1347
1348String* String::AllocFromUtf16(int32_t utf16_length,
1349 const uint16_t* utf16_data_in,
1350 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001351 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001352 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001353 if (string == NULL) {
1354 return NULL;
1355 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001356 // TODO: use 16-bit wide memset variant
1357 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001358 if (array == NULL) {
1359 return NULL;
1360 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001361 for (int i = 0; i < utf16_length; i++) {
1362 array->Set(i, utf16_data_in[i]);
1363 }
1364 if (hash_code != 0) {
1365 string->SetHashCode(hash_code);
1366 } else {
1367 string->ComputeHashCode();
1368 }
1369 return string;
1370}
1371
1372String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001373 if (utf == NULL) {
1374 return NULL;
1375 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 size_t char_count = CountModifiedUtf8Chars(utf);
1377 return AllocFromModifiedUtf8(char_count, utf);
1378}
1379
1380String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1381 const char* utf8_data_in) {
1382 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001383 if (string == NULL) {
1384 return NULL;
1385 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001386 uint16_t* utf16_data_out =
1387 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1388 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1389 string->ComputeHashCode();
1390 return string;
1391}
1392
1393String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001394 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1395 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001396 return NULL;
1397 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001398 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001399}
1400
1401String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001402 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001403 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001404 if (string == NULL) {
1405 return NULL;
1406 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001407 string->SetArray(array);
1408 string->SetCount(array->GetLength());
1409 return string;
1410}
1411
1412bool String::Equals(const String* that) const {
1413 if (this == that) {
1414 // Quick reference equality test
1415 return true;
1416 } else if (that == NULL) {
1417 // Null isn't an instanceof anything
1418 return false;
1419 } else if (this->GetLength() != that->GetLength()) {
1420 // Quick length inequality test
1421 return false;
1422 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001423 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001424 // hash code was already equal
1425 for (int32_t i = 0; i < that->GetLength(); ++i) {
1426 if (this->CharAt(i) != that->CharAt(i)) {
1427 return false;
1428 }
1429 }
1430 return true;
1431 }
1432}
1433
Elliott Hughes5d78d392011-12-13 16:53:05 -08001434bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001435 if (this->GetLength() != that_length) {
1436 return false;
1437 } else {
1438 for (int32_t i = 0; i < that_length; ++i) {
1439 if (this->CharAt(i) != that_chars[that_offset + i]) {
1440 return false;
1441 }
1442 }
1443 return true;
1444 }
1445}
1446
1447bool String::Equals(const char* modified_utf8) const {
1448 for (int32_t i = 0; i < GetLength(); ++i) {
1449 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1450 if (ch == '\0' || ch != CharAt(i)) {
1451 return false;
1452 }
1453 }
1454 return *modified_utf8 == '\0';
1455}
1456
1457bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001458 if (modified_utf8.size() != GetLength()) {
1459 return false;
1460 }
1461 const char* p = modified_utf8.data();
1462 for (int32_t i = 0; i < GetLength(); ++i) {
1463 uint16_t ch = GetUtf16FromUtf8(&p);
1464 if (ch != CharAt(i)) {
1465 return false;
1466 }
1467 }
1468 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001469}
1470
1471// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1472std::string String::ToModifiedUtf8() const {
1473 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001474 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001475 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001476 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1477 return result;
1478}
1479
Ian Rogers1c5eb702012-02-01 09:18:34 -08001480void Throwable::SetCause(Throwable* cause) {
1481 CHECK(cause != NULL);
1482 CHECK(cause != this);
1483 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1484 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1485}
1486
Ian Rogers466bb252011-10-14 03:29:56 -07001487bool Throwable::IsCheckedException() const {
1488 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1489 if (InstanceOf(error)) {
1490 return false;
1491 }
1492 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1493 return !InstanceOf(jlre);
1494}
1495
Ian Rogers9074b992011-10-26 17:41:55 -07001496std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001497 std::string result(PrettyTypeOf(this));
1498 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001499 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001500 if (msg != NULL) {
1501 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001502 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001503 result += "\n";
1504 Object* stack_state = GetStackState();
1505 // check stack state isn't missing or corrupt
1506 if (stack_state != NULL && stack_state->IsObjectArray()) {
1507 // Decode the internal stack trace into the depth and method trace
1508 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1509 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001510 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1511 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001512 for (int32_t i = 0; i < depth; ++i) {
1513 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001514 mh.ChangeMethod(method);
1515 uint32_t native_pc = pc_trace->Get(i);
1516 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1517 const char* source_file = mh.GetDeclaringClassSourceFile();
1518 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1519 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001520 }
Ian Rogers9074b992011-10-26 17:41:55 -07001521 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001522 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001523 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001524 result += "Caused by: ";
1525 result += cause->Dump();
1526 }
Ian Rogers9074b992011-10-26 17:41:55 -07001527 return result;
1528}
1529
Ian Rogers5167c972012-02-03 10:41:20 -08001530
1531Class* Throwable::java_lang_Throwable_ = NULL;
1532
1533void Throwable::SetClass(Class* java_lang_Throwable) {
1534 CHECK(java_lang_Throwable_ == NULL);
1535 CHECK(java_lang_Throwable != NULL);
1536 java_lang_Throwable_ = java_lang_Throwable;
1537}
1538
1539void Throwable::ResetClass() {
1540 CHECK(java_lang_Throwable_ != NULL);
1541 java_lang_Throwable_ = NULL;
1542}
1543
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001544Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1545
1546void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1547 CHECK(java_lang_StackTraceElement_ == NULL);
1548 CHECK(java_lang_StackTraceElement != NULL);
1549 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1550}
1551
1552void StackTraceElement::ResetClass() {
1553 CHECK(java_lang_StackTraceElement_ != NULL);
1554 java_lang_StackTraceElement_ = NULL;
1555}
1556
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001557StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1558 String* method_name,
1559 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001560 int32_t line_number) {
1561 StackTraceElement* trace =
1562 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1563 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1564 const_cast<String*>(declaring_class), false);
1565 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1566 const_cast<String*>(method_name), false);
1567 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1568 const_cast<String*>(file_name), false);
1569 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1570 line_number, false);
1571 return trace;
1572}
1573
Elliott Hughes1f359b02011-07-17 14:27:17 -07001574static const char* kClassStatusNames[] = {
1575 "Error",
1576 "NotReady",
1577 "Idx",
1578 "Loaded",
1579 "Resolved",
1580 "Verifying",
1581 "Verified",
1582 "Initializing",
1583 "Initialized"
1584};
1585std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1586 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001587 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001588 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001589 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001590 }
1591 return os;
1592}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001593
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001594} // namespace art