1 /*
  2  * Copyright (c) 2011, 2023, 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 // HierarchicalFieldStream allows to also iterate over fields of supertypes.
 41 class FieldStreamBase : public StackObj {
 42 
 43  protected:
 44   const Array<u1>*    _fieldinfo_stream;
 45   FieldInfoReader     _reader;
 46   constantPoolHandle  _constants;
 47   int                 _index;
 48   int                 _limit;
 49 
 50   FieldInfo           _fi_buf;
 51   fieldDescriptor     _fd_buf;
 52 
 53   FieldInfo const * field() const {
 54     assert(!done(), "no more fields");
 55     return &_fi_buf;
 56   }
 57 
 58   inline FieldStreamBase(const Array<u1>* fieldinfo_stream, ConstantPool* constants, int start, int limit);
 59 
 60   inline FieldStreamBase(Array<u1>* fieldinfo_stream, ConstantPool* constants);
 61 
 62   private:
 63    void initialize() {
 64     int java_fields_count = _reader.next_uint();
 65     int injected_fields_count = _reader.next_uint();
 66     assert( _limit <= java_fields_count + injected_fields_count, "Safety check");
 67     if (_limit != 0) {
 68       _reader.read_field_info(_fi_buf);
 69     }
 70    }
 71  public:
 72   inline FieldStreamBase(InstanceKlass* klass);
 73 
 74   // accessors
 75   int index() const                 { return _index; }
 76   InstanceKlass* field_holder() const { return _constants->pool_holder(); }
 77 
 78   void next() {
 79     _index += 1;
 80     if (done()) return;
 81     _reader.read_field_info(_fi_buf);
 82   }
 83   bool done() const { return _index >= _limit; }
 84 
 85   // Accessors for current field
 86   AccessFlags access_flags() const {
 87     return field()->access_flags();
 88   }
 89 
 90   FieldInfo::FieldFlags field_flags() const {
 91     return field()->field_flags();
 92   }
 93 
 94   Symbol* name() const {
 95     return field()->name(_constants());
 96   }
 97 
 98   Symbol* signature() const {
 99     return field()->signature(_constants());
100   }
101 
102   Symbol* generic_signature() const {
103     if (field()->field_flags().is_generic()) {
104       return _constants->symbol_at(field()->generic_signature_index());
105     } else {
106       return nullptr;
107     }
108   }
109 
110   int offset() const {
111     return field()->offset();
112   }
113 
114   bool is_null_free_inline_type() {
115     return field()->field_flags().is_null_free_inline_type();
116   }
117 
118   bool is_flat() const {
119     return field()->field_flags().is_flat();
120   }
121 
122   bool is_contended() const {
123     return field()->is_contended();
124   }
125 
126   int contended_group() const {
127     return field()->contended_group();
128   }
129 
130   int null_marker_offset() const {
131     return field()->null_marker_offset();
132   }
133 
134   // Convenient methods
135 
136   FieldInfo to_FieldInfo() {
137     return _fi_buf;
138   }
139 
140   int num_total_fields() const {
141     return FieldInfoStream::num_total_fields(_fieldinfo_stream);
142   }
143 
144   // bridge to a heavier API:
145   fieldDescriptor& field_descriptor() const {
146     fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
147     field.reinitialize(field_holder(), _index);
148     return field;
149   }
150 };
151 
152 // Iterate over only the Java fields
153 class JavaFieldStream : public FieldStreamBase {
154  public:
155   JavaFieldStream(const InstanceKlass* k): FieldStreamBase(k->fieldinfo_stream(), k->constants(), 0, k->java_fields_count()) {}
156 
157   u2 name_index() const {
158     assert(!field()->field_flags().is_injected(), "regular only");
159     return field()->name_index();
160   }
161 
162   u2 signature_index() const {
163     assert(!field()->field_flags().is_injected(), "regular only");
164     return field()->signature_index();
165     return -1;
166   }
167 
168   u2 generic_signature_index() const {
169     assert(!field()->field_flags().is_injected(), "regular only");
170     if (field()->field_flags().is_generic()) {
171       return field()->generic_signature_index();
172     }
173     return 0;
174   }
175 
176   u2 initval_index() const {
177     assert(!field()->field_flags().is_injected(), "regular only");
178     return field()->initializer_index();
179   }
180 };
181 
182 
183 // Iterate over only the internal fields
184 class InternalFieldStream : public FieldStreamBase {
185  public:
186   InternalFieldStream(InstanceKlass* k):      FieldStreamBase(k->fieldinfo_stream(), k->constants(), k->java_fields_count(), 0) {}
187 };
188 
189 
190 class AllFieldStream : public FieldStreamBase {
191  public:
192   AllFieldStream(Array<u1>* fieldinfo, ConstantPool* constants): FieldStreamBase(fieldinfo, constants) {}
193   AllFieldStream(const InstanceKlass* k):      FieldStreamBase(k->fieldinfo_stream(), k->constants()) {}
194 };
195 
196 // Iterate over fields including the ones declared in supertypes
197 template<typename FieldStreamType>
198 class HierarchicalFieldStream : public StackObj  {
199  private:
200   const Array<InstanceKlass*>* _interfaces;
201   InstanceKlass* _next_klass; // null indicates no more type to visit
202   FieldStreamType _current_stream;
203   int _interface_index;
204 
205   void prepare() {
206     _next_klass = next_klass_with_fields();
207     // special case: the initial klass has no fields. If any supertype has any fields, use that directly.
208     // if no such supertype exists, done() will return false already.
209     next_stream_if_done();
210   }
211 
212   InstanceKlass* next_klass_with_fields() {
213     assert(_next_klass != nullptr, "reached end of types already");
214     InstanceKlass* result = _next_klass;
215     do  {
216       if (!result->is_interface() && result->super() != nullptr) {
217         result = result->java_super();
218       } else if (_interface_index > 0) {
219         result = _interfaces->at(--_interface_index);
220       } else {
221         return nullptr; // we did not find any more supertypes with fields
222       }
223     } while (FieldStreamType(result).done());
224     return result;
225   }
226 
227   // sets _current_stream to the next if the current is done and any more is available
228   void next_stream_if_done() {
229     if (_next_klass != nullptr && _current_stream.done()) {
230       _current_stream = FieldStreamType(_next_klass);
231       assert(!_current_stream.done(), "created empty stream");
232       _next_klass = next_klass_with_fields();
233     }
234   }
235 
236  public:
237   HierarchicalFieldStream(InstanceKlass* klass) :
238     _interfaces(klass->transitive_interfaces()),
239     _next_klass(klass),
240     _current_stream(FieldStreamType(klass)),
241     _interface_index(_interfaces->length()) {
242       prepare();
243   }
244 
245   void next() {
246     _current_stream.next();
247     next_stream_if_done();
248   }
249 
250   bool done() const { return _next_klass == nullptr && _current_stream.done(); }
251 
252   // bridge functions from FieldStreamBase
253   int index() const {
254     return _current_stream.index();
255   }
256 
257   AccessFlags access_flags() const {
258     return _current_stream.access_flags();
259   }
260 
261   FieldInfo::FieldFlags field_flags() const {
262     return _current_stream.field_flags();
263   }
264 
265   Symbol* name() const {
266     return _current_stream.name();
267   }
268 
269   Symbol* signature() const {
270     return _current_stream.signature();
271   }
272 
273   Symbol* generic_signature() const {
274     return _current_stream.generic_signature();
275   }
276 
277   int offset() const {
278     return _current_stream.offset();
279   }
280 
281   bool is_contended() const {
282     return _current_stream.is_contended();
283   }
284 
285   int contended_group() const {
286     return _current_stream.contended_group();
287   }
288 
289   FieldInfo to_FieldInfo() {
290     return _current_stream.to_FieldInfo();
291   }
292 
293   fieldDescriptor& field_descriptor() const {
294     return _current_stream.field_descriptor();
295   }
296 
297   bool is_flat() const {
298     return _current_stream.is_flat();
299   }
300 };
301 
302 #endif // SHARE_OOPS_FIELDSTREAMS_HPP