blob: edaddbd2e7db85912d0da0c9c0bcc940f3cf6982 [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:
Mathieu Chartier90443472015-07-16 20:32:27 -070039 static mirror::Class* StaticClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070040 return static_class_.Read();
41 }
42
Mathieu Chartier90443472015-07-16 20:32:27 -070043 static mirror::Class* ArrayClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070044 return array_class_.Read();
45 }
46
Mathieu Chartier90443472015-07-16 20:32:27 -070047 ALWAYS_INLINE uint32_t GetDexFieldIndex() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070048 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_));
49 }
50
Mathieu Chartier90443472015-07-16 20:32:27 -070051 mirror::Class* GetDeclaringClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070052 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_));
53 }
54
Mathieu Chartier90443472015-07-16 20:32:27 -070055 uint32_t GetAccessFlags() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070056 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_));
57 }
58
Mathieu Chartier90443472015-07-16 20:32:27 -070059 bool IsStatic() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070060 return (GetAccessFlags() & kAccStatic) != 0;
61 }
62
Mathieu Chartier90443472015-07-16 20:32:27 -070063 bool IsFinal() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070064 return (GetAccessFlags() & kAccFinal) != 0;
65 }
66
Mathieu Chartier90443472015-07-16 20:32:27 -070067 bool IsVolatile() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070068 return (GetAccessFlags() & kAccVolatile) != 0;
69 }
70
71 ALWAYS_INLINE Primitive::Type GetTypeAsPrimitiveType()
Mathieu Chartier90443472015-07-16 20:32:27 -070072 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070073 return GetType()->GetPrimitiveType();
74 }
75
Mathieu Chartier90443472015-07-16 20:32:27 -070076 mirror::Class* GetType() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070077 return GetFieldObject<mirror::Class>(OFFSET_OF_OBJECT_MEMBER(Field, type_));
78 }
79
Mathieu Chartier90443472015-07-16 20:32:27 -070080 int32_t GetOffset() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070081 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_));
82 }
83
Mathieu Chartier90443472015-07-16 20:32:27 -070084 static void SetClass(Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
85 static void ResetClass() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070086
Mathieu Chartier90443472015-07-16 20:32:27 -070087 static void SetArrayClass(Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
88 static void ResetArrayClass() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070089
Mathieu Chartier90443472015-07-16 20:32:27 -070090 static void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070091
92 // Slow, try to use only for PrettyField and such.
Mathieu Chartier90443472015-07-16 20:32:27 -070093 ArtField* GetArtField() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070094
95 template <bool kTransactionActive = false>
Mathieu Chartierc7853442015-03-27 14:35:38 -070096 static mirror::Field* CreateFromArtField(Thread* self, ArtField* field,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070097 bool force_resolve)
Mathieu Chartiered8990a2015-07-23 14:11:16 -070098 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070099
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>
Mathieu Chartier90443472015-07-16 20:32:27 -0700108 void SetDeclaringClass(mirror::Class* c) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700109 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), c);
110 }
111
112 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700113 void SetType(mirror::Class* type) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700114 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, type_), type);
115 }
116
117 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 void SetAccessFlags(uint32_t flags) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700119 SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), flags);
120 }
121
122 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700123 void SetDexFieldIndex(uint32_t idx) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700124 SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_), idx);
125 }
126
127 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 void SetOffset(uint32_t offset) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700129 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_