1 /* 2 * Copyright (c) 2011, 2017, 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 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_flatten() { 166 return field()->is_flatten(); 167 } 168 169 void set_flattening(bool b) { 170 field()->set_flattening(b); 171 } 172 173 void set_offset(int offset) { 174 field()->set_offset(offset); 175 } 176 177 bool is_offset_set() const { 178 return field()->is_offset_set(); 179 } 180 181 bool is_contended() const { 182 return field()->is_contended(); 183 } 184 185 int contended_group() const { 186 return field()->contended_group(); 187 } 188 189 // bridge to a heavier API: 190 fieldDescriptor& field_descriptor() const { 191 fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf); 192 field.reinitialize(field_holder(), _index); 193 return field; 194 } 195 }; 196 197 // Iterate over only the internal fields 198 class JavaFieldStream : public FieldStreamBase { 199 public: 200 JavaFieldStream(const InstanceKlass* k): FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {} 201 202 int name_index() const { 203 assert(!field()->is_internal(), "regular only"); 204 return field()->name_index(); 205 } 206 void set_name_index(int index) { 207 assert(!field()->is_internal(), "regular only"); 208 field()->set_name_index(index); 209 } 210 int signature_index() const { 211 assert(!field()->is_internal(), "regular only"); 212 return field()->signature_index(); 213 } 214 void set_signature_index(int index) { 215 assert(!field()->is_internal(), "regular only"); 216 field()->set_signature_index(index); 217 } 218 int generic_signature_index() const { 219 assert(!field()->is_internal(), "regular only"); 220 if (access_flags().field_has_generic_signature()) { 221 assert(_generic_signature_slot < _fields->length(), "out of bounds"); 222 return _fields->at(_generic_signature_slot); 223 } else { 224 return 0; 225 } 226 } 227 void set_generic_signature_index(int index) { 228 assert(!field()->is_internal(), "regular only"); 229 if (access_flags().field_has_generic_signature()) { 230 assert(_generic_signature_slot < _fields->length(), "out of bounds"); 231 _fields->at_put(_generic_signature_slot, index); 232 } 233 } 234 int initval_index() const { 235 assert(!field()->is_internal(), "regular only"); 236 return field()->initval_index(); 237 } 238 void set_initval_index(int index) { 239 assert(!field()->is_internal(), "regular only"); 240 return field()->set_initval_index(index); 241 } 242 }; 243 244 245 // Iterate over only the internal fields 246 class InternalFieldStream : public FieldStreamBase { 247 public: 248 InternalFieldStream(InstanceKlass* 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, const constantPoolHandle& constants): FieldStreamBase(fields, constants) {} 255 AllFieldStream(InstanceKlass* k): FieldStreamBase(k->fields(), k->constants()) {} 256 }; 257 258 #endif // SHARE_VM_OOPS_FIELDSTREAMS_HPP