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