blob: b3b1b71e5b09d7c246a81c948f13dd38f691378c [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 "object_utils.h"
24#include "runtime.h"
Ian Rogers62f05122014-03-21 11:21:29 -070025#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "utils.h"
Ian Rogers62f05122014-03-21 11:21:29 -070027#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028
29namespace art {
30namespace mirror {
31
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070032// TODO: Get global references for these
Brian Carlstromea46f952013-07-30 01:26:50 -070033Class* ArtField::java_lang_reflect_ArtField_ = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070035ArtField* ArtField::FromReflectedField(const ScopedObjectAccessAlreadyRunnable& soa,
36 jobject jlr_field) {
Ian Rogers62f05122014-03-21 11:21:29 -070037 mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_reflect_Field_artField);
38 mirror::ArtField* field = f->GetObject(soa.Decode<mirror::Object*>(jlr_field))->AsArtField();
39 DCHECK(field != nullptr);
40 return field;
41}
42
Brian Carlstromea46f952013-07-30 01:26:50 -070043void ArtField::SetClass(Class* java_lang_reflect_ArtField) {
44 CHECK(java_lang_reflect_ArtField_ == NULL);
45 CHECK(java_lang_reflect_ArtField != NULL);
46 java_lang_reflect_ArtField_ = java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047}
48
Brian Carlstromea46f952013-07-30 01:26:50 -070049void ArtField::ResetClass() {
50 CHECK(java_lang_reflect_ArtField_ != NULL);
51 java_lang_reflect_ArtField_ = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
Brian Carlstromea46f952013-07-30 01:26:50 -070054void ArtField::SetOffset(MemberOffset num_bytes) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010056 if (kIsDebugBuild && Runtime::Current()->IsCompiler() &&
57 !Runtime::Current()->UseCompileTimeClassPath()) {
58 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
59 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
60 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
61 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010063 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070064 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ArtField, offset_), num_bytes.Uint32Value());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065}
66
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080067void ArtField::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080068 if (java_lang_reflect_ArtField_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -080069 callback(reinterpret_cast<mirror::Object**>(&java_lang_reflect_ArtField_), arg, 0,
70 kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080071 }
72}
73
Sebastien Hertz479fc1e2014-04-04 17:51:34 +020074// TODO: we could speed up the search if fields are ordered by offsets.
75ArtField* ArtField::FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset) {
76 DCHECK(klass != nullptr);
77 ObjectArray<ArtField>* instance_fields = klass->GetIFields();
78 if (instance_fields != nullptr) {
79 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
80 mirror::ArtField* field = instance_fields->GetWithoutChecks(i);
81 if (field->GetOffset().Uint32Value() == field_offset) {
82 return field;
83 }
84 }
85 }
86 // We did not find field in the class: look into superclass.
87 if (klass->GetSuperClass() != NULL) {
88 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
89 } else {
90 return nullptr;
91 }
92}
93
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094} // namespace mirror
95} // namespace art