1/*
2 * Copyright (C) 2015 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_MIRROR_FIELD_H_
18#define ART_RUNTIME_MIRROR_FIELD_H_
19
20#include "accessible_object.h"
21#include "gc_root.h"
22#include "object.h"
23#include "object_callbacks.h"
24#include "read_barrier_option.h"
25
26namespace art {
27
28class ArtField;
29struct FieldOffsets;
30
31namespace mirror {
32
33class Class;
34class String;
35
36// C++ mirror of java.lang.reflect.Field.
37class MANAGED Field : public AccessibleObject {
38 public:
39  static mirror::Class* StaticClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
40    return static_class_.Read();
41  }
42
43  static mirror::Class* ArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
44    return array_class_.Read();
45  }
46
47  ALWAYS_INLINE uint32_t GetDexFieldIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
48    return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_));
49  }
50
51  mirror::Class* GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
52    return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_));
53  }
54
55  uint32_t GetAccessFlags() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
56    return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_));
57  }
58
59  bool IsStatic() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
60    return (GetAccessFlags() & kAccStatic) != 0;
61  }
62
63  bool IsFinal() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
64    return (GetAccessFlags() & kAccFinal) != 0;
65  }
66
67  bool IsVolatile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68    return (GetAccessFlags() & kAccVolatile) != 0;
69  }
70
71  ALWAYS_INLINE Primitive::Type GetTypeAsPrimitiveType()
72      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
73    return GetType()->GetPrimitiveType();
74  }
75
76  mirror::Class* GetType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
77    return GetFieldObject<mirror::Class>(OFFSET_OF_OBJECT_MEMBER(Field, type_));
78  }
79
80  int32_t GetOffset() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81    return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_));
82  }
83
84  static void SetClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
85  static void ResetClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
87  static void SetArrayClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88  static void ResetArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89
90  static void VisitRoots(RootVisitor* visitor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91
92  // Slow, try to use only for PrettyField and such.
93  ArtField* GetArtField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94
95  template <bool kTransactionActive = false>
96  static mirror::Field* CreateFromArtField(Thread* self, ArtField* field,
97                                           bool force_resolve)
98      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
99
100 private:
101  HeapReference<mirror::Class> declaring_class_;
102  HeapReference<mirror::Class> type_;
103  int32_t access_flags_;
104  int32_t dex_field_index_;
105  int32_t offset_;
106
107  template<bool kTransactionActive>
108  void SetDeclaringClass(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
109    SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), c);
110  }
111
112  template<bool kTransactionActive>
113  void SetType(mirror::Class* type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
114    SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, type_), type);
115  }
116
117  template<bool kTransactionActive>
118  void SetAccessFlags(uint32_t flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
119    SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), flags);
120  }
121
122  template<bool kTransactionActive>
123  void SetDexFieldIndex(uint32_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
124    SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_), idx);
125  }
126
127  template<bool kTransactionActive>
128  void SetOffset(uint32_t offset) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
129    SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, offset_), offset);
130  }
131
132  static GcRoot<Class> static_class_;  // java.lang.reflect.Field.class.
133  static GcRoot<Class> array_class_;  // array of java.lang.reflect.Field.
134
135  friend struct art::FieldOffsets;  // for verifying offset information
136  DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
137};
138
139}  // namespace mirror
140}  // namespace art
141
142#endif  // ART_RUNTIME_MIRROR_FIELD_H_
143