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