Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 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" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 22 | #include "gc_root.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 23 | #include "obj_ptr.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 24 | #include "object.h" |
| 25 | #include "object_callbacks.h" |
| 26 | #include "read_barrier_option.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 30 | class ArtField; |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 31 | struct FieldOffsets; |
| 32 | |
| 33 | namespace mirror { |
| 34 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 35 | class Class; |
| 36 | class String; |
| 37 | |
| 38 | // C++ mirror of java.lang.reflect.Field. |
| 39 | class MANAGED Field : public AccessibleObject { |
| 40 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 41 | static mirror::Class* StaticClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 42 | return static_class_.Read(); |
| 43 | } |
| 44 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 45 | static mirror::Class* ArrayClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 46 | return array_class_.Read(); |
| 47 | } |
| 48 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 49 | ALWAYS_INLINE uint32_t GetDexFieldIndex() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 50 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_)); |
| 51 | } |
| 52 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 53 | mirror::Class* GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 54 | return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_)); |
| 55 | } |
| 56 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 57 | uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 58 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_)); |
| 59 | } |
| 60 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 61 | bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 62 | return (GetAccessFlags() & kAccStatic) != 0; |
| 63 | } |
| 64 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 65 | bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 66 | return (GetAccessFlags() & kAccFinal) != 0; |
| 67 | } |
| 68 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 69 | bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 70 | return (GetAccessFlags() & kAccVolatile) != 0; |
| 71 | } |
| 72 | |
| 73 | ALWAYS_INLINE Primitive::Type GetTypeAsPrimitiveType() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 74 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 75 | return GetType()->GetPrimitiveType(); |
| 76 | } |
| 77 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 78 | mirror::Class* GetType() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 79 | return GetFieldObject<mirror::Class>(OFFSET_OF_OBJECT_MEMBER(Field, type_)); |
| 80 | } |
| 81 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 82 | int32_t GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 83 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_)); |
| 84 | } |
| 85 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 86 | static void SetClass(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 87 | static void ResetClass() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 88 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 89 | static void SetArrayClass(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 90 | static void ResetArrayClass() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 91 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 92 | static void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 93 | |
| 94 | // Slow, try to use only for PrettyField and such. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 95 | ArtField* GetArtField() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 96 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 97 | template <PointerSize kPointerSize, bool kTransactionActive = false> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 98 | static mirror::Field* CreateFromArtField(Thread* self, ArtField* field, |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 99 | bool force_resolve) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 100 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 101 | |
| 102 | private: |
Neil Fuller | 60458a0 | 2016-09-01 15:32:44 +0100 | [diff] [blame] | 103 | // Padding required for matching alignment with the Java peer. |
| 104 | uint8_t padding_[2]; |
| 105 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 106 | HeapReference<mirror::Class> declaring_class_; |
| 107 | HeapReference<mirror::Class> type_; |
| 108 | int32_t access_flags_; |
| 109 | int32_t dex_field_index_; |
| 110 | int32_t offset_; |
| 111 | |
| 112 | template<bool kTransactionActive> |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 113 | void SetDeclaringClass(ObjPtr<mirror::Class> c) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 114 | |
| 115 | template<bool kTransactionActive> |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 116 | void SetType(ObjPtr<mirror::Class> type) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 117 | |
| 118 | template<bool kTransactionActive> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 119 | void SetAccessFlags(uint32_t flags) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 120 | SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), flags); |
| 121 | } |
| 122 | |
| 123 | template<bool kTransactionActive> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 124 | void SetDexFieldIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 125 | SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_), idx); |
| 126 | } |
| 127 | |
| 128 | template<bool kTransactionActive> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 129 | void SetOffset(uint32_t offset) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 130 | SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, offset_), offset); |
| 131 | } |
| 132 | |
| 133 | static GcRoot<Class> static_class_; // java.lang.reflect.Field.class. |
| 134 | static GcRoot<Class> array_class_; // array of java.lang.reflect.Field. |
| 135 | |
| 136 | friend struct art::FieldOffsets; // for verifying offset information |
| 137 | DISALLOW_IMPLICIT_CONSTRUCTORS(Field); |
| 138 | }; |
| 139 | |
| 140 | } // namespace mirror |
| 141 | } // namespace art |
| 142 | |
| 143 | #endif // ART_RUNTIME_MIRROR_FIELD_H_ |