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