blob: 6ba8dc646dc75d745389e0355b8fc856c5f09be9 [file] [log] [blame]
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001/*
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_H_
18#define ART_RUNTIME_MIRROR_FIELD_H_
19
20#include "accessible_object.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
David Sehr8c0961f2018-01-23 16:11:38 -080022#include "dex/modifiers.h"
David Sehr67bf42e2018-02-26 16:43:04 -080023#include "dex/primitive.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070024#include "obj_ptr.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070025#include "object.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070026#include "read_barrier_option.h"
27
28namespace art {
29
Mathieu Chartierc7853442015-03-27 14:35:38 -070030class ArtField;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070031struct FieldOffsets;
32
33namespace mirror {
34
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070035class Class;
36class String;
37
38// C++ mirror of java.lang.reflect.Field.
39class MANAGED Field : public AccessibleObject {
40 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070041 ALWAYS_INLINE uint32_t GetDexFieldIndex() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070042 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_));
43 }
44
Vladimir Marko0eefb9b2019-03-27 15:04:31 +000045 ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070046
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070047 uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070048 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_));
49 }
50
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070052 return (GetAccessFlags() & kAccStatic) != 0;
53 }
54
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070055 bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070056 return (GetAccessFlags() & kAccFinal) != 0;
57 }
58
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070060 return (GetAccessFlags() & kAccVolatile) != 0;
61 }
62
Andreas Gampe895f9222017-07-05 09:53:32 -070063 ALWAYS_INLINE Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070064
Vladimir Marko0eefb9b2019-03-27 15:04:31 +000065 ObjPtr<mirror::Class> GetType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070066
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 int32_t GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070068 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_));
69 }
70
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070071 // Slow, try to use only for PrettyField and such.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 ArtField* GetArtField() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070073
Andreas Gampe542451c2016-07-26 09:02:02 -070074 template <PointerSize kPointerSize, bool kTransactionActive = false>
Vladimir Marko0eefb9b2019-03-27 15:04:31 +000075 static ObjPtr<mirror::Field> CreateFromArtField(Thread* self,
76 ArtField* field,
77 bool force_resolve)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070079
80 private:
Neil Fuller60458a02016-09-01 15:32:44 +010081 // Padding required for matching alignment with the Java peer.
82 uint8_t padding_[2];
83
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070084 HeapReference<mirror::Class> declaring_class_;
85 HeapReference<mirror::Class> type_;
86 int32_t access_flags_;
87 int32_t dex_field_index_;
88 int32_t offset_;
89
90 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -070091 void SetDeclaringClass(ObjPtr<mirror::Class> c) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070092
93 template<bool kTransactionActive>
Mathieu Chartier31e88222016-10-14 18:43:19 -070094 void SetType(ObjPtr<mirror::Class> type) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070095
96 template<bool kTransactionActive>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 void SetAccessFlags(uint32_t flags) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070098 SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), flags);
99 }
100
101 template<bool kTransactionActive>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 void SetDexFieldIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700103 SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, dex_field_index_), idx);
104 }
105
106 template<bool kTransactionActive>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 void SetOffset(uint32_t offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700108 SetField32<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(Field, offset_), offset);
109 }
110
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700111 friend struct art::FieldOffsets; // for verifying offset information
112 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
113};
114
115} // namespace mirror
116} // namespace art
117
118#endif // ART_RUNTIME_MIRROR_FIELD_H_