blob: d91149701e836e328ccfffcbde93ec5d47ebf730 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Chartierc7853442015-03-27 14:35:38 -070017#ifndef ART_RUNTIME_ART_FIELD_INL_H_
18#define ART_RUNTIME_ART_FIELD_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080022#include "base/logging.h"
Ian Rogers08f1f502014-12-02 15:04:37 -080023#include "class_linker.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010024#include "gc_root-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include "gc/accounting/card_table-inl.h"
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080026#include "jvalue.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010027#include "mirror/dex_cache-inl.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070028#include "mirror/object-inl.h"
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080029#include "primitive.h"
Mathieu Chartier76433272014-09-26 14:32:37 -070030#include "thread-inl.h"
31#include "scoped_thread_state_change.h"
32#include "well_known_classes.h"
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080033
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035
Mathieu Chartierc7853442015-03-27 14:35:38 -070036inline mirror::Class* ArtField::GetDeclaringClass() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070037 GcRootSource gc_root_source(this);
38 mirror::Class* result = declaring_class_.Read(&gc_root_source);
Mathieu Chartierc7853442015-03-27 14:35:38 -070039 DCHECK(result != nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080040 DCHECK(result->IsLoaded() || result->IsErroneous()) << result->GetStatus();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 return result;
42}
43
Mathieu Chartierc7853442015-03-27 14:35:38 -070044inline void ArtField::SetDeclaringClass(mirror::Class* new_declaring_class) {
45 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046}
47
Ian Rogersef7d42f2014-01-06 12:55:46 -080048inline uint32_t ArtField::GetAccessFlags() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Mathieu Chartierc7853442015-03-27 14:35:38 -070050 return access_flags_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051}
52
Ian Rogersef7d42f2014-01-06 12:55:46 -080053inline MemberOffset ArtField::GetOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Mathieu Chartierc7853442015-03-27 14:35:38 -070055 return MemberOffset(offset_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056}
57
Ian Rogersef7d42f2014-01-06 12:55:46 -080058inline MemberOffset ArtField::GetOffsetDuringLinking() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Mathieu Chartierc7853442015-03-27 14:35:38 -070060 return MemberOffset(offset_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061}
62
Mathieu Chartierc7853442015-03-27 14:35:38 -070063inline uint32_t ArtField::Get32(mirror::Object* object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070064 DCHECK(object != nullptr) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080065 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070066 if (UNLIKELY(IsVolatile())) {
67 return object->GetField32Volatile(GetOffset());
68 }
69 return object->GetField32(GetOffset());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080070}
71
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010072template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -070073inline void ArtField::Set32(mirror::Object* object, uint32_t new_value) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070074 DCHECK(object != nullptr) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080075 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070076 if (UNLIKELY(IsVolatile())) {
77 object->SetField32Volatile<kTransactionActive>(GetOffset(), new_value);
78 } else {
79 object->SetField32<kTransactionActive>(GetOffset(), new_value);
80 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080081}
82
Mathieu Chartierc7853442015-03-27 14:35:38 -070083inline uint64_t ArtField::Get64(mirror::Object* object) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 DCHECK(object != nullptr) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080085 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070086 if (UNLIKELY(IsVolatile())) {
87 return object->GetField64Volatile(GetOffset());
88 }
89 return object->GetField64(GetOffset());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080090}
91
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010092template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -070093inline void ArtField::Set64(mirror::Object* object, uint64_t new_value) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 DCHECK(object != nullptr) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080095 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070096 if (UNLIKELY(IsVolatile())) {
97 object->SetField64Volatile<kTransactionActive>(GetOffset(), new_value);
98 } else {
99 object->SetField64<kTransactionActive>(GetOffset(), new_value);
100 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800101}
102
Mathieu Chartierc7853442015-03-27 14:35:38 -0700103inline mirror::Object* ArtField::GetObj(mirror::Object* object) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700104 DCHECK(object != nullptr) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800105 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700106 if (UNLIKELY(IsVolatile())) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700107 return object->GetFieldObjectVolatile<mirror::Object>(GetOffset());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700108 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700109 return object->GetFieldObject<mirror::Object>(GetOffset());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800110}
111
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100112template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700113inline void ArtField::SetObj(mirror::Object* object, mirror::Object* new_value) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700114 DCHECK(object != nullptr) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800115 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700116 if (UNLIKELY(IsVolatile())) {
117 object->SetFieldObjectVolatile<kTransactionActive>(GetOffset(), new_value);
118 } else {
119 object->SetFieldObject<kTransactionActive>(GetOffset(), new_value);
120 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800121}
122
Fred Shih37f05ef2014-07-16 18:38:08 -0700123#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 Chartierc7853442015-03-27 14:35:38 -0700142inline uint8_t ArtField::GetBoolean(mirror::Object* object) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700143 FIELD_GET(object, Boolean);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800144}
145
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700147inline void ArtField::SetBoolean(mirror::Object* object, uint8_t z) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700148 FIELD_SET(object, Boolean, z);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800149}
150
Mathieu Chartierc7853442015-03-27 14:35:38 -0700151inline int8_t ArtField::GetByte(mirror::Object* object) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700152 FIELD_GET(object, Byte);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800153}
154
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100155template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700156inline void ArtField::SetByte(mirror::Object* object, int8_t b) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700157 FIELD_SET(object, Byte, b);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800158}
159
Mathieu Chartierc7853442015-03-27 14:35:38 -0700160inline uint16_t ArtField::GetChar(mirror::Object* object) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700161 FIELD_GET(object, Char);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800162}
163
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100164template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700165inline void ArtField::SetChar(mirror::Object* object, uint16_t c) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700166 FIELD_SET(object, Char, c);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800167}
168
Mathieu Chartierc7853442015-03-27 14:35:38 -0700169inline int16_t ArtField::GetShort(mirror::Object* object) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700170 FIELD_GET(object, Short);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800171}
172
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100173template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700174inline void ArtField::SetShort(mirror::Object* object, int16_t s) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700175 FIELD_SET(object, Short, s);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800176}
177
Fred Shih37f05ef2014-07-16 18:38:08 -0700178#undef FIELD_GET
179#undef FIELD_SET
180
Mathieu Chartierc7853442015-03-27 14:35:38 -0700181inline int32_t ArtField::GetInt(mirror::Object* object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100182 if (kIsDebugBuild) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700183 Primitive::Type type = GetTypeAsPrimitiveType();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100184 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this);
185 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800186 return Get32(object);
187}
188
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100189template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700190inline void ArtField::SetInt(mirror::Object* object, int32_t i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100191 if (kIsDebugBuild) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700192 Primitive::Type type = GetTypeAsPrimitiveType();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100193 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this);
194 }
195 Set32<kTransactionActive>(object, i);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800196}
197
Mathieu Chartierc7853442015-03-27 14:35:38 -0700198inline int64_t ArtField::GetLong(mirror::Object* object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100199 if (kIsDebugBuild) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700200 Primitive::Type type = GetTypeAsPrimitiveType();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100201 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this);
202 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800203 return Get64(object);
204}
205
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100206template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700207inline void ArtField::SetLong(mirror::Object* object, int64_t j) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100208 if (kIsDebugBuild) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700209 Primitive::Type type = GetTypeAsPrimitiveType();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100210 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this);
211 }
212 Set64<kTransactionActive>(object, j);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800213}
214
Mathieu Chartierc7853442015-03-27 14:35:38 -0700215inline float ArtField::GetFloat(mirror::Object* object) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700216 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800217 JValue bits;
218 bits.SetI(Get32(object));
219 return bits.GetF();
220}
221
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100222template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700223inline void ArtField::SetFloat(mirror::Object* object, float f) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700224 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800225 JValue bits;
226 bits.SetF(f);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100227 Set32<kTransactionActive>(object, bits.GetI());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800228}
229
Mathieu Chartierc7853442015-03-27 14:35:38 -0700230inline double ArtField::GetDouble(mirror::Object* object) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700231 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800232 JValue bits;
233 bits.SetJ(Get64(object));
234 return bits.GetD();
235}
236
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100237template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700238inline void ArtField::SetDouble(mirror::Object* object, double d) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700239 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800240 JValue bits;
241 bits.SetD(d);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100242 Set64<kTransactionActive>(object, bits.GetJ());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800243}
244
Mathieu Chartierc7853442015-03-27 14:35:38 -0700245inline mirror::Object* ArtField::GetObject(mirror::Object* object) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700246 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField(this);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800247 return GetObj(object);
248}
249
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100250template<bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700251inline void ArtField::SetObject(mirror::Object* object, mirror::Object* l) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700252 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField(this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100253 SetObj<kTransactionActive>(object, l);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800254}
255
Mathieu Chartier90443472015-07-16 20:32:27 -0700256inline const char* ArtField::GetName() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700257 uint32_t field_index = GetDexFieldIndex();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000258 if (UNLIKELY(GetDeclaringClass()->IsProxyClass())) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700259 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 Chartier90443472015-07-16 20:32:27 -0700267inline const char* ArtField::GetTypeDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700268 uint32_t field_index = GetDexFieldIndex();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000269 if (UNLIKELY(GetDeclaringClass()->IsProxyClass())) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700270 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
280inline Primitive::Type ArtField::GetTypeAsPrimitiveType()
Mathieu Chartier90443472015-07-16 20:32:27 -0700281 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700282 return Primitive::GetType(GetTypeDescriptor()[0]);
283}
284
Mathieu Chartier90443472015-07-16 20:32:27 -0700285inline bool ArtField::IsPrimitiveType() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700286 return GetTypeAsPrimitiveType() != Primitive::kPrimNot;
287}
288
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700289template <bool kResolve>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700290inline mirror::Class* ArtField::GetType() {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700291 const uint32_t field_index = GetDexFieldIndex();
292 auto* declaring_class = GetDeclaringClass();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000293 if (UNLIKELY(declaring_class->IsProxyClass())) {
294 return ProxyFindSystemClass(GetTypeDescriptor());
Ian Rogers08f1f502014-12-02 15:04:37 -0800295 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700296 auto* dex_cache = declaring_class->GetDexCache();
297 const DexFile* const dex_file = dex_cache->GetDexFile();
Ian Rogers08f1f502014-12-02 15:04:37 -0800298 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700299 mirror::Class* type = dex_cache->GetResolvedType(field_id.type_idx_);
300 if (kResolve && UNLIKELY(type == nullptr)) {
Vladimir Marko3481ba22015-04-13 12:22:36 +0100301 type = ResolveGetType(field_id.type_idx_);
Ian Rogers08f1f502014-12-02 15:04:37 -0800302 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
303 }
304 return type;
305}
306
Mathieu Chartier90443472015-07-16 20:32:27 -0700307inline size_t ArtField::FieldSize() SHARED_REQUIRES(Locks::mutator_lock_) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700308 return Primitive::ComponentSize(GetTypeAsPrimitiveType());
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700309}
310
Mathieu Chartier90443472015-07-16 20:32:27 -0700311inline mirror::DexCache* ArtField::GetDexCache() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700312 return GetDeclaringClass()->GetDexCache();
313}
314
Mathieu Chartier90443472015-07-16 20:32:27 -0700315inline const DexFile* ArtField::GetDexFile() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700316 return GetDexCache()->GetDexFile();
317}
318
Mathieu Chartierc7853442015-03-27 14:35:38 -0700319inline mirror::String* ArtField::GetStringName(Thread* self, bool resolve) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700320 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 Marko3481ba22015-04-13 12:22:36 +0100327 name = ResolveGetStringName(self, *dex_file, field_id.name_idx_, dex_cache);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700328 }
329 return name;
Mathieu Chartier76433272014-09-26 14:32:37 -0700330}
331
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332template<typename RootVisitorType>
333inline void ArtField::VisitRoots(RootVisitorType& visitor) {
334 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
335}
336
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800337template <typename Visitor>
338inline 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 Chartierec153382016-04-01 13:56:41 -0700346// If kExactOffset is true then we only find the matching offset, not the field containing the
347// offset.
348template <bool kExactOffset>
349static 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
370template <bool kExactOffset>
371inline 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
383template <bool kExactOffset>
384inline 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800389} // namespace art
390
Mathieu Chartierc7853442015-03-27 14:35:38 -0700391#endif // ART_RUNTIME_ART_FIELD_INL_H_