Home | History | Annotate | Download | only in mirror
      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_INL_H_
     18 #define ART_RUNTIME_MIRROR_FIELD_INL_H_
     19 
     20 #include "field.h"
     21 
     22 #include "art_field-inl.h"
     23 #include "class-inl.h"
     24 #include "dex_cache-inl.h"
     25 
     26 namespace art {
     27 
     28 namespace mirror {
     29 
     30 template <PointerSize kPointerSize, bool kTransactionActive>
     31 inline mirror::Field* Field::CreateFromArtField(Thread* self, ArtField* field, bool force_resolve) {
     32   StackHandleScope<2> hs(self);
     33   // Try to resolve type before allocating since this is a thread suspension point.
     34   Handle<mirror::Class> type = hs.NewHandle(field->ResolveType());
     35 
     36   if (type == nullptr) {
     37     if (force_resolve) {
     38       if (kIsDebugBuild) {
     39         self->AssertPendingException();
     40       }
     41       return nullptr;
     42     } else {
     43       // Can't resolve, clear the exception if it isn't OOME and continue with a null type.
     44       mirror::Throwable* exception = self->GetException();
     45       if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
     46         return nullptr;
     47       }
     48       self->ClearException();
     49     }
     50   }
     51   auto ret = hs.NewHandle(ObjPtr<Field>::DownCast(StaticClass()->AllocObject(self)));
     52   if (UNLIKELY(ret == nullptr)) {
     53     self->AssertPendingOOMException();
     54     return nullptr;
     55   }
     56   auto dex_field_index = field->GetDexFieldIndex();
     57   auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, kPointerSize);
     58   if (field->GetDeclaringClass()->IsProxyClass()) {
     59     DCHECK(field->IsStatic());
     60     DCHECK_LT(dex_field_index, 2U);
     61     // The two static fields (interfaces, throws) of all proxy classes
     62     // share the same dex file indices 0 and 1. So, we can't resolve
     63     // them in the dex cache.
     64   } else {
     65     if (resolved_field != nullptr) {
     66       DCHECK_EQ(resolved_field, field);
     67     } else {
     68       // We rely on the field being resolved so that we can back to the ArtField
     69       // (i.e. FromReflectedMethod).
     70       field->GetDexCache()->SetResolvedField(dex_field_index, field, kPointerSize);
     71     }
     72   }
     73   ret->SetType<kTransactionActive>(type.Get());
     74   ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass());
     75   ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags());
     76   ret->SetDexFieldIndex<kTransactionActive>(dex_field_index);
     77   ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value());
     78   return ret.Get();
     79 }
     80 
     81 template<bool kTransactionActive>
     82 inline void Field::SetDeclaringClass(ObjPtr<mirror::Class> c) {
     83   SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), c);
     84 }
     85 
     86 template<bool kTransactionActive>
     87 inline void Field::SetType(ObjPtr<mirror::Class> type) {
     88   SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, type_), type);
     89 }
     90 
     91 inline Primitive::Type Field::GetTypeAsPrimitiveType() {
     92   return GetType()->GetPrimitiveType();
     93 }
     94 
     95 }  // namespace mirror
     96 }  // namespace art
     97 
     98 #endif  // ART_RUNTIME_MIRROR_FIELD_INL_H_
     99