art_field.cc revision 94f7b49578b6aaa80de8ffed230648d601393905
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
28namespace art {
29namespace mirror {
30
31// TODO: Get global references for these
32GcRoot<Class> ArtField::java_lang_reflect_ArtField_;
33
34ArtField* ArtField::FromReflectedField(const ScopedObjectAccessAlreadyRunnable& soa,
35                                       jobject jlr_field) {
36  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_reflect_Field_artField);
37  mirror::ArtField* field = f->GetObject(soa.Decode<mirror::Object*>(jlr_field))->AsArtField();
38  DCHECK(field != nullptr);
39  return field;
40}
41
42void ArtField::SetClass(Class* java_lang_reflect_ArtField) {
43  CHECK(java_lang_reflect_ArtField_.IsNull());
44  CHECK(java_lang_reflect_ArtField != NULL);
45  java_lang_reflect_ArtField_ = GcRoot<Class>(java_lang_reflect_ArtField);
46}
47
48void ArtField::ResetClass() {
49  CHECK(!java_lang_reflect_ArtField_.IsNull());
50  java_lang_reflect_ArtField_ = GcRoot<Class>(nullptr);
51}
52
53void ArtField::SetOffset(MemberOffset num_bytes) {
54  DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
55  if (kIsDebugBuild && Runtime::Current()->IsCompiler() &&
56      !Runtime::Current()->UseCompileTimeClassPath()) {
57    Primitive::Type type = GetTypeAsPrimitiveType();
58    if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
59      DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
60    }
61  }
62  // Not called within a transaction.
63  SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ArtField, offset_), num_bytes.Uint32Value());
64}
65
66void ArtField::VisitRoots(RootCallback* callback, void* arg) {
67  if (!java_lang_reflect_ArtField_.IsNull()) {
68    java_lang_reflect_ArtField_.VisitRoot(callback, arg, 0, kRootStickyClass);
69  }
70}
71
72// TODO: we could speed up the search if fields are ordered by offsets.
73ArtField* ArtField::FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset) {
74  DCHECK(klass != nullptr);
75  ObjectArray<ArtField>* instance_fields = klass->GetIFields();
76  if (instance_fields != nullptr) {
77    for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
78      mirror::ArtField* field = instance_fields->GetWithoutChecks(i);
79      if (field->GetOffset().Uint32Value() == field_offset) {
80        return field;
81      }
82    }
83  }
84  // We did not find field in the class: look into superclass.
85  if (klass->GetSuperClass() != NULL) {
86    return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
87  } else {
88    return nullptr;
89  }
90}
91
92}  // namespace mirror
93}  // namespace art
94