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" |
David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame] | 22 | #include "dex/modifiers.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 23 | #include "dex/primitive.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 24 | #include "obj_ptr.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 25 | #include "object.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 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; |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 32 | class ReflectiveValueVisitor; |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 33 | |
| 34 | namespace mirror { |
| 35 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 36 | class Class; |
| 37 | class String; |
| 38 | |
| 39 | // C++ mirror of java.lang.reflect.Field. |
| 40 | class MANAGED Field : public AccessibleObject { |
| 41 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 42 | ALWAYS_INLINE uint32_t GetDexFieldIndex() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 43 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_)); |
| 44 | } |
| 45 | |
Vladimir Marko | 0eefb9b | 2019-03-27 15:04:31 +0000 | [diff] [blame] | 46 | ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 47 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 48 | uint32_t GetAccessFlags() 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, access_flags_)); |
| 50 | } |
| 51 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 52 | bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 53 | return (GetAccessFlags() & kAccStatic) != 0; |
| 54 | } |
| 55 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 56 | bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 57 | return (GetAccessFlags() & kAccFinal) != 0; |
| 58 | } |
| 59 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 60 | bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 61 | return (GetAccessFlags() & kAccVolatile) != 0; |
| 62 | } |
| 63 | |
Andreas Gampe | 895f922 | 2017-07-05 09:53:32 -0700 | [diff] [blame] | 64 | ALWAYS_INLINE Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 65 | |
Vladimir Marko | 0eefb9b | 2019-03-27 15:04:31 +0000 | [diff] [blame] | 66 | ObjPtr<mirror::Class> GetType() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 67 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 68 | int32_t GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 69 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_)); |
| 70 | } |
| 71 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 72 | // Slow, try to use only for PrettyField and such. Set use-dex-cache to false to not utilize the |
| 73 | // dex-cache when finding the art-field. This is useful for cases where the dex-cache might be |
| 74 | // temporarally invalid. |
| 75 | ArtField* GetArtField(bool use_dex_cache = true) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 76 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 77 | template <PointerSize kPointerSize, bool kTransactionActive = false> |
Vladimir Marko | 0eefb9b | 2019-03-27 15:04:31 +0000 | [diff] [blame] | 78 | static ObjPtr<mirror::Field> CreateFromArtField(Thread* self, |
| 79 | ArtField* field, |
| 80 | bool force_resolve) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 81 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 82 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 83 | |
| 84 | // Used to modify the target of this Field object, if required for structural redefinition or some |
| 85 | // other purpose. |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 86 | void VisitTarget(ReflectiveValueVisitor* v) REQUIRES(Locks::mutator_lock_); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 87 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 88 | private: |
Neil Fuller | 60458a0 | 2016-09-01 15:32:44 +0100 | [diff] [blame] | 89 | // Padding required for matching alignment with the Java peer. |
| 90 | uint8_t padding_[2]; |
| 91 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 92 | HeapReference<mirror::Class> declaring_class_; |
| 93 | HeapReference<mirror::Class> type_; |
| 94 | int32_t access_flags_; |
| 95 | int32_t dex_field_index_; |
| 96 | int32_t offset_; |
| 97 | |
| 98 | template<bool kTransactionActive> |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 99 | void SetDeclaringClass(ObjPtr<mirror::Class> c) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 100 | |
| 101 | template<bool kTransactionActive> |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 102 | void SetType(ObjPtr<mirror::Class> type) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 103 | |
| 104 | template<bool kTransactionActive> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 105 | void SetAccessFlags(uint32_t flags) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 106 | SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), flags); |
| 107 | } |
| 108 | |
| 109 | template<bool kTransactionActive> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 110 | void SetDexFieldIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 111 | SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_), idx); |
| 112 | } |
| 113 | |
| 114 | template<bool kTransactionActive> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 115 | void SetOffset(uint32_t offset) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 116 | SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, offset_), offset); |
| 117 | } |
| 118 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 119 | friend struct art::FieldOffsets; // for verifying offset information |
| 120 | DISALLOW_IMPLICIT_CONSTRUCTORS(Field); |
| 121 | }; |
| 122 | |
| 123 | } // namespace mirror |
| 124 | } // namespace art |
| 125 | |
| 126 | #endif // ART_RUNTIME_MIRROR_FIELD_H_ |