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