blob: b728e285d2e2932c59541c166b7127141944bf39 [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());
Ian Rogers0399dde2012-06-06 17:09:28 -0700547 self->AssertThreadSuspensionIsAllowable();
TDYa12785321912012-04-01 15:24:56 -0700548
Ian Rogers0399dde2012-06-06 17:09:28 -0700549 ManagedStack fragment;
550 self->PushManagedStackFragment(&fragment);
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
586 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700587 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700588}
589
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700590bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700591 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800592 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800593 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700594 return native_method != jni_stub;
595}
596
Ian Rogers60db5ab2012-02-20 17:02:00 -0800597void Method::RegisterNative(Thread* self, const void* native_method) {
598 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700599 CHECK(IsNative()) << PrettyMethod(this);
600 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700601#if defined(ART_USE_LLVM_COMPILER)
602 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
603 native_method, false);
604#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800605 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
606 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
607 native_method, false);
608 } else {
609 // We've been asked to associate this method with the given native method but are working
610 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
611 // the native method to runtime support and store the target somewhere runtime support will
612 // find it.
613#if defined(__arm__)
614 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
615 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
616#else
617 UNIMPLEMENTED(FATAL);
618#endif
619 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
620 reinterpret_cast<const uint8_t*>(native_method), false);
621 }
TDYa12726467572012-04-17 20:51:22 -0700622#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700623}
624
Ian Rogers19846512012-02-24 11:42:47 -0800625void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700626 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700627 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800628 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700629}
630
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700631void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700632 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
633 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700634 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800635 if (new_status == kStatusError) {
636 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
637
638 // stash current exception
639 Thread* self = Thread::Current();
640 SirtRef<Throwable> exception(self->GetException());
641 CHECK(exception.get() != NULL);
642
643 // clear exception to call FindSystemClass
644 self->ClearException();
645 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
646 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
647 CHECK(!self->IsExceptionPending());
648
649 // only verification errors, not initialization problems, should set a verify error.
650 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
651 Class* exception_class = exception->GetClass();
652 if (!eiie_class->IsAssignableFrom(exception_class)) {
653 SetVerifyErrorClass(exception_class);
654 }
655
656 // restore exception
657 self->SetException(exception.get());
658 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700659 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700660}
661
662DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700663 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700664}
665
666void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700667 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700668}
669
Brian Carlstrom1f870082011-08-23 16:02:11 -0700670Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700671 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700672 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500673 // TODO: decide whether we want this check. It currently fails during bootstrap.
674 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700675 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800676 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700677}
678
Ian Rogers0571d352011-11-03 19:51:38 -0700679void Class::SetClassSize(size_t new_class_size) {
680 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
681 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
682}
683
Ian Rogersd418eda2012-01-30 12:14:28 -0800684// Return the class' name. The exact format is bizarre, but it's the specified behavior for
685// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
686// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
687// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
688String* Class::ComputeName() {
689 String* name = GetName();
690 if (name != NULL) {
691 return name;
692 }
693 std::string descriptor(ClassHelper(this).GetDescriptor());
694 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
695 // The descriptor indicates that this is the class for
696 // a primitive type; special-case the return value.
697 const char* c_name = NULL;
698 switch (descriptor[0]) {
699 case 'Z': c_name = "boolean"; break;
700 case 'B': c_name = "byte"; break;
701 case 'C': c_name = "char"; break;
702 case 'S': c_name = "short"; break;
703 case 'I': c_name = "int"; break;
704 case 'J': c_name = "long"; break;
705 case 'F': c_name = "float"; break;
706 case 'D': c_name = "double"; break;
707 case 'V': c_name = "void"; break;
708 default:
709 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
710 }
711 name = String::AllocFromModifiedUtf8(c_name);
712 } else {
713 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
714 // components.
715 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
716 descriptor.erase(0, 1);
717 descriptor.erase(descriptor.size() - 1);
718 }
719 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
720 name = String::AllocFromModifiedUtf8(descriptor.c_str());
721 }
722 SetName(name);
723 return name;
724}
725
Elliott Hughes4681c802011-09-25 18:04:37 -0700726void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700727 if ((flags & kDumpClassFullDetail) == 0) {
728 os << PrettyClass(this);
729 if ((flags & kDumpClassClassLoader) != 0) {
730 os << ' ' << GetClassLoader();
731 }
732 if ((flags & kDumpClassInitialized) != 0) {
733 os << ' ' << GetStatus();
734 }
Elliott Hughese0918552011-10-28 17:18:29 -0700735 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700736 return;
737 }
738
739 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800740 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700741 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800742 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700743 os << " objectSize=" << SizeOf() << " "
744 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
745 os << StringPrintf(" access=0x%04x.%04x\n",
746 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
747 if (super != NULL) {
748 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
749 }
750 if (IsArrayClass()) {
751 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
752 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700753 if (kh.NumDirectInterfaces() > 0) {
754 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
755 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
756 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700757 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800758 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700759 }
760 }
761 os << " vtable (" << NumVirtualMethods() << " entries, "
762 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
763 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800764 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700765 }
766 os << " direct methods (" << NumDirectMethods() << " entries):\n";
767 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800768 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700769 }
770 if (NumStaticFields() > 0) {
771 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700772 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700773 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800774 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700775 }
776 } else {
777 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700778 }
779 }
780 if (NumInstanceFields() > 0) {
781 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700782 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700783 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800784 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700785 }
786 } else {
787 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700788 }
789 }
790}
791
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700792void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
793 if (new_reference_offsets != CLASS_WALK_SUPER) {
794 // Sanity check that the number of bits set in the reference offset bitmap
795 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800796 size_t count = 0;
797 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
798 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700799 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800800 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700801 }
802 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
803 new_reference_offsets, false);
804}
805
806void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
807 if (new_reference_offsets != CLASS_WALK_SUPER) {
808 // Sanity check that the number of bits set in the reference offset bitmap
809 // agrees with the number of references
810 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
811 NumReferenceStaticFieldsDuringLinking());
812 }
813 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
814 new_reference_offsets, false);
815}
816
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700817bool Class::Implements(const Class* klass) const {
818 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700819 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700820 // All interfaces implemented directly and by our superclass, and
821 // recursively all super-interfaces of those interfaces, are listed
822 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700823 int32_t iftable_count = GetIfTableCount();
824 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
825 for (int32_t i = 0; i < iftable_count; i++) {
826 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700827 return true;
828 }
829 }
830 return false;
831}
832
Elliott Hughese84278b2012-03-22 10:06:53 -0700833// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700834// are array classes.
835//
836// Consider an array class, e.g. Y[][], where Y is a subclass of X.
837// Y[][] = Y[][] --> true (identity)
838// X[][] = Y[][] --> true (element superclass)
839// Y = Y[][] --> false
840// Y[] = Y[][] --> false
841// Object = Y[][] --> true (everything is an object)
842// Object[] = Y[][] --> true
843// Object[][] = Y[][] --> true
844// Object[][][] = Y[][] --> false (too many []s)
845// Serializable = Y[][] --> true (all arrays are Serializable)
846// Serializable[] = Y[][] --> true
847// Serializable[][] = Y[][] --> false (unless Y is Serializable)
848//
849// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700850// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700851//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700852bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700853 DCHECK(IsArrayClass()) << PrettyClass(this);
854 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700855 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700856}
857
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700858bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700859 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
860 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700861 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700862 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700863 // src's super should be java_lang_Object, since it is an array.
864 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700865 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
866 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700867 return this == java_lang_Object;
868 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700869 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700870}
871
872bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700873 DCHECK(!IsInterface()) << PrettyClass(this);
874 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700875 const Class* current = this;
876 do {
877 if (current == klass) {
878 return true;
879 }
880 current = current->GetSuperClass();
881 } while (current != NULL);
882 return false;
883}
884
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800885bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700886 size_t i = 0;
887 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
888 ++i;
889 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700890 if (descriptor1.find('/', i) != StringPiece::npos ||
891 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700892 return false;
893 } else {
894 return true;
895 }
896}
897
898bool Class::IsInSamePackage(const Class* that) const {
899 const Class* klass1 = this;
900 const Class* klass2 = that;
901 if (klass1 == klass2) {
902 return true;
903 }
904 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700905 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700906 return false;
907 }
908 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700909 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700910 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700911 }
jeffhao4a801a42011-09-23 13:53:40 -0700912 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700913 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700914 }
915 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800916 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800917 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800918 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800919 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800920 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700921}
922
Elliott Hughesdbb40792011-11-18 17:05:22 -0800923bool Class::IsClassClass() const {
924 Class* java_lang_Class = GetClass()->GetClass();
925 return this == java_lang_Class;
926}
927
928bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800929 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800930}
931
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800932bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700933 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800934}
935
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800936ClassLoader* Class::GetClassLoader() const {
937 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700938}
939
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700940void Class::SetClassLoader(const ClassLoader* new_cl) {
941 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700943}
944
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800945Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700946 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700947 DCHECK(declaring_class != NULL) << PrettyClass(this);
948 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700949 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700950 int32_t iftable_count = GetIfTableCount();
951 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
952 for (int32_t i = 0; i < iftable_count; i++) {
953 InterfaceEntry* interface_entry = iftable->Get(i);
954 if (interface_entry->GetInterface() == declaring_class) {
955 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700956 }
957 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700958 return NULL;
959}
960
Ian Rogers466bb252011-10-14 03:29:56 -0700961Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700962 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800963 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700964 if (method != NULL) {
965 return method;
966 }
967
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700968 int32_t iftable_count = GetIfTableCount();
969 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
970 for (int32_t i = 0; i < iftable_count; i++) {
971 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700972 if (method != NULL) {
973 return method;
974 }
975 }
976 return NULL;
977}
978
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800979Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
980 // Check the current class before checking the interfaces.
981 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
982 if (method != NULL) {
983 return method;
984 }
985
986 int32_t iftable_count = GetIfTableCount();
987 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
988 for (int32_t i = 0; i < iftable_count; i++) {
989 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
990 if (method != NULL) {
991 return method;
992 }
993 }
994 return NULL;
995}
996
997
998Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800999 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001000 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001001 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001002 mh.ChangeMethod(method);
1003 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001004 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001005 }
1006 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001007 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001008}
1009
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001010Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1011 if (GetDexCache() == dex_cache) {
1012 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1013 Method* method = GetDirectMethod(i);
1014 if (method->GetDexMethodIndex() == dex_method_idx) {
1015 return method;
1016 }
1017 }
1018 }
1019 return NULL;
1020}
1021
1022Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1023 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001024 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001025 if (method != NULL) {
1026 return method;
1027 }
1028 }
1029 return NULL;
1030}
1031
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001032Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1033 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1034 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1035 if (method != NULL) {
1036 return method;
1037 }
1038 }
1039 return NULL;
1040}
1041
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001042Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001043 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001044 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001045 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001046 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001047 mh.ChangeMethod(method);
1048 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001049 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001050 }
1051 }
1052 return NULL;
1053}
1054
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001055Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1056 if (GetDexCache() == dex_cache) {
1057 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1058 Method* method = GetVirtualMethod(i);
1059 if (method->GetDexMethodIndex() == dex_method_idx) {
1060 return method;
1061 }
1062 }
1063 }
1064 return NULL;
1065}
1066
Ian Rogers466bb252011-10-14 03:29:56 -07001067Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1068 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1069 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1070 if (method != NULL) {
1071 return method;
1072 }
1073 }
1074 return NULL;
1075}
1076
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001077Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1078 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1079 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1080 if (method != NULL) {
1081 return method;
1082 }
1083 }
1084 return NULL;
1085}
1086
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001087Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 // Is the field in this class?
1089 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001090 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1092 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001093 fh.ChangeField(f);
1094 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001095 return f;
1096 }
1097 }
1098 return NULL;
1099}
1100
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001101Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1102 if (GetDexCache() == dex_cache) {
1103 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1104 Field* f = GetInstanceField(i);
1105 if (f->GetDexFieldIndex() == dex_field_idx) {
1106 return f;
1107 }
1108 }
1109 }
1110 return NULL;
1111}
1112
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001113Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 // Is the field in this class, or any of its superclasses?
1115 // Interfaces are not relevant because they can't contain instance fields.
1116 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001117 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 if (f != NULL) {
1119 return f;
1120 }
1121 }
1122 return NULL;
1123}
1124
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001125Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1126 // Is the field in this class, or any of its superclasses?
1127 // Interfaces are not relevant because they can't contain instance fields.
1128 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1129 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1130 if (f != NULL) {
1131 return f;
1132 }
1133 }
1134 return NULL;
1135}
1136
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001137Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1138 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001139 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001140 for (size_t i = 0; i < NumStaticFields(); ++i) {
1141 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001142 fh.ChangeField(f);
1143 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001144 return f;
1145 }
1146 }
1147 return NULL;
1148}
1149
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001150Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1151 if (dex_cache == GetDexCache()) {
1152 for (size_t i = 0; i < NumStaticFields(); ++i) {
1153 Field* f = GetStaticField(i);
1154 if (f->GetDexFieldIndex() == dex_field_idx) {
1155 return f;
1156 }
1157 }
1158 }
1159 return NULL;
1160}
1161
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001162Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1163 // Is the field in this class (or its interfaces), or any of its
1164 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001165 ClassHelper kh;
1166 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001167 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001168 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001169 if (f != NULL) {
1170 return f;
1171 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001172 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001173 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001174 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1175 Class* interface = kh.GetDirectInterface(i);
1176 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001177 if (f != NULL) {
1178 return f;
1179 }
1180 }
1181 }
1182 return NULL;
1183}
1184
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001185Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1186 ClassHelper kh;
1187 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1188 // Is the field in this class?
1189 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1190 if (f != NULL) {
1191 return f;
1192 }
1193 // Is this field in any of this class' interfaces?
1194 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001195 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1196 Class* interface = kh.GetDirectInterface(i);
1197 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001198 if (f != NULL) {
1199 return f;
1200 }
1201 }
1202 }
1203 return NULL;
1204}
1205
Ian Rogersb067ac22011-12-13 18:05:09 -08001206Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1207 // Find a field using the JLS field resolution order
1208 ClassHelper kh;
1209 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1210 // Is the field in this class?
1211 Field* f = k->FindDeclaredInstanceField(name, type);
1212 if (f != NULL) {
1213 return f;
1214 }
1215 f = k->FindDeclaredStaticField(name, type);
1216 if (f != NULL) {
1217 return f;
1218 }
1219 // Is this field in any of this class' interfaces?
1220 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001221 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1222 Class* interface = kh.GetDirectInterface(i);
1223 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001224 if (f != NULL) {
1225 return f;
1226 }
1227 }
1228 }
1229 return NULL;
1230}
1231
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001232Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001233 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001234 DCHECK_GE(component_count, 0);
1235 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001236
Ian Rogersa15e67d2012-02-28 13:51:55 -08001237 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001238 size_t data_size = component_count * component_size;
1239 size_t size = header_size + data_size;
1240
1241 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1242 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1243 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1244 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001245 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001246 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001247 return NULL;
1248 }
1249
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001250 Heap* heap = Runtime::Current()->GetHeap();
1251 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001252 if (array != NULL) {
1253 DCHECK(array->IsArrayInstance());
1254 array->SetLength(component_count);
1255 }
1256 return array;
1257}
1258
1259Array* Array::Alloc(Class* array_class, int32_t component_count) {
1260 return Alloc(array_class, component_count, array_class->GetComponentSize());
1261}
1262
Elliott Hughes80609252011-09-23 17:24:51 -07001263bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001264 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001265 "length=%i; index=%i", length_, index);
1266 return false;
1267}
1268
1269bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001270 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001271 "Can't store an element of type %s into an array of type %s",
1272 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1273 return false;
1274}
1275
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001276template<typename T>
1277PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001278 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001279 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1280 return down_cast<PrimitiveArray<T>*>(raw_array);
1281}
1282
1283template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1284
1285// Explicitly instantiate all the primitive array types.
1286template class PrimitiveArray<uint8_t>; // BooleanArray
1287template class PrimitiveArray<int8_t>; // ByteArray
1288template class PrimitiveArray<uint16_t>; // CharArray
1289template class PrimitiveArray<double>; // DoubleArray
1290template class PrimitiveArray<float>; // FloatArray
1291template class PrimitiveArray<int32_t>; // IntArray
1292template class PrimitiveArray<int64_t>; // LongArray
1293template class PrimitiveArray<int16_t>; // ShortArray
1294
Ian Rogers466bb252011-10-14 03:29:56 -07001295// Explicitly instantiate Class[][]
1296template class ObjectArray<ObjectArray<Class> >;
1297
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001298// TODO: get global references for these
1299Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001300
Brian Carlstroma663ea52011-08-19 23:33:41 -07001301void String::SetClass(Class* java_lang_String) {
1302 CHECK(java_lang_String_ == NULL);
1303 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001304 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001305}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001306
Brian Carlstroma663ea52011-08-19 23:33:41 -07001307void String::ResetClass() {
1308 CHECK(java_lang_String_ != NULL);
1309 java_lang_String_ = NULL;
1310}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001311
Brian Carlstromc74255f2011-09-11 22:47:39 -07001312String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001313 return Runtime::Current()->GetInternTable()->InternWeak(this);
1314}
1315
Brian Carlstrom395520e2011-09-25 19:35:00 -07001316int32_t String::GetHashCode() {
1317 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1318 if (result == 0) {
1319 ComputeHashCode();
1320 }
1321 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1322 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1323 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001324 return result;
1325}
1326
1327int32_t String::GetLength() const {
1328 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1329 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1330 return result;
1331}
1332
1333uint16_t String::CharAt(int32_t index) const {
1334 // TODO: do we need this? Equals is the only caller, and could
1335 // bounds check itself.
1336 if (index < 0 || index >= count_) {
1337 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001338 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001339 "length=%i; index=%i", count_, index);
1340 return 0;
1341 }
1342 return GetCharArray()->Get(index + GetOffset());
1343}
1344
1345String* String::AllocFromUtf16(int32_t utf16_length,
1346 const uint16_t* utf16_data_in,
1347 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001348 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001349 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001350 if (string == NULL) {
1351 return NULL;
1352 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001353 // TODO: use 16-bit wide memset variant
1354 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001355 if (array == NULL) {
1356 return NULL;
1357 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001358 for (int i = 0; i < utf16_length; i++) {
1359 array->Set(i, utf16_data_in[i]);
1360 }
1361 if (hash_code != 0) {
1362 string->SetHashCode(hash_code);
1363 } else {
1364 string->ComputeHashCode();
1365 }
1366 return string;
1367}
1368
1369String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001370 if (utf == NULL) {
1371 return NULL;
1372 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001373 size_t char_count = CountModifiedUtf8Chars(utf);
1374 return AllocFromModifiedUtf8(char_count, utf);
1375}
1376
1377String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1378 const char* utf8_data_in) {
1379 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001380 if (string == NULL) {
1381 return NULL;
1382 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001383 uint16_t* utf16_data_out =
1384 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1385 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1386 string->ComputeHashCode();
1387 return string;
1388}
1389
1390String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001391 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1392 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001393 return NULL;
1394 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001395 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001396}
1397
1398String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001399 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001400 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001401 if (string == NULL) {
1402 return NULL;
1403 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001404 string->SetArray(array);
1405 string->SetCount(array->GetLength());
1406 return string;
1407}
1408
1409bool String::Equals(const String* that) const {
1410 if (this == that) {
1411 // Quick reference equality test
1412 return true;
1413 } else if (that == NULL) {
1414 // Null isn't an instanceof anything
1415 return false;
1416 } else if (this->GetLength() != that->GetLength()) {
1417 // Quick length inequality test
1418 return false;
1419 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001420 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001421 // hash code was already equal
1422 for (int32_t i = 0; i < that->GetLength(); ++i) {
1423 if (this->CharAt(i) != that->CharAt(i)) {
1424 return false;
1425 }
1426 }
1427 return true;
1428 }
1429}
1430
Elliott Hughes5d78d392011-12-13 16:53:05 -08001431bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001432 if (this->GetLength() != that_length) {
1433 return false;
1434 } else {
1435 for (int32_t i = 0; i < that_length; ++i) {
1436 if (this->CharAt(i) != that_chars[that_offset + i]) {
1437 return false;
1438 }
1439 }
1440 return true;
1441 }
1442}
1443
1444bool String::Equals(const char* modified_utf8) const {
1445 for (int32_t i = 0; i < GetLength(); ++i) {
1446 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1447 if (ch == '\0' || ch != CharAt(i)) {
1448 return false;
1449 }
1450 }
1451 return *modified_utf8 == '\0';
1452}
1453
1454bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001455 if (modified_utf8.size() != GetLength()) {
1456 return false;
1457 }
1458 const char* p = modified_utf8.data();
1459 for (int32_t i = 0; i < GetLength(); ++i) {
1460 uint16_t ch = GetUtf16FromUtf8(&p);
1461 if (ch != CharAt(i)) {
1462 return false;
1463 }
1464 }
1465 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001466}
1467
1468// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1469std::string String::ToModifiedUtf8() const {
1470 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001471 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001472 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001473 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1474 return result;
1475}
1476
Ian Rogers1c5eb702012-02-01 09:18:34 -08001477void Throwable::SetCause(Throwable* cause) {
1478 CHECK(cause != NULL);
1479 CHECK(cause != this);
1480 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1481 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1482}
1483
Ian Rogers466bb252011-10-14 03:29:56 -07001484bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001485 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001486 return false;
1487 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001488 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001489}
1490
Ian Rogers9074b992011-10-26 17:41:55 -07001491std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001492 std::string result(PrettyTypeOf(this));
1493 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001494 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001495 if (msg != NULL) {
1496 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001497 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001498 result += "\n";
1499 Object* stack_state = GetStackState();
1500 // check stack state isn't missing or corrupt
1501 if (stack_state != NULL && stack_state->IsObjectArray()) {
1502 // Decode the internal stack trace into the depth and method trace
1503 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1504 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001505 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1506 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001507 for (int32_t i = 0; i < depth; ++i) {
1508 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001509 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001510 uint32_t dex_pc = pc_trace->Get(i);
1511 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001512 const char* source_file = mh.GetDeclaringClassSourceFile();
1513 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1514 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001515 }
Ian Rogers9074b992011-10-26 17:41:55 -07001516 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001517 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001518 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001519 result += "Caused by: ";
1520 result += cause->Dump();
1521 }
Ian Rogers9074b992011-10-26 17:41:55 -07001522 return result;
1523}
1524
Ian Rogers5167c972012-02-03 10:41:20 -08001525
1526Class* Throwable::java_lang_Throwable_ = NULL;
1527
1528void Throwable::SetClass(Class* java_lang_Throwable) {
1529 CHECK(java_lang_Throwable_ == NULL);
1530 CHECK(java_lang_Throwable != NULL);
1531 java_lang_Throwable_ = java_lang_Throwable;
1532}
1533
1534void Throwable::ResetClass() {
1535 CHECK(java_lang_Throwable_ != NULL);
1536 java_lang_Throwable_ = NULL;
1537}
1538
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001539Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1540
1541void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1542 CHECK(java_lang_StackTraceElement_ == NULL);
1543 CHECK(java_lang_StackTraceElement != NULL);
1544 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1545}
1546
1547void StackTraceElement::ResetClass() {
1548 CHECK(java_lang_StackTraceElement_ != NULL);
1549 java_lang_StackTraceElement_ = NULL;
1550}
1551
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001552StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1553 String* method_name,
1554 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001555 int32_t line_number) {
1556 StackTraceElement* trace =
1557 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1558 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1559 const_cast<String*>(declaring_class), false);
1560 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1561 const_cast<String*>(method_name), false);
1562 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1563 const_cast<String*>(file_name), false);
1564 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1565 line_number, false);
1566 return trace;
1567}
1568
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001569} // namespace art