Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "object.h" |
| 18 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 19 | #include <string.h> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 20 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 22 | #include <iostream> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <utility> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 26 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 27 | #include "class_loader.h" |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 28 | #include "dex_cache.h" |
| 29 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | #include "globals.h" |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 31 | #include "heap.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 32 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | #include "logging.h" |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 34 | #include "monitor.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 35 | #include "object_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 36 | #include "runtime.h" |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 37 | #include "runtime_support.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 38 | #include "stack.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 39 | #include "utils.h" |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 40 | #include "well_known_classes.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 41 | |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 42 | #if defined(ART_USE_LLVM_COMPILER) |
| 43 | #include "compiler_llvm/inferred_reg_category_map.h" |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 44 | #include "compiler_llvm/runtime_support_llvm.h" |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 45 | using art::compiler_llvm::InferredRegCategoryMap; |
| 46 | #endif |
| 47 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 48 | namespace art { |
| 49 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 50 | String* Object::AsString() { |
| 51 | DCHECK(GetClass()->IsStringClass()); |
| 52 | return down_cast<String*>(this); |
| 53 | } |
| 54 | |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 55 | Object* 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 Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 62 | Heap* heap = Runtime::Current()->GetHeap(); |
| 63 | SirtRef<Object> copy(heap->AllocObject(c, num_bytes)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 64 | if (copy.get() == NULL) { |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 65 | 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 Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 71 | byte* dst_bytes = reinterpret_cast<byte*>(copy.get()); |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 72 | size_t offset = sizeof(Object); |
| 73 | memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset); |
| 74 | |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 75 | if (c->IsFinalizable()) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 76 | heap->AddFinalizerReference(Thread::Current(), copy.get()); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 77 | } |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 78 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 79 | return copy.get(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 82 | uint32_t Object::GetThinLockId() { |
| 83 | return Monitor::GetThinLockId(monitor_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void Object::MonitorEnter(Thread* thread) { |
| 87 | Monitor::MonitorEnter(thread, this); |
| 88 | } |
| 89 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 90 | bool Object::MonitorExit(Thread* thread) { |
| 91 | return Monitor::MonitorExit(thread, this); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void Object::Notify() { |
| 95 | Monitor::Notify(Thread::Current(), this); |
| 96 | } |
| 97 | |
| 98 | void Object::NotifyAll() { |
| 99 | Monitor::NotifyAll(Thread::Current(), this); |
| 100 | } |
| 101 | |
| 102 | void Object::Wait(int64_t ms, int32_t ns) { |
| 103 | Monitor::Wait(Thread::Current(), this, ms, ns, true); |
| 104 | } |
| 105 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 106 | // TODO: get global references for these |
| 107 | Class* Field::java_lang_reflect_Field_ = NULL; |
| 108 | |
| 109 | void 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 | |
| 115 | void Field::ResetClass() { |
| 116 | CHECK(java_lang_reflect_Field_ != NULL); |
| 117 | java_lang_reflect_Field_ = NULL; |
| 118 | } |
| 119 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 120 | void Field::SetOffset(MemberOffset num_bytes) { |
| 121 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 122 | #if 0 // TODO enable later in boot and under !NDEBUG |
| 123 | FieldHelper fh(this); |
| 124 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 125 | if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) { |
| 126 | DCHECK_ALIGNED(num_bytes.Uint32Value(), 8); |
| 127 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 128 | #endif |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 129 | SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false); |
| 130 | } |
| 131 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 132 | uint32_t Field::Get32(const Object* object) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 133 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 134 | if (IsStatic()) { |
| 135 | object = declaring_class_; |
| 136 | } |
| 137 | return object->GetField32(GetOffset(), IsVolatile()); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 140 | void Field::Set32(Object* object, uint32_t new_value) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 141 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 142 | if (IsStatic()) { |
| 143 | object = declaring_class_; |
| 144 | } |
| 145 | object->SetField32(GetOffset(), new_value, IsVolatile()); |
| 146 | } |
| 147 | |
| 148 | uint64_t Field::Get64(const Object* object) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 149 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 150 | if (IsStatic()) { |
| 151 | object = declaring_class_; |
| 152 | } |
| 153 | return object->GetField64(GetOffset(), IsVolatile()); |
| 154 | } |
| 155 | |
| 156 | void Field::Set64(Object* object, uint64_t new_value) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 157 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 158 | if (IsStatic()) { |
| 159 | object = declaring_class_; |
| 160 | } |
| 161 | object->SetField64(GetOffset(), new_value, IsVolatile()); |
| 162 | } |
| 163 | |
| 164 | Object* Field::GetObj(const Object* object) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 165 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 166 | if (IsStatic()) { |
| 167 | object = declaring_class_; |
| 168 | } |
| 169 | return object->GetFieldObject<Object*>(GetOffset(), IsVolatile()); |
| 170 | } |
| 171 | |
| 172 | void Field::SetObj(Object* object, const Object* new_value) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 173 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 174 | if (IsStatic()) { |
| 175 | object = declaring_class_; |
| 176 | } |
| 177 | object->SetFieldObject(GetOffset(), new_value, IsVolatile()); |
| 178 | } |
| 179 | |
| 180 | bool Field::GetBoolean(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 181 | DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 182 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 183 | return Get32(object); |
| 184 | } |
| 185 | |
| 186 | void Field::SetBoolean(Object* object, bool z) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 187 | DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 188 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 189 | Set32(object, z); |
| 190 | } |
| 191 | |
| 192 | int8_t Field::GetByte(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 193 | DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 194 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 195 | return Get32(object); |
| 196 | } |
| 197 | |
| 198 | void Field::SetByte(Object* object, int8_t b) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 199 | DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 200 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 201 | Set32(object, b); |
| 202 | } |
| 203 | |
| 204 | uint16_t Field::GetChar(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 205 | DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 206 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 207 | return Get32(object); |
| 208 | } |
| 209 | |
| 210 | void Field::SetChar(Object* object, uint16_t c) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 211 | DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 212 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 213 | Set32(object, c); |
| 214 | } |
| 215 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 216 | int16_t Field::GetShort(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 217 | DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 218 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 219 | return Get32(object); |
| 220 | } |
| 221 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 222 | void Field::SetShort(Object* object, int16_t s) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 223 | DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 224 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 225 | Set32(object, s); |
| 226 | } |
| 227 | |
| 228 | int32_t Field::GetInt(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 229 | DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 230 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 231 | return Get32(object); |
| 232 | } |
| 233 | |
| 234 | void Field::SetInt(Object* object, int32_t i) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 235 | DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 236 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 237 | Set32(object, i); |
| 238 | } |
| 239 | |
| 240 | int64_t Field::GetLong(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 241 | DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 242 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 243 | return Get64(object); |
| 244 | } |
| 245 | |
| 246 | void Field::SetLong(Object* object, int64_t j) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 247 | DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 248 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 249 | Set64(object, j); |
| 250 | } |
| 251 | |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 252 | union Bits { |
| 253 | jdouble d; |
| 254 | jfloat f; |
| 255 | jint i; |
| 256 | jlong j; |
| 257 | }; |
| 258 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 259 | float Field::GetFloat(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 260 | DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 261 | << PrettyField(this); |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 262 | Bits bits; |
| 263 | bits.i = Get32(object); |
| 264 | return bits.f; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void Field::SetFloat(Object* object, float f) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 268 | DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 269 | << PrettyField(this); |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 270 | Bits bits; |
| 271 | bits.f = f; |
| 272 | Set32(object, bits.i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | double Field::GetDouble(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 276 | DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 277 | << PrettyField(this); |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 278 | Bits bits; |
| 279 | bits.j = Get64(object); |
| 280 | return bits.d; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void Field::SetDouble(Object* object, double d) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 284 | DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 285 | << PrettyField(this); |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 286 | Bits bits; |
| 287 | bits.d = d; |
| 288 | Set64(object, bits.j); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | Object* Field::GetObject(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 292 | DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 293 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 294 | return GetObj(object); |
| 295 | } |
| 296 | |
| 297 | void Field::SetObject(Object* object, const Object* l) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 298 | DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 299 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 300 | SetObj(object, l); |
| 301 | } |
| 302 | |
| 303 | // TODO: get global references for these |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 304 | Class* Method::java_lang_reflect_Constructor_ = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 305 | Class* Method::java_lang_reflect_Method_ = NULL; |
| 306 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 307 | void 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 Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 312 | 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 Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 317 | void Method::ResetClasses() { |
| 318 | CHECK(java_lang_reflect_Constructor_ != NULL); |
| 319 | java_lang_reflect_Constructor_ = NULL; |
| 320 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 321 | CHECK(java_lang_reflect_Method_ != NULL); |
| 322 | java_lang_reflect_Method_ = NULL; |
| 323 | } |
| 324 | |
| 325 | ObjectArray<String>* Method::GetDexCacheStrings() const { |
| 326 | return GetFieldObject<ObjectArray<String>*>( |
| 327 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false); |
| 328 | } |
| 329 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 330 | void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) { |
| 331 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), |
| 332 | new_dex_cache_strings, false); |
| 333 | } |
| 334 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 335 | ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const { |
| 336 | return GetFieldObject<ObjectArray<Method>*>( |
| 337 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false); |
| 338 | } |
| 339 | |
| 340 | void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) { |
| 341 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), |
| 342 | new_dex_cache_methods, false); |
| 343 | } |
| 344 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 345 | ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const { |
| 346 | return GetFieldObject<ObjectArray<Class>*>( |
| 347 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false); |
| 348 | } |
| 349 | |
| 350 | void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) { |
| 351 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), |
| 352 | new_dex_cache_classes, false); |
| 353 | } |
| 354 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 355 | ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const { |
| 356 | return GetFieldObject<ObjectArray<StaticStorageBase>*>( |
| 357 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_), |
| 358 | false); |
| 359 | } |
| 360 | |
| 361 | void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 362 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 363 | new_value, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | size_t Method::NumArgRegisters(const StringPiece& shorty) { |
| 367 | CHECK_LE(1, shorty.length()); |
| 368 | uint32_t num_registers = 0; |
| 369 | for (int i = 1; i < shorty.length(); ++i) { |
| 370 | char ch = shorty[i]; |
| 371 | if (ch == 'D' || ch == 'J') { |
| 372 | num_registers += 2; |
| 373 | } else { |
| 374 | num_registers += 1; |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 377 | return num_registers; |
| 378 | } |
| 379 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 380 | bool Method::IsProxyMethod() const { |
| 381 | return GetDeclaringClass()->IsProxyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 384 | Method* Method::FindOverriddenMethod() const { |
| 385 | if (IsStatic()) { |
| 386 | return NULL; |
| 387 | } |
| 388 | Class* declaring_class = GetDeclaringClass(); |
| 389 | Class* super_class = declaring_class->GetSuperClass(); |
| 390 | uint16_t method_index = GetMethodIndex(); |
| 391 | ObjectArray<Method>* super_class_vtable = super_class->GetVTable(); |
| 392 | Method* result = NULL; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 393 | // Did this method override a super class method? If so load the result from the super class' |
| 394 | // vtable |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 395 | if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) { |
| 396 | result = super_class_vtable->Get(method_index); |
| 397 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 398 | // Method didn't override superclass method so search interfaces |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 399 | if (IsProxyMethod()) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 400 | result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex()); |
| 401 | CHECK_EQ(result, |
| 402 | Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this)); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 403 | } else { |
| 404 | MethodHelper mh(this); |
| 405 | MethodHelper interface_mh; |
| 406 | ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable(); |
| 407 | for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) { |
| 408 | InterfaceEntry* entry = iftable->Get(i); |
| 409 | Class* interface = entry->GetInterface(); |
| 410 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 411 | Method* interface_method = interface->GetVirtualMethod(j); |
| 412 | interface_mh.ChangeMethod(interface_method); |
| 413 | if (mh.HasSameNameAndSignature(&interface_mh)) { |
| 414 | result = interface_method; |
| 415 | break; |
| 416 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 421 | #ifndef NDEBUG |
| 422 | MethodHelper result_mh(result); |
| 423 | DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh)); |
| 424 | #endif |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 425 | return result; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Elliott Hughes | 168670b | 2012-02-29 16:43:26 -0800 | [diff] [blame] | 428 | static const void* GetOatCode(const Method* m) { |
| 429 | Runtime* runtime = Runtime::Current(); |
| 430 | const void* code = m->GetCode(); |
| 431 | // Peel off any method tracing trampoline. |
| 432 | if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) { |
| 433 | code = runtime->GetTracer()->GetSavedCodeFromMap(m); |
| 434 | } |
| 435 | // Peel off any resolution stub. |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 436 | if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) { |
Elliott Hughes | 168670b | 2012-02-29 16:43:26 -0800 | [diff] [blame] | 437 | code = runtime->GetClassLinker()->GetOatCodeFor(m); |
| 438 | } |
| 439 | return code; |
| 440 | } |
| 441 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 442 | uint32_t Method::ToDexPC(const uintptr_t pc) const { |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 443 | #if !defined(ART_USE_LLVM_COMPILER) |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 444 | const uint32_t* mapping_table = GetMappingTable(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 445 | if (mapping_table == NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 446 | DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 447 | return DexFile::kDexNoIndex; // Special no mapping case |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 448 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 449 | size_t mapping_table_length = GetMappingTableLength(); |
Elliott Hughes | 168670b | 2012-02-29 16:43:26 -0800 | [diff] [blame] | 450 | uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this)); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 451 | uint32_t best_offset = 0; |
| 452 | uint32_t best_dex_offset = 0; |
| 453 | for (size_t i = 0; i < mapping_table_length; i += 2) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 454 | uint32_t map_offset = mapping_table[i]; |
| 455 | uint32_t map_dex_offset = mapping_table[i + 1]; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 456 | if (map_offset == sought_offset) { |
| 457 | best_offset = map_offset; |
| 458 | best_dex_offset = map_dex_offset; |
| 459 | break; |
| 460 | } |
| 461 | if (map_offset < sought_offset && map_offset > best_offset) { |
| 462 | best_offset = map_offset; |
| 463 | best_dex_offset = map_dex_offset; |
| 464 | } |
| 465 | } |
| 466 | return best_dex_offset; |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 467 | #else |
| 468 | // Compiler LLVM doesn't use the machine pc, we just use dex pc instead. |
| 469 | return static_cast<uint32_t>(pc); |
| 470 | #endif |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | uintptr_t Method::ToNativePC(const uint32_t dex_pc) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 474 | const uint32_t* mapping_table = GetMappingTable(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 475 | if (mapping_table == NULL) { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 476 | DCHECK_EQ(dex_pc, 0U); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 477 | return 0; // Special no mapping/pc == 0 case |
| 478 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 479 | size_t mapping_table_length = GetMappingTableLength(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 480 | for (size_t i = 0; i < mapping_table_length; i += 2) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 481 | uint32_t map_offset = mapping_table[i]; |
| 482 | uint32_t map_dex_offset = mapping_table[i + 1]; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 483 | if (map_dex_offset == dex_pc) { |
Elliott Hughes | 168670b | 2012-02-29 16:43:26 -0800 | [diff] [blame] | 484 | return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | LOG(FATAL) << "Looking up Dex PC not contained in method"; |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 492 | MethodHelper mh(this); |
| 493 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 494 | // Iterate over the catch handlers associated with dex_pc |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 495 | for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) { |
| 496 | uint16_t iter_type_idx = it.GetHandlerTypeIndex(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 497 | // Catch all case |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 498 | if (iter_type_idx == DexFile::kDexNoIndex16) { |
| 499 | return it.GetHandlerAddress(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 500 | } |
| 501 | // Does this catch exception type apply? |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 502 | Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 503 | if (iter_exception_type == NULL) { |
| 504 | // The verifier should take care of resolving all exception classes early |
| 505 | LOG(WARNING) << "Unresolved exception class when finding catch block: " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 506 | << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 507 | } else if (iter_exception_type->IsAssignableFrom(exception_type)) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 508 | return it.GetHandlerAddress(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | // Handler not found |
| 512 | return DexFile::kDexNoIndex; |
| 513 | } |
| 514 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 515 | void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const { |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 516 | // Push a transition back into managed code onto the linked list in thread. |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 517 | CHECK_EQ(kRunnable, self->GetState()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 518 | self->AssertThreadSuspensionIsAllowable(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 519 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 520 | ManagedStack fragment; |
| 521 | self->PushManagedStackFragment(&fragment); |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 522 | |
| 523 | // Call the invoke stub associated with the method. |
| 524 | // Pass everything as arguments. |
| 525 | const Method::InvokeStub* stub = GetInvokeStub(); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 526 | |
| 527 | bool have_executable_code = (GetCode() != NULL); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 528 | |
TDYa127 | 05fe3b6 | 2012-04-21 00:28:54 -0700 | [diff] [blame] | 529 | #if defined(ART_USE_LLVM_COMPILER) |
| 530 | if (stub == NULL || !have_executable_code) { |
| 531 | Runtime::Current()->GetClassLinker()->LinkOatCodeFor(const_cast<Method*>(this)); |
| 532 | stub = GetInvokeStub(); |
| 533 | have_executable_code = (GetCode() != NULL); |
| 534 | } |
| 535 | #endif |
| 536 | |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 537 | if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) { |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 538 | bool log = false; |
| 539 | if (log) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 540 | LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p", |
| 541 | PrettyMethod(this).c_str(), GetCode(), stub); |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 542 | } |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 543 | (*stub)(this, receiver, self, args, result); |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 544 | if (log) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 545 | LOG(INFO) << StringPrintf("returned %s code=%p stub=%p", |
| 546 | PrettyMethod(this).c_str(), GetCode(), stub); |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 547 | } |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 548 | } else { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 549 | LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s", |
| 550 | PrettyMethod(this).c_str(), GetCode(), stub, |
| 551 | Runtime::Current()->IsStarted() ? "true" : "false"); |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 552 | if (result != NULL) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 553 | result->SetJ(0); |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | // Pop transition. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 558 | self->PopManagedStackFragment(fragment); |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 561 | bool Method::IsRegistered() const { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 562 | void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 563 | CHECK(native_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 564 | void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData(); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 565 | return native_method != jni_stub; |
| 566 | } |
| 567 | |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 568 | void Method::RegisterNative(Thread* self, const void* native_method) { |
| 569 | DCHECK(Thread::Current() == self); |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 570 | CHECK(IsNative()) << PrettyMethod(this); |
| 571 | CHECK(native_method != NULL) << PrettyMethod(this); |
TDYa127 | 2646757 | 2012-04-17 20:51:22 -0700 | [diff] [blame] | 572 | #if defined(ART_USE_LLVM_COMPILER) |
| 573 | SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), |
| 574 | native_method, false); |
| 575 | #else |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 576 | if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) { |
| 577 | SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), |
| 578 | native_method, false); |
| 579 | } else { |
| 580 | // We've been asked to associate this method with the given native method but are working |
| 581 | // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct |
| 582 | // the native method to runtime support and store the target somewhere runtime support will |
| 583 | // find it. |
| 584 | #if defined(__arm__) |
| 585 | SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), |
| 586 | reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false); |
| 587 | #else |
| 588 | UNIMPLEMENTED(FATAL); |
| 589 | #endif |
| 590 | SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), |
| 591 | reinterpret_cast<const uint8_t*>(native_method), false); |
| 592 | } |
TDYa127 | 2646757 | 2012-04-17 20:51:22 -0700 | [diff] [blame] | 593 | #endif |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 596 | void Method::UnregisterNative(Thread* self) { |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 597 | CHECK(IsNative()) << PrettyMethod(this); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 598 | // restore stub to lookup native pointer via dlsym |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 599 | RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 602 | void Class::SetStatus(Status new_status) { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 603 | CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted()) |
| 604 | << PrettyClass(this) << " " << GetStatus() << " -> " << new_status; |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 605 | CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this); |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 606 | if (new_status == kStatusError) { |
| 607 | CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this); |
| 608 | |
| 609 | // stash current exception |
| 610 | Thread* self = Thread::Current(); |
| 611 | SirtRef<Throwable> exception(self->GetException()); |
| 612 | CHECK(exception.get() != NULL); |
| 613 | |
| 614 | // clear exception to call FindSystemClass |
| 615 | self->ClearException(); |
| 616 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 617 | Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;"); |
| 618 | CHECK(!self->IsExceptionPending()); |
| 619 | |
| 620 | // only verification errors, not initialization problems, should set a verify error. |
| 621 | // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case. |
| 622 | Class* exception_class = exception->GetClass(); |
| 623 | if (!eiie_class->IsAssignableFrom(exception_class)) { |
| 624 | SetVerifyErrorClass(exception_class); |
| 625 | } |
| 626 | |
| 627 | // restore exception |
| 628 | self->SetException(exception.get()); |
| 629 | } |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 630 | return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | DexCache* Class::GetDexCache() const { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 634 | return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | void Class::SetDexCache(DexCache* new_dex_cache) { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 638 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 641 | Object* Class::AllocObject() { |
Brian Carlstrom | 96a253a | 2011-10-27 18:38:10 -0700 | [diff] [blame] | 642 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 643 | DCHECK(IsInstantiable()) << PrettyClass(this); |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 644 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 645 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
Brian Carlstrom | 96a253a | 2011-10-27 18:38:10 -0700 | [diff] [blame] | 646 | DCHECK_GE(this->object_size_, sizeof(Object)); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 647 | return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 650 | void Class::SetClassSize(size_t new_class_size) { |
| 651 | DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); |
| 652 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false); |
| 653 | } |
| 654 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 655 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 656 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 657 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 658 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
| 659 | String* Class::ComputeName() { |
| 660 | String* name = GetName(); |
| 661 | if (name != NULL) { |
| 662 | return name; |
| 663 | } |
| 664 | std::string descriptor(ClassHelper(this).GetDescriptor()); |
| 665 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 666 | // The descriptor indicates that this is the class for |
| 667 | // a primitive type; special-case the return value. |
| 668 | const char* c_name = NULL; |
| 669 | switch (descriptor[0]) { |
| 670 | case 'Z': c_name = "boolean"; break; |
| 671 | case 'B': c_name = "byte"; break; |
| 672 | case 'C': c_name = "char"; break; |
| 673 | case 'S': c_name = "short"; break; |
| 674 | case 'I': c_name = "int"; break; |
| 675 | case 'J': c_name = "long"; break; |
| 676 | case 'F': c_name = "float"; break; |
| 677 | case 'D': c_name = "double"; break; |
| 678 | case 'V': c_name = "void"; break; |
| 679 | default: |
| 680 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 681 | } |
| 682 | name = String::AllocFromModifiedUtf8(c_name); |
| 683 | } else { |
| 684 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 685 | // components. |
| 686 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 687 | descriptor.erase(0, 1); |
| 688 | descriptor.erase(descriptor.size() - 1); |
| 689 | } |
| 690 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
| 691 | name = String::AllocFromModifiedUtf8(descriptor.c_str()); |
| 692 | } |
| 693 | SetName(name); |
| 694 | return name; |
| 695 | } |
| 696 | |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 697 | void Class::DumpClass(std::ostream& os, int flags) const { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 698 | if ((flags & kDumpClassFullDetail) == 0) { |
| 699 | os << PrettyClass(this); |
| 700 | if ((flags & kDumpClassClassLoader) != 0) { |
| 701 | os << ' ' << GetClassLoader(); |
| 702 | } |
| 703 | if ((flags & kDumpClassInitialized) != 0) { |
| 704 | os << ' ' << GetStatus(); |
| 705 | } |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 706 | os << "\n"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | |
| 710 | Class* super = GetSuperClass(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 711 | ClassHelper kh(this); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 712 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 713 | << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n", |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 714 | os << " objectSize=" << SizeOf() << " " |
| 715 | << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n", |
| 716 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 717 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
| 718 | if (super != NULL) { |
| 719 | os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n"; |
| 720 | } |
| 721 | if (IsArrayClass()) { |
| 722 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 723 | } |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 724 | if (kh.NumDirectInterfaces() > 0) { |
| 725 | os << " interfaces (" << kh.NumDirectInterfaces() << "):\n"; |
| 726 | for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 727 | Class* interface = kh.GetDirectInterface(i); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 728 | const ClassLoader* cl = interface->GetClassLoader(); |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 729 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | os << " vtable (" << NumVirtualMethods() << " entries, " |
| 733 | << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n"; |
| 734 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 735 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str()); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 736 | } |
| 737 | os << " direct methods (" << NumDirectMethods() << " entries):\n"; |
| 738 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 739 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str()); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 740 | } |
| 741 | if (NumStaticFields() > 0) { |
| 742 | os << " static fields (" << NumStaticFields() << " entries):\n"; |
Elliott Hughes | 03f0349 | 2011-09-26 13:38:08 -0700 | [diff] [blame] | 743 | if (IsResolved() || IsErroneous()) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 744 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 745 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 746 | } |
| 747 | } else { |
| 748 | os << " <not yet available>"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | if (NumInstanceFields() > 0) { |
| 752 | os << " instance fields (" << NumInstanceFields() << " entries):\n"; |
Elliott Hughes | 03f0349 | 2011-09-26 13:38:08 -0700 | [diff] [blame] | 753 | if (IsResolved() || IsErroneous()) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 754 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 755 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 756 | } |
| 757 | } else { |
| 758 | os << " <not yet available>"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 763 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
| 764 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 765 | // Sanity check that the number of bits set in the reference offset bitmap |
| 766 | // agrees with the number of references |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 767 | size_t count = 0; |
| 768 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 769 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 770 | } |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 771 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 772 | } |
| 773 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
| 774 | new_reference_offsets, false); |
| 775 | } |
| 776 | |
| 777 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 778 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 779 | // Sanity check that the number of bits set in the reference offset bitmap |
| 780 | // agrees with the number of references |
| 781 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), |
| 782 | NumReferenceStaticFieldsDuringLinking()); |
| 783 | } |
| 784 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
| 785 | new_reference_offsets, false); |
| 786 | } |
| 787 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 788 | bool Class::Implements(const Class* klass) const { |
| 789 | DCHECK(klass != NULL); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 790 | DCHECK(klass->IsInterface()) << PrettyClass(this); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 791 | // All interfaces implemented directly and by our superclass, and |
| 792 | // recursively all super-interfaces of those interfaces, are listed |
| 793 | // in iftable_, so we can just do a linear scan through that. |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 794 | int32_t iftable_count = GetIfTableCount(); |
| 795 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 796 | for (int32_t i = 0; i < iftable_count; i++) { |
| 797 | if (iftable->Get(i)->GetInterface() == klass) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 798 | return true; |
| 799 | } |
| 800 | } |
| 801 | return false; |
| 802 | } |
| 803 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 804 | // Determine whether "this" is assignable from "src", where both of these |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 805 | // are array classes. |
| 806 | // |
| 807 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 808 | // Y[][] = Y[][] --> true (identity) |
| 809 | // X[][] = Y[][] --> true (element superclass) |
| 810 | // Y = Y[][] --> false |
| 811 | // Y[] = Y[][] --> false |
| 812 | // Object = Y[][] --> true (everything is an object) |
| 813 | // Object[] = Y[][] --> true |
| 814 | // Object[][] = Y[][] --> true |
| 815 | // Object[][][] = Y[][] --> false (too many []s) |
| 816 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 817 | // Serializable[] = Y[][] --> true |
| 818 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 819 | // |
| 820 | // Don't forget about primitive types. |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 821 | // Object[] = int[] --> false |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 822 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 823 | bool Class::IsArrayAssignableFromArray(const Class* src) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 824 | DCHECK(IsArrayClass()) << PrettyClass(this); |
| 825 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 826 | return GetComponentType()->IsAssignableFrom(src->GetComponentType()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 829 | bool Class::IsAssignableFromArray(const Class* src) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 830 | DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom |
| 831 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 832 | if (!IsArrayClass()) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 833 | // If "this" is not also an array, it must be Object. |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 834 | // src's super should be java_lang_Object, since it is an array. |
| 835 | Class* java_lang_Object = src->GetSuperClass(); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 836 | DCHECK(java_lang_Object != NULL) << PrettyClass(src); |
| 837 | DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 838 | return this == java_lang_Object; |
| 839 | } |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 840 | return IsArrayAssignableFromArray(src); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | bool Class::IsSubClass(const Class* klass) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 844 | DCHECK(!IsInterface()) << PrettyClass(this); |
| 845 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 846 | const Class* current = this; |
| 847 | do { |
| 848 | if (current == klass) { |
| 849 | return true; |
| 850 | } |
| 851 | current = current->GetSuperClass(); |
| 852 | } while (current != NULL); |
| 853 | return false; |
| 854 | } |
| 855 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 856 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 857 | size_t i = 0; |
| 858 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 859 | ++i; |
| 860 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 861 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 862 | descriptor2.find('/', i) != StringPiece::npos) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 863 | return false; |
| 864 | } else { |
| 865 | return true; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | bool Class::IsInSamePackage(const Class* that) const { |
| 870 | const Class* klass1 = this; |
| 871 | const Class* klass2 = that; |
| 872 | if (klass1 == klass2) { |
| 873 | return true; |
| 874 | } |
| 875 | // Class loaders must match. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 876 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 877 | return false; |
| 878 | } |
| 879 | // Arrays are in the same package when their element classes are. |
jeffhao | 4a801a4 | 2011-09-23 13:53:40 -0700 | [diff] [blame] | 880 | while (klass1->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 881 | klass1 = klass1->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 882 | } |
jeffhao | 4a801a4 | 2011-09-23 13:53:40 -0700 | [diff] [blame] | 883 | while (klass2->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 884 | klass2 = klass2->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 885 | } |
| 886 | // Compare the package part of the descriptor string. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 887 | ClassHelper kh(klass1); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 888 | std::string descriptor1(kh.GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 889 | kh.ChangeClass(klass2); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 890 | std::string descriptor2(kh.GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 891 | return IsInSamePackage(descriptor1, descriptor2); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 894 | bool Class::IsClassClass() const { |
| 895 | Class* java_lang_Class = GetClass()->GetClass(); |
| 896 | return this == java_lang_Class; |
| 897 | } |
| 898 | |
| 899 | bool Class::IsStringClass() const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 900 | return this == String::GetJavaLangString(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 903 | bool Class::IsThrowableClass() const { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 904 | return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 905 | } |
| 906 | |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 907 | ClassLoader* Class::GetClassLoader() const { |
| 908 | return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame^] | 911 | void Class::SetClassLoader(ClassLoader* new_class_loader) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 912 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 915 | Method* Class::FindVirtualMethodForInterface(Method* method) { |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 916 | Class* declaring_class = method->GetDeclaringClass(); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 917 | DCHECK(declaring_class != NULL) << PrettyClass(this); |
| 918 | DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 919 | // TODO cache to improve lookup speed |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 920 | int32_t iftable_count = GetIfTableCount(); |
| 921 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 922 | for (int32_t i = 0; i < iftable_count; i++) { |
| 923 | InterfaceEntry* interface_entry = iftable->Get(i); |
| 924 | if (interface_entry->GetInterface() == declaring_class) { |
| 925 | return interface_entry->GetMethodArray()->Get(method->GetMethodIndex()); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 926 | } |
| 927 | } |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 928 | return NULL; |
| 929 | } |
| 930 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 931 | Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 932 | // Check the current class before checking the interfaces. |
Ian Rogers | 94c0e33 | 2012-01-18 22:11:47 -0800 | [diff] [blame] | 933 | Method* method = FindDeclaredVirtualMethod(name, signature); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 934 | if (method != NULL) { |
| 935 | return method; |
| 936 | } |
| 937 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 938 | int32_t iftable_count = GetIfTableCount(); |
| 939 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 940 | for (int32_t i = 0; i < iftable_count; i++) { |
| 941 | method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 942 | if (method != NULL) { |
| 943 | return method; |
| 944 | } |
| 945 | } |
| 946 | return NULL; |
| 947 | } |
| 948 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 949 | Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 950 | // Check the current class before checking the interfaces. |
| 951 | Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 952 | if (method != NULL) { |
| 953 | return method; |
| 954 | } |
| 955 | |
| 956 | int32_t iftable_count = GetIfTableCount(); |
| 957 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 958 | for (int32_t i = 0; i < iftable_count; i++) { |
| 959 | method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx); |
| 960 | if (method != NULL) { |
| 961 | return method; |
| 962 | } |
| 963 | } |
| 964 | return NULL; |
| 965 | } |
| 966 | |
| 967 | |
| 968 | Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 969 | MethodHelper mh; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 970 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 971 | Method* method = GetDirectMethod(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 972 | mh.ChangeMethod(method); |
| 973 | if (name == mh.GetName() && signature == mh.GetSignature()) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 974 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 975 | } |
| 976 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 977 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 980 | Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 981 | if (GetDexCache() == dex_cache) { |
| 982 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 983 | Method* method = GetDirectMethod(i); |
| 984 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 985 | return method; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | return NULL; |
| 990 | } |
| 991 | |
| 992 | Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
| 993 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 994 | Method* method = klass->FindDeclaredDirectMethod(name, signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 995 | if (method != NULL) { |
| 996 | return method; |
| 997 | } |
| 998 | } |
| 999 | return NULL; |
| 1000 | } |
| 1001 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1002 | Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 1003 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 1004 | Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); |
| 1005 | if (method != NULL) { |
| 1006 | return method; |
| 1007 | } |
| 1008 | } |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1012 | Method* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1013 | const StringPiece& signature) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1014 | MethodHelper mh; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1015 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1016 | Method* method = GetVirtualMethod(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1017 | mh.ChangeMethod(method); |
| 1018 | if (name == mh.GetName() && signature == mh.GetSignature()) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1019 | return method; |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1025 | Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 1026 | if (GetDexCache() == dex_cache) { |
| 1027 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 1028 | Method* method = GetVirtualMethod(i); |
| 1029 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 1030 | return method; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1037 | Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const { |
| 1038 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 1039 | Method* method = klass->FindDeclaredVirtualMethod(name, signature); |
| 1040 | if (method != NULL) { |
| 1041 | return method; |
| 1042 | } |
| 1043 | } |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1047 | Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 1048 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 1049 | Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 1050 | if (method != NULL) { |
| 1051 | return method; |
| 1052 | } |
| 1053 | } |
| 1054 | return NULL; |
| 1055 | } |
| 1056 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1057 | Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1058 | // Is the field in this class? |
| 1059 | // Interfaces are not relevant because they can't contain instance fields. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1060 | FieldHelper fh; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1061 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 1062 | Field* f = GetInstanceField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1063 | fh.ChangeField(f); |
| 1064 | if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1065 | return f; |
| 1066 | } |
| 1067 | } |
| 1068 | return NULL; |
| 1069 | } |
| 1070 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1071 | Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 1072 | if (GetDexCache() == dex_cache) { |
| 1073 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 1074 | Field* f = GetInstanceField(i); |
| 1075 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 1076 | return f; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | return NULL; |
| 1081 | } |
| 1082 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1083 | Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1084 | // Is the field in this class, or any of its superclasses? |
| 1085 | // Interfaces are not relevant because they can't contain instance fields. |
| 1086 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1087 | Field* f = c->FindDeclaredInstanceField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1088 | if (f != NULL) { |
| 1089 | return f; |
| 1090 | } |
| 1091 | } |
| 1092 | return NULL; |
| 1093 | } |
| 1094 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1095 | Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 1096 | // Is the field in this class, or any of its superclasses? |
| 1097 | // Interfaces are not relevant because they can't contain instance fields. |
| 1098 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 1099 | Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
| 1100 | if (f != NULL) { |
| 1101 | return f; |
| 1102 | } |
| 1103 | } |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1107 | Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
| 1108 | DCHECK(type != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1109 | FieldHelper fh; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1110 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 1111 | Field* f = GetStaticField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1112 | fh.ChangeField(f); |
| 1113 | if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1114 | return f; |
| 1115 | } |
| 1116 | } |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1120 | Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 1121 | if (dex_cache == GetDexCache()) { |
| 1122 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 1123 | Field* f = GetStaticField(i); |
| 1124 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 1125 | return f; |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | return NULL; |
| 1130 | } |
| 1131 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1132 | Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) { |
| 1133 | // Is the field in this class (or its interfaces), or any of its |
| 1134 | // superclasses (or their interfaces)? |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1135 | ClassHelper kh; |
| 1136 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1137 | // Is the field in this class? |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1138 | Field* f = k->FindDeclaredStaticField(name, type); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1139 | if (f != NULL) { |
| 1140 | return f; |
| 1141 | } |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1142 | // Is this field in any of this class' interfaces? |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1143 | kh.ChangeClass(k); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1144 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 1145 | Class* interface = kh.GetDirectInterface(i); |
| 1146 | f = interface->FindStaticField(name, type); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1147 | if (f != NULL) { |
| 1148 | return f; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | return NULL; |
| 1153 | } |
| 1154 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1155 | Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 1156 | ClassHelper kh; |
| 1157 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 1158 | // Is the field in this class? |
| 1159 | Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
| 1160 | if (f != NULL) { |
| 1161 | return f; |
| 1162 | } |
| 1163 | // Is this field in any of this class' interfaces? |
| 1164 | kh.ChangeClass(k); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1165 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 1166 | Class* interface = kh.GetDirectInterface(i); |
| 1167 | f = interface->FindStaticField(dex_cache, dex_field_idx); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1168 | if (f != NULL) { |
| 1169 | return f; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | return NULL; |
| 1174 | } |
| 1175 | |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1176 | Field* Class::FindField(const StringPiece& name, const StringPiece& type) { |
| 1177 | // Find a field using the JLS field resolution order |
| 1178 | ClassHelper kh; |
| 1179 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 1180 | // Is the field in this class? |
| 1181 | Field* f = k->FindDeclaredInstanceField(name, type); |
| 1182 | if (f != NULL) { |
| 1183 | return f; |
| 1184 | } |
| 1185 | f = k->FindDeclaredStaticField(name, type); |
| 1186 | if (f != NULL) { |
| 1187 | return f; |
| 1188 | } |
| 1189 | // Is this field in any of this class' interfaces? |
| 1190 | kh.ChangeClass(k); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1191 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 1192 | Class* interface = kh.GetDirectInterface(i); |
| 1193 | f = interface->FindStaticField(name, type); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1194 | if (f != NULL) { |
| 1195 | return f; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1202 | Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) { |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 1203 | DCHECK(array_class != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1204 | DCHECK_GE(component_count, 0); |
| 1205 | DCHECK(array_class->IsArrayClass()); |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 1206 | |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1207 | size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4); |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 1208 | size_t data_size = component_count * component_size; |
| 1209 | size_t size = header_size + data_size; |
| 1210 | |
| 1211 | // Check for overflow and throw OutOfMemoryError if this was an unreasonable request. |
| 1212 | size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size); |
| 1213 | if (data_size >> component_shift != size_t(component_count) || size < data_size) { |
| 1214 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;", |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1215 | "%s of length %d would overflow", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1216 | PrettyDescriptor(array_class).c_str(), component_count); |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 1217 | return NULL; |
| 1218 | } |
| 1219 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1220 | Heap* heap = Runtime::Current()->GetHeap(); |
| 1221 | Array* array = down_cast<Array*>(heap->AllocObject(array_class, size)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1222 | if (array != NULL) { |
| 1223 | DCHECK(array->IsArrayInstance()); |
| 1224 | array->SetLength(component_count); |
| 1225 | } |
| 1226 | return array; |
| 1227 | } |
| 1228 | |
| 1229 | Array* Array::Alloc(Class* array_class, int32_t component_count) { |
| 1230 | return Alloc(array_class, component_count, array_class->GetComponentSize()); |
| 1231 | } |
| 1232 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1233 | bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1234 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1235 | "length=%i; index=%i", length_, index); |
| 1236 | return false; |
| 1237 | } |
| 1238 | |
| 1239 | bool Array::ThrowArrayStoreException(Object* object) const { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1240 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1241 | "Can't store an element of type %s into an array of type %s", |
| 1242 | PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str()); |
| 1243 | return false; |
| 1244 | } |
| 1245 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1246 | template<typename T> |
| 1247 | PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1248 | DCHECK(array_class_ != NULL); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1249 | Array* raw_array = Array::Alloc(array_class_, length, sizeof(T)); |
| 1250 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 1251 | } |
| 1252 | |
| 1253 | template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL; |
| 1254 | |
| 1255 | // Explicitly instantiate all the primitive array types. |
| 1256 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 1257 | template class PrimitiveArray<int8_t>; // ByteArray |
| 1258 | template class PrimitiveArray<uint16_t>; // CharArray |
| 1259 | template class PrimitiveArray<double>; // DoubleArray |
| 1260 | template class PrimitiveArray<float>; // FloatArray |
| 1261 | template class PrimitiveArray<int32_t>; // IntArray |
| 1262 | template class PrimitiveArray<int64_t>; // LongArray |
| 1263 | template class PrimitiveArray<int16_t>; // ShortArray |
| 1264 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1265 | // Explicitly instantiate Class[][] |
| 1266 | template class ObjectArray<ObjectArray<Class> >; |
| 1267 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1268 | // TODO: get global references for these |
| 1269 | Class* String::java_lang_String_ = NULL; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1270 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1271 | void String::SetClass(Class* java_lang_String) { |
| 1272 | CHECK(java_lang_String_ == NULL); |
| 1273 | CHECK(java_lang_String != NULL); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1274 | java_lang_String_ = java_lang_String; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1275 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1276 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1277 | void String::ResetClass() { |
| 1278 | CHECK(java_lang_String_ != NULL); |
| 1279 | java_lang_String_ = NULL; |
| 1280 | } |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1281 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 1282 | String* String::Intern() { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1283 | return Runtime::Current()->GetInternTable()->InternWeak(this); |
| 1284 | } |
| 1285 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 1286 | int32_t String::GetHashCode() { |
| 1287 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); |
| 1288 | if (result == 0) { |
| 1289 | ComputeHashCode(); |
| 1290 | } |
| 1291 | result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); |
| 1292 | DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0) |
| 1293 | << ToModifiedUtf8() << " " << result; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1294 | return result; |
| 1295 | } |
| 1296 | |
| 1297 | int32_t String::GetLength() const { |
| 1298 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false); |
| 1299 | DCHECK(result >= 0 && result <= GetCharArray()->GetLength()); |
| 1300 | return result; |
| 1301 | } |
| 1302 | |
| 1303 | uint16_t String::CharAt(int32_t index) const { |
| 1304 | // TODO: do we need this? Equals is the only caller, and could |
| 1305 | // bounds check itself. |
| 1306 | if (index < 0 || index >= count_) { |
| 1307 | Thread* self = Thread::Current(); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1308 | self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;", |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1309 | "length=%i; index=%i", count_, index); |
| 1310 | return 0; |
| 1311 | } |
| 1312 | return GetCharArray()->Get(index + GetOffset()); |
| 1313 | } |
| 1314 | |
| 1315 | String* String::AllocFromUtf16(int32_t utf16_length, |
| 1316 | const uint16_t* utf16_data_in, |
| 1317 | int32_t hash_code) { |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1318 | CHECK(utf16_data_in != NULL || utf16_length == 0); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1319 | String* string = Alloc(GetJavaLangString(), utf16_length); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1320 | if (string == NULL) { |
| 1321 | return NULL; |
| 1322 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1323 | // TODO: use 16-bit wide memset variant |
| 1324 | CharArray* array = const_cast<CharArray*>(string->GetCharArray()); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1325 | if (array == NULL) { |
| 1326 | return NULL; |
| 1327 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1328 | for (int i = 0; i < utf16_length; i++) { |
| 1329 | array->Set(i, utf16_data_in[i]); |
| 1330 | } |
| 1331 | if (hash_code != 0) { |
| 1332 | string->SetHashCode(hash_code); |
| 1333 | } else { |
| 1334 | string->ComputeHashCode(); |
| 1335 | } |
| 1336 | return string; |
| 1337 | } |
| 1338 | |
| 1339 | String* String::AllocFromModifiedUtf8(const char* utf) { |
Ian Rogers | 4860131 | 2011-12-07 16:45:19 -0800 | [diff] [blame] | 1340 | if (utf == NULL) { |
| 1341 | return NULL; |
| 1342 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1343 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 1344 | return AllocFromModifiedUtf8(char_count, utf); |
| 1345 | } |
| 1346 | |
| 1347 | String* String::AllocFromModifiedUtf8(int32_t utf16_length, |
| 1348 | const char* utf8_data_in) { |
| 1349 | String* string = Alloc(GetJavaLangString(), utf16_length); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1350 | if (string == NULL) { |
| 1351 | return NULL; |
| 1352 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1353 | uint16_t* utf16_data_out = |
| 1354 | const_cast<uint16_t*>(string->GetCharArray()->GetData()); |
| 1355 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 1356 | string->ComputeHashCode(); |
| 1357 | return string; |
| 1358 | } |
| 1359 | |
| 1360 | String* String::Alloc(Class* java_lang_String, int32_t utf16_length) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1361 | SirtRef<CharArray> array(CharArray::Alloc(utf16_length)); |
| 1362 | if (array.get() == NULL) { |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1363 | return NULL; |
| 1364 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1365 | return Alloc(java_lang_String, array.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | String* String::Alloc(Class* java_lang_String, CharArray* array) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1369 | SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1370 | String* string = down_cast<String*>(java_lang_String->AllocObject()); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1371 | if (string == NULL) { |
| 1372 | return NULL; |
| 1373 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1374 | string->SetArray(array); |
| 1375 | string->SetCount(array->GetLength()); |
| 1376 | return string; |
| 1377 | } |
| 1378 | |
| 1379 | bool String::Equals(const String* that) const { |
| 1380 | if (this == that) { |
| 1381 | // Quick reference equality test |
| 1382 | return true; |
| 1383 | } else if (that == NULL) { |
| 1384 | // Null isn't an instanceof anything |
| 1385 | return false; |
| 1386 | } else if (this->GetLength() != that->GetLength()) { |
| 1387 | // Quick length inequality test |
| 1388 | return false; |
| 1389 | } else { |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1390 | // Note: don't short circuit on hash code as we're presumably here as the |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1391 | // hash code was already equal |
| 1392 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
| 1393 | if (this->CharAt(i) != that->CharAt(i)) { |
| 1394 | return false; |
| 1395 | } |
| 1396 | } |
| 1397 | return true; |
| 1398 | } |
| 1399 | } |
| 1400 | |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 1401 | bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1402 | if (this->GetLength() != that_length) { |
| 1403 | return false; |
| 1404 | } else { |
| 1405 | for (int32_t i = 0; i < that_length; ++i) { |
| 1406 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 1407 | return false; |
| 1408 | } |
| 1409 | } |
| 1410 | return true; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | bool String::Equals(const char* modified_utf8) const { |
| 1415 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 1416 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 1417 | if (ch == '\0' || ch != CharAt(i)) { |
| 1418 | return false; |
| 1419 | } |
| 1420 | } |
| 1421 | return *modified_utf8 == '\0'; |
| 1422 | } |
| 1423 | |
| 1424 | bool String::Equals(const StringPiece& modified_utf8) const { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1425 | if (modified_utf8.size() != GetLength()) { |
| 1426 | return false; |
| 1427 | } |
| 1428 | const char* p = modified_utf8.data(); |
| 1429 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 1430 | uint16_t ch = GetUtf16FromUtf8(&p); |
| 1431 | if (ch != CharAt(i)) { |
| 1432 | return false; |
| 1433 | } |
| 1434 | } |
| 1435 | return true; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 1439 | std::string String::ToModifiedUtf8() const { |
| 1440 | const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); |
jeffhao | 0ce1315 | 2012-03-27 19:45:50 -0700 | [diff] [blame] | 1441 | size_t byte_count = GetUtfLength(); |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1442 | std::string result(byte_count, static_cast<char>(0)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1443 | ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); |
| 1444 | return result; |
| 1445 | } |
| 1446 | |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1447 | void Throwable::SetCause(Throwable* cause) { |
| 1448 | CHECK(cause != NULL); |
| 1449 | CHECK(cause != this); |
| 1450 | CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL); |
| 1451 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false); |
| 1452 | } |
| 1453 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1454 | bool Throwable::IsCheckedException() const { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 1455 | if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1456 | return false; |
| 1457 | } |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 1458 | return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException)); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1461 | std::string Throwable::Dump() const { |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1462 | std::string result(PrettyTypeOf(this)); |
| 1463 | result += ": "; |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 1464 | String* msg = GetDetailMessage(); |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1465 | if (msg != NULL) { |
| 1466 | result += msg->ToModifiedUtf8(); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1467 | } |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1468 | result += "\n"; |
| 1469 | Object* stack_state = GetStackState(); |
| 1470 | // check stack state isn't missing or corrupt |
| 1471 | if (stack_state != NULL && stack_state->IsObjectArray()) { |
| 1472 | // Decode the internal stack trace into the depth and method trace |
| 1473 | ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state); |
| 1474 | int32_t depth = method_trace->GetLength() - 1; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1475 | IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 1476 | MethodHelper mh; |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1477 | for (int32_t i = 0; i < depth; ++i) { |
| 1478 | Method* method = down_cast<Method*>(method_trace->Get(i)); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1479 | mh.ChangeMethod(method); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1480 | uint32_t dex_pc = pc_trace->Get(i); |
| 1481 | int32_t line_number = mh.GetLineNumFromDexPC(dex_pc); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1482 | const char* source_file = mh.GetDeclaringClassSourceFile(); |
| 1483 | result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(), |
| 1484 | source_file, line_number); |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1485 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1486 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1487 | Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false); |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 1488 | if (cause != NULL && cause != this) { // Constructor makes cause == this by default. |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1489 | result += "Caused by: "; |
| 1490 | result += cause->Dump(); |
| 1491 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1492 | return result; |
| 1493 | } |
| 1494 | |
Ian Rogers | 5167c97 | 2012-02-03 10:41:20 -0800 | [diff] [blame] | 1495 | |
| 1496 | Class* Throwable::java_lang_Throwable_ = NULL; |
| 1497 | |
| 1498 | void Throwable::SetClass(Class* java_lang_Throwable) { |
| 1499 | CHECK(java_lang_Throwable_ == NULL); |
| 1500 | CHECK(java_lang_Throwable != NULL); |
| 1501 | java_lang_Throwable_ = java_lang_Throwable; |
| 1502 | } |
| 1503 | |
| 1504 | void Throwable::ResetClass() { |
| 1505 | CHECK(java_lang_Throwable_ != NULL); |
| 1506 | java_lang_Throwable_ = NULL; |
| 1507 | } |
| 1508 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1509 | Class* StackTraceElement::java_lang_StackTraceElement_ = NULL; |
| 1510 | |
| 1511 | void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) { |
| 1512 | CHECK(java_lang_StackTraceElement_ == NULL); |
| 1513 | CHECK(java_lang_StackTraceElement != NULL); |
| 1514 | java_lang_StackTraceElement_ = java_lang_StackTraceElement; |
| 1515 | } |
| 1516 | |
| 1517 | void StackTraceElement::ResetClass() { |
| 1518 | CHECK(java_lang_StackTraceElement_ != NULL); |
| 1519 | java_lang_StackTraceElement_ = NULL; |
| 1520 | } |
| 1521 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1522 | StackTraceElement* StackTraceElement::Alloc(String* declaring_class, |
| 1523 | String* method_name, |
| 1524 | String* file_name, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1525 | int32_t line_number) { |
| 1526 | StackTraceElement* trace = |
| 1527 | down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject()); |
| 1528 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), |
| 1529 | const_cast<String*>(declaring_class), false); |
| 1530 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), |
| 1531 | const_cast<String*>(method_name), false); |
| 1532 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), |
| 1533 | const_cast<String*>(file_name), false); |
| 1534 | trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), |
| 1535 | line_number, false); |
| 1536 | return trace; |
| 1537 | } |
| 1538 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1539 | } // namespace art |