blob: aaccbf3699babbd5c91b45dc0571f33b5d43e8c3 [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
22#include "gc_root.h"
23#include "modifiers.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "offsets.h"
25#include "primitive.h"
26#include "read_barrier_option.h"
27
28namespace art {
29
30class DexFile;
31class ScopedObjectAccessAlreadyRunnable;
32
33namespace mirror {
34class Class;
35class DexCache;
36class Object;
37class String;
38} // namespace mirror
39
Mathieu Chartiere401d142015-04-22 13:56:20 -070040class ArtField FINAL {
Mathieu Chartierc7853442015-03-27 14:35:38 -070041 public:
42 ArtField();
43
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -070044 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier90443472015-07-16 20:32:27 -070045 mirror::Class* GetDeclaringClass() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070046
47 void SetDeclaringClass(mirror::Class *new_declaring_class)
Mathieu Chartier90443472015-07-16 20:32:27 -070048 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070049
Mathieu Chartier90443472015-07-16 20:32:27 -070050 uint32_t GetAccessFlags() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070051
Mathieu Chartier90443472015-07-16 20:32:27 -070052 void SetAccessFlags(uint32_t new_access_flags) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070053 // Not called within a transaction.
54 access_flags_ = new_access_flags;
55 }
56
Mathieu Chartier90443472015-07-16 20:32:27 -070057 bool IsPublic() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070058 return (GetAccessFlags() & kAccPublic) != 0;
59 }
60
Mathieu Chartier90443472015-07-16 20:32:27 -070061 bool IsStatic() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070062 return (GetAccessFlags() & kAccStatic) != 0;
63 }
64
Mathieu Chartier90443472015-07-16 20:32:27 -070065 bool IsFinal() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070066 return (GetAccessFlags() & kAccFinal) != 0;
67 }
68
69 uint32_t GetDexFieldIndex() {
70 return field_dex_idx_;
71 }
72
73 void SetDexFieldIndex(uint32_t new_idx) {
74 // Not called within a transaction.
75 field_dex_idx_ = new_idx;
76 }
77
78 // Offset to field within an Object.
Mathieu Chartier90443472015-07-16 20:32:27 -070079 MemberOffset GetOffset() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070080
81 static MemberOffset OffsetOffset() {
82 return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
83 }
84
Mathieu Chartier90443472015-07-16 20:32:27 -070085 MemberOffset GetOffsetDuringLinking() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070086
Mathieu Chartier90443472015-07-16 20:32:27 -070087 void SetOffset(MemberOffset num_bytes) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070088
89 // field access, null object for static fields
Mathieu Chartier90443472015-07-16 20:32:27 -070090 uint8_t GetBoolean(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070091
92 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -070093 void SetBoolean(mirror::Object* object, uint8_t z) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070094
Mathieu Chartier90443472015-07-16 20:32:27 -070095 int8_t GetByte(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070096
97 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -070098 void SetByte(mirror::Object* object, int8_t b) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070099
Mathieu Chartier90443472015-07-16 20:32:27 -0700100 uint16_t GetChar(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700101
102 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 void SetChar(mirror::Object* object, uint16_t c) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700104
Mathieu Chartier90443472015-07-16 20:32:27 -0700105 int16_t GetShort(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700106
107 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700108 void SetShort(mirror::Object* object, int16_t s) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700109
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 int32_t GetInt(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700111
112 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700113 void SetInt(mirror::Object* object, int32_t i) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700114
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 int64_t GetLong(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700116
117 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 void SetLong(mirror::Object* object, int64_t j) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700119
Mathieu Chartier90443472015-07-16 20:32:27 -0700120 float GetFloat(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700121
122 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700123 void SetFloat(mirror::Object* object, float f) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700124
Mathieu Chartier90443472015-07-16 20:32:27 -0700125 double GetDouble(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700126
127 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 void SetDouble(mirror::Object* object, double d) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700129
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 mirror::Object* GetObject(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700131
132 template<bool kTransactionActive>
133 void SetObject(mirror::Object* object, mirror::Object* l)
Mathieu Chartier90443472015-07-16 20:32:27 -0700134 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700135
136 // Raw field accesses.
Mathieu Chartier90443472015-07-16 20:32:27 -0700137 uint32_t Get32(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700138
139 template<bool kTransactionActive>
140 void Set32(mirror::Object* object, uint32_t new_value)
Mathieu Chartier90443472015-07-16 20:32:27 -0700141 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700142
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 uint64_t Get64(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700144
145 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700146 void Set64(mirror::Object* object, uint64_t new_value) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700147
Mathieu Chartier90443472015-07-16 20:32:27 -0700148 mirror::Object* GetObj(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700149
150 template<bool kTransactionActive>
151 void SetObj(mirror::Object* object, mirror::Object* new_value)
Mathieu Chartier90443472015-07-16 20:32:27 -0700152 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700153
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700154 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155 template<typename RootVisitorType>
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700156 void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700157
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 bool IsVolatile() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700159 return (GetAccessFlags() & kAccVolatile) != 0;
160 }
161
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700162 // Returns an instance field with this offset in the given class or null if not found.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700163 // If kExactOffset is true then we only find the matching offset, not the field containing the
164 // offset.
165 template <bool kExactOffset = true>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700166 static ArtField* FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700168
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700169 // Returns a static field with this offset in the given class or null if not found.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700170 // If kExactOffset is true then we only find the matching offset, not the field containing the
171 // offset.
172 template <bool kExactOffset = true>
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700173 static ArtField* FindStaticFieldWithOffset(mirror::Class* klass, uint32_t field_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700175
Mathieu Chartier90443472015-07-16 20:32:27 -0700176 const char* GetName() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700177
178 // Resolves / returns the name from the dex cache.
179 mirror::String* GetStringName(Thread* self, bool resolve)
Mathieu Chartier90443472015-07-16 20:32:27 -0700180 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700181
Mathieu Chartier90443472015-07-16 20:32:27 -0700182 const char* GetTypeDescriptor() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700183
Mathieu Chartier90443472015-07-16 20:32:27 -0700184 Primitive::Type GetTypeAsPrimitiveType() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700185
Mathieu Chartier90443472015-07-16 20:32:27 -0700186 bool IsPrimitiveType() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700187
188 template <bool kResolve>
Mathieu Chartier90443472015-07-16 20:32:27 -0700189 mirror::Class* GetType() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700190
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 size_t FieldSize() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700192
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 mirror::DexCache* GetDexCache() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700194
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 const DexFile* GetDexFile() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700196
197 GcRoot<mirror::Class>& DeclaringClassRoot() {
198 return declaring_class_;
199 }
200
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800201 // Update the declaring class with the passed in visitor. Does not use read barrier.
202 template <typename Visitor>
203 ALWAYS_INLINE void UpdateObjects(const Visitor& visitor)
204 SHARED_REQUIRES(Locks::mutator_lock_);
205
Mathieu Chartierc7853442015-03-27 14:35:38 -0700206 private:
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000207 mirror::Class* ProxyFindSystemClass(const char* descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700208 SHARED_REQUIRES(Locks::mutator_lock_);
209 mirror::Class* ResolveGetType(uint32_t type_idx) SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko3481ba22015-04-13 12:22:36 +0100210 mirror::String* ResolveGetStringName(Thread* self, const DexFile& dex_file, uint32_t string_idx,
211 mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700212 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko3481ba22015-04-13 12:22:36 +0100213
Mathieu Chartierc7853442015-03-27 14:35:38 -0700214 GcRoot<mirror::Class> declaring_class_;
215
216 uint32_t access_flags_;
217
218 // Dex cache index of field id
219 uint32_t field_dex_idx_;
220
221 // Offset of field within an instance or in the Class' static fields
222 uint32_t offset_;
223};
224
225} // namespace art
226
227#endif // ART_RUNTIME_ART_FIELD_H_