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