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" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 23 | #include "class-alloc-inl.h" |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 24 | #include "class_root.h" |
Andreas Gampe | 895f922 | 2017-07-05 09:53:32 -0700 | [diff] [blame] | 25 | #include "dex_cache-inl.h" |
Vladimir Marko | 0eefb9b | 2019-03-27 15:04:31 +0000 | [diff] [blame] | 26 | #include "object-inl.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | namespace mirror { |
| 31 | |
Vladimir Marko | 0eefb9b | 2019-03-27 15:04:31 +0000 | [diff] [blame] | 32 | inline ObjPtr<mirror::Class> Field::GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 33 | return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_)); |
| 34 | } |
| 35 | |
| 36 | inline Primitive::Type Field::GetTypeAsPrimitiveType() { |
| 37 | return GetType()->GetPrimitiveType(); |
| 38 | } |
| 39 | |
| 40 | inline ObjPtr<mirror::Class> Field::GetType() { |
| 41 | return GetFieldObject<mirror::Class>(OFFSET_OF_OBJECT_MEMBER(Field, type_)); |
| 42 | } |
| 43 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 44 | template <PointerSize kPointerSize, bool kTransactionActive> |
Vladimir Marko | 0eefb9b | 2019-03-27 15:04:31 +0000 | [diff] [blame] | 45 | inline ObjPtr<mirror::Field> Field::CreateFromArtField(Thread* self, |
| 46 | ArtField* field, |
| 47 | bool force_resolve) { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 48 | StackHandleScope<2> hs(self); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 49 | // Try to resolve type before allocating since this is a thread suspension point. |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 50 | Handle<mirror::Class> type = hs.NewHandle(field->ResolveType()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 51 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 52 | if (type == nullptr) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 53 | if (force_resolve) { |
| 54 | if (kIsDebugBuild) { |
| 55 | self->AssertPendingException(); |
| 56 | } |
| 57 | return nullptr; |
| 58 | } else { |
| 59 | // Can't resolve, clear the exception if it isn't OOME and continue with a null type. |
| 60 | mirror::Throwable* exception = self->GetException(); |
| 61 | if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) { |
| 62 | return nullptr; |
| 63 | } |
| 64 | self->ClearException(); |
| 65 | } |
| 66 | } |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 67 | auto ret = hs.NewHandle(ObjPtr<Field>::DownCast(GetClassRoot<Field>()->AllocObject(self))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 68 | if (UNLIKELY(ret == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 69 | self->AssertPendingOOMException(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 70 | return nullptr; |
| 71 | } |
| 72 | auto dex_field_index = field->GetDexFieldIndex(); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 73 | auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, kPointerSize); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 74 | if (field->GetDeclaringClass()->IsProxyClass()) { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 75 | DCHECK(field->IsStatic()); |
| 76 | DCHECK_LT(dex_field_index, 2U); |
| 77 | // The two static fields (interfaces, throws) of all proxy classes |
| 78 | // share the same dex file indices 0 and 1. So, we can't resolve |
| 79 | // them in the dex cache. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 80 | } else { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 81 | if (resolved_field != nullptr) { |
| 82 | DCHECK_EQ(resolved_field, field); |
| 83 | } else { |
| 84 | // We rely on the field being resolved so that we can back to the ArtField |
| 85 | // (i.e. FromReflectedMethod). |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 86 | field->GetDexCache()->SetResolvedField(dex_field_index, field, kPointerSize); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 87 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 88 | } |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 89 | ret->SetType<kTransactionActive>(type.Get()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 90 | ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass()); |
| 91 | ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags()); |
| 92 | ret->SetDexFieldIndex<kTransactionActive>(dex_field_index); |
| 93 | ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value()); |
| 94 | return ret.Get(); |
| 95 | } |
| 96 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 97 | template<bool kTransactionActive> |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 98 | inline void Field::SetDeclaringClass(ObjPtr<mirror::Class> c) { |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 99 | SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), c); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 102 | template<bool kTransactionActive> |
| 103 | inline void Field::SetType(ObjPtr<mirror::Class> type) { |
| 104 | SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, type_), type); |
| 105 | } |
| 106 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 107 | } // namespace mirror |
| 108 | } // namespace art |
| 109 | |
| 110 | #endif // ART_RUNTIME_MIRROR_FIELD_INL_H_ |