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