blob: 7db181112ad176d2329ad019e731d53138c8e942 [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"
23#include "runtime-inl.h"
24
25namespace art {
26
27namespace mirror {
28
29template <bool kTransactionActive>
Mathieu Chartierc7853442015-03-27 14:35:38 -070030inline mirror::Field* Field::CreateFromArtField(Thread* self, ArtField* field,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070031 bool force_resolve) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070032 // Try to resolve type before allocating since this is a thread suspension point.
33 mirror::Class* type = 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 StackHandleScope<1> hs(self);
51 auto ret = hs.NewHandle(static_cast<Field*>(StaticClass()->AllocObject(self)));
52 if (ret.Get() == nullptr) {
53 if (kIsDebugBuild) {
54 self->AssertPendingException();
55 }
56 return nullptr;
57 }
58 auto dex_field_index = field->GetDexFieldIndex();
Mathieu Chartierc7853442015-03-27 14:35:38 -070059 auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, sizeof(void*));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070060 if (resolved_field != nullptr) {
61 DCHECK_EQ(resolved_field, field);
62 } else {
63 // We rely on the field being resolved so that we can back to the ArtField
64 // (i.e. FromReflectedMethod).
Mathieu Chartierc7853442015-03-27 14:35:38 -070065 field->GetDexCache()->SetResolvedField(dex_field_index, field, sizeof(void*));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070066 }
67 ret->SetType<kTransactionActive>(type);
68 ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass());
69 ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags());
70 ret->SetDexFieldIndex<kTransactionActive>(dex_field_index);
71 ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value());
72 return ret.Get();
73}
74
75} // namespace mirror
76} // namespace art
77
78#endif // ART_RUNTIME_MIRROR_FIELD_INL_H_