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: 42 MIRROR_CLASS("Ljava/lang/reflect/Field;"); 43 GetArtFieldIndex()44 ALWAYS_INLINE uint32_t GetArtFieldIndex() REQUIRES_SHARED(Locks::mutator_lock_) { 45 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, art_field_index_)); 46 } 47 48 ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_); 49 GetAccessFlags()50 uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) { 51 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_)); 52 } 53 IsStatic()54 bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) { 55 return (GetAccessFlags() & kAccStatic) != 0; 56 } 57 IsFinal()58 bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) { 59 return (GetAccessFlags() & kAccFinal) != 0; 60 } 61 IsVolatile()62 bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) { 63 return (GetAccessFlags() & kAccVolatile) != 0; 64 } 65 66 ALWAYS_INLINE Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_); 67 68 ObjPtr<mirror::Class> GetType() REQUIRES_SHARED(Locks::mutator_lock_); 69 GetOffset()70 int32_t GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) { 71 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_)); 72 } 73 74 ArtField* GetArtField() REQUIRES_SHARED(Locks::mutator_lock_); 75 76 static ObjPtr<mirror::Field> CreateFromArtField(Thread* self, 77 ArtField* field, 78 bool force_resolve) 79 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 80 81 82 // Used to modify the target of this Field object, if required for structural redefinition or some 83 // other purpose. 84 void VisitTarget(ReflectiveValueVisitor* v) REQUIRES(Locks::mutator_lock_); 85 86 private: 87 // Padding required for matching alignment with the Java peer. 88 uint8_t padding_[3]; 89 90 HeapReference<mirror::Class> declaring_class_; 91 HeapReference<mirror::Class> type_; 92 int32_t access_flags_; 93 int32_t art_field_index_; 94 int32_t offset_; 95 DeclaringClassOffset()96 static constexpr MemberOffset DeclaringClassOffset() { 97 return OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_); 98 } 99 TypeOffset()100 static constexpr MemberOffset TypeOffset() { 101 return OFFSET_OF_OBJECT_MEMBER(Field, type_); 102 } 103 AccessFlagsOffset()104 static constexpr MemberOffset AccessFlagsOffset() { 105 return OFFSET_OF_OBJECT_MEMBER(Field, access_flags_); 106 } 107 ArtFieldIndexOffset()108 static constexpr MemberOffset ArtFieldIndexOffset() { 109 return OFFSET_OF_OBJECT_MEMBER(Field, art_field_index_); 110 } 111 OffsetOffset()112 static constexpr MemberOffset OffsetOffset() { 113 return OFFSET_OF_OBJECT_MEMBER(Field, offset_); 114 } 115 116 template<bool kTransactionActive, bool kCheckTransaction = true> 117 void SetDeclaringClass(ObjPtr<Class> c) REQUIRES_SHARED(Locks::mutator_lock_); 118 119 template<bool kTransactionActive, bool kCheckTransaction = true> 120 void SetType(ObjPtr<Class> type) REQUIRES_SHARED(Locks::mutator_lock_); 121 122 template<bool kTransactionActive, bool kCheckTransaction = true> SetAccessFlags(uint32_t access_flags)123 void SetAccessFlags(uint32_t access_flags) REQUIRES_SHARED(Locks::mutator_lock_) { 124 SetField32<kTransactionActive, kCheckTransaction>(AccessFlagsOffset(), access_flags); 125 } 126 127 template<bool kTransactionActive, bool kCheckTransaction = true> SetArtFieldIndex(uint32_t idx)128 void SetArtFieldIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) { 129 SetField32<kTransactionActive, kCheckTransaction>(ArtFieldIndexOffset(), idx); 130 } 131 132 template<bool kTransactionActive, bool kCheckTransaction = true> SetOffset(uint32_t offset)133 void SetOffset(uint32_t offset) REQUIRES_SHARED(Locks::mutator_lock_) { 134 SetField32<kTransactionActive, kCheckTransaction>(OffsetOffset(), offset); 135 } 136 137 friend struct art::FieldOffsets; // for verifying offset information 138 DISALLOW_IMPLICIT_CONSTRUCTORS(Field); 139 }; 140 141 } // namespace mirror 142 } // namespace art 143 144 #endif // ART_RUNTIME_MIRROR_FIELD_H_ 145