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 #include "art_field.h"
     18 
     19 #include "art_field-inl.h"
     20 #include "gc/accounting/card_table-inl.h"
     21 #include "object-inl.h"
     22 #include "object_array-inl.h"
     23 #include "runtime.h"
     24 #include "scoped_thread_state_change.h"
     25 #include "utils.h"
     26 #include "well_known_classes.h"
     27 
     28 namespace art {
     29 namespace mirror {
     30 
     31 // TODO: Get global references for these
     32 GcRoot<Class> ArtField::java_lang_reflect_ArtField_;
     33 
     34 void ArtField::SetClass(Class* java_lang_reflect_ArtField) {
     35   CHECK(java_lang_reflect_ArtField_.IsNull());
     36   CHECK(java_lang_reflect_ArtField != NULL);
     37   java_lang_reflect_ArtField_ = GcRoot<Class>(java_lang_reflect_ArtField);
     38 }
     39 
     40 void ArtField::ResetClass() {
     41   CHECK(!java_lang_reflect_ArtField_.IsNull());
     42   java_lang_reflect_ArtField_ = GcRoot<Class>(nullptr);
     43 }
     44 
     45 void ArtField::SetOffset(MemberOffset num_bytes) {
     46   DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
     47   if (kIsDebugBuild && Runtime::Current()->IsCompiler() &&
     48       !Runtime::Current()->UseCompileTimeClassPath()) {
     49     Primitive::Type type = GetTypeAsPrimitiveType();
     50     if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
     51       DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
     52     }
     53   }
     54   // Not called within a transaction.
     55   SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ArtField, offset_), num_bytes.Uint32Value());
     56 }
     57 
     58 void ArtField::VisitRoots(RootCallback* callback, void* arg) {
     59   if (!java_lang_reflect_ArtField_.IsNull()) {
     60     java_lang_reflect_ArtField_.VisitRoot(callback, arg, 0, kRootStickyClass);
     61   }
     62 }
     63 
     64 // TODO: we could speed up the search if fields are ordered by offsets.
     65 ArtField* ArtField::FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset) {
     66   DCHECK(klass != nullptr);
     67   ObjectArray<ArtField>* instance_fields = klass->GetIFields();
     68   if (instance_fields != nullptr) {
     69     for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
     70       mirror::ArtField* field = instance_fields->GetWithoutChecks(i);
     71       if (field->GetOffset().Uint32Value() == field_offset) {
     72         return field;
     73       }
     74     }
     75   }
     76   // We did not find field in the class: look into superclass.
     77   if (klass->GetSuperClass() != NULL) {
     78     return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
     79   } else {
     80     return nullptr;
     81   }
     82 }
     83 
     84 }  // namespace mirror
     85 }  // namespace art
     86