1 /*
2 * Copyright (c) 2011, 2012, 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 *
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 Array<u2>* _fields;
42 constantPoolHandle _constants;
43 int _index;
44 int _limit;
45 int _generic_signature_slot;
46
47 FieldInfo* field() const { return FieldInfo::from_field_array(_fields, _index); }
48
49 int init_generic_signature_start_slot() {
50 int length = _fields->length();
51 int num_fields = 0;
52 int skipped_generic_signature_slots = 0;
53 FieldInfo* fi;
54 AccessFlags flags;
55 /* Scan from 0 to the current _index. Count the number of generic
56 signature slots for field[0] to field[_index - 1]. */
57 for (int i = 0; i < _index; i++) {
58 fi = FieldInfo::from_field_array(_fields, i);
59 flags.set_flags(fi->access_flags());
60 if (flags.field_has_generic_signature()) {
61 length --;
62 skipped_generic_signature_slots ++;
63 }
64 }
65 /* Scan from the current _index. */
66 for (int i = _index; i*FieldInfo::field_slots < length; i++) {
67 fi = FieldInfo::from_field_array(_fields, i);
85 _limit = num_fields;
86 } else {
87 _limit = limit;
88 }
89 }
90
91 FieldStreamBase(Array<u2>* fields, constantPoolHandle constants) {
92 _fields = fields;
93 _constants = constants;
94 _index = 0;
95 _limit = init_generic_signature_start_slot();
96 }
97
98 public:
99 FieldStreamBase(InstanceKlass* klass) {
100 _fields = klass->fields();
101 _constants = klass->constants();
102 _index = 0;
103 _limit = klass->java_fields_count();
104 init_generic_signature_start_slot();
105 }
106 FieldStreamBase(instanceKlassHandle klass) {
107 _fields = klass->fields();
108 _constants = klass->constants();
109 _index = 0;
110 _limit = klass->java_fields_count();
111 init_generic_signature_start_slot();
112 }
113
114 // accessors
115 int index() const { return _index; }
116
117 void next() {
118 if (access_flags().field_has_generic_signature()) {
119 _generic_signature_slot ++;
120 assert(_generic_signature_slot <= _fields->length(), "");
121 }
122 _index += 1;
123 }
124 bool done() const { return _index >= _limit; }
125
126 // Accessors for current field
127 AccessFlags access_flags() const {
128 AccessFlags flags;
129 flags.set_flags(field()->access_flags());
130 return flags;
131 }
163 int allocation_type() const {
164 return field()->allocation_type();
165 }
166
167 void set_offset(int offset) {
168 field()->set_offset(offset);
169 }
170
171 bool is_offset_set() const {
172 return field()->is_offset_set();
173 }
174
175 bool is_contended() const {
176 return field()->is_contended();
177 }
178
179 int contended_group() const {
180 return field()->contended_group();
181 }
182
183 };
184
185 // Iterate over only the internal fields
186 class JavaFieldStream : public FieldStreamBase {
187 public:
188 JavaFieldStream(instanceKlassHandle k): FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {}
189
190 int name_index() const {
191 assert(!field()->is_internal(), "regular only");
192 return field()->name_index();
193 }
194 void set_name_index(int index) {
195 assert(!field()->is_internal(), "regular only");
196 field()->set_name_index(index);
197 }
198 int signature_index() const {
199 assert(!field()->is_internal(), "regular only");
200 return field()->signature_index();
201 }
202 void set_signature_index(int index) {
|
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 *
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 Array<u2>* _fields;
42 constantPoolHandle _constants;
43 int _index;
44 int _limit;
45 int _generic_signature_slot;
46 fieldDescriptor _fd_buf;
47
48 FieldInfo* field() const { return FieldInfo::from_field_array(_fields, _index); }
49 InstanceKlass* field_holder() const { return _constants->pool_holder(); }
50
51 int init_generic_signature_start_slot() {
52 int length = _fields->length();
53 int num_fields = 0;
54 int skipped_generic_signature_slots = 0;
55 FieldInfo* fi;
56 AccessFlags flags;
57 /* Scan from 0 to the current _index. Count the number of generic
58 signature slots for field[0] to field[_index - 1]. */
59 for (int i = 0; i < _index; i++) {
60 fi = FieldInfo::from_field_array(_fields, i);
61 flags.set_flags(fi->access_flags());
62 if (flags.field_has_generic_signature()) {
63 length --;
64 skipped_generic_signature_slots ++;
65 }
66 }
67 /* Scan from the current _index. */
68 for (int i = _index; i*FieldInfo::field_slots < length; i++) {
69 fi = FieldInfo::from_field_array(_fields, i);
87 _limit = num_fields;
88 } else {
89 _limit = limit;
90 }
91 }
92
93 FieldStreamBase(Array<u2>* fields, constantPoolHandle constants) {
94 _fields = fields;
95 _constants = constants;
96 _index = 0;
97 _limit = init_generic_signature_start_slot();
98 }
99
100 public:
101 FieldStreamBase(InstanceKlass* klass) {
102 _fields = klass->fields();
103 _constants = klass->constants();
104 _index = 0;
105 _limit = klass->java_fields_count();
106 init_generic_signature_start_slot();
107 assert(klass == field_holder(), "");
108 }
109 FieldStreamBase(instanceKlassHandle klass) {
110 _fields = klass->fields();
111 _constants = klass->constants();
112 _index = 0;
113 _limit = klass->java_fields_count();
114 init_generic_signature_start_slot();
115 assert(klass == field_holder(), "");
116 }
117
118 // accessors
119 int index() const { return _index; }
120
121 void next() {
122 if (access_flags().field_has_generic_signature()) {
123 _generic_signature_slot ++;
124 assert(_generic_signature_slot <= _fields->length(), "");
125 }
126 _index += 1;
127 }
128 bool done() const { return _index >= _limit; }
129
130 // Accessors for current field
131 AccessFlags access_flags() const {
132 AccessFlags flags;
133 flags.set_flags(field()->access_flags());
134 return flags;
135 }
167 int allocation_type() const {
168 return field()->allocation_type();
169 }
170
171 void set_offset(int offset) {
172 field()->set_offset(offset);
173 }
174
175 bool is_offset_set() const {
176 return field()->is_offset_set();
177 }
178
179 bool is_contended() const {
180 return field()->is_contended();
181 }
182
183 int contended_group() const {
184 return field()->contended_group();
185 }
186
187 // bridge to a heavier API:
188 fieldDescriptor& field_descriptor() const {
189 fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
190 field.reinitialize(field_holder(), _index);
191 return field;
192 }
193 };
194
195 // Iterate over only the internal fields
196 class JavaFieldStream : public FieldStreamBase {
197 public:
198 JavaFieldStream(instanceKlassHandle k): FieldStreamBase(k->fields(), k->constants(), 0, k->java_fields_count()) {}
199
200 int name_index() const {
201 assert(!field()->is_internal(), "regular only");
202 return field()->name_index();
203 }
204 void set_name_index(int index) {
205 assert(!field()->is_internal(), "regular only");
206 field()->set_name_index(index);
207 }
208 int signature_index() const {
209 assert(!field()->is_internal(), "regular only");
210 return field()->signature_index();
211 }
212 void set_signature_index(int index) {
|