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 #include "field-inl.h"
18
19 #include "class-inl.h"
20 #include "dex_cache-inl.h"
21 #include "object-inl.h"
22 #include "object_array-inl.h"
23 #include "write_barrier.h"
24
25 namespace art HIDDEN {
26 namespace mirror {
27
VisitTarget(ReflectiveValueVisitor * v)28 void Field::VisitTarget(ReflectiveValueVisitor* v) {
29 HeapReflectiveSourceInfo hrsi(kSourceJavaLangReflectField, this);
30 ArtField* orig = GetArtField();
31 ArtField* new_value = v->VisitField(orig, hrsi);
32 if (orig != new_value) {
33 SetOffset<false>(new_value->GetOffset().Int32Value());
34 SetDeclaringClass<false>(new_value->GetDeclaringClass());
35 auto new_range = GetDeclaringClass()->GetFields();
36 auto position = std::find_if(
37 new_range.begin(), new_range.end(), [&](const auto& f) { return &f == new_value; });
38 DCHECK(position != new_range.end());
39 SetArtFieldIndex<false>(std::distance(new_range.begin(), position));
40 WriteBarrier::ForEveryFieldWrite(this);
41 }
42 DCHECK_EQ(new_value, GetArtField());
43 }
44
GetArtField()45 ArtField* Field::GetArtField() {
46 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
47 return declaring_class->GetField(GetArtFieldIndex());
48 }
49
CreateFromArtField(Thread * self,ArtField * field,bool force_resolve)50 ObjPtr<mirror::Field> Field::CreateFromArtField(Thread* self,
51 ArtField* field,
52 bool force_resolve) {
53 StackHandleScope<2> hs(self);
54 // Try to resolve type before allocating since this is a thread suspension point.
55 Handle<mirror::Class> type = hs.NewHandle(field->ResolveType());
56
57 if (type == nullptr) {
58 DCHECK(self->IsExceptionPending());
59 if (force_resolve) {
60 return nullptr;
61 } else {
62 // Can't resolve, clear the exception if it isn't OOME and continue with a null type.
63 mirror::Throwable* exception = self->GetException();
64 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
65 return nullptr;
66 }
67 self->ClearException();
68 }
69 }
70 auto ret = hs.NewHandle(ObjPtr<Field>::DownCast(GetClassRoot<Field>()->AllocObject(self)));
71 if (UNLIKELY(ret == nullptr)) {
72 self->AssertPendingOOMException();
73 return nullptr;
74 }
75 // We're initializing a newly allocated object, so we do not need to record that under
76 // a transaction. If the transaction is aborted, the whole object shall be unreachable.
77 ret->SetType</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(type.Get());
78 ret->SetDeclaringClass</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
79 field->GetDeclaringClass());
80 ret->SetAccessFlags</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
81 field->GetAccessFlags());
82 auto iter_range = field->GetDeclaringClass()->GetFields();
83 auto position = std::find_if(
84 iter_range.begin(), iter_range.end(), [&](const auto& f) { return &f == field; });
85 DCHECK(position != iter_range.end());
86 ret->SetArtFieldIndex</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
87 std::distance(iter_range.begin(), position));
88 ret->SetOffset</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
89 field->GetOffset().Int32Value());
90 return ret.Get();
91 }
92
93 } // namespace mirror
94 } // namespace art
95