• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
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 =
36         IsStatic() ? GetDeclaringClass()->GetSFields() : GetDeclaringClass()->GetIFields();
37     auto position = std::find_if(
38         new_range.begin(), new_range.end(), [&](const auto& f) { return &f == new_value; });
39     DCHECK(position != new_range.end());
40     SetArtFieldIndex<false>(std::distance(new_range.begin(), position));
41     WriteBarrier::ForEveryFieldWrite(this);
42   }
43   DCHECK_EQ(new_value, GetArtField());
44 }
45 
GetArtField()46 ArtField* Field::GetArtField() {
47   ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
48   DCHECK_LT(GetArtFieldIndex(),
49             IsStatic() ? declaring_class->NumStaticFields() : declaring_class->NumInstanceFields());
50   if (IsStatic()) {
51     return declaring_class->GetStaticField(GetArtFieldIndex());
52   } else {
53     return declaring_class->GetInstanceField(GetArtFieldIndex());
54   }
55 }
56 
57 }  // namespace mirror
58 }  // namespace art
59