blob: b4d93b6d4dfc473506a2f53bfe63d187cbfd4ccd [file] [log] [blame]
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001/*
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#include "field-inl.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "class-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070020#include "dex_cache-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070021#include "object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include "object_array-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070023
24namespace art {
25namespace mirror {
26
27GcRoot<Class> Field::static_class_;
28GcRoot<Class> Field::array_class_;
29
Mathieu Chartier31e88222016-10-14 18:43:19 -070030void Field::SetClass(ObjPtr<Class> klass) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070031 CHECK(static_class_.IsNull()) << static_class_.Read() << " " << klass;
32 CHECK(klass != nullptr);
33 static_class_ = GcRoot<Class>(klass);
34}
35
36void Field::ResetClass() {
37 CHECK(!static_class_.IsNull());
38 static_class_ = GcRoot<Class>(nullptr);
39}
40
Mathieu Chartier31e88222016-10-14 18:43:19 -070041void Field::SetArrayClass(ObjPtr<Class> klass) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070042 CHECK(array_class_.IsNull()) << array_class_.Read() << " " << klass;
43 CHECK(klass != nullptr);
44 array_class_ = GcRoot<Class>(klass);
45}
46
47void Field::ResetArrayClass() {
48 CHECK(!array_class_.IsNull());
49 array_class_ = GcRoot<Class>(nullptr);
50}
51
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070052void Field::VisitRoots(RootVisitor* visitor) {
53 static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
54 array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070055}
56
57ArtField* Field::GetArtField() {
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070058 mirror::Class* declaring_class = GetDeclaringClass();
Nicolas Geoffray3a090922015-11-24 09:17:30 +000059 if (UNLIKELY(declaring_class->IsProxyClass())) {
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070060 DCHECK(IsStatic());
61 DCHECK_EQ(declaring_class->NumStaticFields(), 2U);
62 // 0 == Class[] interfaces; 1 == Class[][] throws;
63 if (GetDexFieldIndex() == 0) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -070064 return &declaring_class->GetSFieldsPtr()->At(0);
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070065 } else {
66 DCHECK_EQ(GetDexFieldIndex(), 1U);
Mathieu Chartier54d220e2015-07-30 16:20:06 -070067 return &declaring_class->GetSFieldsPtr()->At(1);
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070068 }
69 }
70 mirror::DexCache* const dex_cache = declaring_class->GetDexCache();
Vladimir Markof44d36c2017-03-14 14:18:46 +000071 ArtField* art_field = dex_cache->GetResolvedField(GetDexFieldIndex(), kRuntimePointerSize);
72 if (UNLIKELY(art_field == nullptr)) {
73 if (IsStatic()) {
74 art_field = declaring_class->FindDeclaredStaticField(dex_cache, GetDexFieldIndex());
75 } else {
76 art_field = declaring_class->FindInstanceField(dex_cache, GetDexFieldIndex());
77 }
78 CHECK(art_field != nullptr);
79 dex_cache->SetResolvedField(GetDexFieldIndex(), art_field, kRuntimePointerSize);
80 }
Hiroshi Yamauchi679b1cf2015-05-21 12:05:27 -070081 CHECK_EQ(declaring_class, art_field->GetDeclaringClass());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070082 return art_field;
83}
84
85} // namespace mirror
86} // namespace art