1 /*
  2  * Copyright (c) 2011, 2026, 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/fieldInfo.hpp"
 29 #include "oops/instanceKlass.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  protected:
 43   const Array<u1>*    _fieldinfo_stream;
 44   FieldInfoReader     _reader;
 45   constantPoolHandle  _constants;
 46   int                 _index;
 47   int                 _limit;
 48 
 49   FieldInfo           _fi_buf;
 50   fieldDescriptor     _fd_buf;
 51 
 52   FieldInfo const * field() const {
 53     assert(!done(), "no more fields");
 54     return &_fi_buf;
 55   }
 56 
 57   inline FieldStreamBase(const Array<u1>* fieldinfo_stream, ConstantPool* constants, int start, int limit);
 58 
 59   inline FieldStreamBase(const Array<u1>* fieldinfo_stream, ConstantPool* constants);
 60 
 61  private:
 62    void initialize() {
 63     int java_fields_count;
 64     int injected_fields_count;
 65     _reader.read_field_counts(&java_fields_count, &injected_fields_count);
 66     if (_limit < _index) {
 67       _limit = java_fields_count + injected_fields_count;
 68     }
 69     assert(_limit <= java_fields_count + injected_fields_count, "Safety check");
 70     if (_index < _limit) {
 71       if (_index != 0) {
 72         _reader.skip_to_field_info(_index);
 73       }
 74       _reader.read_field_info(_fi_buf);
 75     }
 76    }
 77 
 78  public:
 79   inline FieldStreamBase(InstanceKlass* klass);
 80 
 81   // accessors
 82   int index() const                 { return _index; }
 83   InstanceKlass* field_holder() const { return _constants->pool_holder(); }
 84 
 85   void next() {
 86     _index += 1;
 87     if (done()) return;
 88     _reader.read_field_info(_fi_buf);
 89   }
 90   bool done() const { return _index >= _limit; }
 91 
 92   // Accessors for current field
 93   AccessFlags access_flags() const {
 94     return field()->access_flags();
 95   }
 96 
 97   FieldInfo::FieldFlags field_flags() const {
 98     return field()->field_flags();
 99   }
100 
101   Symbol* name() const {
102     return field()->name(_constants());
103   }
104 
105   Symbol* signature() const {
106     return field()->signature(_constants());
107   }
108 
109   Symbol* generic_signature() const {
110     if (field()->field_flags().is_generic()) {
111       return _constants->symbol_at(field()->generic_signature_index());
112     } else {
113       return nullptr;
114     }
115   }
116 
117   int offset() const {
118     return field()->offset();
119   }
120 
121   bool is_contended() const {
122     return field()->is_contended();
123   }
124 
125   int contended_group() const {
126     return field()->contended_group();
127   }
128 
129   // Convenient methods
130 
131   const FieldInfo& to_FieldInfo() const {
132     return _fi_buf;
133   }
134 
135   int num_total_fields() const {
136     return FieldInfoStream::num_total_fields(_fieldinfo_stream);
137   }
138 
139   // bridge to a heavier API:
140   fieldDescriptor& field_descriptor() const {
141     fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
142     field.reinitialize(field_holder(), to_FieldInfo());
143     return field;
144   }
145 };
146 
147 // Iterate over only the Java fields
148 class JavaFieldStream : public FieldStreamBase {
149   Array<u1>* _search_table;
150 
151  public:
152   JavaFieldStream(const InstanceKlass* k): FieldStreamBase(k->fieldinfo_stream(), k->constants(), 0, k->java_fields_count()),
153     _search_table(k->fieldinfo_search_table()) {}
154 
155   u2 name_index() const {
156     assert(!field()->field_flags().is_injected(), "regular only");
157     return field()->name_index();
158   }
159 
160   u2 signature_index() const {
161     assert(!field()->field_flags().is_injected(), "regular only");
162     return field()->signature_index();
163   }
164 
165   u2 generic_signature_index() const {
166     assert(!field()->field_flags().is_injected(), "regular only");
167     if (field()->field_flags().is_generic()) {
168       return field()->generic_signature_index();
169     }
170     return 0;
171   }
172 
173   u2 initval_index() const {
174     assert(!field()->field_flags().is_injected(), "regular only");
175     return field()->initializer_index();
176   }
177 
178   // Performs either a linear search or binary search through the stream
179   // looking for a matching name/signature combo
180   bool lookup(const Symbol* name, const Symbol* signature);
181 };
182 
183 
184 // Iterate over only the internal fields
185 class InternalFieldStream : public FieldStreamBase {
186  public:
187   InternalFieldStream(InstanceKlass* k):      FieldStreamBase(k->fieldinfo_stream(), k->constants(), k->java_fields_count(), -1) {}
188 };
189 
190 
191 class AllFieldStream : public FieldStreamBase {
192  public:
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->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 
254   AccessFlags access_flags() const {
255     return _current_stream.access_flags();
256   }
257 
258   FieldInfo::FieldFlags field_flags() const {
259     return _current_stream.field_flags();
260   }
261 
262   Symbol* name() const {
263     return _current_stream.name();
264   }
265 
266   Symbol* signature() const {
267     return _current_stream.signature();
268   }
269 
270   Symbol* generic_signature() const {
271     return _current_stream.generic_signature();
272   }
273 
274   int offset() const {
275     return _current_stream.offset();
276   }
277 
278   bool is_contended() const {
279     return _current_stream.is_contended();
280   }
281 
282   int contended_group() const {
283     return _current_stream.contended_group();
284   }
285 
286   FieldInfo to_FieldInfo() {
287     return _current_stream.to_FieldInfo();
288   }
289 
290   fieldDescriptor& field_descriptor() const {
291     return _current_stream.field_descriptor();
292   }
293 
294 };
295 
296 #endif // SHARE_OOPS_FIELDSTREAMS_HPP