blob: a943a34174f80281bd51e15ada8a0f155b6e4faf [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
Mathieu Chartier90443472015-07-16 20:32:27 -070044 mirror::Class* GetDeclaringClass() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070045
46 void SetDeclaringClass(mirror::Class *new_declaring_class)
Mathieu Chartier90443472015-07-16 20:32:27 -070047 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070048
Mathieu Chartier90443472015-07-16 20:32:27 -070049 uint32_t GetAccessFlags() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070050
Mathieu Chartier90443472015-07-16 20:32:27 -070051 void SetAccessFlags(uint32_t new_access_flags) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070052 // Not called within a transaction.
53 access_flags_ = new_access_flags;
54 }
55
Mathieu Chartier90443472015-07-16 20:32:27 -070056 bool IsPublic() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070057 return (GetAccessFlags() & kAccPublic) != 0;
58 }
59
Mathieu Chartier90443472015-07-16 20:32:27 -070060 bool IsStatic() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070061 return (GetAccessFlags() & kAccStatic) != 0;
62 }
63
Mathieu Chartier90443472015-07-16 20:32:27 -070064 bool IsFinal() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070065 return (GetAccessFlags() & kAccFinal) != 0;
66 }
67
68 uint32_t GetDexFieldIndex() {
69 return field_dex_idx_;
70 }
71
72 void SetDexFieldIndex(uint32_t new_idx) {
73 // Not called within a transaction.
74 field_dex_idx_ = new_idx;
75 }
76
77 // Offset to field within an Object.
Mathieu Chartier90443472015-07-16 20:32:27 -070078 MemberOffset GetOffset() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070079
80 static MemberOffset OffsetOffset() {
81 return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
82 }
83
Mathieu Chartier90443472015-07-16 20:32:27 -070084 MemberOffset GetOffsetDuringLinking() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070085
Mathieu Chartier90443472015-07-16 20:32:27 -070086 void SetOffset(MemberOffset num_bytes) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070087
88 // field access, null object for static fields
Mathieu Chartier90443472015-07-16 20:32:27 -070089 uint8_t GetBoolean(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070090
91 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -070092 void SetBoolean(mirror::Object* object, uint8_t z) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070093
Mathieu Chartier90443472015-07-16 20:32:27 -070094 int8_t GetByte(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070095
96 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -070097 void SetByte(mirror::Object* object, int8_t b) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070098
Mathieu Chartier90443472015-07-16 20:32:27 -070099 uint16_t GetChar(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700100
101 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700102 void SetChar(mirror::Object* object, uint16_t c) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700103
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 int16_t GetShort(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700105
106 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 void SetShort(mirror::Object* object, int16_t s) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700108
Mathieu Chartier90443472015-07-16 20:32:27 -0700109 int32_t GetInt(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700110
111 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 void SetInt(mirror::Object* object, int32_t i) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700113
Mathieu Chartier90443472015-07-16 20:32:27 -0700114 int64_t GetLong(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700115
116 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 void SetLong(mirror::Object* object, int64_t j) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700118
Mathieu Chartier90443472015-07-16 20:32:27 -0700119 float GetFloat(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700120
121 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 void SetFloat(mirror::Object* object, float f) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700123
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 double GetDouble(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700125
126 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700127 void SetDouble(mirror::Object* object, double d) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700128
Mathieu Chartier90443472015-07-16 20:32:27 -0700129 mirror::Object* GetObject(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700130
131 template<bool kTransactionActive>
132 void SetObject(mirror::Object* object, mirror::Object* l)
Mathieu Chartier90443472015-07-16 20:32:27 -0700133 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700134
135 // Raw field accesses.
Mathieu Chartier90443472015-07-16 20:32:27 -0700136 uint32_t Get32(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700137
138 template<bool kTransactionActive>
139 void Set32(mirror::Object* object, uint32_t new_value)
Mathieu Chartier90443472015-07-16 20:32:27 -0700140 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700141
Mathieu Chartier90443472015-07-16 20:32:27 -0700142 uint64_t Get64(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700143
144 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700145 void Set64(mirror::Object* object, uint64_t new_value) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700146
Mathieu Chartier90443472015-07-16 20:32:27 -0700147 mirror::Object* GetObj(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700148
149 template<bool kTransactionActive>
150 void SetObj(mirror::Object* object, mirror::Object* new_value)
Mathieu Chartier90443472015-07-16 20:32:27 -0700151 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700152
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700153 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 template<typename RootVisitorType>
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700155 void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700156
Mathieu Chartier90443472015-07-16 20:32:27 -0700157 bool IsVolatile() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700158 return (GetAccessFlags() & kAccVolatile) != 0;
159 }
160
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700161 // Returns an instance field with this offset in the given class or null if not found.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700162 static ArtField* FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700164 // Returns a static field with this offset in the given class or null if not found.
165 static ArtField* FindStaticFieldWithOffset(mirror::Class* klass, uint32_t field_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700167
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 const char* GetName() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700169
170 // Resolves / returns the name from the dex cache.
171 mirror::String* GetStringName(Thread* self, bool resolve)
Mathieu Chartier90443472015-07-16 20:32:27 -0700172 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700173
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 const char* GetTypeDescriptor() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700175
Mathieu Chartier90443472015-07-16 20:32:27 -0700176 Primitive::Type GetTypeAsPrimitiveType() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700177
Mathieu Chartier90443472015-07-16 20:32:27 -0700178 bool IsPrimitiveType() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700179
180 template <bool kResolve>
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 mirror::Class* GetType() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700182
Mathieu Chartier90443472015-07-16 20:32:27 -0700183 size_t FieldSize() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700184
Mathieu Chartier90443472015-07-16 20:32:27 -0700185 mirror::DexCache* GetDexCache() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700186
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 const DexFile* GetDexFile() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700188
189 GcRoot<mirror::Class>& DeclaringClassRoot() {
190 return declaring_class_;
191 }
192
193 private:
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000194 mirror::Class* ProxyFindSystemClass(const char* descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 SHARED_REQUIRES(Locks::mutator_lock_);
196 mirror::Class* ResolveGetType(uint32_t type_idx) SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko3481ba22015-04-13 12:22:36 +0100197 mirror::String* ResolveGetStringName(Thread* self, const DexFile& dex_file, uint32_t string_idx,
198 mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700199 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko3481ba22015-04-13 12:22:36 +0100200
Mathieu Chartierc7853442015-03-27 14:35:38 -0700201 GcRoot<mirror::Class> declaring_class_;
202
203 uint32_t access_flags_;
204
205 // Dex cache index of field id
206 uint32_t field_dex_idx_;
207
208 // Offset of field within an instance or in the Class' static fields
209 uint32_t offset_;
210};
211
212} // namespace art
213
214#endif // ART_RUNTIME_ART_FIELD_H_