1 /* 2 * Copyright (C) 2011 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_ART_FIELD_H_ 18 #define ART_RUNTIME_ART_FIELD_H_ 19 20 #include <jni.h> 21 22 #include "gc_root.h" 23 #include "modifiers.h" 24 #include "offsets.h" 25 #include "primitive.h" 26 #include "read_barrier_option.h" 27 28 namespace art { 29 30 class DexFile; 31 class ScopedObjectAccessAlreadyRunnable; 32 33 namespace mirror { 34 class Class; 35 class DexCache; 36 class Object; 37 class String; 38 } // namespace mirror 39 40 class ArtField FINAL { 41 public: 42 ArtField(); 43 44 mirror::Class* GetDeclaringClass() SHARED_REQUIRES(Locks::mutator_lock_); 45 46 void SetDeclaringClass(mirror::Class *new_declaring_class) 47 SHARED_REQUIRES(Locks::mutator_lock_); 48 49 uint32_t GetAccessFlags() SHARED_REQUIRES(Locks::mutator_lock_); 50 51 void SetAccessFlags(uint32_t new_access_flags) SHARED_REQUIRES(Locks::mutator_lock_) { 52 // Not called within a transaction. 53 access_flags_ = new_access_flags; 54 } 55 56 bool IsPublic() SHARED_REQUIRES(Locks::mutator_lock_) { 57 return (GetAccessFlags() & kAccPublic) != 0; 58 } 59 60 bool IsStatic() SHARED_REQUIRES(Locks::mutator_lock_) { 61 return (GetAccessFlags() & kAccStatic) != 0; 62 } 63 64 bool IsFinal() SHARED_REQUIRES(Locks::mutator_lock_) { 65 return (GetAccessFlags() & kAccFinal) != 0; 66 } 67 68 uint32_t GetDexFieldIndex() { 69 return field_dex_idx_; 70 } 71 72 void SetDexFieldIndex(uint32_t new_idx) { 73 // Not called within a transaction. 74 field_dex_idx_ = new_idx; 75 } 76 77 // Offset to field within an Object. 78 MemberOffset GetOffset() SHARED_REQUIRES(Locks::mutator_lock_); 79 80 static MemberOffset OffsetOffset() { 81 return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_)); 82 } 83 84 MemberOffset GetOffsetDuringLinking() SHARED_REQUIRES(Locks::mutator_lock_); 85 86 void SetOffset(MemberOffset num_bytes) SHARED_REQUIRES(Locks::mutator_lock_); 87 88 // field access, null object for static fields 89 uint8_t GetBoolean(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 90 91 template<bool kTransactionActive> 92 void SetBoolean(mirror::Object* object, uint8_t z) SHARED_REQUIRES(Locks::mutator_lock_); 93 94 int8_t GetByte(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 95 96 template<bool kTransactionActive> 97 void SetByte(mirror::Object* object, int8_t b) SHARED_REQUIRES(Locks::mutator_lock_); 98 99 uint16_t GetChar(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 100 101 template<bool kTransactionActive> 102 void SetChar(mirror::Object* object, uint16_t c) SHARED_REQUIRES(Locks::mutator_lock_); 103 104 int16_t GetShort(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 105 106 template<bool kTransactionActive> 107 void SetShort(mirror::Object* object, int16_t s) SHARED_REQUIRES(Locks::mutator_lock_); 108 109 int32_t GetInt(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 110 111 template<bool kTransactionActive> 112 void SetInt(mirror::Object* object, int32_t i) SHARED_REQUIRES(Locks::mutator_lock_); 113 114 int64_t GetLong(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 115 116 template<bool kTransactionActive> 117 void SetLong(mirror::Object* object, int64_t j) SHARED_REQUIRES(Locks::mutator_lock_); 118 119 float GetFloat(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 120 121 template<bool kTransactionActive> 122 void SetFloat(mirror::Object* object, float f) SHARED_REQUIRES(Locks::mutator_lock_); 123 124 double GetDouble(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 125 126 template<bool kTransactionActive> 127 void SetDouble(mirror::Object* object, double d) SHARED_REQUIRES(Locks::mutator_lock_); 128 129 mirror::Object* GetObject(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 130 131 template<bool kTransactionActive> 132 void SetObject(mirror::Object* object, mirror::Object* l) 133 SHARED_REQUIRES(Locks::mutator_lock_); 134 135 // Raw field accesses. 136 uint32_t Get32(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 137 138 template<bool kTransactionActive> 139 void Set32(mirror::Object* object, uint32_t new_value) 140 SHARED_REQUIRES(Locks::mutator_lock_); 141 142 uint64_t Get64(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 143 144 template<bool kTransactionActive> 145 void Set64(mirror::Object* object, uint64_t new_value) SHARED_REQUIRES(Locks::mutator_lock_); 146 147 mirror::Object* GetObj(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); 148 149 template<bool kTransactionActive> 150 void SetObj(mirror::Object* object, mirror::Object* new_value) 151 SHARED_REQUIRES(Locks::mutator_lock_); 152 153 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires. 154 template<typename RootVisitorType> 155 void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS; 156 157 bool IsVolatile() SHARED_REQUIRES(Locks::mutator_lock_) { 158 return (GetAccessFlags() & kAccVolatile) != 0; 159 } 160 161 // Returns an instance field with this offset in the given class or null if not found. 162 // If kExactOffset is true then we only find the matching offset, not the field containing the 163 // offset. 164 template <bool kExactOffset = true> 165 static ArtField* FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset) 166 SHARED_REQUIRES(Locks::mutator_lock_); 167 168 // Returns a static field with this offset in the given class or null if not found. 169 // If kExactOffset is true then we only find the matching offset, not the field containing the 170 // offset. 171 template <bool kExactOffset = true> 172 static ArtField* FindStaticFieldWithOffset(mirror::Class* klass, uint32_t field_offset) 173 SHARED_REQUIRES(Locks::mutator_lock_); 174 175 const char* GetName() SHARED_REQUIRES(Locks::mutator_lock_); 176 177 // Resolves / returns the name from the dex cache. 178 mirror::String* GetStringName(Thread* self, bool resolve) 179 SHARED_REQUIRES(Locks::mutator_lock_); 180 181 const char* GetTypeDescriptor() SHARED_REQUIRES(Locks::mutator_lock_); 182 183 Primitive::Type GetTypeAsPrimitiveType() SHARED_REQUIRES(Locks::mutator_lock_); 184 185 bool IsPrimitiveType() SHARED_REQUIRES(Locks::mutator_lock_); 186 187 template <bool kResolve> 188 mirror::Class* GetType() SHARED_REQUIRES(Locks::mutator_lock_); 189 190 size_t FieldSize() SHARED_REQUIRES(Locks::mutator_lock_); 191 192 mirror::DexCache* GetDexCache() SHARED_REQUIRES(Locks::mutator_lock_); 193 194 const DexFile* GetDexFile() SHARED_REQUIRES(Locks::mutator_lock_); 195 196 GcRoot<mirror::Class>& DeclaringClassRoot() { 197 return declaring_class_; 198 } 199 200 // Update the declaring class with the passed in visitor. Does not use read barrier. 201 template <typename Visitor> 202 ALWAYS_INLINE void UpdateObjects(const Visitor& visitor) 203 SHARED_REQUIRES(Locks::mutator_lock_); 204 205 private: 206 mirror::Class* ProxyFindSystemClass(const char* descriptor) 207 SHARED_REQUIRES(Locks::mutator_lock_); 208 mirror::Class* ResolveGetType(uint32_t type_idx) SHARED_REQUIRES(Locks::mutator_lock_); 209 mirror::String* ResolveGetStringName(Thread* self, const DexFile& dex_file, uint32_t string_idx, 210 mirror::DexCache* dex_cache) 211 SHARED_REQUIRES(Locks::mutator_lock_); 212 213 GcRoot<mirror::Class> declaring_class_; 214 215 uint32_t access_flags_; 216 217 // Dex cache index of field id 218 uint32_t field_dex_idx_; 219 220 // Offset of field within an instance or in the Class' static fields 221 uint32_t offset_; 222 }; 223 224 } // namespace art 225 226 #endif // ART_RUNTIME_ART_FIELD_H_ 227