field.h revision 7940e44f4517de5e2634a7e07d58d0fb26160513
1/*
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
17#ifndef ART_SRC_MIRROR_FIELD_H_
18#define ART_SRC_MIRROR_FIELD_H_
19
20#include "class.h"
21#include "modifiers.h"
22#include "object.h"
23
24namespace art {
25
26struct FieldClassOffsets;
27struct FieldOffsets;
28
29namespace mirror {
30
31// C++ mirror of java.lang.reflect.Field
32class MANAGED Field : public Object {
33 public:
34  Class* GetDeclaringClass() const;
35
36  void SetDeclaringClass(Class *new_declaring_class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
37
38  uint32_t GetAccessFlags() const;
39
40  void SetAccessFlags(uint32_t new_access_flags) {
41    SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
42  }
43
44  bool IsPublic() const {
45    return (GetAccessFlags() & kAccPublic) != 0;
46  }
47
48  bool IsStatic() const {
49    return (GetAccessFlags() & kAccStatic) != 0;
50  }
51
52  bool IsFinal() const {
53    return (GetAccessFlags() & kAccFinal) != 0;
54  }
55
56  uint32_t GetDexFieldIndex() const {
57    return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
58  }
59
60  void SetDexFieldIndex(uint32_t new_idx) {
61    SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
62  }
63
64  // Offset to field within an Object
65  MemberOffset GetOffset() const;
66
67  static MemberOffset OffsetOffset() {
68    return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
69  }
70
71  MemberOffset GetOffsetDuringLinking() const;
72
73  void SetOffset(MemberOffset num_bytes);
74
75  // field access, null object for static fields
76  bool GetBoolean(const Object* object) const
77      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
78  void SetBoolean(Object* object, bool z) const
79      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80  int8_t GetByte(const Object* object) const
81      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82  void SetByte(Object* object, int8_t b) const
83      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
84  uint16_t GetChar(const Object* object) const
85      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86  void SetChar(Object* object, uint16_t c) const
87      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88  int16_t GetShort(const Object* object) const
89      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
90  void SetShort(Object* object, int16_t s) const
91      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
92  int32_t GetInt(const Object* object) const
93      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94  void SetInt(Object* object, int32_t i) const
95      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96  int64_t GetLong(const Object* object) const
97      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98  void SetLong(Object* object, int64_t j) const
99      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
100  float GetFloat(const Object* object) const
101      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102  void SetFloat(Object* object, float f) const
103      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
104  double GetDouble(const Object* object) const
105      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106  void SetDouble(Object* object, double d) const
107      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108  Object* GetObject(const Object* object) const
109      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110  void SetObject(Object* object, const Object* l) const
111      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112
113  // raw field accesses
114  uint32_t Get32(const Object* object) const
115      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116  void Set32(Object* object, uint32_t new_value) const
117      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
118  uint64_t Get64(const Object* object) const
119      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120  void Set64(Object* object, uint64_t new_value) const
121      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122  Object* GetObj(const Object* object) const
123      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
124  void SetObj(Object* object, const Object* new_value) const
125      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126
127  static Class* GetJavaLangReflectField() {
128    DCHECK(java_lang_reflect_Field_ != NULL);
129    return java_lang_reflect_Field_;
130  }
131
132  static void SetClass(Class* java_lang_reflect_Field);
133  static void ResetClass();
134
135  bool IsVolatile() const {
136    return (GetAccessFlags() & kAccVolatile) != 0;
137  }
138
139 private:
140  // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
141  // The class we are a part of
142  Class* declaring_class_;
143
144  uint32_t access_flags_;
145
146  // Dex cache index of field id
147  uint32_t field_dex_idx_;
148
149  // Offset of field within an instance or in the Class' static fields
150  uint32_t offset_;
151
152  static Class* java_lang_reflect_Field_;
153
154  friend struct art::FieldOffsets;  // for verifying offset information
155  DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
156};
157
158class MANAGED FieldClass : public Class {
159 private:
160  Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
161  friend struct art::FieldClassOffsets;  // for verifying offset information
162  DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
163};
164
165}  // namespace mirror
166}  // namespace art
167
168#endif  // ART_SRC_MIRROR_FIELD_H_
169