blob: 5a4ebd1f6e615bc76928529ad459366de521ea38 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 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
Brian Carlstromea46f952013-07-30 01:26:50 -070017#include "art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "art_field-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070020#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "object-inl.h"
Sebastien Hertz479fc1e2014-04-04 17:51:34 +020022#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "runtime.h"
Ian Rogers62f05122014-03-21 11:21:29 -070024#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "utils.h"
Ian Rogers62f05122014-03-21 11:21:29 -070026#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027
28namespace art {
29namespace mirror {
30
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031// TODO: Get global references for these
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070032GcRoot<Class> ArtField::java_lang_reflect_ArtField_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033
Brian Carlstromea46f952013-07-30 01:26:50 -070034void ArtField::SetClass(Class* java_lang_reflect_ArtField) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070035 CHECK(java_lang_reflect_ArtField_.IsNull());
Brian Carlstromea46f952013-07-30 01:26:50 -070036 CHECK(java_lang_reflect_ArtField != NULL);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070037 java_lang_reflect_ArtField_ = GcRoot<Class>(java_lang_reflect_ArtField);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038}
39
Brian Carlstromea46f952013-07-30 01:26:50 -070040void ArtField::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070041 CHECK(!java_lang_reflect_ArtField_.IsNull());
42 java_lang_reflect_ArtField_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043}
44
Brian Carlstromea46f952013-07-30 01:26:50 -070045void ArtField::SetOffset(MemberOffset num_bytes) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010047 if (kIsDebugBuild && Runtime::Current()->IsCompiler() &&
48 !Runtime::Current()->UseCompileTimeClassPath()) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070049 Primitive::Type type = GetTypeAsPrimitiveType();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010050 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
51 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
52 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010054 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070055 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ArtField, offset_), num_bytes.Uint32Value());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056}
57
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080058void ArtField::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080059 java_lang_reflect_ArtField_.VisitRootIfNonNull(callback, arg, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080060}
61
Sebastien Hertz479fc1e2014-04-04 17:51:34 +020062// TODO: we could speed up the search if fields are ordered by offsets.
63ArtField* ArtField::FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset) {
64 DCHECK(klass != nullptr);
65 ObjectArray<ArtField>* instance_fields = klass->GetIFields();
66 if (instance_fields != nullptr) {
67 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
68 mirror::ArtField* field = instance_fields->GetWithoutChecks(i);
69 if (field->GetOffset().Uint32Value() == field_offset) {
70 return field;
71 }
72 }
73 }
74 // We did not find field in the class: look into superclass.
75 if (klass->GetSuperClass() != NULL) {
76 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
77 } else {
78 return nullptr;
79 }
80}
81
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082} // namespace mirror
83} // namespace art