blob: 47d5a76dc77d2b0c0352eb8e16157ac55e56f88a [file] [log] [blame]
Mathieu Chartierc7853442015-03-27 14:35:38 -07001/*
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"
Vladimir Marko3481ba22015-04-13 12:22:36 +010020#include "class_linker-inl.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "gc/accounting/card_table-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010022#include "handle_scope.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "mirror/object-inl.h"
24#include "mirror/object_array-inl.h"
25#include "runtime.h"
26#include "scoped_thread_state_change.h"
27#include "utils.h"
28#include "well_known_classes.h"
29
30namespace art {
31
32ArtField::ArtField() : access_flags_(0), field_dex_idx_(0), offset_(0) {
33 declaring_class_ = GcRoot<mirror::Class>(nullptr);
34}
35
36void ArtField::SetOffset(MemberOffset num_bytes) {
37 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
38 if (kIsDebugBuild && Runtime::Current()->IsAotCompiler() &&
39 Runtime::Current()->IsCompilingBootImage()) {
40 Primitive::Type type = GetTypeAsPrimitiveType();
41 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
42 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
43 }
44 }
45 // Not called within a transaction.
46 offset_ = num_bytes.Uint32Value();
47}
48
49void ArtField::VisitRoots(RootVisitor* visitor) {
50 declaring_class_.VisitRoot(visitor, RootInfo(kRootStickyClass));
51}
52
53ArtField* ArtField::FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset) {
54 DCHECK(klass != nullptr);
55 auto* instance_fields = klass->GetIFields();
56 for (size_t i = 0, count = klass->NumInstanceFields(); i < count; ++i) {
57 if (instance_fields[i].GetOffset().Uint32Value() == field_offset) {
58 return &instance_fields[i];
59 }
60 }
61 // We did not find field in the class: look into superclass.
62 return (klass->GetSuperClass() != nullptr) ?
63 FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset) : nullptr;
64}
65
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -070066ArtField* ArtField::FindStaticFieldWithOffset(mirror::Class* klass, uint32_t field_offset) {
67 DCHECK(klass != nullptr);
68 auto* static_fields = klass->GetSFields();
69 for (size_t i = 0, count = klass->NumStaticFields(); i < count; ++i) {
70 if (static_fields[i].GetOffset().Uint32Value() == field_offset) {
71 return &static_fields[i];
72 }
73 }
74 return nullptr;
75}
76
Vladimir Marko3481ba22015-04-13 12:22:36 +010077mirror::Class* ArtField::ProxyFindSystemClass(const char* descriptor) {
78 DCHECK(GetDeclaringClass()->IsProxyClass());
79 return Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(), descriptor);
80}
81
82mirror::Class* ArtField::ResolveGetType(uint32_t type_idx) {
83 return Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
84}
85
86mirror::String* ArtField::ResolveGetStringName(Thread* self, const DexFile& dex_file,
87 uint32_t string_idx, mirror::DexCache* dex_cache) {
88 StackHandleScope<1> hs(self);
89 return Runtime::Current()->GetClassLinker()->ResolveString(
90 dex_file, string_idx, hs.NewHandle(dex_cache));
91}
92
Mathieu Chartierc7853442015-03-27 14:35:38 -070093} // namespace art