1 /*
   2  * Copyright (c) 1997, 2009, 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 "incls/_precompiled.incl"
  26 #include "incls/_fieldDescriptor.cpp.incl"
  27 
  28 
  29 oop fieldDescriptor::loader() const {
  30   return instanceKlass::cast(_cp->pool_holder())->class_loader();
  31 }
  32 
  33 typeArrayOop fieldDescriptor::annotations() const {
  34   instanceKlass* ik = instanceKlass::cast(field_holder());
  35   objArrayOop md = ik->fields_annotations();
  36   if (md == NULL)
  37     return NULL;
  38   assert((index() % instanceKlass::next_offset) == 0, "");
  39   return typeArrayOop(md->obj_at(index() / instanceKlass::next_offset));
  40 }
  41 
  42 constantTag fieldDescriptor::initial_value_tag() const {
  43   return constants()->tag_at(_initial_value_index);
  44 }
  45 
  46 jint fieldDescriptor::int_initial_value() const {
  47   return constants()->int_at(_initial_value_index);
  48 }
  49 
  50 jlong fieldDescriptor::long_initial_value() const {
  51   return constants()->long_at(_initial_value_index);
  52 }
  53 
  54 jfloat fieldDescriptor::float_initial_value() const {
  55   return constants()->float_at(_initial_value_index);
  56 }
  57 
  58 jdouble fieldDescriptor::double_initial_value() const {
  59   return constants()->double_at(_initial_value_index);
  60 }
  61 
  62 oop fieldDescriptor::string_initial_value(TRAPS) const {
  63   return constants()->string_at(_initial_value_index, CHECK_0);
  64 }
  65 
  66 void fieldDescriptor::initialize(klassOop k, int index) {
  67   instanceKlass* ik = instanceKlass::cast(k);
  68   _cp = ik->constants();
  69   typeArrayOop fields = ik->fields();
  70 
  71   assert(fields->length() % instanceKlass::next_offset == 0, "Illegal size of field array");
  72   assert(fields->length() >= index + instanceKlass::next_offset, "Illegal size of field array");
  73 
  74   _access_flags.set_field_flags(fields->ushort_at(index + instanceKlass::access_flags_offset));
  75   _name_index = fields->ushort_at(index + instanceKlass::name_index_offset);
  76   _signature_index = fields->ushort_at(index + instanceKlass::signature_index_offset);
  77   _initial_value_index = fields->ushort_at(index + instanceKlass::initval_index_offset);
  78   guarantee(_name_index != 0 && _signature_index != 0, "bad constant pool index for fieldDescriptor");
  79   _offset = ik->offset_from_fields( index );
  80   _generic_signature_index = fields->ushort_at(index + instanceKlass::generic_signature_offset);
  81   _index = index;
  82 }
  83 
  84 #ifndef PRODUCT
  85 
  86 void fieldDescriptor::print_on(outputStream* st) const {
  87   _access_flags.print_on(st);
  88   constants()->symbol_at(_name_index)->print_value_on(st);
  89   st->print(" ");
  90   constants()->symbol_at(_signature_index)->print_value_on(st);
  91   st->print(" @%d ", offset());
  92   if (WizardMode && has_initial_value()) {
  93     st->print("(initval ");
  94     constantTag t = initial_value_tag();
  95     if (t.is_int()) {
  96       st->print("int %d)", int_initial_value());
  97     } else if (t.is_long()){
  98       st->print_jlong(long_initial_value());
  99     } else if (t.is_float()){
 100       st->print("float %f)", float_initial_value());
 101     } else if (t.is_double()){
 102       st->print("double %lf)", double_initial_value());
 103     }
 104   }
 105 }
 106 
 107 void fieldDescriptor::print_on_for(outputStream* st, oop obj) {
 108   print_on(st);
 109   BasicType ft = field_type();
 110   jint as_int = 0;
 111   switch (ft) {
 112     case T_BYTE:
 113       as_int = (jint)obj->byte_field(offset());
 114       st->print(" %d", obj->byte_field(offset()));
 115       break;
 116     case T_CHAR:
 117       as_int = (jint)obj->char_field(offset());
 118       {
 119         jchar c = obj->char_field(offset());
 120         as_int = c;
 121         st->print(" %c %d", isprint(c) ? c : ' ', c);
 122       }
 123       break;
 124     case T_DOUBLE:
 125       st->print(" %lf", obj->double_field(offset()));
 126       break;
 127     case T_FLOAT:
 128       as_int = obj->int_field(offset());
 129       st->print(" %f", obj->float_field(offset()));
 130       break;
 131     case T_INT:
 132       as_int = obj->int_field(offset());
 133       st->print(" %d", obj->int_field(offset()));
 134       break;
 135     case T_LONG:
 136       st->print(" ");
 137       st->print_jlong(obj->long_field(offset()));
 138       break;
 139     case T_SHORT:
 140       as_int = obj->short_field(offset());
 141       st->print(" %d", obj->short_field(offset()));
 142       break;
 143     case T_BOOLEAN:
 144       as_int = obj->bool_field(offset());
 145       st->print(" %s", obj->bool_field(offset()) ? "true" : "false");
 146       break;
 147     case T_ARRAY:
 148       st->print(" ");
 149       NOT_LP64(as_int = obj->int_field(offset()));
 150       obj->obj_field(offset())->print_value_on(st);
 151       break;
 152     case T_OBJECT:
 153       st->print(" ");
 154       NOT_LP64(as_int = obj->int_field(offset()));
 155       obj->obj_field(offset())->print_value_on(st);
 156       break;
 157     default:
 158       ShouldNotReachHere();
 159       break;
 160   }
 161   // Print a hint as to the underlying integer representation. This can be wrong for
 162   // pointers on an LP64 machine
 163   if (ft == T_LONG || ft == T_DOUBLE LP64_ONLY(|| !is_java_primitive(ft)) ) {
 164     st->print(" (%x %x)", obj->int_field(offset()), obj->int_field(offset()+sizeof(jint)));
 165   } else if (as_int < 0 || as_int > 9) {
 166     st->print(" (%x)", as_int);
 167   }
 168 }
 169 
 170 #endif /* PRODUCT */