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 "base/macros.h"
22 #include "base/pointer_size.h"
23 #include "dex/modifiers.h"
24 #include "dex/primitive.h"
25 #include "obj_ptr.h"
26 #include "object.h"
27 #include "read_barrier_option.h"
28 
29 namespace art HIDDEN {
30 
31 class ArtField;
32 struct FieldOffsets;
33 class ReflectiveValueVisitor;
34 
35 namespace mirror {
36 
37 class Class;
38 class String;
39 
40 // C++ mirror of java.lang.reflect.Field.
41 class MANAGED Field : public AccessibleObject {
42  public:
43   MIRROR_CLASS("Ljava/lang/reflect/Field;");
44 
GetArtFieldIndex()45   ALWAYS_INLINE uint32_t GetArtFieldIndex() REQUIRES_SHARED(Locks::mutator_lock_) {
46     return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, art_field_index_));
47   }
48 
49   ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
50 
GetAccessFlags()51   uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) {
52     return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_));
53   }
54 
IsStatic()55   bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) {
56     return (GetAccessFlags() & kAccStatic) != 0;
57   }
58 
IsFinal()59   bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) {
60     return (GetAccessFlags() & kAccFinal) != 0;
61   }
62 
IsVolatile()63   bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) {
64     return (GetAccessFlags() & kAccVolatile) != 0;
65   }
66 
67   ALWAYS_INLINE Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
68 
69   ObjPtr<mirror::Class> GetType() REQUIRES_SHARED(Locks::mutator_lock_);
70 
GetOffset()71   int32_t GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) {
72     return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_));
73   }
74 
75   ArtField* GetArtField() REQUIRES_SHARED(Locks::mutator_lock_);
76 
77   static ObjPtr<mirror::Field> CreateFromArtField(Thread* self,
78                                                   ArtField* field,
79                                                   bool force_resolve)
80       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
81 
82 
83   // Used to modify the target of this Field object, if required for structural redefinition or some
84   // other purpose.
85   void VisitTarget(ReflectiveValueVisitor* v) REQUIRES(Locks::mutator_lock_);
86 
87  private:
88   // Padding required for matching alignment with the Java peer.
89   uint8_t padding_[3];
90 
91   HeapReference<mirror::Class> declaring_class_;
92   HeapReference<mirror::Class> type_;
93   int32_t access_flags_;
94   int32_t art_field_index_;
95   int32_t offset_;
96 
DeclaringClassOffset()97   static constexpr MemberOffset DeclaringClassOffset() {
98     return OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_);
99   }
100 
TypeOffset()101   static constexpr MemberOffset TypeOffset() {
102     return OFFSET_OF_OBJECT_MEMBER(Field, type_);
103   }
104 
AccessFlagsOffset()105   static constexpr MemberOffset AccessFlagsOffset() {
106     return OFFSET_OF_OBJECT_MEMBER(Field, access_flags_);
107   }
108 
ArtFieldIndexOffset()109   static constexpr MemberOffset ArtFieldIndexOffset() {
110     return OFFSET_OF_OBJECT_MEMBER(Field, art_field_index_);
111   }
112 
OffsetOffset()113   static constexpr MemberOffset OffsetOffset() {
114     return OFFSET_OF_OBJECT_MEMBER(Field, offset_);
115   }
116 
117   template<bool kTransactionActive, bool kCheckTransaction = true>
118   void SetDeclaringClass(ObjPtr<Class> c) REQUIRES_SHARED(Locks::mutator_lock_);
119 
120   template<bool kTransactionActive, bool kCheckTransaction = true>
121   void SetType(ObjPtr<Class> type) REQUIRES_SHARED(Locks::mutator_lock_);
122 
123   template<bool kTransactionActive, bool kCheckTransaction = true>
SetAccessFlags(uint32_t access_flags)124   void SetAccessFlags(uint32_t access_flags) REQUIRES_SHARED(Locks::mutator_lock_) {
125     SetField32<kTransactionActive, kCheckTransaction>(AccessFlagsOffset(), access_flags);
126   }
127 
128   template<bool kTransactionActive, bool kCheckTransaction = true>
SetArtFieldIndex(uint32_t idx)129   void SetArtFieldIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) {
130     SetField32<kTransactionActive, kCheckTransaction>(ArtFieldIndexOffset(), idx);
131   }
132 
133   template<bool kTransactionActive, bool kCheckTransaction = true>
SetOffset(uint32_t offset)134   void SetOffset(uint32_t offset) REQUIRES_SHARED(Locks::mutator_lock_) {
135     SetField32<kTransactionActive, kCheckTransaction>(OffsetOffset(), offset);
136   }
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_
146