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