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