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   // Public for use by class redefinition code.
46   template<bool kTransactionActive>
SetArtFieldIndex(uint32_t idx)47   void SetArtFieldIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) {
48     SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, art_field_index_), idx);
49   }
50 
51 
52   ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
53 
GetAccessFlags()54   uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) {
55     return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_));
56   }
57 
IsStatic()58   bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) {
59     return (GetAccessFlags() & kAccStatic) != 0;
60   }
61 
IsFinal()62   bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) {
63     return (GetAccessFlags() & kAccFinal) != 0;
64   }
65 
IsVolatile()66   bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) {
67     return (GetAccessFlags() & kAccVolatile) != 0;
68   }
69 
70   ALWAYS_INLINE Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
71 
72   ObjPtr<mirror::Class> GetType() REQUIRES_SHARED(Locks::mutator_lock_);
73 
GetOffset()74   int32_t GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) {
75     return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_));
76   }
77 
78   ArtField* GetArtField() REQUIRES_SHARED(Locks::mutator_lock_);
79 
80   template <PointerSize kPointerSize, bool kTransactionActive = false>
81   static ObjPtr<mirror::Field> CreateFromArtField(Thread* self,
82                                                   ArtField* field,
83                                                   bool force_resolve)
84       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
85 
86 
87   // Used to modify the target of this Field object, if required for structural redefinition or some
88   // other purpose.
89   void VisitTarget(ReflectiveValueVisitor* v) REQUIRES(Locks::mutator_lock_);
90 
91  private:
92   // Padding required for matching alignment with the Java peer.
93   uint8_t padding_[2];
94 
95   HeapReference<mirror::Class> declaring_class_;
96   HeapReference<mirror::Class> type_;
97   int32_t access_flags_;
98   int32_t art_field_index_;
99   int32_t offset_;
100 
101   template<bool kTransactionActive>
102   void SetDeclaringClass(ObjPtr<mirror::Class> c) REQUIRES_SHARED(Locks::mutator_lock_);
103 
104   template<bool kTransactionActive>
105   void SetType(ObjPtr<mirror::Class> type) REQUIRES_SHARED(Locks::mutator_lock_);
106 
107   template<bool kTransactionActive>
SetAccessFlags(uint32_t flags)108   void SetAccessFlags(uint32_t flags) REQUIRES_SHARED(Locks::mutator_lock_) {
109     SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), flags);
110   }
111 
112   template<bool kTransactionActive>
SetOffset(uint32_t offset)113   void SetOffset(uint32_t offset) REQUIRES_SHARED(Locks::mutator_lock_) {
114     SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, offset_), offset);
115   }
116 
117   friend struct art::FieldOffsets;  // for verifying offset information
118   DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
119 };
120 
121 }  // namespace mirror
122 }  // namespace art
123 
124 #endif  // ART_RUNTIME_MIRROR_FIELD_H_
125