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