blob: 5114578933307b4338ad675f4328714a49c912bb [file] [log] [blame]
Mathieu Chartierc7853442015-03-27 14:35:38 -07001/*
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
17#ifndef ART_RUNTIME_ART_FIELD_H_
18#define ART_RUNTIME_ART_FIELD_H_
19
20#include <jni.h>
21
Andreas Gampea5b09a62016-11-17 15:21:22 -080022#include "dex_file_types.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "gc_root.h"
24#include "modifiers.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070025#include "obj_ptr.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070026#include "offsets.h"
27#include "primitive.h"
28#include "read_barrier_option.h"
29
30namespace art {
31
32class DexFile;
33class ScopedObjectAccessAlreadyRunnable;
34
35namespace mirror {
36class Class;
37class DexCache;
38class Object;
39class String;
40} // namespace mirror
41
Mathieu Chartiere401d142015-04-22 13:56:20 -070042class ArtField FINAL {
Mathieu Chartierc7853442015-03-27 14:35:38 -070043 public:
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -070044 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier3398c782016-09-30 10:27:43 -070045 ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070046
Mathieu Chartier3398c782016-09-30 10:27:43 -070047 void SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070048 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070049
Mathieu Chartier8c19d242017-03-06 12:35:10 -080050 mirror::CompressedReference<mirror::Object>* GetDeclaringClassAddressWithoutBarrier() {
51 return declaring_class_.AddressWithoutBarrier();
52 }
53
Andreas Gampea1d2f952017-04-20 22:53:58 -070054 uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) {
55 if (kIsDebugBuild) {
56 GetAccessFlagsDCheck();
57 }
58 return access_flags_;
59 }
Mathieu Chartierc7853442015-03-27 14:35:38 -070060
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070061 void SetAccessFlags(uint32_t new_access_flags) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070062 // Not called within a transaction.
63 access_flags_ = new_access_flags;
64 }
65
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 bool IsPublic() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070067 return (GetAccessFlags() & kAccPublic) != 0;
68 }
69
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070071 return (GetAccessFlags() & kAccStatic) != 0;
72 }
73
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070074 bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070075 return (GetAccessFlags() & kAccFinal) != 0;
76 }
77
78 uint32_t GetDexFieldIndex() {
79 return field_dex_idx_;
80 }
81
82 void SetDexFieldIndex(uint32_t new_idx) {
83 // Not called within a transaction.
84 field_dex_idx_ = new_idx;
85 }
86
87 // Offset to field within an Object.
Andreas Gampea1d2f952017-04-20 22:53:58 -070088 MemberOffset GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) {
89 if (kIsDebugBuild) {
90 GetOffsetDCheck();
91 }
92 return MemberOffset(offset_);
93 }
Mathieu Chartierc7853442015-03-27 14:35:38 -070094
95 static MemberOffset OffsetOffset() {
96 return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
97 }
98
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 MemberOffset GetOffsetDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700100
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 void SetOffset(MemberOffset num_bytes) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700102
103 // field access, null object for static fields
Mathieu Chartier3398c782016-09-30 10:27:43 -0700104 uint8_t GetBoolean(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700105
106 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700107 void SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700108
Mathieu Chartier3398c782016-09-30 10:27:43 -0700109 int8_t GetByte(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700110
111 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700112 void SetByte(ObjPtr<mirror::Object> object, int8_t b) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700113
Mathieu Chartier3398c782016-09-30 10:27:43 -0700114 uint16_t GetChar(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700115
116 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700117 void SetChar(ObjPtr<mirror::Object> object, uint16_t c) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700118
Mathieu Chartier3398c782016-09-30 10:27:43 -0700119 int16_t GetShort(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700120
121 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700122 void SetShort(ObjPtr<mirror::Object> object, int16_t s) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700123
Mathieu Chartier3398c782016-09-30 10:27:43 -0700124 int32_t GetInt(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700125
126 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700127 void SetInt(ObjPtr<mirror::Object> object, int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700128
Mathieu Chartier3398c782016-09-30 10:27:43 -0700129 int64_t GetLong(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700130
131 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700132 void SetLong(ObjPtr<mirror::Object> object, int64_t j) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700133
Mathieu Chartier3398c782016-09-30 10:27:43 -0700134 float GetFloat(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700135
136 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700137 void SetFloat(ObjPtr<mirror::Object> object, float f) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700138
Mathieu Chartier3398c782016-09-30 10:27:43 -0700139 double GetDouble(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700140
141 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700142 void SetDouble(ObjPtr<mirror::Object> object, double d) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700143
Mathieu Chartier3398c782016-09-30 10:27:43 -0700144 ObjPtr<mirror::Object> GetObject(ObjPtr<mirror::Object> object)
145 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700146
147 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700148 void SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700150
151 // Raw field accesses.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700152 uint32_t Get32(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700153
154 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700155 void Set32(ObjPtr<mirror::Object> object, uint32_t new_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700156 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700157
Mathieu Chartier3398c782016-09-30 10:27:43 -0700158 uint64_t Get64(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700159
160 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700161 void Set64(ObjPtr<mirror::Object> object, uint64_t new_value)
162 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700163
Mathieu Chartier3398c782016-09-30 10:27:43 -0700164 template<class MirrorType = mirror::Object>
165 ObjPtr<MirrorType> GetObj(ObjPtr<mirror::Object> object)
166 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700167
168 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700169 void SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700170 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700171
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700172 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700173 template<typename RootVisitorType>
Andreas Gamped4901292017-05-30 18:41:34 -0700174 ALWAYS_INLINE inline void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS {
175 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
176 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700177
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700179 return (GetAccessFlags() & kAccVolatile) != 0;
180 }
181
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700182 // Returns an instance field with this offset in the given class or null if not found.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700183 // If kExactOffset is true then we only find the matching offset, not the field containing the
184 // offset.
185 template <bool kExactOffset = true>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700186 static ArtField* FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700187 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700188
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700189 // Returns a static field with this offset in the given class or null if not found.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700190 // If kExactOffset is true then we only find the matching offset, not the field containing the
191 // offset.
192 template <bool kExactOffset = true>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700193 static ArtField* FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700194 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700195
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700196 const char* GetName() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700197
198 // Resolves / returns the name from the dex cache.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700199 ObjPtr<mirror::String> GetStringName(Thread* self, bool resolve)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700200 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700201
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 const char* GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700203
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700207
208 template <bool kResolve>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700209 ObjPtr<mirror::Class> GetType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 size_t FieldSize() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700212
Mathieu Chartier3398c782016-09-30 10:27:43 -0700213 ObjPtr<mirror::DexCache> GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 const DexFile* GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700216
217 GcRoot<mirror::Class>& DeclaringClassRoot() {
218 return declaring_class_;
219 }
220
David Sehr709b0702016-10-13 09:12:37 -0700221 // Returns a human-readable signature. Something like "a.b.C.f" or
222 // "int a.b.C.f" (depending on the value of 'with_type').
223 static std::string PrettyField(ArtField* f, bool with_type = true)
224 REQUIRES_SHARED(Locks::mutator_lock_);
225 std::string PrettyField(bool with_type = true)
226 REQUIRES_SHARED(Locks::mutator_lock_);
227
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800228 // Update the declaring class with the passed in visitor. Does not use read barrier.
229 template <typename Visitor>
230 ALWAYS_INLINE void UpdateObjects(const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800232
Mathieu Chartierc7853442015-03-27 14:35:38 -0700233 private:
Mathieu Chartier3398c782016-09-30 10:27:43 -0700234 ObjPtr<mirror::Class> ProxyFindSystemClass(const char* descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700236 ObjPtr<mirror::String> ResolveGetStringName(Thread* self,
237 const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800238 dex::StringIndex string_idx,
Mathieu Chartier3398c782016-09-30 10:27:43 -0700239 ObjPtr<mirror::DexCache> dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko3481ba22015-04-13 12:22:36 +0100241
Andreas Gampea1d2f952017-04-20 22:53:58 -0700242 void GetAccessFlagsDCheck() REQUIRES_SHARED(Locks::mutator_lock_);
243 void GetOffsetDCheck() REQUIRES_SHARED(Locks::mutator_lock_);
244
Mathieu Chartierc7853442015-03-27 14:35:38 -0700245 GcRoot<mirror::Class> declaring_class_;
246
Mathieu Chartier3398c782016-09-30 10:27:43 -0700247 uint32_t access_flags_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700248
249 // Dex cache index of field id
Mathieu Chartier3398c782016-09-30 10:27:43 -0700250 uint32_t field_dex_idx_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700251
252 // Offset of field within an instance or in the Class' static fields
Mathieu Chartier3398c782016-09-30 10:27:43 -0700253 uint32_t offset_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700254};
255
256} // namespace art
257
258#endif // ART_RUNTIME_ART_FIELD_H_