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 | |
| 30 | template <bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 31 | inline mirror::Field* Field::CreateFromArtField(Thread* self, ArtField* field, |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 32 | bool force_resolve) { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 33 | StackHandleScope<2> hs(self); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 34 | // 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] | 35 | Handle<mirror::Class> type = hs.NewHandle(field->GetType<true>()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 36 | |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 37 | if (type.Get() == nullptr) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 38 | 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 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 52 | auto ret = hs.NewHandle(static_cast<Field*>(StaticClass()->AllocObject(self))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 53 | if (UNLIKELY(ret.Get() == nullptr)) { |
| 54 | self->AssertPendingOOMException(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 55 | return nullptr; |
| 56 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 57 | const auto pointer_size = kTransactionActive ? |
| 58 | Runtime::Current()->GetClassLinker()->GetImagePointerSize() : sizeof(void*); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 59 | auto dex_field_index = field->GetDexFieldIndex(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 60 | auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, pointer_size); |
| 61 | if (field->GetDeclaringClass()->IsProxyClass()) { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 62 | DCHECK(field->IsStatic()); |
| 63 | DCHECK_LT(dex_field_index, 2U); |
| 64 | // The two static fields (interfaces, throws) of all proxy classes |
| 65 | // share the same dex file indices 0 and 1. So, we can't resolve |
| 66 | // them in the dex cache. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 67 | } else { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 68 | if (resolved_field != nullptr) { |
| 69 | DCHECK_EQ(resolved_field, field); |
| 70 | } else { |
| 71 | // We rely on the field being resolved so that we can back to the ArtField |
| 72 | // (i.e. FromReflectedMethod). |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 73 | field->GetDexCache()->SetResolvedField(dex_field_index, field, pointer_size); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 74 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 75 | } |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 76 | ret->SetType<kTransactionActive>(type.Get()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 77 | ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass()); |
| 78 | ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags()); |
| 79 | ret->SetDexFieldIndex<kTransactionActive>(dex_field_index); |
| 80 | ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value()); |
| 81 | return ret.Get(); |
| 82 | } |
| 83 | |
| 84 | } // namespace mirror |
| 85 | } // namespace art |
| 86 | |
| 87 | #endif // ART_RUNTIME_MIRROR_FIELD_INL_H_ |