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 "class-alloc-inl.h"
24 #include "class_root.h"
25 #include "dex_cache-inl.h"
26 #include "object-inl.h"
27
28 namespace art {
29
30 namespace mirror {
31
GetDeclaringClass()32 inline ObjPtr<mirror::Class> Field::GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) {
33 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_));
34 }
35
GetTypeAsPrimitiveType()36 inline Primitive::Type Field::GetTypeAsPrimitiveType() {
37 return GetType()->GetPrimitiveType();
38 }
39
GetType()40 inline ObjPtr<mirror::Class> Field::GetType() {
41 return GetFieldObject<mirror::Class>(OFFSET_OF_OBJECT_MEMBER(Field, type_));
42 }
43
44 template <PointerSize kPointerSize, bool kTransactionActive>
CreateFromArtField(Thread * self,ArtField * field,bool force_resolve)45 inline ObjPtr<mirror::Field> Field::CreateFromArtField(Thread* self,
46 ArtField* field,
47 bool force_resolve) {
48 StackHandleScope<2> hs(self);
49 // Try to resolve type before allocating since this is a thread suspension point.
50 Handle<mirror::Class> type = hs.NewHandle(field->ResolveType());
51
52 if (type == nullptr) {
53 if (force_resolve) {
54 if (kIsDebugBuild) {
55 self->AssertPendingException();
56 }
57 return nullptr;
58 } else {
59 // Can't resolve, clear the exception if it isn't OOME and continue with a null type.
60 mirror::Throwable* exception = self->GetException();
61 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
62 return nullptr;
63 }
64 self->ClearException();
65 }
66 }
67 auto ret = hs.NewHandle(ObjPtr<Field>::DownCast(GetClassRoot<Field>()->AllocObject(self)));
68 if (UNLIKELY(ret == nullptr)) {
69 self->AssertPendingOOMException();
70 return nullptr;
71 }
72 auto dex_field_index = field->GetDexFieldIndex();
73 auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, kPointerSize);
74 if (field->GetDeclaringClass()->IsProxyClass()) {
75 DCHECK(field->IsStatic());
76 DCHECK_LT(dex_field_index, 2U);
77 // The two static fields (interfaces, throws) of all proxy classes
78 // share the same dex file indices 0 and 1. So, we can't resolve
79 // them in the dex cache.
80 } else {
81 if (resolved_field != nullptr) {
82 DCHECK_EQ(resolved_field, field);
83 } else {
84 // We rely on the field being resolved so that we can back to the ArtField
85 // (i.e. FromReflectedMethod).
86 field->GetDexCache()->SetResolvedField(dex_field_index, field, kPointerSize);
87 }
88 }
89 ret->SetType<kTransactionActive>(type.Get());
90 ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass());
91 ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags());
92 auto iter_range = field->IsStatic() ? field->GetDeclaringClass()->GetSFields()
93 : field->GetDeclaringClass()->GetIFields();
94 auto position = std::find_if(
95 iter_range.begin(), iter_range.end(), [&](const auto& f) { return &f == field; });
96 DCHECK(position != iter_range.end());
97 ret->SetArtFieldIndex<kTransactionActive>(std::distance(iter_range.begin(), position));
98 ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value());
99 return ret.Get();
100 }
101
102 template<bool kTransactionActive>
SetDeclaringClass(ObjPtr<mirror::Class> c)103 inline void Field::SetDeclaringClass(ObjPtr<mirror::Class> c) {
104 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), c);
105 }
106
107 template<bool kTransactionActive>
SetType(ObjPtr<mirror::Class> type)108 inline void Field::SetType(ObjPtr<mirror::Class> type) {
109 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, type_), type);
110 }
111
112 } // namespace mirror
113 } // namespace art
114
115 #endif // ART_RUNTIME_MIRROR_FIELD_INL_H_
116