blob: 803b880701e0aec992aa9934fc2c08a2445a92c0 [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#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"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070023#include "class-alloc-inl.h"
Vladimir Marko679730e2018-05-25 15:06:48 +010024#include "class_root.h"
Andreas Gampe895f9222017-07-05 09:53:32 -070025#include "dex_cache-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070026
27namespace art {
28
29namespace mirror {
30
Andreas Gampe542451c2016-07-26 09:02:02 -070031template <PointerSize kPointerSize, bool kTransactionActive>
32inline mirror::Field* Field::CreateFromArtField(Thread* self, ArtField* field, bool force_resolve) {
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070033 StackHandleScope<2> hs(self);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070034 // Try to resolve type before allocating since this is a thread suspension point.
Vladimir Marko4098a7a2017-11-06 16:00:51 +000035 Handle<mirror::Class> type = hs.NewHandle(field->ResolveType());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070036
Andreas Gampefa4333d2017-02-14 11:10:34 -080037 if (type == nullptr) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070038 if (force_resolve) {
39 if (kIsDebugBuild) {
40 self->AssertPendingException();
41 }
42 return nullptr;
43 } else {
44 // Can't resolve, clear the exception if it isn't OOME and continue with a null type.
45 mirror::Throwable* exception = self->GetException();
46 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
47 return nullptr;
48 }
49 self->ClearException();
50 }
51 }
Vladimir Marko679730e2018-05-25 15:06:48 +010052 auto ret = hs.NewHandle(ObjPtr<Field>::DownCast(GetClassRoot<Field>()->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -080053 if (UNLIKELY(ret == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070054 self->AssertPendingOOMException();
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070055 return nullptr;
56 }
57 auto dex_field_index = field->GetDexFieldIndex();
Andreas Gampee01e3642016-07-25 13:06:04 -070058 auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, kPointerSize);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000059 if (field->GetDeclaringClass()->IsProxyClass()) {
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070060 DCHECK(field->IsStatic());
61 DCHECK_LT(dex_field_index, 2U);
62 // The two static fields (interfaces, throws) of all proxy classes
63 // share the same dex file indices 0 and 1. So, we can't resolve
64 // them in the dex cache.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070065 } else {
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070066 if (resolved_field != nullptr) {
67 DCHECK_EQ(resolved_field, field);
68 } else {
69 // We rely on the field being resolved so that we can back to the ArtField
70 // (i.e. FromReflectedMethod).
Andreas Gampee01e3642016-07-25 13:06:04 -070071 field->GetDexCache()->SetResolvedField(dex_field_index, field, kPointerSize);
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070072 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070073 }
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070074 ret->SetType<kTransactionActive>(type.Get());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070075 ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass());
76 ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags());
77 ret->SetDexFieldIndex<kTransactionActive>(dex_field_index);
78 ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value());
79 return ret.Get();
80}
81
Mathieu Chartier3398c782016-09-30 10:27:43 -070082template<bool kTransactionActive>
Mathieu Chartier31e88222016-10-14 18:43:19 -070083inline void Field::SetDeclaringClass(ObjPtr<mirror::Class> c) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070084 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), c);
Mathieu Chartier3398c782016-09-30 10:27:43 -070085}
86
Mathieu Chartier31e88222016-10-14 18:43:19 -070087template<bool kTransactionActive>
88inline void Field::SetType(ObjPtr<mirror::Class> type) {
89 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, type_), type);
90}
91
Andreas Gampe895f9222017-07-05 09:53:32 -070092inline Primitive::Type Field::GetTypeAsPrimitiveType() {
93 return GetType()->GetPrimitiveType();
94}
95
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070096} // namespace mirror
97} // namespace art
98
99#endif // ART_RUNTIME_MIRROR_FIELD_INL_H_