Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 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" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 23 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 24 | #include "runtime-inl.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | namespace mirror { |
| 29 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 30 | template <PointerSize kPointerSize, bool kTransactionActive> |
| 31 | inline mirror::Field* Field::CreateFromArtField(Thread* self, ArtField* field, bool force_resolve) { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 32 | StackHandleScope<2> hs(self); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 33 | // Try to resolve type before allocating since this is a thread suspension point. |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 34 | Handle<mirror::Class> type = hs.NewHandle(field->GetType<true>()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 35 | |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 36 | if (type.Get() == nullptr) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 37 | 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 Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 51 | auto ret = hs.NewHandle(static_cast<Field*>(StaticClass()->AllocObject(self))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 52 | if (UNLIKELY(ret.Get() == nullptr)) { |
| 53 | self->AssertPendingOOMException(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 54 | return nullptr; |
| 55 | } |
| 56 | auto dex_field_index = field->GetDexFieldIndex(); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 57 | auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, kPointerSize); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 58 | if (field->GetDeclaringClass()->IsProxyClass()) { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 59 | 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 Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 64 | } else { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 65 | 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 Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 70 | field->GetDexCache()->SetResolvedField(dex_field_index, field, kPointerSize); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 71 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 72 | } |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 73 | ret->SetType<kTransactionActive>(type.Get()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 74 | 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_ |