blob: 8b0f8ce1f0c360c513d865e6650536a25456d449 [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"
Vladimir Marko3481ba22015-04-13 12:22:36 +010023#include "mirror/dex_cache-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070024#include "runtime-inl.h"
25
26namespace art {
27
28namespace mirror {
29
Andreas Gampe542451c2016-07-26 09:02:02 -070030template <PointerSize kPointerSize, bool kTransactionActive>
31inline mirror::Field* Field::CreateFromArtField(Thread* self, ArtField* field, bool force_resolve) {
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070032 StackHandleScope<2> hs(self);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070033 // Try to resolve type before allocating since this is a thread suspension point.
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070034 Handle<mirror::Class> type = hs.NewHandle(field->GetType<true>());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070035
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070036 if (type.Get() == nullptr) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070037 if (force_resolve) {
38 if (kIsDebugBuild) {
39 self->AssertPendingException();
40 }
41 return nullptr;
42 } else {
43 // Can't resolve, clear the exception if it isn't OOME and continue with a null type.
44 mirror::Throwable* exception = self->GetException();
45 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
46 return nullptr;
47 }
48 self->ClearException();
49 }
50 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070051 auto ret = hs.NewHandle(static_cast<Field*>(StaticClass()->AllocObject(self)));
Mathieu Chartiere401d142015-04-22 13:56:20 -070052 if (UNLIKELY(ret.Get() == nullptr)) {
53 self->AssertPendingOOMException();
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070054 return nullptr;
55 }
56 auto dex_field_index = field->GetDexFieldIndex();
Andreas Gampee01e3642016-07-25 13:06:04 -070057 auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, kPointerSize);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000058 if (field->GetDeclaringClass()->IsProxyClass()) {
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070059 DCHECK(field->IsStatic());
60 DCHECK_LT(dex_field_index, 2U);
61 // The two static fields (interfaces, throws) of all proxy classes
62 // share the same dex file indices 0 and 1. So, we can't resolve
63 // them in the dex cache.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070064 } else {
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070065 if (resolved_field != nullptr) {
66 DCHECK_EQ(resolved_field, field);
67 } else {
68 // We rely on the field being resolved so that we can back to the ArtField
69 // (i.e. FromReflectedMethod).
Andreas Gampee01e3642016-07-25 13:06:04 -070070 field->GetDexCache()->SetResolvedField(dex_field_index, field, kPointerSize);
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070071 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070072 }
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070073 ret->SetType<kTransactionActive>(type.Get());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070074 ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass());
75 ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags());
76 ret->SetDexFieldIndex<kTransactionActive>(dex_field_index);
77 ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value());
78 return ret.Get();
79}
80
81} // namespace mirror
82} // namespace art
83
84#endif // ART_RUNTIME_MIRROR_FIELD_INL_H_