blob: 6dda684cdf24fc2fae99d63de1786fb875d2d39d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro3ee755d2011-06-28 12:11:04 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020
Ian Rogersdf20fe02011-07-20 20:34:16 -070021#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include <string>
24#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070028#include "dex_cache.h"
29#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070034#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Ian Rogers60db5ab2012-02-20 17:02:00 -080037#include "runtime_support.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070038#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070039#include "utils.h"
Elliott Hughesa4f94742012-05-29 16:28:38 -070040#include "well_known_classes.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070041
Logan Chienfca7e872011-12-20 20:08:22 +080042#if defined(ART_USE_LLVM_COMPILER)
43#include "compiler_llvm/inferred_reg_category_map.h"
TDYa12785321912012-04-01 15:24:56 -070044#include "compiler_llvm/runtime_support_llvm.h"
Logan Chienfca7e872011-12-20 20:08:22 +080045using art::compiler_llvm::InferredRegCategoryMap;
46#endif
47
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048namespace art {
49
Elliott Hughesdbb40792011-11-18 17:05:22 -080050String* Object::AsString() {
51 DCHECK(GetClass()->IsStringClass());
52 return down_cast<String*>(this);
53}
54
Elliott Hughes081be7f2011-09-18 16:50:26 -070055Object* Object::Clone() {
56 Class* c = GetClass();
57 DCHECK(!c->IsClassClass());
58
59 // Object::SizeOf gets the right size even if we're an array.
60 // Using c->AllocObject() here would be wrong.
61 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080062 Heap* heap = Runtime::Current()->GetHeap();
63 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070064 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070065 return NULL;
66 }
67
68 // Copy instance data. We assume memcpy copies by words.
69 // TODO: expose and use move32.
70 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070071 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070072 size_t offset = sizeof(Object);
73 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
74
Elliott Hughes20cde902011-10-04 17:37:27 -070075 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080076 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070077 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070078
Brian Carlstrom40381fb2011-10-19 14:13:40 -070079 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070080}
81
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070082uint32_t Object::GetThinLockId() {
83 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070084}
85
86void Object::MonitorEnter(Thread* thread) {
87 Monitor::MonitorEnter(thread, this);
88}
89
Ian Rogersff1ed472011-09-20 13:46:24 -070090bool Object::MonitorExit(Thread* thread) {
91 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070092}
93
94void Object::Notify() {
95 Monitor::Notify(Thread::Current(), this);
96}
97
98void Object::NotifyAll() {
99 Monitor::NotifyAll(Thread::Current(), this);
100}
101
102void Object::Wait(int64_t ms, int32_t ns) {
103 Monitor::Wait(Thread::Current(), this, ms, ns, true);
104}
105
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700106// TODO: get global references for these
107Class* Field::java_lang_reflect_Field_ = NULL;
108
109void Field::SetClass(Class* java_lang_reflect_Field) {
110 CHECK(java_lang_reflect_Field_ == NULL);
111 CHECK(java_lang_reflect_Field != NULL);
112 java_lang_reflect_Field_ = java_lang_reflect_Field;
113}
114
115void Field::ResetClass() {
116 CHECK(java_lang_reflect_Field_ != NULL);
117 java_lang_reflect_Field_ = NULL;
118}
119
Ian Rogers0571d352011-11-03 19:51:38 -0700120void Field::SetOffset(MemberOffset num_bytes) {
121 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800122#if 0 // TODO enable later in boot and under !NDEBUG
123 FieldHelper fh(this);
124 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700125 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
126 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
127 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700129 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
130}
131
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700132uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700133 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700134 if (IsStatic()) {
135 object = declaring_class_;
136 }
137 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700138}
139
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700140void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700141 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700142 if (IsStatic()) {
143 object = declaring_class_;
144 }
145 object->SetField32(GetOffset(), new_value, IsVolatile());
146}
147
148uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700149 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150 if (IsStatic()) {
151 object = declaring_class_;
152 }
153 return object->GetField64(GetOffset(), IsVolatile());
154}
155
156void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700157 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700158 if (IsStatic()) {
159 object = declaring_class_;
160 }
161 object->SetField64(GetOffset(), new_value, IsVolatile());
162}
163
164Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700165 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 if (IsStatic()) {
167 object = declaring_class_;
168 }
169 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
170}
171
172void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700173 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700174 if (IsStatic()) {
175 object = declaring_class_;
176 }
177 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
178}
179
180bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800181 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
182 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700183 return Get32(object);
184}
185
186void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800187 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
188 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189 Set32(object, z);
190}
191
192int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
194 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 return Get32(object);
196}
197
198void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
200 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 Set32(object, b);
202}
203
204uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
206 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 return Get32(object);
208}
209
210void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
212 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 Set32(object, c);
214}
215
Ian Rogers466bb252011-10-14 03:29:56 -0700216int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
218 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 return Get32(object);
220}
221
Ian Rogers466bb252011-10-14 03:29:56 -0700222void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
224 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 Set32(object, s);
226}
227
228int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800229 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
230 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 return Get32(object);
232}
233
234void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800235 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
236 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 Set32(object, i);
238}
239
240int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800241 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
242 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 return Get64(object);
244}
245
246void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
248 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 Set64(object, j);
250}
251
Elliott Hughes1d878f32012-04-11 15:17:54 -0700252union Bits {
253 jdouble d;
254 jfloat f;
255 jint i;
256 jlong j;
257};
258
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
261 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700262 Bits bits;
263 bits.i = Get32(object);
264 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265}
266
267void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
269 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700270 Bits bits;
271 bits.f = f;
272 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273}
274
275double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
277 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700278 Bits bits;
279 bits.j = Get64(object);
280 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281}
282
283void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800284 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
285 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700286 Bits bits;
287 bits.d = d;
288 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289}
290
291Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
293 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 return GetObj(object);
295}
296
297void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
299 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 SetObj(object, l);
301}
302
303// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700304Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305Class* Method::java_lang_reflect_Method_ = NULL;
306
Elliott Hughes80609252011-09-23 17:24:51 -0700307void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
308 CHECK(java_lang_reflect_Constructor_ == NULL);
309 CHECK(java_lang_reflect_Constructor != NULL);
310 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
311
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 CHECK(java_lang_reflect_Method_ == NULL);
313 CHECK(java_lang_reflect_Method != NULL);
314 java_lang_reflect_Method_ = java_lang_reflect_Method;
315}
316
Elliott Hughes80609252011-09-23 17:24:51 -0700317void Method::ResetClasses() {
318 CHECK(java_lang_reflect_Constructor_ != NULL);
319 java_lang_reflect_Constructor_ = NULL;
320
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 CHECK(java_lang_reflect_Method_ != NULL);
322 java_lang_reflect_Method_ = NULL;
323}
324
Elliott Hughes418d20f2011-09-22 14:00:39 -0700325Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
326 if (*p == '[') {
327 // Something like "[[[Ljava/lang/String;".
328 const char* start = p;
329 while (*p == '[') {
330 ++p;
331 }
332 if (*p == 'L') {
333 while (*p != ';') {
334 ++p;
335 }
336 }
337 ++p; // Either the ';' or the primitive type.
338
Brian Carlstromaded5f72011-10-07 17:15:04 -0700339 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800340 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700341 } else if (*p == 'L') {
342 const char* start = p;
343 while (*p != ';') {
344 ++p;
345 }
346 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800347 std::string descriptor(start, (p - start));
348 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700349 } else {
350 return class_linker->FindPrimitiveClass(*p++);
351 }
352}
353
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700354ObjectArray<String>* Method::GetDexCacheStrings() const {
355 return GetFieldObject<ObjectArray<String>*>(
356 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
357}
358
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700359void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
360 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
361 new_dex_cache_strings, false);
362}
363
Ian Rogers19846512012-02-24 11:42:47 -0800364ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
365 return GetFieldObject<ObjectArray<Method>*>(
366 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
367}
368
369void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
370 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
371 new_dex_cache_methods, false);
372}
373
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
375 return GetFieldObject<ObjectArray<Class>*>(
376 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
377}
378
379void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
380 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
381 new_dex_cache_classes, false);
382}
383
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
385 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
386 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
387 false);
388}
389
390void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700391 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393}
394
395size_t Method::NumArgRegisters(const StringPiece& shorty) {
396 CHECK_LE(1, shorty.length());
397 uint32_t num_registers = 0;
398 for (int i = 1; i < shorty.length(); ++i) {
399 char ch = shorty[i];
400 if (ch == 'D' || ch == 'J') {
401 num_registers += 2;
402 } else {
403 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700404 }
405 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700406 return num_registers;
407}
408
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800409bool Method::IsProxyMethod() const {
410 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700411}
412
Ian Rogers466bb252011-10-14 03:29:56 -0700413Method* Method::FindOverriddenMethod() const {
414 if (IsStatic()) {
415 return NULL;
416 }
417 Class* declaring_class = GetDeclaringClass();
418 Class* super_class = declaring_class->GetSuperClass();
419 uint16_t method_index = GetMethodIndex();
420 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
421 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800422 // Did this method override a super class method? If so load the result from the super class'
423 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700424 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
425 result = super_class_vtable->Get(method_index);
426 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800427 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800428 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800429 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
430 CHECK_EQ(result,
431 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800432 } else {
433 MethodHelper mh(this);
434 MethodHelper interface_mh;
435 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
436 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
437 InterfaceEntry* entry = iftable->Get(i);
438 Class* interface = entry->GetInterface();
439 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
440 Method* interface_method = interface->GetVirtualMethod(j);
441 interface_mh.ChangeMethod(interface_method);
442 if (mh.HasSameNameAndSignature(&interface_mh)) {
443 result = interface_method;
444 break;
445 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 }
447 }
Ian Rogers466bb252011-10-14 03:29:56 -0700448 }
449 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800450#ifndef NDEBUG
451 MethodHelper result_mh(result);
452 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
453#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700454 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455}
456
Elliott Hughes168670b2012-02-29 16:43:26 -0800457static const void* GetOatCode(const Method* m) {
458 Runtime* runtime = Runtime::Current();
459 const void* code = m->GetCode();
460 // Peel off any method tracing trampoline.
461 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
462 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
463 }
464 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800465 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800466 code = runtime->GetClassLinker()->GetOatCodeFor(m);
467 }
468 return code;
469}
470
Ian Rogersbdb03912011-09-14 00:55:44 -0700471uint32_t Method::ToDexPC(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700472#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700473 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700474 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800475 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700476 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700477 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700478 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800479 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700480 uint32_t best_offset = 0;
481 uint32_t best_dex_offset = 0;
482 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700483 uint32_t map_offset = mapping_table[i];
484 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700485 if (map_offset == sought_offset) {
486 best_offset = map_offset;
487 best_dex_offset = map_dex_offset;
488 break;
489 }
490 if (map_offset < sought_offset && map_offset > best_offset) {
491 best_offset = map_offset;
492 best_dex_offset = map_dex_offset;
493 }
494 }
495 return best_dex_offset;
TDYa127c8dc1012012-04-19 07:03:33 -0700496#else
497 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
498 return static_cast<uint32_t>(pc);
499#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700500}
501
502uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700504 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700505 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700506 return 0; // Special no mapping/pc == 0 case
507 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700508 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700509 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700510 uint32_t map_offset = mapping_table[i];
511 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700512 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800513 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700514 }
515 }
516 LOG(FATAL) << "Looking up Dex PC not contained in method";
517 return 0;
518}
519
520uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521 MethodHelper mh(this);
522 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700523 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700524 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
525 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700526 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700527 if (iter_type_idx == DexFile::kDexNoIndex16) {
528 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700529 }
530 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800531 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700532 if (iter_exception_type == NULL) {
533 // The verifier should take care of resolving all exception classes early
534 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800535 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700536 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700537 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700538 }
539 }
540 // Handler not found
541 return DexFile::kDexNoIndex;
542}
543
Elliott Hughes77405792012-03-15 15:22:12 -0700544void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700545 // Push a transition back into managed code onto the linked list in thread.
Elliott Hughes34e06962012-04-09 13:55:55 -0700546 CHECK_EQ(kRunnable, self->GetState());
TDYa12785321912012-04-01 15:24:56 -0700547
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700548#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700549 NativeToManagedRecord record;
550 self->PushNativeToManagedRecord(&record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700551#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700552
553 // Call the invoke stub associated with the method.
554 // Pass everything as arguments.
555 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700556
557 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700558
TDYa12705fe3b62012-04-21 00:28:54 -0700559#if defined(ART_USE_LLVM_COMPILER)
560 if (stub == NULL || !have_executable_code) {
561 Runtime::Current()->GetClassLinker()->LinkOatCodeFor(const_cast<Method*>(this));
562 stub = GetInvokeStub();
563 have_executable_code = (GetCode() != NULL);
564 }
565#endif
566
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500567 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700568 bool log = false;
569 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800570 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
571 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700572 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700573 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700574 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800575 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
576 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700577 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700578 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800579 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
580 PrettyMethod(this).c_str(), GetCode(), stub,
581 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700582 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700583 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700584 }
585 }
586
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700587#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700588 // Pop transition.
589 self->PopNativeToManagedRecord(record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700590#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700591}
592
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700593bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700594 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800595 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800596 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700597 return native_method != jni_stub;
598}
599
Ian Rogers60db5ab2012-02-20 17:02:00 -0800600void Method::RegisterNative(Thread* self, const void* native_method) {
601 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700602 CHECK(IsNative()) << PrettyMethod(this);
603 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700604#if defined(ART_USE_LLVM_COMPILER)
605 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
606 native_method, false);
607#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800608 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
609 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
610 native_method, false);
611 } else {
612 // We've been asked to associate this method with the given native method but are working
613 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
614 // the native method to runtime support and store the target somewhere runtime support will
615 // find it.
616#if defined(__arm__)
617 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
618 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
619#else
620 UNIMPLEMENTED(FATAL);
621#endif
622 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
623 reinterpret_cast<const uint8_t*>(native_method), false);
624 }
TDYa12726467572012-04-17 20:51:22 -0700625#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700626}
627
Ian Rogers19846512012-02-24 11:42:47 -0800628void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700629 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700630 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800631 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700632}
633
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700634void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700635 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
636 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700637 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800638 if (new_status == kStatusError) {
639 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
640
641 // stash current exception
642 Thread* self = Thread::Current();
643 SirtRef<Throwable> exception(self->GetException());
644 CHECK(exception.get() != NULL);
645
646 // clear exception to call FindSystemClass
647 self->ClearException();
648 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
649 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
650 CHECK(!self->IsExceptionPending());
651
652 // only verification errors, not initialization problems, should set a verify error.
653 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
654 Class* exception_class = exception->GetClass();
655 if (!eiie_class->IsAssignableFrom(exception_class)) {
656 SetVerifyErrorClass(exception_class);
657 }
658
659 // restore exception
660 self->SetException(exception.get());
661 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700662 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700663}
664
665DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700666 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700667}
668
669void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700670 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700671}
672
Brian Carlstrom1f870082011-08-23 16:02:11 -0700673Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700674 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700675 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500676 // TODO: decide whether we want this check. It currently fails during bootstrap.
677 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700678 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800679 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700680}
681
Ian Rogers0571d352011-11-03 19:51:38 -0700682void Class::SetClassSize(size_t new_class_size) {
683 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
684 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
685}
686
Ian Rogersd418eda2012-01-30 12:14:28 -0800687// Return the class' name. The exact format is bizarre, but it's the specified behavior for
688// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
689// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
690// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
691String* Class::ComputeName() {
692 String* name = GetName();
693 if (name != NULL) {
694 return name;
695 }
696 std::string descriptor(ClassHelper(this).GetDescriptor());
697 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
698 // The descriptor indicates that this is the class for
699 // a primitive type; special-case the return value.
700 const char* c_name = NULL;
701 switch (descriptor[0]) {
702 case 'Z': c_name = "boolean"; break;
703 case 'B': c_name = "byte"; break;
704 case 'C': c_name = "char"; break;
705 case 'S': c_name = "short"; break;
706 case 'I': c_name = "int"; break;
707 case 'J': c_name = "long"; break;
708 case 'F': c_name = "float"; break;
709 case 'D': c_name = "double"; break;
710 case 'V': c_name = "void"; break;
711 default:
712 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
713 }
714 name = String::AllocFromModifiedUtf8(c_name);
715 } else {
716 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
717 // components.
718 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
719 descriptor.erase(0, 1);
720 descriptor.erase(descriptor.size() - 1);
721 }
722 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
723 name = String::AllocFromModifiedUtf8(descriptor.c_str());
724 }
725 SetName(name);
726 return name;
727}
728
Elliott Hughes4681c802011-09-25 18:04:37 -0700729void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700730 if ((flags & kDumpClassFullDetail) == 0) {
731 os << PrettyClass(this);
732 if ((flags & kDumpClassClassLoader) != 0) {
733 os << ' ' << GetClassLoader();
734 }
735 if ((flags & kDumpClassInitialized) != 0) {
736 os << ' ' << GetStatus();
737 }
Elliott Hughese0918552011-10-28 17:18:29 -0700738 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700739 return;
740 }
741
742 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800743 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700744 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800745 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700746 os << " objectSize=" << SizeOf() << " "
747 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
748 os << StringPrintf(" access=0x%04x.%04x\n",
749 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
750 if (super != NULL) {
751 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
752 }
753 if (IsArrayClass()) {
754 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
755 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800756 if (kh.NumInterfaces() > 0) {
757 os << " interfaces (" << kh.NumInterfaces() << "):\n";
758 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
759 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700760 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800761 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700762 }
763 }
764 os << " vtable (" << NumVirtualMethods() << " entries, "
765 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
766 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800767 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700768 }
769 os << " direct methods (" << NumDirectMethods() << " entries):\n";
770 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800771 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700772 }
773 if (NumStaticFields() > 0) {
774 os << " static fields (" << NumStaticFields() << " 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 < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800777 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(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 if (NumInstanceFields() > 0) {
784 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700785 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700786 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800787 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700788 }
789 } else {
790 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700791 }
792 }
793}
794
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700795void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
796 if (new_reference_offsets != CLASS_WALK_SUPER) {
797 // Sanity check that the number of bits set in the reference offset bitmap
798 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800799 size_t count = 0;
800 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
801 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700802 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800803 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700804 }
805 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
806 new_reference_offsets, false);
807}
808
809void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
810 if (new_reference_offsets != CLASS_WALK_SUPER) {
811 // Sanity check that the number of bits set in the reference offset bitmap
812 // agrees with the number of references
813 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
814 NumReferenceStaticFieldsDuringLinking());
815 }
816 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
817 new_reference_offsets, false);
818}
819
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700820bool Class::Implements(const Class* klass) const {
821 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700822 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700823 // All interfaces implemented directly and by our superclass, and
824 // recursively all super-interfaces of those interfaces, are listed
825 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700826 int32_t iftable_count = GetIfTableCount();
827 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
828 for (int32_t i = 0; i < iftable_count; i++) {
829 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700830 return true;
831 }
832 }
833 return false;
834}
835
Elliott Hughese84278b2012-03-22 10:06:53 -0700836// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700837// are array classes.
838//
839// Consider an array class, e.g. Y[][], where Y is a subclass of X.
840// Y[][] = Y[][] --> true (identity)
841// X[][] = Y[][] --> true (element superclass)
842// Y = Y[][] --> false
843// Y[] = Y[][] --> false
844// Object = Y[][] --> true (everything is an object)
845// Object[] = Y[][] --> true
846// Object[][] = Y[][] --> true
847// Object[][][] = Y[][] --> false (too many []s)
848// Serializable = Y[][] --> true (all arrays are Serializable)
849// Serializable[] = Y[][] --> true
850// Serializable[][] = Y[][] --> false (unless Y is Serializable)
851//
852// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700853// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700854//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700855bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700856 DCHECK(IsArrayClass()) << PrettyClass(this);
857 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700858 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700859}
860
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700861bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700862 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
863 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700864 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700865 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700866 // src's super should be java_lang_Object, since it is an array.
867 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700868 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
869 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700870 return this == java_lang_Object;
871 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700872 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700873}
874
875bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700876 DCHECK(!IsInterface()) << PrettyClass(this);
877 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700878 const Class* current = this;
879 do {
880 if (current == klass) {
881 return true;
882 }
883 current = current->GetSuperClass();
884 } while (current != NULL);
885 return false;
886}
887
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800888bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700889 size_t i = 0;
890 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
891 ++i;
892 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700893 if (descriptor1.find('/', i) != StringPiece::npos ||
894 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700895 return false;
896 } else {
897 return true;
898 }
899}
900
901bool Class::IsInSamePackage(const Class* that) const {
902 const Class* klass1 = this;
903 const Class* klass2 = that;
904 if (klass1 == klass2) {
905 return true;
906 }
907 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700908 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700909 return false;
910 }
911 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700912 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700913 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700914 }
jeffhao4a801a42011-09-23 13:53:40 -0700915 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700916 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700917 }
918 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800919 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800920 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800921 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800922 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800923 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700924}
925
Elliott Hughesdbb40792011-11-18 17:05:22 -0800926bool Class::IsClassClass() const {
927 Class* java_lang_Class = GetClass()->GetClass();
928 return this == java_lang_Class;
929}
930
931bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800932 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800933}
934
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800935bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700936 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800937}
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 {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001488 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001489 return false;
1490 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001491 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001492}
1493
Ian Rogers9074b992011-10-26 17:41:55 -07001494std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001495 std::string result(PrettyTypeOf(this));
1496 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001497 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001498 if (msg != NULL) {
1499 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001500 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001501 result += "\n";
1502 Object* stack_state = GetStackState();
1503 // check stack state isn't missing or corrupt
1504 if (stack_state != NULL && stack_state->IsObjectArray()) {
1505 // Decode the internal stack trace into the depth and method trace
1506 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1507 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001508 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1509 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001510 for (int32_t i = 0; i < depth; ++i) {
1511 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001512 mh.ChangeMethod(method);
1513 uint32_t native_pc = pc_trace->Get(i);
1514 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1515 const char* source_file = mh.GetDeclaringClassSourceFile();
1516 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1517 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001518 }
Ian Rogers9074b992011-10-26 17:41:55 -07001519 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001520 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001521 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001522 result += "Caused by: ";
1523 result += cause->Dump();
1524 }
Ian Rogers9074b992011-10-26 17:41:55 -07001525 return result;
1526}
1527
Ian Rogers5167c972012-02-03 10:41:20 -08001528
1529Class* Throwable::java_lang_Throwable_ = NULL;
1530
1531void Throwable::SetClass(Class* java_lang_Throwable) {
1532 CHECK(java_lang_Throwable_ == NULL);
1533 CHECK(java_lang_Throwable != NULL);
1534 java_lang_Throwable_ = java_lang_Throwable;
1535}
1536
1537void Throwable::ResetClass() {
1538 CHECK(java_lang_Throwable_ != NULL);
1539 java_lang_Throwable_ = NULL;
1540}
1541
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001542Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1543
1544void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1545 CHECK(java_lang_StackTraceElement_ == NULL);
1546 CHECK(java_lang_StackTraceElement != NULL);
1547 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1548}
1549
1550void StackTraceElement::ResetClass() {
1551 CHECK(java_lang_StackTraceElement_ != NULL);
1552 java_lang_StackTraceElement_ = NULL;
1553}
1554
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001555StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1556 String* method_name,
1557 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001558 int32_t line_number) {
1559 StackTraceElement* trace =
1560 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1561 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1562 const_cast<String*>(declaring_class), false);
1563 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1564 const_cast<String*>(method_name), false);
1565 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1566 const_cast<String*>(file_name), false);
1567 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1568 line_number, false);
1569 return trace;
1570}
1571
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001572} // namespace art