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
SetClass(Class * java_lang_reflect_ArtField)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
ResetClass()40 void ArtField::ResetClass() {
41 CHECK(!java_lang_reflect_ArtField_.IsNull());
42 java_lang_reflect_ArtField_ = GcRoot<Class>(nullptr);
43 }
44
SetOffset(MemberOffset num_bytes)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
VisitRoots(RootCallback * callback,void * arg)58 void ArtField::VisitRoots(RootCallback* callback, void* arg) {
59 java_lang_reflect_ArtField_.VisitRootIfNonNull(callback, arg, RootInfo(kRootStickyClass));
60 }
61
62 // TODO: we could speed up the search if fields are ordered by offsets.
FindInstanceFieldWithOffset(mirror::Class * klass,uint32_t field_offset)63 ArtField* ArtField::FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset) {
64 DCHECK(klass != nullptr);
65 ObjectArray<ArtField>* instance_fields = klass->GetIFields();
66 if (instance_fields != nullptr) {
67 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
68 mirror::ArtField* field = instance_fields->GetWithoutChecks(i);
69 if (field->GetOffset().Uint32Value() == field_offset) {
70 return field;
71 }
72 }
73 }
74 // We did not find field in the class: look into superclass.
75 if (klass->GetSuperClass() != NULL) {
76 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
77 } else {
78 return nullptr;
79 }
80 }
81
82 } // namespace mirror
83 } // namespace art
84