1 /*
2  * Copyright (c) 2011, 2020, 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); }
50 
init_generic_signature_start_slot()51   int init_generic_signature_start_slot() {
52     int length = _fields->length();
53     int num_fields = _index;
54     int skipped_generic_signature_slots = 0;
55     FieldInfo* fi;
56     AccessFlags flags;
57     /* Scan from 0 to the current _index. Count the number of generic
58        signature slots for field[0] to field[_index - 1]. */
59     for (int i = 0; i < _index; i++) {
60       fi = FieldInfo::from_field_array(_fields, i);
61       flags.set_flags(fi->access_flags());
62       if (flags.field_has_generic_signature()) {
63         length --;
64         skipped_generic_signature_slots ++;
65       }
66     }
67     /* Scan from the current _index. */
68     for (int i = _index; i*FieldInfo::field_slots < length; i++) {
69       fi = FieldInfo::from_field_array(_fields, i);
70       flags.set_flags(fi->access_flags());
71       if (flags.field_has_generic_signature()) {
72         length --;
73       }
74       num_fields ++;
75     }
76     _generic_signature_slot = length + skipped_generic_signature_slots;
77     assert(_generic_signature_slot <= _fields->length(), "");
78     return num_fields;
79   }
80 
81   inline FieldStreamBase(Array<u2>* fields, ConstantPool* constants, int start, int limit);
82 
83   inline FieldStreamBase(Array<u2>* fields, ConstantPool* constants);
84  public:
85   inline FieldStreamBase(InstanceKlass* klass);
86 
87   // accessors
index() const88   int index() const                 { return _index; }
field_holder() const89   InstanceKlass* field_holder() const { return _constants->pool_holder(); }
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 
set_offset(int offset)137   void set_offset(int offset) {
138     field()->set_offset(offset);
139   }
140 
is_offset_set() const141   bool is_offset_set() const {
142     return field()->is_offset_set();
143   }
144 
is_contended() const145   bool is_contended() const {
146     return field()->is_contended();
147   }
148 
contended_group() const149   int contended_group() const {
150     return field()->contended_group();
151   }
152 
153   // bridge to a heavier API:
field_descriptor() const154   fieldDescriptor& field_descriptor() const {
155     fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
156     field.reinitialize(field_holder(), _index);
157     return field;
158   }
159 };
160 
161 // Iterate over only the internal fields
162 class JavaFieldStream : public FieldStreamBase {
163  public:
JavaFieldStream(const InstanceKlass * k)164   JavaFieldStream(const InstanceKlass* k): FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {}
165 
name_index() const166   int name_index() const {
167     assert(!field()->is_internal(), "regular only");
168     return field()->name_index();
169   }
set_name_index(int index)170   void set_name_index(int index) {
171     assert(!field()->is_internal(), "regular only");
172     field()->set_name_index(index);
173   }
signature_index() const174   int signature_index() const {
175     assert(!field()->is_internal(), "regular only");
176     return field()->signature_index();
177   }
set_signature_index(int index)178   void set_signature_index(int index) {
179     assert(!field()->is_internal(), "regular only");
180     field()->set_signature_index(index);
181   }
generic_signature_index() const182   int generic_signature_index() const {
183     assert(!field()->is_internal(), "regular only");
184     if (access_flags().field_has_generic_signature()) {
185       assert(_generic_signature_slot < _fields->length(), "out of bounds");
186       return _fields->at(_generic_signature_slot);
187     } else {
188       return 0;
189     }
190   }
set_generic_signature_index(int index)191   void set_generic_signature_index(int index) {
192     assert(!field()->is_internal(), "regular only");
193     if (access_flags().field_has_generic_signature()) {
194       assert(_generic_signature_slot < _fields->length(), "out of bounds");
195       _fields->at_put(_generic_signature_slot, index);
196     }
197   }
initval_index() const198   int initval_index() const {
199     assert(!field()->is_internal(), "regular only");
200     return field()->initval_index();
201   }
set_initval_index(int index)202   void set_initval_index(int index) {
203     assert(!field()->is_internal(), "regular only");
204     return field()->set_initval_index(index);
205   }
206 };
207 
208 
209 // Iterate over only the internal fields
210 class InternalFieldStream : public FieldStreamBase {
211  public:
InternalFieldStream(InstanceKlass * k)212   InternalFieldStream(InstanceKlass* k):      FieldStreamBase(k->fields(), k->constants(), k->java_fields_count(), 0) {}
213 };
214 
215 
216 class AllFieldStream : public FieldStreamBase {
217  public:
AllFieldStream(Array<u2> * fields,ConstantPool * constants)218   AllFieldStream(Array<u2>* fields, ConstantPool* constants): FieldStreamBase(fields, constants) {}
AllFieldStream(InstanceKlass * k)219   AllFieldStream(InstanceKlass* k):      FieldStreamBase(k->fields(), k->constants()) {}
220 };
221 
222 #endif // SHARE_OOPS_FIELDSTREAMS_HPP
223