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