1 /*
2  * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_OOPS_FIELDSTREAMS_HPP
26 #define SHARE_OOPS_FIELDSTREAMS_HPP
27 
28 #include "oops/instanceKlass.hpp"
29 #include "oops/fieldInfo.hpp"
30 #include "runtime/fieldDescriptor.hpp"
31 
32 // The is the base class for iteration over the fields array
33 // describing the declared fields in the class.  Several subclasses
34 // are provided depending on the kind of iteration required.  The
35 // JavaFieldStream is for iterating over regular Java fields and it
36 // generally the preferred iterator.  InternalFieldStream only
37 // iterates over fields that have been injected by the JVM.
38 // AllFieldStream exposes all fields and should only be used in rare
39 // cases.
40 class FieldStreamBase : public StackObj {
41  protected:
42   Array<u2>*          _fields;
43   constantPoolHandle  _constants;
44   int                 _index;
45   int                 _limit;
46   int                 _generic_signature_slot;
47   fieldDescriptor     _fd_buf;
48 
field() const49   FieldInfo* field() const { return FieldInfo::from_field_array(_fields, _index); }
field_holder() const50   InstanceKlass* field_holder() const { return _constants->pool_holder(); }
51 
init_generic_signature_start_slot()52   int init_generic_signature_start_slot() {
53     int length = _fields->length();
54     int num_fields = _index;
55     int skipped_generic_signature_slots = 0;
56     FieldInfo* fi;
57     AccessFlags flags;
58     /* Scan from 0 to the current _index. Count the number of generic
59        signature slots for field[0] to field[_index - 1]. */
60     for (int i = 0; i < _index; i++) {
61       fi = FieldInfo::from_field_array(_fields, i);
62       flags.set_flags(fi->access_flags());
63       if (flags.field_has_generic_signature()) {
64         length --;
65         skipped_generic_signature_slots ++;
66       }
67     }
68     /* Scan from the current _index. */
69     for (int i = _index; i*FieldInfo::field_slots < length; i++) {
70       fi = FieldInfo::from_field_array(_fields, i);
71       flags.set_flags(fi->access_flags());
72       if (flags.field_has_generic_signature()) {
73         length --;
74       }
75       num_fields ++;
76     }
77     _generic_signature_slot = length + skipped_generic_signature_slots;
78     assert(_generic_signature_slot <= _fields->length(), "");
79     return num_fields;
80   }
81 
82   inline FieldStreamBase(Array<u2>* fields, ConstantPool* constants, int start, int limit);
83 
84   inline FieldStreamBase(Array<u2>* fields, ConstantPool* constants);
85  public:
86   inline FieldStreamBase(InstanceKlass* klass);
87 
88   // accessors
index() const89   int index() const                 { return _index; }
90 
next()91   void next() {
92     if (access_flags().field_has_generic_signature()) {
93       _generic_signature_slot ++;
94       assert(_generic_signature_slot <= _fields->length(), "");
95     }
96     _index += 1;
97   }
done() const98   bool done() const { return _index >= _limit; }
99 
100   // Accessors for current field
access_flags() const101   AccessFlags access_flags() const {
102     AccessFlags flags;
103     flags.set_flags(field()->access_flags());
104     return flags;
105   }
106 
set_access_flags(u2 flags) const107   void set_access_flags(u2 flags) const {
108     field()->set_access_flags(flags);
109   }
110 
set_access_flags(AccessFlags flags) const111   void set_access_flags(AccessFlags flags) const {
112     set_access_flags(flags.as_short());
113   }
114 
name() const115   Symbol* name() const {
116     return field()->name(_constants());
117   }
118 
signature() const119   Symbol* signature() const {
120     return field()->signature(_constants());
121   }
122 
generic_signature() const123   Symbol* generic_signature() const {
124     if (access_flags().field_has_generic_signature()) {
125       assert(_generic_signature_slot < _fields->length(), "out of bounds");
126       int index = _fields->at(_generic_signature_slot);
127       return _constants->symbol_at(index);
128     } else {
129       return NULL;
130     }
131   }
132 
offset() const133   int offset() const {
134     return field()->offset();
135   }
136 
allocation_type() const137   int allocation_type() const {
138     return field()->allocation_type();
139   }
140 
set_offset(int offset)141   void set_offset(int offset) {
142     field()->set_offset(offset);
143   }
144 
is_offset_set() const145   bool is_offset_set() const {
146     return field()->is_offset_set();
147   }
148 
is_contended() const149   bool is_contended() const {
150     return field()->is_contended();
151   }
152 
contended_group() const153   int contended_group() const {
154     return field()->contended_group();
155   }
156 
157   // bridge to a heavier API:
field_descriptor() const158   fieldDescriptor& field_descriptor() const {
159     fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
160     field.reinitialize(field_holder(), _index);
161     return field;
162   }
163 };
164 
165 // Iterate over only the internal fields
166 class JavaFieldStream : public FieldStreamBase {
167  public:
JavaFieldStream(const InstanceKlass * k)168   JavaFieldStream(const InstanceKlass* k): FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {}
169 
name_index() const170   int name_index() const {
171     assert(!field()->is_internal(), "regular only");
172     return field()->name_index();
173   }
set_name_index(int index)174   void set_name_index(int index) {
175     assert(!field()->is_internal(), "regular only");
176     field()->set_name_index(index);
177   }
signature_index() const178   int signature_index() const {
179     assert(!field()->is_internal(), "regular only");
180     return field()->signature_index();
181   }
set_signature_index(int index)182   void set_signature_index(int index) {
183     assert(!field()->is_internal(), "regular only");
184     field()->set_signature_index(index);
185   }
generic_signature_index() const186   int generic_signature_index() const {
187     assert(!field()->is_internal(), "regular only");
188     if (access_flags().field_has_generic_signature()) {
189       assert(_generic_signature_slot < _fields->length(), "out of bounds");
190       return _fields->at(_generic_signature_slot);
191     } else {
192       return 0;
193     }
194   }
set_generic_signature_index(int index)195   void set_generic_signature_index(int index) {
196     assert(!field()->is_internal(), "regular only");
197     if (access_flags().field_has_generic_signature()) {
198       assert(_generic_signature_slot < _fields->length(), "out of bounds");
199       _fields->at_put(_generic_signature_slot, index);
200     }
201   }
initval_index() const202   int initval_index() const {
203     assert(!field()->is_internal(), "regular only");
204     return field()->initval_index();
205   }
set_initval_index(int index)206   void set_initval_index(int index) {
207     assert(!field()->is_internal(), "regular only");
208     return field()->set_initval_index(index);
209   }
210 };
211 
212 
213 // Iterate over only the internal fields
214 class InternalFieldStream : public FieldStreamBase {
215  public:
InternalFieldStream(InstanceKlass * k)216   InternalFieldStream(InstanceKlass* k):      FieldStreamBase(k->fields(), k->constants(), k->java_fields_count(), 0) {}
217 };
218 
219 
220 class AllFieldStream : public FieldStreamBase {
221  public:
AllFieldStream(Array<u2> * fields,ConstantPool * constants)222   AllFieldStream(Array<u2>* fields, ConstantPool* constants): FieldStreamBase(fields, constants) {}
AllFieldStream(InstanceKlass * k)223   AllFieldStream(InstanceKlass* k):      FieldStreamBase(k->fields(), k->constants()) {}
224 };
225 
226 #endif // SHARE_OOPS_FIELDSTREAMS_HPP
227