1 /*
   2  * Copyright (c) 2011, 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 
  31 // The is the base class for iteration over the fields array
  32 // describing the declared fields in the class.  Several subclasses
  33 // are provided depending on the kind of iteration required.  The
  34 // JavaFieldStream is for iterating over regular Java fields and it
  35 // generally the preferred iterator.  InternalFieldStream only
  36 // iterates over fields that have been injected by the JVM.
  37 // AllFieldStream exposes all fields and should only be used in rare
  38 // cases.
  39 class FieldStreamBase : public StackObj {
  40  protected:
  41   typeArrayHandle     _fields;
  42   constantPoolHandle  _constants;
  43   int                 _index;
  44   int                 _limit;
  45 
  46   FieldInfo* field() const { return FieldInfo::from_field_array(_fields(), _index); }
  47 
  48   FieldStreamBase(typeArrayHandle fields, constantPoolHandle constants, int start, int limit) {
  49     _fields = fields;
  50     _constants = constants;
  51     _index = start;
  52     _limit = limit;
  53   }
  54 
  55   FieldStreamBase(typeArrayHandle fields, constantPoolHandle constants) {
  56     _fields = fields;
  57     _constants = constants;
  58     _index = 0;
  59     _limit = fields->length() / FieldInfo::field_slots;
  60   }
  61 
  62  public:
  63   FieldStreamBase(instanceKlass* klass) {
  64     _fields = klass->fields();
  65     _constants = klass->constants();
  66     _index = 0;
  67     _limit = klass->java_fields_count();
  68   }
  69   FieldStreamBase(instanceKlassHandle klass) {
  70     _fields = klass->fields();
  71     _constants = klass->constants();
  72     _index = 0;
  73     _limit = klass->java_fields_count();
  74   }
  75 
  76   // accessors
  77   int index() const                 { return _index; }
  78 
  79   void next() { _index += 1; }
  80   bool done() const { return _index >= _limit; }
  81 
  82   // Accessors for current field
  83   AccessFlags access_flags() const {
  84     AccessFlags flags;
  85     flags.set_flags(field()->access_flags());
  86     return flags;
  87   }
  88 
  89   void set_access_flags(u2 flags) const {
  90     field()->set_access_flags(flags);
  91   }
  92 
  93   void set_access_flags(AccessFlags flags) const {
  94     set_access_flags(flags.as_short());
  95   }
  96 
  97   Symbol* name() const {
  98     return field()->name(_constants);
  99   }
 100 
 101   Symbol* signature() const {
 102     return field()->signature(_constants);
 103   }
 104 
 105   Symbol* generic_signature() const {
 106     return field()->generic_signature(_constants);
 107   }
 108 
 109   int offset() const {
 110     return field()->offset();
 111   }
 112 
 113   void set_offset(int offset) {
 114     field()->set_offset(offset);
 115   }
 116 };
 117 
 118 // Iterate over only the internal fields
 119 class JavaFieldStream : public FieldStreamBase {
 120  public:
 121   JavaFieldStream(instanceKlass* k):      FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {}
 122   JavaFieldStream(instanceKlassHandle k): FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {}
 123 
 124   int name_index() const {
 125     assert(!field()->is_internal(), "regular only");
 126     return field()->name_index();
 127   }
 128   void set_name_index(int index) {
 129     assert(!field()->is_internal(), "regular only");
 130     field()->set_name_index(index);
 131   }
 132   int signature_index() const {
 133     assert(!field()->is_internal(), "regular only");
 134     return field()->signature_index();
 135   }
 136   void set_signature_index(int index) {
 137     assert(!field()->is_internal(), "regular only");
 138     field()->set_signature_index(index);
 139   }
 140   int generic_signature_index() const {
 141     assert(!field()->is_internal(), "regular only");
 142     return field()->generic_signature_index();
 143   }
 144   void set_generic_signature_index(int index) {
 145     assert(!field()->is_internal(), "regular only");
 146     field()->set_generic_signature_index(index);
 147   }
 148   int initval_index() const {
 149     assert(!field()->is_internal(), "regular only");
 150     return field()->initval_index();
 151   }
 152   void set_initval_index(int index) {
 153     assert(!field()->is_internal(), "regular only");
 154     return field()->set_initval_index(index);
 155   }
 156 };
 157 
 158 
 159 // Iterate over only the internal fields
 160 class InternalFieldStream : public FieldStreamBase {
 161  public:
 162   InternalFieldStream(instanceKlass* k):      FieldStreamBase(k->fields(), k->constants(), k->java_fields_count(), k->all_fields_count()) {}
 163   InternalFieldStream(instanceKlassHandle k): FieldStreamBase(k->fields(), k->constants(), k->java_fields_count(), k->all_fields_count()) {}
 164 };
 165 
 166 
 167 class AllFieldStream : public FieldStreamBase {
 168  public:
 169   AllFieldStream(typeArrayHandle fields, constantPoolHandle constants): FieldStreamBase(fields, constants) {}
 170   AllFieldStream(instanceKlass* k):      FieldStreamBase(k->fields(), k->constants()) {}
 171   AllFieldStream(instanceKlassHandle k): FieldStreamBase(k->fields(), k->constants()) {}
 172 };
 173 
 174 #endif // SHARE_VM_OOPS_FIELDSTREAMS_HPP