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