Home | History | Annotate | Download | only in mirror
      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_MIRROR_ART_FIELD_INL_H_
     18 #define ART_RUNTIME_MIRROR_ART_FIELD_INL_H_
     19 
     20 #include "art_field.h"
     21 
     22 #include "base/logging.h"
     23 #include "gc/accounting/card_table-inl.h"
     24 #include "jvalue.h"
     25 #include "object-inl.h"
     26 #include "object_utils.h"
     27 #include "primitive.h"
     28 
     29 namespace art {
     30 namespace mirror {
     31 
     32 inline Class* ArtField::GetDeclaringClass() const {
     33   Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(ArtField, declaring_class_), false);
     34   DCHECK(result != NULL);
     35   DCHECK(result->IsLoaded() || result->IsErroneous());
     36   return result;
     37 }
     38 
     39 inline void ArtField::SetDeclaringClass(Class *new_declaring_class) {
     40   SetFieldObject(OFFSET_OF_OBJECT_MEMBER(ArtField, declaring_class_), new_declaring_class, false);
     41 }
     42 
     43 inline uint32_t ArtField::GetAccessFlags() const {
     44   DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
     45   return GetField32(OFFSET_OF_OBJECT_MEMBER(ArtField, access_flags_), false);
     46 }
     47 
     48 inline MemberOffset ArtField::GetOffset() const {
     49   DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
     50   return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(ArtField, offset_), false));
     51 }
     52 
     53 inline MemberOffset ArtField::GetOffsetDuringLinking() const {
     54   DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
     55   return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(ArtField, offset_), false));
     56 }
     57 
     58 inline uint32_t ArtField::Get32(const Object* object) const {
     59   DCHECK(object != NULL) << PrettyField(this);
     60   DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
     61   return object->GetField32(GetOffset(), IsVolatile());
     62 }
     63 
     64 inline void ArtField::Set32(Object* object, uint32_t new_value) const {
     65   DCHECK(object != NULL) << PrettyField(this);
     66   DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
     67   object->SetField32(GetOffset(), new_value, IsVolatile());
     68 }
     69 
     70 inline uint64_t ArtField::Get64(const Object* object) const {
     71   DCHECK(object != NULL) << PrettyField(this);
     72   DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
     73   return object->GetField64(GetOffset(), IsVolatile());
     74 }
     75 
     76 inline void ArtField::Set64(Object* object, uint64_t new_value) const {
     77   DCHECK(object != NULL) << PrettyField(this);
     78   DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
     79   object->SetField64(GetOffset(), new_value, IsVolatile());
     80 }
     81 
     82 inline Object* ArtField::GetObj(const Object* object) const {
     83   DCHECK(object != NULL) << PrettyField(this);
     84   DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
     85   return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
     86 }
     87 
     88 inline void ArtField::SetObj(Object* object, const Object* new_value) const {
     89   DCHECK(object != NULL) << PrettyField(this);
     90   DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
     91   object->SetFieldObject(GetOffset(), new_value, IsVolatile());
     92 }
     93 
     94 inline bool ArtField::GetBoolean(const Object* object) const {
     95   DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
     96       << PrettyField(this);
     97   return Get32(object);
     98 }
     99 
    100 inline void ArtField::SetBoolean(Object* object, bool z) const {
    101   DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
    102       << PrettyField(this);
    103   Set32(object, z);
    104 }
    105 
    106 inline int8_t ArtField::GetByte(const Object* object) const {
    107   DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
    108       << PrettyField(this);
    109   return Get32(object);
    110 }
    111 
    112 inline void ArtField::SetByte(Object* object, int8_t b) const {
    113   DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
    114       << PrettyField(this);
    115   Set32(object, b);
    116 }
    117 
    118 inline uint16_t ArtField::GetChar(const Object* object) const {
    119   DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
    120       << PrettyField(this);
    121   return Get32(object);
    122 }
    123 
    124 inline void ArtField::SetChar(Object* object, uint16_t c) const {
    125   DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
    126        << PrettyField(this);
    127   Set32(object, c);
    128 }
    129 
    130 inline int16_t ArtField::GetShort(const Object* object) const {
    131   DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
    132        << PrettyField(this);
    133   return Get32(object);
    134 }
    135 
    136 inline void ArtField::SetShort(Object* object, int16_t s) const {
    137   DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
    138        << PrettyField(this);
    139   Set32(object, s);
    140 }
    141 
    142 inline int32_t ArtField::GetInt(const Object* object) const {
    143 #ifndef NDEBUG
    144   Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
    145   CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this);
    146 #endif
    147   return Get32(object);
    148 }
    149 
    150 inline void ArtField::SetInt(Object* object, int32_t i) const {
    151 #ifndef NDEBUG
    152   Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
    153   CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this);
    154 #endif
    155   Set32(object, i);
    156 }
    157 
    158 inline int64_t ArtField::GetLong(const Object* object) const {
    159 #ifndef NDEBUG
    160   Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
    161   CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this);
    162 #endif
    163   return Get64(object);
    164 }
    165 
    166 inline void ArtField::SetLong(Object* object, int64_t j) const {
    167 #ifndef NDEBUG
    168   Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
    169   CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this);
    170 #endif
    171   Set64(object, j);
    172 }
    173 
    174 inline float ArtField::GetFloat(const Object* object) const {
    175   DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
    176        << PrettyField(this);
    177   JValue bits;
    178   bits.SetI(Get32(object));
    179   return bits.GetF();
    180 }
    181 
    182 inline void ArtField::SetFloat(Object* object, float f) const {
    183   DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
    184        << PrettyField(this);
    185   JValue bits;
    186   bits.SetF(f);
    187   Set32(object, bits.GetI());
    188 }
    189 
    190 inline double ArtField::GetDouble(const Object* object) const {
    191   DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
    192        << PrettyField(this);
    193   JValue bits;
    194   bits.SetJ(Get64(object));
    195   return bits.GetD();
    196 }
    197 
    198 inline void ArtField::SetDouble(Object* object, double d) const {
    199   DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
    200        << PrettyField(this);
    201   JValue bits;
    202   bits.SetD(d);
    203   Set64(object, bits.GetJ());
    204 }
    205 
    206 inline Object* ArtField::GetObject(const Object* object) const {
    207   DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
    208        << PrettyField(this);
    209   return GetObj(object);
    210 }
    211 
    212 inline void ArtField::SetObject(Object* object, const Object* l) const {
    213   DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
    214        << PrettyField(this);
    215   SetObj(object, l);
    216 }
    217 
    218 }  // namespace mirror
    219 }  // namespace art
    220 
    221 #endif  // ART_RUNTIME_MIRROR_ART_FIELD_INL_H_
    222