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 
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"
24 #include "object_callbacks.h"
25 #include "offsets.h"
26 #include "primitive.h"
27 #include "read_barrier_option.h"
28 
29 namespace art {
30 
31 class DexFile;
32 class ScopedObjectAccessAlreadyRunnable;
33 
34 namespace mirror {
35 class Class;
36 class DexCache;
37 class Object;
38 class String;
39 }  // namespace mirror
40 
41 class ArtField FINAL {
42  public:
43   ArtField();
44 
45   mirror::Class* GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
46 
47   void SetDeclaringClass(mirror::Class *new_declaring_class)
48       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
49 
50   uint32_t GetAccessFlags() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
51 
SetAccessFlags(uint32_t new_access_flags)52   void SetAccessFlags(uint32_t new_access_flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
53     // Not called within a transaction.
54     access_flags_ = new_access_flags;
55   }
56 
IsPublic()57   bool IsPublic() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
58     return (GetAccessFlags() & kAccPublic) != 0;
59   }
60 
IsStatic()61   bool IsStatic() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
62     return (GetAccessFlags() & kAccStatic) != 0;
63   }
64 
IsFinal()65   bool IsFinal() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
66     return (GetAccessFlags() & kAccFinal) != 0;
67   }
68 
GetDexFieldIndex()69   uint32_t GetDexFieldIndex() {
70     return field_dex_idx_;
71   }
72 
SetDexFieldIndex(uint32_t new_idx)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.
79   MemberOffset GetOffset() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80 
OffsetOffset()81   static MemberOffset OffsetOffset() {
82     return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
83   }
84 
85   MemberOffset GetOffsetDuringLinking() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86 
87   void SetOffset(MemberOffset num_bytes) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88 
89   // field access, null object for static fields
90   uint8_t GetBoolean(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91 
92   template<bool kTransactionActive>
93   void SetBoolean(mirror::Object* object, uint8_t z) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94 
95   int8_t GetByte(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96 
97   template<bool kTransactionActive>
98   void SetByte(mirror::Object* object, int8_t b) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
99 
100   uint16_t GetChar(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101 
102   template<bool kTransactionActive>
103   void SetChar(mirror::Object* object, uint16_t c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
104 
105   int16_t GetShort(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106 
107   template<bool kTransactionActive>
108   void SetShort(mirror::Object* object, int16_t s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
109 
110   int32_t GetInt(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111 
112   template<bool kTransactionActive>
113   void SetInt(mirror::Object* object, int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114 
115   int64_t GetLong(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116 
117   template<bool kTransactionActive>
118   void SetLong(mirror::Object* object, int64_t j) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
119 
120   float GetFloat(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121 
122   template<bool kTransactionActive>
123   void SetFloat(mirror::Object* object, float f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
124 
125   double GetDouble(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126 
127   template<bool kTransactionActive>
128   void SetDouble(mirror::Object* object, double d) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129 
130   mirror::Object* GetObject(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
131 
132   template<bool kTransactionActive>
133   void SetObject(mirror::Object* object, mirror::Object* l)
134       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135 
136   // Raw field accesses.
137   uint32_t Get32(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
138 
139   template<bool kTransactionActive>
140   void Set32(mirror::Object* object, uint32_t new_value)
141       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
142 
143   uint64_t Get64(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144 
145   template<bool kTransactionActive>
146   void Set64(mirror::Object* object, uint64_t new_value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147 
148   mirror::Object* GetObj(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
149 
150   template<bool kTransactionActive>
151   void SetObj(mirror::Object* object, mirror::Object* new_value)
152       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
153 
154   template<typename RootVisitorType>
155   void VisitRoots(RootVisitorType& visitor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156 
IsVolatile()157   bool IsVolatile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
158     return (GetAccessFlags() & kAccVolatile) != 0;
159   }
160 
161   // Returns an instance field with this offset in the given class or null if not found.
162   static ArtField* FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset)
163       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
164   // 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)
166       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
167 
168   const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
169 
170   // Resolves / returns the name from the dex cache.
171   mirror::String* GetStringName(Thread* self, bool resolve)
172       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
173 
174   const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
175 
176   Primitive::Type GetTypeAsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
177 
178   bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179 
180   template <bool kResolve>
181   mirror::Class* GetType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
182 
183   size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
184 
185   mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186 
187   const DexFile* GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
188 
DeclaringClassRoot()189   GcRoot<mirror::Class>& DeclaringClassRoot() {
190     return declaring_class_;
191   }
192 
193  private:
194   mirror::Class* ProxyFindSystemClass(const char* descriptor)
195       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
196   mirror::Class* ResolveGetType(uint32_t type_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
197   mirror::String* ResolveGetStringName(Thread* self, const DexFile& dex_file, uint32_t string_idx,
198                                        mirror::DexCache* dex_cache)
199       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
200 
201   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_
215