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