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 
 42  protected:
 43   Array<u2>*          _fields;
 44   constantPoolHandle  _constants;
 45   int                 _index;
 46   int                 _limit;
 47   int                 _generic_signature_slot;
 48   fieldDescriptor     _fd_buf;
 49 
 50   FieldInfo* field() const { return FieldInfo::from_field_array(_fields, _index); }
 51 
 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
 89   int index() const                 { return _index; }
 90   InstanceKlass* field_holder() const { return _constants->pool_holder(); }
 91 
 92   void next() {
 93     if (access_flags().field_has_generic_signature()) {
 94       _generic_signature_slot ++;
 95       assert(_generic_signature_slot <= _fields->length(), "");
 96     }
 97     _index += 1;
 98   }
 99   bool done() const { return _index >= _limit; }
100 
101   // Accessors for current field
102   AccessFlags access_flags() const {
103     AccessFlags flags;
104     flags.set_flags(field()->access_flags());
105     return flags;
106   }
107 
108   void set_access_flags(u2 flags) const {
109     field()->set_access_flags(flags);
110   }
111 
112   void set_access_flags(AccessFlags flags) const {
113     set_access_flags(flags.as_short());
114   }
115 
116   Symbol* name() const {
117     return field()->name(_constants());
118   }
119 
120   Symbol* signature() const {
121     return field()->signature(_constants());
122   }
123 
124   Symbol* generic_signature() const {
125     if (access_flags().field_has_generic_signature()) {
126       assert(_generic_signature_slot < _fields->length(), "out of bounds");
127       int index = _fields->at(_generic_signature_slot);
128       return _constants->symbol_at(index);
129     } else {
130       return NULL;
131     }
132   }
133 
134   int offset() const {
135     return field()->offset();
136   }
137 
138   bool is_inlined() {
139     return field()->is_inlined();
140   }
141 
142   void set_inlined(bool b) {
143     field()->set_inlined(b);
144   }
145 
146   void set_offset(int offset) {
147     field()->set_offset(offset);
148   }
149 
150   bool is_offset_set() const {
151     return field()->is_offset_set();
152   }
153 
154   bool is_contended() const {
155     return field()->is_contended();
156   }
157 
158   int contended_group() const {
159     return field()->contended_group();
160   }
161 
162   // bridge to a heavier API:
163   fieldDescriptor& field_descriptor() const {
164     fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
165     field.reinitialize(field_holder(), _index);
166     return field;
167   }
168 };
169 
170 // Iterate over only the internal fields
171 class JavaFieldStream : public FieldStreamBase {
172  public:
173   JavaFieldStream(const InstanceKlass* k): FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {}
174 
175   int name_index() const {
176     assert(!field()->is_internal(), "regular only");
177     return field()->name_index();
178   }
179   void set_name_index(int index) {
180     assert(!field()->is_internal(), "regular only");
181     field()->set_name_index(index);
182   }
183   int signature_index() const {
184     assert(!field()->is_internal(), "regular only");
185     return field()->signature_index();
186   }
187   void set_signature_index(int index) {
188     assert(!field()->is_internal(), "regular only");
189     field()->set_signature_index(index);
190   }
191   int generic_signature_index() const {
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       return _fields->at(_generic_signature_slot);
196     } else {
197       return 0;
198     }
199   }
200   void set_generic_signature_index(int index) {
201     assert(!field()->is_internal(), "regular only");
202     if (access_flags().field_has_generic_signature()) {
203       assert(_generic_signature_slot < _fields->length(), "out of bounds");
204       _fields->at_put(_generic_signature_slot, index);
205     }
206   }
207   int initval_index() const {
208     assert(!field()->is_internal(), "regular only");
209     return field()->initval_index();
210   }
211   void set_initval_index(int index) {
212     assert(!field()->is_internal(), "regular only");
213     return field()->set_initval_index(index);
214   }
215 };
216 
217 
218 // Iterate over only the internal fields
219 class InternalFieldStream : public FieldStreamBase {
220  public:
221   InternalFieldStream(InstanceKlass* k):      FieldStreamBase(k->fields(), k->constants(), k->java_fields_count(), 0) {}
222 };
223 
224 
225 class AllFieldStream : public FieldStreamBase {
226  public:
227   AllFieldStream(Array<u2>* fields, ConstantPool* constants): FieldStreamBase(fields, constants) {}
228   AllFieldStream(InstanceKlass* k):      FieldStreamBase(k->fields(), k->constants()) {}
229 };
230 
231 #endif // SHARE_OOPS_FIELDSTREAMS_HPP