art_field.h revision 3398c7874e002beaa6c2b2fadf183e7d1ddad23a
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 "obj_ptr.h"
25#include "offsets.h"
26#include "primitive.h"
27#include "read_barrier_option.h"
28
29namespace art {
30
31class DexFile;
32class ScopedObjectAccessAlreadyRunnable;
33
34namespace mirror {
35class Class;
36class DexCache;
37class Object;
38class String;
39}  // namespace mirror
40
41class ArtField FINAL {
42 public:
43  template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
44  ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
45
46  void SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class)
47      REQUIRES_SHARED(Locks::mutator_lock_);
48
49  uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_);
50
51  void SetAccessFlags(uint32_t new_access_flags) REQUIRES_SHARED(Locks::mutator_lock_) {
52    // Not called within a transaction.
53    access_flags_ = new_access_flags;
54  }
55
56  bool IsPublic() REQUIRES_SHARED(Locks::mutator_lock_) {
57    return (GetAccessFlags() & kAccPublic) != 0;
58  }
59
60  bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) {
61    return (GetAccessFlags() & kAccStatic) != 0;
62  }
63
64  bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) {
65    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.
78  MemberOffset GetOffset() REQUIRES_SHARED(Locks::mutator_lock_);
79
80  static MemberOffset OffsetOffset() {
81    return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
82  }
83
84  MemberOffset GetOffsetDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_);
85
86  void SetOffset(MemberOffset num_bytes) REQUIRES_SHARED(Locks::mutator_lock_);
87
88  // field access, null object for static fields
89  uint8_t GetBoolean(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
90
91  template<bool kTransactionActive>
92  void SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) REQUIRES_SHARED(Locks::mutator_lock_);
93
94  int8_t GetByte(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
95
96  template<bool kTransactionActive>
97  void SetByte(ObjPtr<mirror::Object> object, int8_t b) REQUIRES_SHARED(Locks::mutator_lock_);
98
99  uint16_t GetChar(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
100
101  template<bool kTransactionActive>
102  void SetChar(ObjPtr<mirror::Object> object, uint16_t c) REQUIRES_SHARED(Locks::mutator_lock_);
103
104  int16_t GetShort(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
105
106  template<bool kTransactionActive>
107  void SetShort(ObjPtr<mirror::Object> object, int16_t s) REQUIRES_SHARED(Locks::mutator_lock_);
108
109  int32_t GetInt(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
110
111  template<bool kTransactionActive>
112  void SetInt(ObjPtr<mirror::Object> object, int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
113
114  int64_t GetLong(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
115
116  template<bool kTransactionActive>
117  void SetLong(ObjPtr<mirror::Object> object, int64_t j) REQUIRES_SHARED(Locks::mutator_lock_);
118
119  float GetFloat(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
120
121  template<bool kTransactionActive>
122  void SetFloat(ObjPtr<mirror::Object> object, float f) REQUIRES_SHARED(Locks::mutator_lock_);
123
124  double GetDouble(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
125
126  template<bool kTransactionActive>
127  void SetDouble(ObjPtr<mirror::Object> object, double d) REQUIRES_SHARED(Locks::mutator_lock_);
128
129  ObjPtr<mirror::Object> GetObject(ObjPtr<mirror::Object> object)
130      REQUIRES_SHARED(Locks::mutator_lock_);
131
132  template<bool kTransactionActive>
133  void SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l)
134      REQUIRES_SHARED(Locks::mutator_lock_);
135
136  // Raw field accesses.
137  uint32_t Get32(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
138
139  template<bool kTransactionActive>
140  void Set32(ObjPtr<mirror::Object> object, uint32_t new_value)
141      REQUIRES_SHARED(Locks::mutator_lock_);
142
143  uint64_t Get64(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
144
145  template<bool kTransactionActive>
146  void Set64(ObjPtr<mirror::Object> object, uint64_t new_value)
147      REQUIRES_SHARED(Locks::mutator_lock_);
148
149  template<class MirrorType = mirror::Object>
150  ObjPtr<MirrorType> GetObj(ObjPtr<mirror::Object> object)
151      REQUIRES_SHARED(Locks::mutator_lock_);
152
153  template<bool kTransactionActive>
154  void SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value)
155      REQUIRES_SHARED(Locks::mutator_lock_);
156
157  // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
158  template<typename RootVisitorType>
159  void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS;
160
161  bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) {
162    return (GetAccessFlags() & kAccVolatile) != 0;
163  }
164
165  // Returns an instance field with this offset in the given class or null if not found.
166  // If kExactOffset is true then we only find the matching offset, not the field containing the
167  // offset.
168  template <bool kExactOffset = true>
169  static ArtField* FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
170      REQUIRES_SHARED(Locks::mutator_lock_);
171
172  // Returns a static field with this offset in the given class or null if not found.
173  // If kExactOffset is true then we only find the matching offset, not the field containing the
174  // offset.
175  template <bool kExactOffset = true>
176  static ArtField* FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
177      REQUIRES_SHARED(Locks::mutator_lock_);
178
179  const char* GetName() REQUIRES_SHARED(Locks::mutator_lock_);
180
181  // Resolves / returns the name from the dex cache.
182  ObjPtr<mirror::String> GetStringName(Thread* self, bool resolve)
183      REQUIRES_SHARED(Locks::mutator_lock_);
184
185  const char* GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_);
186
187  Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
188
189  bool IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
190
191  template <bool kResolve>
192  ObjPtr<mirror::Class> GetType() REQUIRES_SHARED(Locks::mutator_lock_);
193
194  size_t FieldSize() REQUIRES_SHARED(Locks::mutator_lock_);
195
196  ObjPtr<mirror::DexCache> GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
197
198  const DexFile* GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_);
199
200  GcRoot<mirror::Class>& DeclaringClassRoot() {
201    return declaring_class_;
202  }
203
204  // Update the declaring class with the passed in visitor. Does not use read barrier.
205  template <typename Visitor>
206  ALWAYS_INLINE void UpdateObjects(const Visitor& visitor)
207      REQUIRES_SHARED(Locks::mutator_lock_);
208
209 private:
210  ObjPtr<mirror::Class> ProxyFindSystemClass(const char* descriptor)
211      REQUIRES_SHARED(Locks::mutator_lock_);
212  ObjPtr<mirror::Class> ResolveGetType(uint32_t type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
213  ObjPtr<mirror::String> ResolveGetStringName(Thread* self,
214                                              const DexFile& dex_file,
215                                              uint32_t string_idx,
216                                              ObjPtr<mirror::DexCache> dex_cache)
217      REQUIRES_SHARED(Locks::mutator_lock_);
218
219  GcRoot<mirror::Class> declaring_class_;
220
221  uint32_t access_flags_ = 0;
222
223  // Dex cache index of field id
224  uint32_t field_dex_idx_ = 0;
225
226  // Offset of field within an instance or in the Class' static fields
227  uint32_t offset_ = 0;
228};
229
230}  // namespace art
231
232#endif  // ART_RUNTIME_ART_FIELD_H_
233