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