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 #ifndef ART_RUNTIME_MIRROR_FIELD_INL_H_
18 #define ART_RUNTIME_MIRROR_FIELD_INL_H_
19
20 #include "field.h"
21
22 #include "art_field-inl.h"
23 #include "mirror/dex_cache-inl.h"
24
25 namespace art {
26
27 namespace mirror {
28
29 template <PointerSize kPointerSize, bool kTransactionActive>
CreateFromArtField(Thread * self,ArtField * field,bool force_resolve)30 inline mirror::Field* Field::CreateFromArtField(Thread* self, ArtField* field, bool force_resolve) {
31 StackHandleScope<2> hs(self);
32 // Try to resolve type before allocating since this is a thread suspension point.
33 Handle<mirror::Class> type = hs.NewHandle(field->GetType<true>());
34
35 if (type == nullptr) {
36 if (force_resolve) {
37 if (kIsDebugBuild) {
38 self->AssertPendingException();
39 }
40 return nullptr;
41 } else {
42 // Can't resolve, clear the exception if it isn't OOME and continue with a null type.
43 mirror::Throwable* exception = self->GetException();
44 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
45 return nullptr;
46 }
47 self->ClearException();
48 }
49 }
50 auto ret = hs.NewHandle(ObjPtr<Field>::DownCast(StaticClass()->AllocObject(self)));
51 if (UNLIKELY(ret == nullptr)) {
52 self->AssertPendingOOMException();
53 return nullptr;
54 }
55 auto dex_field_index = field->GetDexFieldIndex();
56 auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, kPointerSize);
57 if (field->GetDeclaringClass()->IsProxyClass()) {
58 DCHECK(field->IsStatic());
59 DCHECK_LT(dex_field_index, 2U);
60 // The two static fields (interfaces, throws) of all proxy classes
61 // share the same dex file indices 0 and 1. So, we can't resolve
62 // them in the dex cache.
63 } else {
64 if (resolved_field != nullptr) {
65 DCHECK_EQ(resolved_field, field);
66 } else {
67 // We rely on the field being resolved so that we can back to the ArtField
68 // (i.e. FromReflectedMethod).
69 field->GetDexCache()->SetResolvedField(dex_field_index, field, kPointerSize);
70 }
71 }
72 ret->SetType<kTransactionActive>(type.Get());
73 ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass());
74 ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags());
75 ret->SetDexFieldIndex<kTransactionActive>(dex_field_index);
76 ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value());
77 return ret.Get();
78 }
79
80 template<bool kTransactionActive>
SetDeclaringClass(ObjPtr<mirror::Class> c)81 inline void Field::SetDeclaringClass(ObjPtr<mirror::Class> c) {
82 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), c);
83 }
84
85 template<bool kTransactionActive>
SetType(ObjPtr<mirror::Class> type)86 inline void Field::SetType(ObjPtr<mirror::Class> type) {
87 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, type_), type);
88 }
89
90 } // namespace mirror
91 } // namespace art
92
93 #endif // ART_RUNTIME_MIRROR_FIELD_INL_H_
94