blob: 9988f84a7249255fec4e80d718a2a2c89b8e986d [file] [log] [blame]
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001/*
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
Mathieu Chartierc7853442015-03-27 14:35:38 -070028class ArtField;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070029struct FieldOffsets;
30
31namespace mirror {
32
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070033class 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
86 static void SetArrayClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
87
88 static void ResetClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89
90 static void ResetArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070092 static void VisitRoots(RootVisitor* visitor)
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94
95 // Slow, try to use only for PrettyField and such.
Mathieu Chartierc7853442015-03-27 14:35:38 -070096 ArtField* GetArtField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070097
98 template <bool kTransactionActive = false>
Mathieu Chartierc7853442015-03-27 14:35:38 -070099 static mirror::Field* CreateFromArtField(Thread* self, ArtField* field,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700100 bool force_resolve)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102
103 private:
104 HeapReference<mirror::Class> declaring_class_;
105 HeapReference<mirror::Class> type_;
106 int32_t access_flags_;
107 int32_t dex_field_index_;
108 int32_t offset_;
109
110 template<bool kTransactionActive>
111 void SetDeclaringClass(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
112 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), c);
113 }
114
115 template<bool kTransactionActive>
116 void SetType(mirror::Class* type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, type_), type);
118 }
119
120 template<bool kTransactionActive>
121 void SetAccessFlags(uint32_t flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
122 SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), flags);
123 }
124
125 template<bool kTransactionActive>
126 void SetDexFieldIndex(uint32_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
127 SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_), idx);
128 }
129
130 template<bool kTransactionActive>
131 void SetOffset(uint32_t offset) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
132 SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, offset_), offset);
133 }
134
135 static GcRoot<Class> static_class_; // java.lang.reflect.Field.class.
136 static GcRoot<Class> array_class_; // array of java.lang.reflect.Field.
137
138 friend struct art::FieldOffsets; // for verifying offset information
139 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
140};
141
142} // namespace mirror
143} // namespace art
144
145#endif // ART_RUNTIME_MIRROR_FIELD_H_