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/instanceKlass.hpp"
 30 #include "oops/klass.inline.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "oops/fieldStreams.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, int index) {
 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= ik->field(index);
 98   assert((int)_fieldinfo.index() == index, "just checking");
 99   guarantee(_fieldinfo.name_index() != 0 && _fieldinfo.signature_index() != 0, "bad constant pool index for fieldDescriptor");
100 }
101 
102 void fieldDescriptor::print_on(outputStream* st) const {
103   access_flags().print_on(st);
104   if (field_flags().is_injected()) st->print("injected ");
105   name()->print_value_on(st);
106   st->print(" ");
107   signature()->print_value_on(st);
108   st->print(" @%d ", offset());
109   if (WizardMode && has_initial_value()) {
110     st->print("(initval ");
111     constantTag t = initial_value_tag();
112     if (t.is_int()) {
113       st->print("int %d)", int_initial_value());
114     } else if (t.is_long()){
115       st->print_jlong(long_initial_value());
116     } else if (t.is_float()){
117       st->print("float %f)", float_initial_value());
118     } else if (t.is_double()){
119       st->print("double %lf)", double_initial_value());
120     }
121   }
122 }
123 
124 void fieldDescriptor::print() const { print_on(tty); }
125 
126 void fieldDescriptor::print_on_for(outputStream* st, oop obj) {
127   print_on(st);
128   st->print(" ");
129 
130   BasicType ft = field_type();



131   switch (ft) {
132     case T_BYTE:
133       st->print("%d", obj->byte_field(offset()));
134       break;
135     case T_CHAR:
136       {
137         jchar c = obj->char_field(offset());
138         st->print("%c %d", isprint(c) ? c : ' ', c);
139       }
140       break;
141     case T_DOUBLE:
142       st->print("%lf", obj->double_field(offset()));
143       break;
144     case T_FLOAT:
145       st->print("%f", obj->float_field(offset()));
146       break;
147     case T_INT:
148       st->print("%d", obj->int_field(offset()));
149       break;
150     case T_LONG:
151       st->print_jlong(obj->long_field(offset()));
152       break;
153     case T_SHORT:
154       st->print("%d", obj->short_field(offset()));
155       break;
156     case T_BOOLEAN:
157       st->print("%s", obj->bool_field(offset()) ? "true" : "false");
158       break;
159     case T_ARRAY:
160       if (obj->obj_field(offset()) != nullptr) {
161         obj->obj_field(offset())->print_value_on(st);
162       } else {
163         st->print("null");
164       }
165       break;
166     case T_OBJECT:



























167       if (obj->obj_field(offset()) != nullptr) {
168         obj->obj_field(offset())->print_value_on(st);
169       } else {
170         st->print("null");
171       }
172       break;
173     default:
174       ShouldNotReachHere();
175       break;
176   }
177 
178   // Print a hint as to the underlying integer representation.
179   if (is_reference_type(ft)) {
180 #ifdef _LP64
181     if (UseCompressedOops) {
182       st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));
183     } else {
184       st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset()));
185     }
186 #else
187     st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));
188 #endif
189   } else { // Primitives
190     switch (ft) {
191       case T_LONG:    st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset())); break;
192       case T_DOUBLE:  st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset())); break;
193       case T_BYTE:    st->print(" (" INT8_FORMAT_X_0  ")", obj->byte_field(offset()));          break;
194       case T_CHAR:    st->print(" (" INT16_FORMAT_X_0 ")", obj->char_field(offset()));          break;
195       case T_FLOAT:   st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));           break;
196       case T_INT:     st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));           break;
197       case T_SHORT:   st->print(" (" INT16_FORMAT_X_0 ")", obj->short_field(offset()));         break;
198       case T_BOOLEAN: st->print(" (" INT8_FORMAT_X_0  ")", obj->bool_field(offset()));          break;
199     default:
200       ShouldNotReachHere();
201       break;
202     }
203   }
204 }
--- EOF ---