Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -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 | */ |
| 16 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ART_FIELD_INL_H_ |
| 18 | #define ART_RUNTIME_ART_FIELD_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 20 | #include "art_field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 23 | #include "class_linker.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 24 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 25 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 26 | #include "jvalue.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 27 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 28 | #include "mirror/object-inl.h" |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 29 | #include "primitive.h" |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 30 | #include "thread-inl.h" |
| 31 | #include "scoped_thread_state_change.h" |
| 32 | #include "well_known_classes.h" |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 33 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 36 | inline mirror::Class* ArtField::GetDeclaringClass() { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 37 | GcRootSource gc_root_source(this); |
| 38 | mirror::Class* result = declaring_class_.Read(&gc_root_source); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 39 | DCHECK(result != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 40 | DCHECK(result->IsLoaded() || result->IsErroneous()) << result->GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | return result; |
| 42 | } |
| 43 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 44 | inline void ArtField::SetDeclaringClass(mirror::Class* new_declaring_class) { |
| 45 | declaring_class_ = GcRoot<mirror::Class>(new_declaring_class); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 48 | inline uint32_t ArtField::GetAccessFlags() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 50 | return access_flags_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 53 | inline MemberOffset ArtField::GetOffset() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 55 | return MemberOffset(offset_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 58 | inline MemberOffset ArtField::GetOffsetDuringLinking() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 60 | return MemberOffset(offset_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 63 | inline uint32_t ArtField::Get32(mirror::Object* object) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 64 | DCHECK(object != nullptr) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 65 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 66 | if (UNLIKELY(IsVolatile())) { |
| 67 | return object->GetField32Volatile(GetOffset()); |
| 68 | } |
| 69 | return object->GetField32(GetOffset()); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 72 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 73 | inline void ArtField::Set32(mirror::Object* object, uint32_t new_value) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 74 | DCHECK(object != nullptr) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 75 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 76 | if (UNLIKELY(IsVolatile())) { |
| 77 | object->SetField32Volatile<kTransactionActive>(GetOffset(), new_value); |
| 78 | } else { |
| 79 | object->SetField32<kTransactionActive>(GetOffset(), new_value); |
| 80 | } |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 83 | inline uint64_t ArtField::Get64(mirror::Object* object) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 84 | DCHECK(object != nullptr) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 85 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 86 | if (UNLIKELY(IsVolatile())) { |
| 87 | return object->GetField64Volatile(GetOffset()); |
| 88 | } |
| 89 | return object->GetField64(GetOffset()); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 92 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 93 | inline void ArtField::Set64(mirror::Object* object, uint64_t new_value) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 94 | DCHECK(object != nullptr) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 95 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 96 | if (UNLIKELY(IsVolatile())) { |
| 97 | object->SetField64Volatile<kTransactionActive>(GetOffset(), new_value); |
| 98 | } else { |
| 99 | object->SetField64<kTransactionActive>(GetOffset(), new_value); |
| 100 | } |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 103 | inline mirror::Object* ArtField::GetObj(mirror::Object* object) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 104 | DCHECK(object != nullptr) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 105 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 106 | if (UNLIKELY(IsVolatile())) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 107 | return object->GetFieldObjectVolatile<mirror::Object>(GetOffset()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 108 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 109 | return object->GetFieldObject<mirror::Object>(GetOffset()); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 112 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 113 | inline void ArtField::SetObj(mirror::Object* object, mirror::Object* new_value) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 114 | DCHECK(object != nullptr) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 115 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 116 | if (UNLIKELY(IsVolatile())) { |
| 117 | object->SetFieldObjectVolatile<kTransactionActive>(GetOffset(), new_value); |
| 118 | } else { |
| 119 | object->SetFieldObject<kTransactionActive>(GetOffset(), new_value); |
| 120 | } |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 123 | #define FIELD_GET(object, type) \ |
| 124 | DCHECK_EQ(Primitive::kPrim ## type, GetTypeAsPrimitiveType()) << PrettyField(this); \ |
| 125 | DCHECK(object != nullptr) << PrettyField(this); \ |
| 126 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \ |
| 127 | if (UNLIKELY(IsVolatile())) { \ |
| 128 | return object->GetField ## type ## Volatile(GetOffset()); \ |
| 129 | } \ |
| 130 | return object->GetField ## type(GetOffset()); |
| 131 | |
| 132 | #define FIELD_SET(object, type, value) \ |
| 133 | DCHECK_EQ(Primitive::kPrim ## type, GetTypeAsPrimitiveType()) << PrettyField(this); \ |
| 134 | DCHECK(object != nullptr) << PrettyField(this); \ |
| 135 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \ |
| 136 | if (UNLIKELY(IsVolatile())) { \ |
| 137 | object->SetField ## type ## Volatile<kTransactionActive>(GetOffset(), value); \ |
| 138 | } else { \ |
| 139 | object->SetField ## type<kTransactionActive>(GetOffset(), value); \ |
| 140 | } |
| 141 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 142 | inline uint8_t ArtField::GetBoolean(mirror::Object* object) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 143 | FIELD_GET(object, Boolean); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 146 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 147 | inline void ArtField::SetBoolean(mirror::Object* object, uint8_t z) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 148 | FIELD_SET(object, Boolean, z); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 151 | inline int8_t ArtField::GetByte(mirror::Object* object) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 152 | FIELD_GET(object, Byte); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 155 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 156 | inline void ArtField::SetByte(mirror::Object* object, int8_t b) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 157 | FIELD_SET(object, Byte, b); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 160 | inline uint16_t ArtField::GetChar(mirror::Object* object) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 161 | FIELD_GET(object, Char); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 164 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 165 | inline void ArtField::SetChar(mirror::Object* object, uint16_t c) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 166 | FIELD_SET(object, Char, c); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 169 | inline int16_t ArtField::GetShort(mirror::Object* object) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 170 | FIELD_GET(object, Short); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 173 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 174 | inline void ArtField::SetShort(mirror::Object* object, int16_t s) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 175 | FIELD_SET(object, Short, s); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 178 | #undef FIELD_GET |
| 179 | #undef FIELD_SET |
| 180 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 181 | inline int32_t ArtField::GetInt(mirror::Object* object) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 182 | if (kIsDebugBuild) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 183 | Primitive::Type type = GetTypeAsPrimitiveType(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 184 | CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this); |
| 185 | } |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 186 | return Get32(object); |
| 187 | } |
| 188 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 189 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 190 | inline void ArtField::SetInt(mirror::Object* object, int32_t i) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 191 | if (kIsDebugBuild) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 192 | Primitive::Type type = GetTypeAsPrimitiveType(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 193 | CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this); |
| 194 | } |
| 195 | Set32<kTransactionActive>(object, i); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 198 | inline int64_t ArtField::GetLong(mirror::Object* object) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 199 | if (kIsDebugBuild) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 200 | Primitive::Type type = GetTypeAsPrimitiveType(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 201 | CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this); |
| 202 | } |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 203 | return Get64(object); |
| 204 | } |
| 205 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 206 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 207 | inline void ArtField::SetLong(mirror::Object* object, int64_t j) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 208 | if (kIsDebugBuild) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 209 | Primitive::Type type = GetTypeAsPrimitiveType(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 210 | CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this); |
| 211 | } |
| 212 | Set64<kTransactionActive>(object, j); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 215 | inline float ArtField::GetFloat(mirror::Object* object) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 216 | DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 217 | JValue bits; |
| 218 | bits.SetI(Get32(object)); |
| 219 | return bits.GetF(); |
| 220 | } |
| 221 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 222 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 223 | inline void ArtField::SetFloat(mirror::Object* object, float f) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 224 | DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 225 | JValue bits; |
| 226 | bits.SetF(f); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 227 | Set32<kTransactionActive>(object, bits.GetI()); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 230 | inline double ArtField::GetDouble(mirror::Object* object) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 231 | DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 232 | JValue bits; |
| 233 | bits.SetJ(Get64(object)); |
| 234 | return bits.GetD(); |
| 235 | } |
| 236 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 237 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 238 | inline void ArtField::SetDouble(mirror::Object* object, double d) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 239 | DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 240 | JValue bits; |
| 241 | bits.SetD(d); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 242 | Set64<kTransactionActive>(object, bits.GetJ()); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 245 | inline mirror::Object* ArtField::GetObject(mirror::Object* object) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 246 | DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField(this); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 247 | return GetObj(object); |
| 248 | } |
| 249 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 250 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 251 | inline void ArtField::SetObject(mirror::Object* object, mirror::Object* l) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 252 | DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField(this); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 253 | SetObj<kTransactionActive>(object, l); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 256 | inline const char* ArtField::GetName() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 257 | uint32_t field_index = GetDexFieldIndex(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 258 | if (UNLIKELY(GetDeclaringClass()->IsProxyClass())) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 259 | DCHECK(IsStatic()); |
| 260 | DCHECK_LT(field_index, 2U); |
| 261 | return field_index == 0 ? "interfaces" : "throws"; |
| 262 | } |
| 263 | const DexFile* dex_file = GetDexFile(); |
| 264 | return dex_file->GetFieldName(dex_file->GetFieldId(field_index)); |
| 265 | } |
| 266 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 267 | inline const char* ArtField::GetTypeDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 268 | uint32_t field_index = GetDexFieldIndex(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 269 | if (UNLIKELY(GetDeclaringClass()->IsProxyClass())) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 270 | DCHECK(IsStatic()); |
| 271 | DCHECK_LT(field_index, 2U); |
| 272 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
| 273 | return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;"; |
| 274 | } |
| 275 | const DexFile* dex_file = GetDexFile(); |
| 276 | const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index); |
| 277 | return dex_file->GetFieldTypeDescriptor(field_id); |
| 278 | } |
| 279 | |
| 280 | inline Primitive::Type ArtField::GetTypeAsPrimitiveType() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 281 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 282 | return Primitive::GetType(GetTypeDescriptor()[0]); |
| 283 | } |
| 284 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 285 | inline bool ArtField::IsPrimitiveType() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 286 | return GetTypeAsPrimitiveType() != Primitive::kPrimNot; |
| 287 | } |
| 288 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 289 | template <bool kResolve> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 290 | inline mirror::Class* ArtField::GetType() { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 291 | const uint32_t field_index = GetDexFieldIndex(); |
| 292 | auto* declaring_class = GetDeclaringClass(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 293 | if (UNLIKELY(declaring_class->IsProxyClass())) { |
| 294 | return ProxyFindSystemClass(GetTypeDescriptor()); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 295 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 296 | auto* dex_cache = declaring_class->GetDexCache(); |
| 297 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 298 | const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 299 | mirror::Class* type = dex_cache->GetResolvedType(field_id.type_idx_); |
| 300 | if (kResolve && UNLIKELY(type == nullptr)) { |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 301 | type = ResolveGetType(field_id.type_idx_); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 302 | CHECK(type != nullptr || Thread::Current()->IsExceptionPending()); |
| 303 | } |
| 304 | return type; |
| 305 | } |
| 306 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 307 | inline size_t ArtField::FieldSize() SHARED_REQUIRES(Locks::mutator_lock_) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 308 | return Primitive::ComponentSize(GetTypeAsPrimitiveType()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 311 | inline mirror::DexCache* ArtField::GetDexCache() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 312 | return GetDeclaringClass()->GetDexCache(); |
| 313 | } |
| 314 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 315 | inline const DexFile* ArtField::GetDexFile() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 316 | return GetDexCache()->GetDexFile(); |
| 317 | } |
| 318 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 319 | inline mirror::String* ArtField::GetStringName(Thread* self, bool resolve) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 320 | auto dex_field_index = GetDexFieldIndex(); |
| 321 | CHECK_NE(dex_field_index, DexFile::kDexNoIndex); |
| 322 | auto* dex_cache = GetDexCache(); |
| 323 | const auto* dex_file = dex_cache->GetDexFile(); |
| 324 | const auto& field_id = dex_file->GetFieldId(dex_field_index); |
| 325 | auto* name = dex_cache->GetResolvedString(field_id.name_idx_); |
| 326 | if (resolve && name == nullptr) { |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 327 | name = ResolveGetStringName(self, *dex_file, field_id.name_idx_, dex_cache); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 328 | } |
| 329 | return name; |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 332 | template<typename RootVisitorType> |
| 333 | inline void ArtField::VisitRoots(RootVisitorType& visitor) { |
| 334 | visitor.VisitRoot(declaring_class_.AddressWithoutBarrier()); |
| 335 | } |
| 336 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 337 | template <typename Visitor> |
| 338 | inline void ArtField::UpdateObjects(const Visitor& visitor) { |
| 339 | mirror::Class* old_class = DeclaringClassRoot().Read<kWithoutReadBarrier>(); |
| 340 | mirror::Class* new_class = visitor(old_class); |
| 341 | if (old_class != new_class) { |
| 342 | SetDeclaringClass(new_class); |
| 343 | } |
| 344 | } |
| 345 | |
Mathieu Chartier | ec15338 | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 346 | // If kExactOffset is true then we only find the matching offset, not the field containing the |
| 347 | // offset. |
| 348 | template <bool kExactOffset> |
| 349 | static inline ArtField* FindFieldWithOffset( |
| 350 | const IterationRange<StrideIterator<ArtField>>& fields, |
| 351 | uint32_t field_offset) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 352 | for (ArtField& field : fields) { |
| 353 | if (kExactOffset) { |
| 354 | if (field.GetOffset().Uint32Value() == field_offset) { |
| 355 | return &field; |
| 356 | } |
| 357 | } else { |
| 358 | const uint32_t offset = field.GetOffset().Uint32Value(); |
| 359 | Primitive::Type type = field.GetTypeAsPrimitiveType(); |
| 360 | const size_t field_size = Primitive::ComponentSize(type); |
| 361 | DCHECK_GT(field_size, 0u); |
| 362 | if (offset <= field_offset && field_offset < offset + field_size) { |
| 363 | return &field; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | return nullptr; |
| 368 | } |
| 369 | |
| 370 | template <bool kExactOffset> |
| 371 | inline ArtField* ArtField::FindInstanceFieldWithOffset(mirror::Class* klass, |
| 372 | uint32_t field_offset) { |
| 373 | DCHECK(klass != nullptr); |
| 374 | ArtField* field = FindFieldWithOffset<kExactOffset>(klass->GetIFields(), field_offset); |
| 375 | if (field != nullptr) { |
| 376 | return field; |
| 377 | } |
| 378 | // We did not find field in the class: look into superclass. |
| 379 | return (klass->GetSuperClass() != nullptr) ? |
| 380 | FindInstanceFieldWithOffset<kExactOffset>(klass->GetSuperClass(), field_offset) : nullptr; |
| 381 | } |
| 382 | |
| 383 | template <bool kExactOffset> |
| 384 | inline ArtField* ArtField::FindStaticFieldWithOffset(mirror::Class* klass, uint32_t field_offset) { |
| 385 | DCHECK(klass != nullptr); |
| 386 | return FindFieldWithOffset<kExactOffset>(klass->GetSFields(), field_offset); |
| 387 | } |
| 388 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 389 | } // namespace art |
| 390 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 391 | #endif // ART_RUNTIME_ART_FIELD_INL_H_ |