1 /*
   2  * Copyright (c) 2011, 2013, 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_VM_OOPS_FIELDSTREAMS_HPP
  26 #define SHARE_VM_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 
  49   FieldInfo* field() const { return FieldInfo::from_field_array(_fields, _index); }
  50   InstanceKlass* field_holder() const { return _constants->pool_holder(); }
  51 
  52   int init_generic_signature_start_slot() {
  53     int length = _fields->length();
  54     int num_fields = 0;
  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   FieldStreamBase(Array<u2>* fields, constantPoolHandle constants, int start, int limit) {
  83     _fields = fields;
  84     _constants = constants;
  85     _index = start;
  86     int num_fields = init_generic_signature_start_slot();
  87     if (limit < start) {
  88       _limit = num_fields;
  89     } else {
  90       _limit = limit;
  91     }
  92   }
  93 
  94   FieldStreamBase(Array<u2>* fields, constantPoolHandle constants) {
  95     _fields = fields;
  96     _constants = constants;
  97     _index = 0;
  98     _limit = init_generic_signature_start_slot();
  99   }
 100 
 101  public:
 102   FieldStreamBase(InstanceKlass* klass) {
 103     _fields = klass->fields();
 104     _constants = klass->constants();
 105     _index = 0;
 106     _limit = klass->java_fields_count();
 107     init_generic_signature_start_slot();
 108     assert(klass == field_holder(), "");
 109   }
 110   FieldStreamBase(instanceKlassHandle klass) {
 111     _fields = klass->fields();
 112     _constants = klass->constants();
 113     _index = 0;
 114     _limit = klass->java_fields_count();
 115     init_generic_signature_start_slot();
 116     assert(klass == field_holder(), "");
 117   }
 118 
 119   // accessors
 120   int index() const                 { return _index; }
 121 
 122   void next() {
 123     if (access_flags().field_has_generic_signature()) {
 124       _generic_signature_slot ++;
 125       assert(_generic_signature_slot <= _fields->length(), "");
 126     }
 127     _index += 1;
 128   }
 129   bool done() const { return _index >= _limit; }
 130 
 131   // Accessors for current field
 132   AccessFlags access_flags() const {
 133     AccessFlags flags;
 134     flags.set_flags(field()->access_flags());
 135     return flags;
 136   }
 137 
 138   void set_access_flags(u2 flags) const {
 139     field()->set_access_flags(flags);
 140   }
 141 
 142   void set_access_flags(AccessFlags flags) const {
 143     set_access_flags(flags.as_short());
 144   }
 145 
 146   Symbol* name() const {
 147     return field()->name(_constants);
 148   }
 149 
 150   Symbol* signature() const {
 151     return field()->signature(_constants);
 152   }
 153 
 154   Symbol* generic_signature() const {
 155     if (access_flags().field_has_generic_signature()) {
 156       assert(_generic_signature_slot < _fields->length(), "out of bounds");
 157       int index = _fields->at(_generic_signature_slot);
 158       return _constants->symbol_at(index);
 159     } else {
 160       return NULL;
 161     }
 162   }
 163 
 164   int offset() const {
 165     return field()->offset();
 166   }
 167 
 168   int allocation_type() const {
 169     return field()->allocation_type();
 170   }
 171 
 172   void set_offset(int offset) {
 173     field()->set_offset(offset);
 174   }
 175 
 176   bool is_offset_set() const {
 177     return field()->is_offset_set();
 178   }
 179 
 180   bool is_contended() const {
 181     return field()->is_contended();
 182   }
 183 
 184   int contended_group() const {
 185     return field()->contended_group();
 186   }
 187 
 188   // bridge to a heavier API:
 189   fieldDescriptor& field_descriptor() const {
 190     fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
 191     field.reinitialize(field_holder(), _index);
 192     return field;
 193   }
 194 };
 195 
 196 // Iterate over only the internal fields
 197 class JavaFieldStream : public FieldStreamBase {
 198  public:
 199   JavaFieldStream(instanceKlassHandle k): FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {}
 200 
 201   int name_index() const {
 202     assert(!field()->is_internal(), "regular only");
 203     return field()->name_index();
 204   }
 205   void set_name_index(int index) {
 206     assert(!field()->is_internal(), "regular only");
 207     field()->set_name_index(index);
 208   }
 209   int signature_index() const {
 210     assert(!field()->is_internal(), "regular only");
 211     return field()->signature_index();
 212   }
 213   void set_signature_index(int index) {
 214     assert(!field()->is_internal(), "regular only");
 215     field()->set_signature_index(index);
 216   }
 217   int generic_signature_index() const {
 218     assert(!field()->is_internal(), "regular only");
 219     if (access_flags().field_has_generic_signature()) {
 220       assert(_generic_signature_slot < _fields->length(), "out of bounds");
 221       return _fields->at(_generic_signature_slot);
 222     } else {
 223       return 0;
 224     }
 225   }
 226   void set_generic_signature_index(int index) {
 227     assert(!field()->is_internal(), "regular only");
 228     if (access_flags().field_has_generic_signature()) {
 229       assert(_generic_signature_slot < _fields->length(), "out of bounds");
 230       _fields->at_put(_generic_signature_slot, index);
 231     }
 232   }
 233   int initval_index() const {
 234     assert(!field()->is_internal(), "regular only");
 235     return field()->initval_index();
 236   }
 237   void set_initval_index(int index) {
 238     assert(!field()->is_internal(), "regular only");
 239     return field()->set_initval_index(index);
 240   }
 241 };
 242 
 243 
 244 // Iterate over only the internal fields
 245 class InternalFieldStream : public FieldStreamBase {
 246  public:
 247   InternalFieldStream(InstanceKlass* k):      FieldStreamBase(k->fields(), k->constants(), k->java_fields_count(), 0) {}
 248   InternalFieldStream(instanceKlassHandle k): FieldStreamBase(k->fields(), k->constants(), k->java_fields_count(), 0) {}
 249 };
 250 
 251 
 252 class AllFieldStream : public FieldStreamBase {
 253  public:
 254   AllFieldStream(Array<u2>* fields, constantPoolHandle constants): FieldStreamBase(fields, constants) {}
 255   AllFieldStream(InstanceKlass* k):      FieldStreamBase(k->fields(), k->constants()) {}
 256   AllFieldStream(instanceKlassHandle k): FieldStreamBase(k->fields(), k->constants()) {}
 257 };
 258 
 259 #endif // SHARE_VM_OOPS_FIELDSTREAMS_HPP