1 /*
2 * Copyright (c) 1997, 2025, 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 #include "classfile/vmSymbols.hpp"
26 #include "memory/resourceArea.hpp"
27 #include "oops/annotations.hpp"
28 #include "oops/constantPool.hpp"
29 #include "oops/fieldStreams.inline.hpp"
30 #include "oops/instanceKlass.hpp"
31 #include "oops/klass.inline.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/fieldDescriptor.inline.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/signature.hpp"
36
37 Symbol* fieldDescriptor::generic_signature() const {
38 if (!has_generic_signature()) {
39 return nullptr;
40 }
41 return _cp->symbol_at(_fieldinfo.generic_signature_index());
42 }
43
44 bool fieldDescriptor::is_trusted_final() const {
45 InstanceKlass* ik = field_holder();
46 return is_final() && (is_static() || ik->is_hidden() || ik->is_record());
47 }
48
49 AnnotationArray* fieldDescriptor::annotations() const {
50 InstanceKlass* ik = field_holder();
51 Array<AnnotationArray*>* md = ik->fields_annotations();
52 if (md == nullptr)
53 return nullptr;
54 return md->at(index());
55 }
56
57 AnnotationArray* fieldDescriptor::type_annotations() const {
58 InstanceKlass* ik = field_holder();
59 Array<AnnotationArray*>* type_annos = ik->fields_type_annotations();
60 if (type_annos == nullptr)
61 return nullptr;
62 return type_annos->at(index());
63 }
64
65 constantTag fieldDescriptor::initial_value_tag() const {
66 return constants()->tag_at(initial_value_index());
67 }
68
69 jint fieldDescriptor::int_initial_value() const {
70 return constants()->int_at(initial_value_index());
71 }
72
73 jlong fieldDescriptor::long_initial_value() const {
74 return constants()->long_at(initial_value_index());
75 }
76
77 jfloat fieldDescriptor::float_initial_value() const {
78 return constants()->float_at(initial_value_index());
79 }
80
81 jdouble fieldDescriptor::double_initial_value() const {
82 return constants()->double_at(initial_value_index());
83 }
84
85 oop fieldDescriptor::string_initial_value(TRAPS) const {
86 return constants()->uncached_string_at(initial_value_index(), THREAD);
87 }
88
89 void fieldDescriptor::reinitialize(InstanceKlass* ik, const FieldInfo& fieldinfo) {
90 if (_cp.is_null() || field_holder() != ik) {
91 _cp = constantPoolHandle(Thread::current(), ik->constants());
92 // _cp should now reference ik's constant pool; i.e., ik is now field_holder.
93 // If the class is a scratch class, the constant pool points to the original class,
94 // but that's ok because of constant pool merging.
95 assert(field_holder() == ik || ik->is_scratch_class(), "must be already initialized to this class");
96 }
97 _fieldinfo = fieldinfo;
98 guarantee(_fieldinfo.name_index() != 0 && _fieldinfo.signature_index() != 0, "bad constant pool index for fieldDescriptor");
99 }
100
101 void fieldDescriptor::print_on(outputStream* st) const {
102 access_flags().print_on(st);
103 if (field_flags().is_injected()) st->print("injected ");
104 name()->print_value_on(st);
105 st->print(" ");
106 signature()->print_value_on(st);
107 st->print(" @%d ", offset());
108 if (WizardMode && has_initial_value()) {
109 st->print("(initval ");
110 constantTag t = initial_value_tag();
111 if (t.is_int()) {
112 st->print("int %d)", int_initial_value());
113 } else if (t.is_long()){
114 st->print_jlong(long_initial_value());
115 } else if (t.is_float()){
116 st->print("float %f)", float_initial_value());
117 } else if (t.is_double()){
118 st->print("double %lf)", double_initial_value());
119 }
120 }
121 }
122
123 void fieldDescriptor::print() const { print_on(tty); }
124
125 void fieldDescriptor::print_on_for(outputStream* st, oop obj) {
126 print_on(st);
127 st->print(" ");
128
129 BasicType ft = field_type();
130 switch (ft) {
131 case T_BYTE:
132 st->print("%d", obj->byte_field(offset()));
133 break;
134 case T_CHAR:
135 {
136 jchar c = obj->char_field(offset());
137 st->print("%c %d", isprint(c) ? c : ' ', c);
138 }
139 break;
140 case T_DOUBLE:
141 st->print("%lf", obj->double_field(offset()));
142 break;
143 case T_FLOAT:
144 st->print("%f", obj->float_field(offset()));
145 break;
146 case T_INT:
147 st->print("%d", obj->int_field(offset()));
148 break;
149 case T_LONG:
150 st->print_jlong(obj->long_field(offset()));
151 break;
152 case T_SHORT:
153 st->print("%d", obj->short_field(offset()));
154 break;
155 case T_BOOLEAN:
156 st->print("%s", obj->bool_field(offset()) ? "true" : "false");
157 break;
158 case T_ARRAY:
159 if (obj->obj_field(offset()) != nullptr) {
160 obj->obj_field(offset())->print_value_on(st);
161 } else {
162 st->print("null");
163 }
164 break;
165 case T_OBJECT:
166 if (obj->obj_field(offset()) != nullptr) {
167 obj->obj_field(offset())->print_value_on(st);
168 } else {
169 st->print("null");
170 }
171 break;
172 default:
173 ShouldNotReachHere();
174 break;
175 }
176
177 // Print a hint as to the underlying integer representation.
178 if (is_reference_type(ft)) {
179 #ifdef _LP64
180 if (UseCompressedOops) {
181 st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));
182 } else {
183 st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset()));
184 }
185 #else
186 st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));
187 #endif
188 } else { // Primitives
189 switch (ft) {
190 case T_LONG: st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset())); break;
191 case T_DOUBLE: st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset())); break;
192 case T_BYTE: st->print(" (" INT8_FORMAT_X_0 ")", obj->byte_field(offset())); break;
193 case T_CHAR: st->print(" (" INT16_FORMAT_X_0 ")", obj->char_field(offset())); break;
194 case T_FLOAT: st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset())); break;
195 case T_INT: st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset())); break;
196 case T_SHORT: st->print(" (" INT16_FORMAT_X_0 ")", obj->short_field(offset())); break;
197 case T_BOOLEAN: st->print(" (" INT8_FORMAT_X_0 ")", obj->bool_field(offset())); break;
198 default:
199 ShouldNotReachHere();
200 break;
201 }
202 }
203 }