1 /*
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 
19 #include "class-inl.h"
20 #include "dex_cache-inl.h"
21 #include "object-inl.h"
22 #include "object_array-inl.h"
23 
24 namespace art {
25 namespace mirror {
26 
GetArtField()27 ArtField* Field::GetArtField() {
28   ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
29   if (UNLIKELY(declaring_class->IsProxyClass())) {
30     DCHECK(IsStatic());
31     DCHECK_EQ(declaring_class->NumStaticFields(), 2U);
32     // 0 == Class[] interfaces; 1 == Class[][] throws;
33     if (GetDexFieldIndex() == 0) {
34       return &declaring_class->GetSFieldsPtr()->At(0);
35     } else {
36       DCHECK_EQ(GetDexFieldIndex(), 1U);
37       return &declaring_class->GetSFieldsPtr()->At(1);
38     }
39   }
40   const ObjPtr<mirror::DexCache> dex_cache = declaring_class->GetDexCache();
41   ArtField* art_field = dex_cache->GetResolvedField(GetDexFieldIndex(), kRuntimePointerSize);
42   if (UNLIKELY(art_field == nullptr)) {
43     if (IsStatic()) {
44       art_field = declaring_class->FindDeclaredStaticField(dex_cache, GetDexFieldIndex());
45     } else {
46       art_field = declaring_class->FindInstanceField(dex_cache, GetDexFieldIndex());
47     }
48     CHECK(art_field != nullptr);
49     dex_cache->SetResolvedField(GetDexFieldIndex(), art_field, kRuntimePointerSize);
50   }
51   CHECK_EQ(declaring_class, art_field->GetDeclaringClass());
52   return art_field;
53 }
54 
55 }  // namespace mirror
56 }  // namespace art
57