rev 11483 : [mq]: webrev.01
rev 11484 : [mq]: webrev.02

   1 /*
   2  * Copyright (c) 1997, 2016, 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 "precompiled.hpp"
  26 #include "classfile/altHashing.hpp"
  27 #include "classfile/classLoaderData.inline.hpp"
  28 #include "classfile/javaClasses.inline.hpp"
  29 #include "classfile/moduleEntry.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/debugInfo.hpp"
  33 #include "code/dependencyContext.hpp"
  34 #include "code/pcDesc.hpp"
  35 #include "interpreter/interpreter.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.inline.hpp"
  39 #include "oops/fieldStreams.hpp"
  40 #include "oops/instanceKlass.hpp"
  41 #include "oops/instanceMirrorKlass.hpp"
  42 #include "oops/klass.hpp"
  43 #include "oops/method.hpp"
  44 #include "oops/objArrayOop.inline.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "oops/symbol.hpp"
  47 #include "oops/typeArrayOop.hpp"
  48 #include "runtime/fieldDescriptor.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/interfaceSupport.hpp"
  51 #include "runtime/java.hpp"
  52 #include "runtime/javaCalls.hpp"
  53 #include "runtime/safepoint.hpp"
  54 #include "runtime/thread.inline.hpp"
  55 #include "runtime/vframe.hpp"
  56 #include "utilities/preserveException.hpp"
  57 
  58 #if INCLUDE_JVMCI
  59 #include "jvmci/jvmciJavaClasses.hpp"
  60 #endif
  61 
  62 #define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java)    \
  63   klass::_##name##_offset = JavaClasses::compute_injected_offset(JavaClasses::klass##_##name##_enum);
  64 
  65 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java)           \
  66   { SystemDictionary::WK_KLASS_ENUM_NAME(klass), vmSymbols::VM_SYMBOL_ENUM_NAME(name##_name), vmSymbols::VM_SYMBOL_ENUM_NAME(signature), may_be_java },
  67 
  68 InjectedField JavaClasses::_injected_fields[] = {
  69   ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
  70 };
  71 
  72 int JavaClasses::compute_injected_offset(InjectedFieldID id) {
  73   return _injected_fields[id].compute_offset();
  74 }
  75 
  76 
  77 InjectedField* JavaClasses::get_injected(Symbol* class_name, int* field_count) {
  78   *field_count = 0;
  79 
  80   vmSymbols::SID sid = vmSymbols::find_sid(class_name);
  81   if (sid == vmSymbols::NO_SID) {
  82     // Only well known classes can inject fields
  83     return NULL;
  84   }
  85 
  86   int count = 0;
  87   int start = -1;
  88 
  89 #define LOOKUP_INJECTED_FIELD(klass, name, signature, may_be_java) \
  90   if (sid == vmSymbols::VM_SYMBOL_ENUM_NAME(klass)) {              \
  91     count++;                                                       \
  92     if (start == -1) start = klass##_##name##_enum;                \
  93   }
  94   ALL_INJECTED_FIELDS(LOOKUP_INJECTED_FIELD);
  95 #undef LOOKUP_INJECTED_FIELD
  96 
  97   if (start != -1) {
  98     *field_count = count;
  99     return _injected_fields + start;
 100   }
 101   return NULL;
 102 }
 103 
 104 
 105 static bool find_field(InstanceKlass* ik,
 106                        Symbol* name_symbol, Symbol* signature_symbol,
 107                        fieldDescriptor* fd,
 108                        bool is_static = false, bool allow_super = false) {
 109   if (allow_super || is_static) {
 110     return ik->find_field(name_symbol, signature_symbol, is_static, fd) != NULL;
 111   } else {
 112     return ik->find_local_field(name_symbol, signature_symbol, fd);
 113   }
 114 }
 115 
 116 // Helpful routine for computing field offsets at run time rather than hardcoding them
 117 static void
 118 compute_offset(int &dest_offset,
 119                Klass* klass, Symbol* name_symbol, Symbol* signature_symbol,
 120                bool is_static = false, bool allow_super = false) {
 121   fieldDescriptor fd;
 122   InstanceKlass* ik = InstanceKlass::cast(klass);
 123   if (!find_field(ik, name_symbol, signature_symbol, &fd, is_static, allow_super)) {
 124     ResourceMark rm;
 125     tty->print_cr("Invalid layout of %s at %s", ik->external_name(), name_symbol->as_C_string());
 126 #ifndef PRODUCT
 127     ik->print();
 128     tty->print_cr("all fields:");
 129     for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
 130       tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
 131     }
 132 #endif //PRODUCT
 133     vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
 134   }
 135   dest_offset = fd.offset();
 136 }
 137 
 138 // Same as above but for "optional" offsets that might not be present in certain JDK versions
 139 static void
 140 compute_optional_offset(int& dest_offset,
 141                         Klass* klass, Symbol* name_symbol, Symbol* signature_symbol,
 142                         bool allow_super = false) {
 143   fieldDescriptor fd;
 144   InstanceKlass* ik = InstanceKlass::cast(klass);
 145   if (find_field(ik, name_symbol, signature_symbol, &fd, allow_super)) {
 146     dest_offset = fd.offset();
 147   }
 148 }
 149 
 150 
 151 int java_lang_String::value_offset  = 0;
 152 int java_lang_String::hash_offset   = 0;
 153 int java_lang_String::coder_offset  = 0;
 154 
 155 bool java_lang_String::initialized  = false;
 156 
 157 bool java_lang_String::is_instance(oop obj) {
 158   return is_instance_inlined(obj);
 159 }
 160 
 161 void java_lang_String::compute_offsets() {
 162   assert(!initialized, "offsets should be initialized only once");
 163 
 164   Klass* k = SystemDictionary::String_klass();
 165   compute_offset(value_offset,           k, vmSymbols::value_name(),  vmSymbols::byte_array_signature());
 166   compute_optional_offset(hash_offset,   k, vmSymbols::hash_name(),   vmSymbols::int_signature());
 167   compute_optional_offset(coder_offset,  k, vmSymbols::coder_name(),  vmSymbols::byte_signature());
 168 
 169   initialized = true;
 170 }
 171 
 172 class CompactStringsFixup : public FieldClosure {
 173 private:
 174   bool _value;
 175 
 176 public:
 177   CompactStringsFixup(bool value) : _value(value) {}
 178 
 179   void do_field(fieldDescriptor* fd) {
 180     if (fd->name() == vmSymbols::compact_strings_name()) {
 181       oop mirror = fd->field_holder()->java_mirror();
 182       assert(fd->field_holder() == SystemDictionary::String_klass(), "Should be String");
 183       assert(mirror != NULL, "String must have mirror already");
 184       mirror->bool_field_put(fd->offset(), _value);
 185     }
 186   }
 187 };
 188 
 189 void java_lang_String::set_compact_strings(bool value) {
 190   CompactStringsFixup fix(value);
 191   InstanceKlass::cast(SystemDictionary::String_klass())->do_local_static_fields(&fix);
 192 }
 193 
 194 Handle java_lang_String::basic_create(int length, bool is_latin1, TRAPS) {
 195   assert(initialized, "Must be initialized");
 196   assert(CompactStrings || !is_latin1, "Must be UTF16 without CompactStrings");
 197 
 198   // Create the String object first, so there's a chance that the String
 199   // and the char array it points to end up in the same cache line.
 200   oop obj;
 201   obj = SystemDictionary::String_klass()->allocate_instance(CHECK_NH);
 202 
 203   // Create the char array.  The String object must be handlized here
 204   // because GC can happen as a result of the allocation attempt.
 205   Handle h_obj(THREAD, obj);
 206   int arr_length = is_latin1 ? length : length << 1; // 2 bytes per UTF16.
 207   typeArrayOop buffer = oopFactory::new_byteArray(arr_length, CHECK_NH);;
 208 
 209   // Point the String at the char array
 210   obj = h_obj();
 211   set_value(obj, buffer);
 212   // No need to zero the offset, allocation zero'ed the entire String object
 213   set_coder(obj, is_latin1 ? CODER_LATIN1 : CODER_UTF16);
 214   return h_obj;
 215 }
 216 
 217 Handle java_lang_String::create_from_unicode(jchar* unicode, int length, TRAPS) {
 218   bool is_latin1 = CompactStrings && UNICODE::is_latin1(unicode, length);
 219   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 220   typeArrayOop buffer = value(h_obj());
 221   assert(TypeArrayKlass::cast(buffer->klass())->element_type() == T_BYTE, "only byte[]");
 222   if (is_latin1) {
 223     for (int index = 0; index < length; index++) {
 224       buffer->byte_at_put(index, (jbyte)unicode[index]);
 225     }
 226   } else {
 227     for (int index = 0; index < length; index++) {
 228       buffer->char_at_put(index, unicode[index]);
 229     }
 230   }
 231 
 232 #ifdef ASSERT
 233   {
 234     ResourceMark rm;
 235     char* expected = UNICODE::as_utf8(unicode, length);
 236     char* actual = as_utf8_string(h_obj());
 237     if (strcmp(expected, actual) != 0) {
 238       tty->print_cr("Unicode conversion failure: %s --> %s", expected, actual);
 239       ShouldNotReachHere();
 240     }
 241   }
 242 #endif
 243 
 244   return h_obj;
 245 }
 246 
 247 oop java_lang_String::create_oop_from_unicode(jchar* unicode, int length, TRAPS) {
 248   Handle h_obj = create_from_unicode(unicode, length, CHECK_0);
 249   return h_obj();
 250 }
 251 
 252 Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) {
 253   if (utf8_str == NULL) {
 254     return Handle();
 255   }
 256   bool has_multibyte, is_latin1;
 257   int length = UTF8::unicode_length(utf8_str, is_latin1, has_multibyte);
 258   if (!CompactStrings) {
 259     has_multibyte = true;
 260     is_latin1 = false;
 261   }
 262 
 263   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 264   if (length > 0) {
 265     if (!has_multibyte) {
 266       strncpy((char*)value(h_obj())->byte_at_addr(0), utf8_str, length);
 267     } else if (is_latin1) {
 268       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 269     } else {
 270       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 271     }
 272   }
 273 
 274 #ifdef ASSERT
 275   // This check is too strict because the input string is not necessarily valid UTF8.
 276   // For example, it may be created with arbitrary content via jni_NewStringUTF.
 277   /*
 278   {
 279     ResourceMark rm;
 280     const char* expected = utf8_str;
 281     char* actual = as_utf8_string(h_obj());
 282     if (strcmp(expected, actual) != 0) {
 283       tty->print_cr("String conversion failure: %s --> %s", expected, actual);
 284       ShouldNotReachHere();
 285     }
 286   }
 287   */
 288 #endif
 289 
 290   return h_obj;
 291 }
 292 
 293 oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) {
 294   Handle h_obj = create_from_str(utf8_str, CHECK_0);
 295   return h_obj();
 296 }
 297 
 298 Handle java_lang_String::create_from_symbol(Symbol* symbol, TRAPS) {
 299   const char* utf8_str = (char*)symbol->bytes();
 300   int utf8_len = symbol->utf8_length();
 301 
 302   bool has_multibyte, is_latin1;
 303   int length = UTF8::unicode_length(utf8_str, utf8_len, is_latin1, has_multibyte);
 304   if (!CompactStrings) {
 305     has_multibyte = true;
 306     is_latin1 = false;
 307   }
 308 
 309   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 310   if (length > 0) {
 311     if (!has_multibyte) {
 312       strncpy((char*)value(h_obj())->byte_at_addr(0), utf8_str, length);
 313     } else if (is_latin1) {
 314       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 315     } else {
 316       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 317     }
 318   }
 319 
 320 #ifdef ASSERT
 321   {
 322     ResourceMark rm;
 323     const char* expected = symbol->as_utf8();
 324     char* actual = as_utf8_string(h_obj());
 325     if (strncmp(expected, actual, utf8_len) != 0) {
 326       tty->print_cr("Symbol conversion failure: %s --> %s", expected, actual);
 327       ShouldNotReachHere();
 328     }
 329   }
 330 #endif
 331 
 332   return h_obj;
 333 }
 334 
 335 // Converts a C string to a Java String based on current encoding
 336 Handle java_lang_String::create_from_platform_dependent_str(const char* str, TRAPS) {
 337   assert(str != NULL, "bad arguments");
 338 
 339   typedef jstring (*to_java_string_fn_t)(JNIEnv*, const char *);
 340   static to_java_string_fn_t _to_java_string_fn = NULL;
 341 
 342   if (_to_java_string_fn == NULL) {
 343     void *lib_handle = os::native_java_library();
 344     _to_java_string_fn = CAST_TO_FN_PTR(to_java_string_fn_t, os::dll_lookup(lib_handle, "NewStringPlatform"));
 345     if (_to_java_string_fn == NULL) {
 346       fatal("NewStringPlatform missing");
 347     }
 348   }
 349 
 350   jstring js = NULL;
 351   { JavaThread* thread = (JavaThread*)THREAD;
 352     assert(thread->is_Java_thread(), "must be java thread");
 353     HandleMark hm(thread);
 354     ThreadToNativeFromVM ttn(thread);
 355     js = (_to_java_string_fn)(thread->jni_environment(), str);
 356   }
 357   return Handle(THREAD, JNIHandles::resolve(js));
 358 }
 359 
 360 // Converts a Java String to a native C string that can be used for
 361 // native OS calls.
 362 char* java_lang_String::as_platform_dependent_str(Handle java_string, TRAPS) {
 363   typedef char* (*to_platform_string_fn_t)(JNIEnv*, jstring, bool*);
 364   static to_platform_string_fn_t _to_platform_string_fn = NULL;
 365 
 366   if (_to_platform_string_fn == NULL) {
 367     void *lib_handle = os::native_java_library();
 368     _to_platform_string_fn = CAST_TO_FN_PTR(to_platform_string_fn_t, os::dll_lookup(lib_handle, "GetStringPlatformChars"));
 369     if (_to_platform_string_fn == NULL) {
 370       fatal("GetStringPlatformChars missing");
 371     }
 372   }
 373 
 374   char *native_platform_string;
 375   { JavaThread* thread = (JavaThread*)THREAD;
 376     assert(thread->is_Java_thread(), "must be java thread");
 377     JNIEnv *env = thread->jni_environment();
 378     jstring js = (jstring) JNIHandles::make_local(env, java_string());
 379     bool is_copy;
 380     HandleMark hm(thread);
 381     ThreadToNativeFromVM ttn(thread);
 382     native_platform_string = (_to_platform_string_fn)(env, js, &is_copy);
 383     assert(is_copy == JNI_TRUE, "is_copy value changed");
 384     JNIHandles::destroy_local(js);
 385   }
 386   return native_platform_string;
 387 }
 388 
 389 Handle java_lang_String::char_converter(Handle java_string, jchar from_char, jchar to_char, TRAPS) {
 390   oop          obj    = java_string();
 391   // Typical usage is to convert all '/' to '.' in string.
 392   typeArrayOop value  = java_lang_String::value(obj);
 393   int          length = java_lang_String::length(obj);
 394   bool      is_latin1 = java_lang_String::is_latin1(obj);
 395 
 396   // First check if any from_char exist
 397   int index; // Declared outside, used later
 398   for (index = 0; index < length; index++) {
 399     jchar c = !is_latin1 ? value->char_at(index) :
 400                   ((jchar) value->byte_at(index)) & 0xff;
 401     if (c == from_char) {
 402       break;
 403     }
 404   }
 405   if (index == length) {
 406     // No from_char, so do not copy.
 407     return java_string;
 408   }
 409 
 410   // Check if result string will be latin1
 411   bool to_is_latin1 = false;
 412 
 413   // Replacement char must be latin1
 414   if (CompactStrings && UNICODE::is_latin1(to_char)) {
 415     if (is_latin1) {
 416       // Source string is latin1 as well
 417       to_is_latin1 = true;
 418     } else if (!UNICODE::is_latin1(from_char)) {
 419       // We are replacing an UTF16 char. Scan string to
 420       // check if result can be latin1 encoded.
 421       to_is_latin1 = true;
 422       for (index = 0; index < length; index++) {
 423         jchar c = value->char_at(index);
 424         if (c != from_char && !UNICODE::is_latin1(c)) {
 425           to_is_latin1 = false;
 426           break;
 427         }
 428       }
 429     }
 430   }
 431 
 432   // Create new UNICODE (or byte) buffer. Must handlize value because GC
 433   // may happen during String and char array creation.
 434   typeArrayHandle h_value(THREAD, value);
 435   Handle string = basic_create(length, to_is_latin1, CHECK_NH);
 436   typeArrayOop from_buffer = h_value();
 437   typeArrayOop to_buffer = java_lang_String::value(string());
 438 
 439   // Copy contents
 440   for (index = 0; index < length; index++) {
 441     jchar c = (!is_latin1) ? from_buffer->char_at(index) :
 442                     ((jchar) from_buffer->byte_at(index)) & 0xff;
 443     if (c == from_char) {
 444       c = to_char;
 445     }
 446     if (!to_is_latin1) {
 447       to_buffer->char_at_put(index, c);
 448     } else {
 449       to_buffer->byte_at_put(index, (jbyte) c);
 450     }
 451   }
 452   return string;
 453 }
 454 
 455 jchar* java_lang_String::as_unicode_string(oop java_string, int& length, TRAPS) {
 456   typeArrayOop value  = java_lang_String::value(java_string);
 457                length = java_lang_String::length(java_string);
 458   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 459 
 460   jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length);
 461   if (result != NULL) {
 462     if (!is_latin1) {
 463       for (int index = 0; index < length; index++) {
 464         result[index] = value->char_at(index);
 465       }
 466     } else {
 467       for (int index = 0; index < length; index++) {
 468         result[index] = ((jchar) value->byte_at(index)) & 0xff;
 469       }
 470     }
 471   } else {
 472     THROW_MSG_0(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string");
 473   }
 474   return result;
 475 }
 476 
 477 unsigned int java_lang_String::hash_code(oop java_string) {
 478   int          length = java_lang_String::length(java_string);
 479   // Zero length string will hash to zero with String.hashCode() function.
 480   if (length == 0) return 0;
 481 
 482   typeArrayOop value  = java_lang_String::value(java_string);
 483   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 484 
 485   if (is_latin1) {
 486     return java_lang_String::hash_code(value->byte_at_addr(0), length);
 487   } else {
 488     return java_lang_String::hash_code(value->char_at_addr(0), length);
 489   }
 490 }
 491 
 492 char* java_lang_String::as_quoted_ascii(oop java_string) {
 493   typeArrayOop value  = java_lang_String::value(java_string);
 494   int          length = java_lang_String::length(java_string);
 495   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 496 
 497   if (length == 0) return NULL;
 498 
 499   char* result;
 500   int result_length;
 501   if (!is_latin1) {
 502     jchar* base = value->char_at_addr(0);
 503     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 504     result = NEW_RESOURCE_ARRAY(char, result_length);
 505     UNICODE::as_quoted_ascii(base, length, result, result_length);
 506   } else {
 507     jbyte* base = value->byte_at_addr(0);
 508     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 509     result = NEW_RESOURCE_ARRAY(char, result_length);
 510     UNICODE::as_quoted_ascii(base, length, result, result_length);
 511   }
 512   assert(result_length >= length + 1, "must not be shorter");
 513   assert(result_length == (int)strlen(result) + 1, "must match");
 514   return result;
 515 }
 516 
 517 Symbol* java_lang_String::as_symbol(Handle java_string, TRAPS) {
 518   oop          obj    = java_string();
 519   typeArrayOop value  = java_lang_String::value(obj);
 520   int          length = java_lang_String::length(obj);
 521   bool      is_latin1 = java_lang_String::is_latin1(obj);
 522   if (!is_latin1) {
 523     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 524     Symbol* sym = SymbolTable::lookup_unicode(base, length, THREAD);
 525     return sym;
 526   } else {
 527     ResourceMark rm;
 528     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 529     const char* base = UNICODE::as_utf8(position, length);
 530     Symbol* sym = SymbolTable::lookup(base, length, THREAD);
 531     return sym;
 532   }
 533 }
 534 
 535 Symbol* java_lang_String::as_symbol_or_null(oop java_string) {
 536   typeArrayOop value  = java_lang_String::value(java_string);
 537   int          length = java_lang_String::length(java_string);
 538   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 539   if (!is_latin1) {
 540     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 541     return SymbolTable::probe_unicode(base, length);
 542   } else {
 543     ResourceMark rm;
 544     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 545     const char* base = UNICODE::as_utf8(position, length);
 546     return SymbolTable::probe(base, length);
 547   }
 548 }
 549 
 550 int java_lang_String::utf8_length(oop java_string) {
 551   typeArrayOop value  = java_lang_String::value(java_string);
 552   int          length = java_lang_String::length(java_string);
 553   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 554   if (length == 0) {
 555     return 0;
 556   }
 557   if (!is_latin1) {
 558     return UNICODE::utf8_length(value->char_at_addr(0), length);
 559   } else {
 560     return UNICODE::utf8_length(value->byte_at_addr(0), length);
 561   }
 562 }
 563 
 564 char* java_lang_String::as_utf8_string(oop java_string) {
 565   typeArrayOop value  = java_lang_String::value(java_string);
 566   int          length = java_lang_String::length(java_string);
 567   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 568   if (!is_latin1) {
 569     jchar* position = (length == 0) ? NULL : value->char_at_addr(0);
 570     return UNICODE::as_utf8(position, length);
 571   } else {
 572     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 573     return UNICODE::as_utf8(position, length);
 574   }
 575 }
 576 
 577 char* java_lang_String::as_utf8_string(oop java_string, char* buf, int buflen) {
 578   typeArrayOop value  = java_lang_String::value(java_string);
 579   int          length = java_lang_String::length(java_string);
 580   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 581   if (!is_latin1) {
 582     jchar* position = (length == 0) ? NULL : value->char_at_addr(0);
 583     return UNICODE::as_utf8(position, length, buf, buflen);
 584   } else {
 585     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 586     return UNICODE::as_utf8(position, length, buf, buflen);
 587   }
 588 }
 589 
 590 char* java_lang_String::as_utf8_string(oop java_string, int start, int len) {
 591   typeArrayOop value  = java_lang_String::value(java_string);
 592   int          length = java_lang_String::length(java_string);
 593   assert(start + len <= length, "just checking");
 594   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 595   if (!is_latin1) {
 596     jchar* position = value->char_at_addr(start);
 597     return UNICODE::as_utf8(position, len);
 598   } else {
 599     jbyte* position = value->byte_at_addr(start);
 600     return UNICODE::as_utf8(position, len);
 601   }
 602 }
 603 
 604 char* java_lang_String::as_utf8_string(oop java_string, int start, int len, char* buf, int buflen) {
 605   typeArrayOop value  = java_lang_String::value(java_string);
 606   int          length = java_lang_String::length(java_string);
 607   assert(start + len <= length, "just checking");
 608   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 609   if (!is_latin1) {
 610     jchar* position = value->char_at_addr(start);
 611     return UNICODE::as_utf8(position, len, buf, buflen);
 612   } else {
 613     jbyte* position = value->byte_at_addr(start);
 614     return UNICODE::as_utf8(position, len, buf, buflen);
 615   }
 616 }
 617 
 618 bool java_lang_String::equals(oop java_string, jchar* chars, int len) {
 619   assert(java_string->klass() == SystemDictionary::String_klass(),
 620          "must be java_string");
 621   typeArrayOop value  = java_lang_String::value(java_string);
 622   int          length = java_lang_String::length(java_string);
 623   if (length != len) {
 624     return false;
 625   }
 626   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 627   if (!is_latin1) {
 628     for (int i = 0; i < len; i++) {
 629       if (value->char_at(i) != chars[i]) {
 630         return false;
 631       }
 632     }
 633   } else {
 634     for (int i = 0; i < len; i++) {
 635       if ((((jchar) value->byte_at(i)) & 0xff) != chars[i]) {
 636         return false;
 637       }
 638     }
 639   }
 640   return true;
 641 }
 642 
 643 bool java_lang_String::equals(oop str1, oop str2) {
 644   assert(str1->klass() == SystemDictionary::String_klass(),
 645          "must be java String");
 646   assert(str2->klass() == SystemDictionary::String_klass(),
 647          "must be java String");
 648   typeArrayOop value1  = java_lang_String::value(str1);
 649   int          length1 = java_lang_String::length(str1);
 650   bool       is_latin1 = java_lang_String::is_latin1(str1);
 651   typeArrayOop value2  = java_lang_String::value(str2);
 652   int          length2 = java_lang_String::length(str2);
 653   bool       is_latin2 = java_lang_String::is_latin1(str2);
 654 
 655   if ((length1 != length2) || (is_latin1 != is_latin2)) {
 656     // Strings of different size or with different
 657     // coders are never equal.
 658     return false;
 659   }
 660   int blength1 = value1->length();
 661   for (int i = 0; i < value1->length(); i++) {
 662     if (value1->byte_at(i) != value2->byte_at(i)) {
 663       return false;
 664     }
 665   }
 666   return true;
 667 }
 668 
 669 void java_lang_String::print(oop java_string, outputStream* st) {
 670   assert(java_string->klass() == SystemDictionary::String_klass(), "must be java_string");
 671   typeArrayOop value  = java_lang_String::value(java_string);
 672 
 673   if (value == NULL) {
 674     // This can happen if, e.g., printing a String
 675     // object before its initializer has been called
 676     st->print("NULL");
 677     return;
 678   }
 679 
 680   int length = java_lang_String::length(java_string);
 681   bool is_latin1 = java_lang_String::is_latin1(java_string);
 682 
 683   st->print("\"");
 684   for (int index = 0; index < length; index++) {
 685     st->print("%c", (!is_latin1) ?  value->char_at(index) :
 686                            ((jchar) value->byte_at(index)) & 0xff );
 687   }
 688   st->print("\"");
 689 }
 690 
 691 
 692 static void initialize_static_field(fieldDescriptor* fd, Handle mirror, TRAPS) {
 693   assert(mirror.not_null() && fd->is_static(), "just checking");
 694   if (fd->has_initial_value()) {
 695     BasicType t = fd->field_type();
 696     switch (t) {
 697       case T_BYTE:
 698         mirror()->byte_field_put(fd->offset(), fd->int_initial_value());
 699               break;
 700       case T_BOOLEAN:
 701         mirror()->bool_field_put(fd->offset(), fd->int_initial_value());
 702               break;
 703       case T_CHAR:
 704         mirror()->char_field_put(fd->offset(), fd->int_initial_value());
 705               break;
 706       case T_SHORT:
 707         mirror()->short_field_put(fd->offset(), fd->int_initial_value());
 708               break;
 709       case T_INT:
 710         mirror()->int_field_put(fd->offset(), fd->int_initial_value());
 711         break;
 712       case T_FLOAT:
 713         mirror()->float_field_put(fd->offset(), fd->float_initial_value());
 714         break;
 715       case T_DOUBLE:
 716         mirror()->double_field_put(fd->offset(), fd->double_initial_value());
 717         break;
 718       case T_LONG:
 719         mirror()->long_field_put(fd->offset(), fd->long_initial_value());
 720         break;
 721       case T_OBJECT:
 722         {
 723           #ifdef ASSERT
 724           TempNewSymbol sym = SymbolTable::new_symbol("Ljava/lang/String;", CHECK);
 725           assert(fd->signature() == sym, "just checking");
 726           #endif
 727           oop string = fd->string_initial_value(CHECK);
 728           mirror()->obj_field_put(fd->offset(), string);
 729         }
 730         break;
 731       default:
 732         THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
 733                   "Illegal ConstantValue attribute in class file");
 734     }
 735   }
 736 }
 737 
 738 
 739 void java_lang_Class::fixup_mirror(KlassHandle k, TRAPS) {
 740   assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
 741 
 742   // If the offset was read from the shared archive, it was fixed up already
 743   if (!k->is_shared()) {
 744     if (k->is_instance_klass()) {
 745       // During bootstrap, java.lang.Class wasn't loaded so static field
 746       // offsets were computed without the size added it.  Go back and
 747       // update all the static field offsets to included the size.
 748         for (JavaFieldStream fs(InstanceKlass::cast(k())); !fs.done(); fs.next()) {
 749         if (fs.access_flags().is_static()) {
 750           int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
 751           fs.set_offset(real_offset);
 752         }
 753       }
 754     }
 755   }
 756   create_mirror(k, Handle(NULL), Handle(NULL), Handle(NULL), CHECK);
 757 }
 758 
 759 void java_lang_Class::initialize_mirror_fields(KlassHandle k,
 760                                                Handle mirror,
 761                                                Handle protection_domain,
 762                                                TRAPS) {
 763   // Allocate a simple java object for a lock.
 764   // This needs to be a java object because during class initialization
 765   // it can be held across a java call.
 766   typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
 767   set_init_lock(mirror(), r);
 768 
 769   // Set protection domain also
 770   set_protection_domain(mirror(), protection_domain());
 771 
 772   // Initialize static fields
 773   InstanceKlass::cast(k())->do_local_static_fields(&initialize_static_field, mirror, CHECK);
 774 }
 775 
 776 void java_lang_Class::create_mirror(KlassHandle k, Handle class_loader,
 777                                     Handle module, Handle protection_domain, TRAPS) {
 778   assert(k->java_mirror() == NULL, "should only assign mirror once");
 779   // Use this moment of initialization to cache modifier_flags also,
 780   // to support Class.getModifiers().  Instance classes recalculate
 781   // the cached flags after the class file is parsed, but before the
 782   // class is put into the system dictionary.
 783   int computed_modifiers = k->compute_modifier_flags(CHECK);
 784   k->set_modifier_flags(computed_modifiers);
 785   // Class_klass has to be loaded because it is used to allocate
 786   // the mirror.
 787   if (SystemDictionary::Class_klass_loaded()) {
 788     // Allocate mirror (java.lang.Class instance)
 789     Handle mirror = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
 790 
 791     // Setup indirection from mirror->klass
 792     if (!k.is_null()) {
 793       java_lang_Class::set_klass(mirror(), k());
 794     }
 795 
 796     InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
 797     assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
 798 
 799     java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
 800 
 801     // It might also have a component mirror.  This mirror must already exist.
 802     if (k->is_array_klass()) {
 803       Handle comp_mirror;
 804       if (k->is_typeArray_klass()) {
 805         BasicType type = TypeArrayKlass::cast(k())->element_type();
 806         comp_mirror = Universe::java_mirror(type);
 807       } else {
 808         assert(k->is_objArray_klass(), "Must be");
 809         Klass* element_klass = ObjArrayKlass::cast(k())->element_klass();
 810         assert(element_klass != NULL, "Must have an element klass");
 811         comp_mirror = element_klass->java_mirror();
 812       }
 813       assert(comp_mirror.not_null(), "must have a mirror");
 814 
 815       // Two-way link between the array klass and its component mirror:
 816       // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
 817       set_component_mirror(mirror(), comp_mirror());
 818       set_array_klass(comp_mirror(), k());
 819     } else {
 820       assert(k->is_instance_klass(), "Must be");
 821 
 822       initialize_mirror_fields(k, mirror, protection_domain, THREAD);
 823       if (HAS_PENDING_EXCEPTION) {
 824         // If any of the fields throws an exception like OOM remove the klass field
 825         // from the mirror so GC doesn't follow it after the klass has been deallocated.
 826         // This mirror looks like a primitive type, which logically it is because it
 827         // it represents no class.
 828         java_lang_Class::set_klass(mirror(), NULL);
 829         return;
 830       }
 831     }
 832 
 833     // set the classLoader field in the java_lang_Class instance
 834     assert(class_loader() == k->class_loader(), "should be same");
 835     set_class_loader(mirror(), class_loader());
 836 
 837     // set the module field in the java_lang_Class instance
 838     // This may be null during bootstrap but will get fixed up later on.
 839     set_module(mirror(), module());
 840 
 841     // Setup indirection from klass->mirror last
 842     // after any exceptions can happen during allocations.
 843     if (!k.is_null()) {
 844       k->set_java_mirror(mirror());
 845     }
 846 
 847     // Keep list of classes needing java.base module fixup.
 848     if (!ModuleEntryTable::javabase_defined()) {
 849       if (fixup_module_field_list() == NULL) {
 850         GrowableArray<Klass*>* list =
 851           new (ResourceObj::C_HEAP, mtModule) GrowableArray<Klass*>(500, true);
 852         set_fixup_module_field_list(list);
 853       }
 854       k->class_loader_data()->inc_keep_alive();
 855       fixup_module_field_list()->push(k());
 856     }
 857   } else {
 858     if (fixup_mirror_list() == NULL) {
 859       GrowableArray<Klass*>* list =
 860        new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
 861       set_fixup_mirror_list(list);
 862     }
 863     fixup_mirror_list()->push(k());
 864   }
 865 }
 866 
 867 void java_lang_Class::fixup_module_field(KlassHandle k, Handle module) {
 868   assert(_module_offset != 0, "must have been computed already");
 869   java_lang_Class::set_module(k->java_mirror(), module());
 870 }
 871 

 872 int  java_lang_Class::oop_size(oop java_class) {
 873   assert(_oop_size_offset != 0, "must be set");
 874   return java_class->int_field(_oop_size_offset);
 875 }
 876 void java_lang_Class::set_oop_size(oop java_class, int size) {
 877   assert(_oop_size_offset != 0, "must be set");
 878   java_class->int_field_put(_oop_size_offset, size);
 879 }

 880 int  java_lang_Class::static_oop_field_count(oop java_class) {
 881   assert(_static_oop_field_count_offset != 0, "must be set");
 882   return java_class->int_field(_static_oop_field_count_offset);
 883 }
 884 void java_lang_Class::set_static_oop_field_count(oop java_class, int size) {
 885   assert(_static_oop_field_count_offset != 0, "must be set");
 886   java_class->int_field_put(_static_oop_field_count_offset, size);
 887 }
 888 
 889 oop java_lang_Class::protection_domain(oop java_class) {
 890   assert(_protection_domain_offset != 0, "must be set");
 891   return java_class->obj_field(_protection_domain_offset);
 892 }
 893 void java_lang_Class::set_protection_domain(oop java_class, oop pd) {
 894   assert(_protection_domain_offset != 0, "must be set");
 895   java_class->obj_field_put(_protection_domain_offset, pd);
 896 }
 897 
 898 void java_lang_Class::set_component_mirror(oop java_class, oop comp_mirror) {
 899   assert(_component_mirror_offset != 0, "must be set");
 900     java_class->obj_field_put(_component_mirror_offset, comp_mirror);
 901   }
 902 oop java_lang_Class::component_mirror(oop java_class) {
 903   assert(_component_mirror_offset != 0, "must be set");
 904   return java_class->obj_field(_component_mirror_offset);
 905 }
 906 
 907 oop java_lang_Class::init_lock(oop java_class) {
 908   assert(_init_lock_offset != 0, "must be set");
 909   return java_class->obj_field(_init_lock_offset);
 910 }
 911 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
 912   assert(_init_lock_offset != 0, "must be set");
 913   java_class->obj_field_put(_init_lock_offset, init_lock);
 914 }
 915 
 916 objArrayOop java_lang_Class::signers(oop java_class) {
 917   assert(_signers_offset != 0, "must be set");
 918   return (objArrayOop)java_class->obj_field(_signers_offset);
 919 }
 920 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
 921   assert(_signers_offset != 0, "must be set");
 922   java_class->obj_field_put(_signers_offset, (oop)signers);
 923 }
 924 
 925 
 926 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
 927   // jdk7 runs Queens in bootstrapping and jdk8-9 has no coordinated pushes yet.
 928   if (_class_loader_offset != 0) {
 929     java_class->obj_field_put(_class_loader_offset, loader);
 930   }
 931 }
 932 
 933 oop java_lang_Class::class_loader(oop java_class) {
 934   assert(_class_loader_offset != 0, "must be set");
 935   return java_class->obj_field(_class_loader_offset);
 936 }
 937 
 938 oop java_lang_Class::module(oop java_class) {
 939   assert(_module_offset != 0, "must be set");
 940   return java_class->obj_field(_module_offset);
 941 }
 942 
 943 void java_lang_Class::set_module(oop java_class, oop module) {
 944   assert(_module_offset != 0, "must be set");
 945   java_class->obj_field_put(_module_offset, module);
 946 }
 947 
 948 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
 949   // This should be improved by adding a field at the Java level or by
 950   // introducing a new VM klass (see comment in ClassFileParser)
 951   oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0);
 952   if (type != T_VOID) {
 953     Klass* aklass = Universe::typeArrayKlassObj(type);
 954     assert(aklass != NULL, "correct bootstrap");
 955     set_array_klass(java_class, aklass);
 956   }
 957 #ifdef ASSERT
 958   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass());
 959   assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
 960 #endif
 961   return java_class;
 962 }
 963 
 964 
 965 Klass* java_lang_Class::as_Klass(oop java_class) {
 966   //%note memory_2
 967   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
 968   Klass* k = ((Klass*)java_class->metadata_field(_klass_offset));
 969   assert(k == NULL || k->is_klass(), "type check");
 970   return k;
 971 }
 972 
 973 
 974 void java_lang_Class::set_klass(oop java_class, Klass* klass) {
 975   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
 976   java_class->metadata_field_put(_klass_offset, klass);
 977 }
 978 
 979 
 980 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
 981   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
 982   Symbol* name = NULL;
 983   bool is_instance = false;
 984   if (is_primitive(java_class)) {
 985     name = vmSymbols::type_signature(primitive_type(java_class));
 986   } else {
 987     Klass* k = as_Klass(java_class);
 988     is_instance = k->is_instance_klass();
 989     name = k->name();
 990   }
 991   if (name == NULL) {
 992     st->print("<null>");
 993     return;
 994   }
 995   if (is_instance)  st->print("L");
 996   st->write((char*) name->base(), (int) name->utf8_length());
 997   if (is_instance)  st->print(";");
 998 }
 999 
1000 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found, TRAPS) {
1001   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1002   Symbol* name;
1003   if (is_primitive(java_class)) {
1004     name = vmSymbols::type_signature(primitive_type(java_class));
1005     // Because this can create a new symbol, the caller has to decrement
1006     // the refcount, so make adjustment here and below for symbols returned
1007     // that are not created or incremented due to a successful lookup.
1008     name->increment_refcount();
1009   } else {
1010     Klass* k = as_Klass(java_class);
1011     if (!k->is_instance_klass()) {
1012       name = k->name();
1013       name->increment_refcount();
1014     } else {
1015       ResourceMark rm;
1016       const char* sigstr = k->signature_name();
1017       int         siglen = (int) strlen(sigstr);
1018       if (!intern_if_not_found) {
1019         name = SymbolTable::probe(sigstr, siglen);
1020       } else {
1021         name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
1022       }
1023     }
1024   }
1025   return name;
1026 }
1027 
1028 // Returns the Java name for this Java mirror (Resource allocated)
1029 // See Klass::external_name().
1030 // For primitive type Java mirrors, its type name is returned.
1031 const char* java_lang_Class::as_external_name(oop java_class) {
1032   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1033   const char* name = NULL;
1034   if (is_primitive(java_class)) {
1035     name = type2name(primitive_type(java_class));
1036   } else {
1037     name = as_Klass(java_class)->external_name();
1038   }
1039   if (name == NULL) {
1040     name = "<null>";
1041   }
1042   return name;
1043 }
1044 
1045 Klass* java_lang_Class::array_klass(oop java_class) {
1046   Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
1047   assert(k == NULL || k->is_klass() && k->is_array_klass(), "should be array klass");
1048   return k;
1049 }
1050 
1051 
1052 void java_lang_Class::set_array_klass(oop java_class, Klass* klass) {
1053   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1054   java_class->metadata_field_put(_array_klass_offset, klass);
1055 }
1056 
1057 
1058 bool java_lang_Class::is_primitive(oop java_class) {
1059   // should assert:
1060   //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1061   bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);
1062 
1063 #ifdef ASSERT
1064   if (is_primitive) {
1065     Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
1066     assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
1067         "Should be either the T_VOID primitive or a java primitive");
1068   }
1069 #endif
1070 
1071   return is_primitive;
1072 }
1073 
1074 
1075 BasicType java_lang_Class::primitive_type(oop java_class) {
1076   assert(java_lang_Class::is_primitive(java_class), "just checking");
1077   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1078   BasicType type = T_VOID;
1079   if (ak != NULL) {
1080     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1081     type = ArrayKlass::cast(ak)->element_type();
1082   } else {
1083     assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1084   }
1085   assert(Universe::java_mirror(type) == java_class, "must be consistent");
1086   return type;
1087 }
1088 
1089 BasicType java_lang_Class::as_BasicType(oop java_class, Klass** reference_klass) {
1090   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1091   if (is_primitive(java_class)) {
1092     if (reference_klass != NULL)
1093       (*reference_klass) = NULL;
1094     return primitive_type(java_class);
1095   } else {
1096     if (reference_klass != NULL)
1097       (*reference_klass) = as_Klass(java_class);
1098     return T_OBJECT;
1099   }
1100 }
1101 
1102 
1103 oop java_lang_Class::primitive_mirror(BasicType t) {
1104   oop mirror = Universe::java_mirror(t);
1105   assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
1106   assert(java_lang_Class::is_primitive(mirror), "must be primitive");
1107   return mirror;
1108 }
1109 
1110 bool java_lang_Class::offsets_computed = false;
1111 int  java_lang_Class::classRedefinedCount_offset = -1;
1112 
1113 void java_lang_Class::compute_offsets() {
1114   assert(!offsets_computed, "offsets should be initialized only once");
1115   offsets_computed = true;
1116 
1117   Klass* k = SystemDictionary::Class_klass();
1118   // The classRedefinedCount field is only present starting in 1.5,
1119   // so don't go fatal.
1120   compute_optional_offset(classRedefinedCount_offset,
1121                           k, vmSymbols::classRedefinedCount_name(), vmSymbols::int_signature());
1122 
1123   // Needs to be optional because the old build runs Queens during bootstrapping
1124   // and jdk8-9 doesn't have coordinated pushes yet.
1125   compute_optional_offset(_class_loader_offset,
1126                  k, vmSymbols::classLoader_name(),
1127                  vmSymbols::classloader_signature());
1128 
1129   compute_offset(_component_mirror_offset,
1130                  k, vmSymbols::componentType_name(),
1131                  vmSymbols::class_signature());
1132 
1133   compute_offset(_module_offset,
1134                  k, vmSymbols::module_name(),
1135                  vmSymbols::module_signature());
1136 
1137   // Init lock is a C union with component_mirror.  Only instanceKlass mirrors have
1138   // init_lock and only ArrayKlass mirrors have component_mirror.  Since both are oops
1139   // GC treats them the same.
1140   _init_lock_offset = _component_mirror_offset;
1141 
1142   CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1143 }
1144 
1145 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
1146   if (classRedefinedCount_offset == -1) {
1147     // If we don't have an offset for it then just return -1 as a marker.
1148     return -1;
1149   }
1150 
1151   return the_class_mirror->int_field(classRedefinedCount_offset);
1152 }
1153 
1154 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
1155   if (classRedefinedCount_offset == -1) {
1156     // If we don't have an offset for it then nothing to set.
1157     return;
1158   }
1159 
1160   the_class_mirror->int_field_put(classRedefinedCount_offset, value);
1161 }
1162 
1163 
1164 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1165 //       platform thread structure, and a eetop offset which was used for thread
1166 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1167 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1168 //       instead of the privateInfo_offset.
1169 //
1170 // Note: The stackSize field is only present starting in 1.4.
1171 
1172 int java_lang_Thread::_name_offset = 0;
1173 int java_lang_Thread::_group_offset = 0;
1174 int java_lang_Thread::_contextClassLoader_offset = 0;
1175 int java_lang_Thread::_inheritedAccessControlContext_offset = 0;
1176 int java_lang_Thread::_priority_offset = 0;
1177 int java_lang_Thread::_eetop_offset = 0;
1178 int java_lang_Thread::_daemon_offset = 0;
1179 int java_lang_Thread::_stillborn_offset = 0;
1180 int java_lang_Thread::_stackSize_offset = 0;
1181 int java_lang_Thread::_tid_offset = 0;
1182 int java_lang_Thread::_thread_status_offset = 0;
1183 int java_lang_Thread::_park_blocker_offset = 0;
1184 int java_lang_Thread::_park_event_offset = 0 ;
1185 
1186 
1187 void java_lang_Thread::compute_offsets() {
1188   assert(_group_offset == 0, "offsets should be initialized only once");
1189 
1190   Klass* k = SystemDictionary::Thread_klass();
1191   compute_offset(_name_offset,      k, vmSymbols::name_name(),      vmSymbols::string_signature());
1192   compute_offset(_group_offset,     k, vmSymbols::group_name(),     vmSymbols::threadgroup_signature());
1193   compute_offset(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), vmSymbols::classloader_signature());
1194   compute_offset(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), vmSymbols::accesscontrolcontext_signature());
1195   compute_offset(_priority_offset,  k, vmSymbols::priority_name(),  vmSymbols::int_signature());
1196   compute_offset(_daemon_offset,    k, vmSymbols::daemon_name(),    vmSymbols::bool_signature());
1197   compute_offset(_eetop_offset,     k, vmSymbols::eetop_name(),     vmSymbols::long_signature());
1198   compute_offset(_stillborn_offset, k, vmSymbols::stillborn_name(), vmSymbols::bool_signature());
1199   // The stackSize field is only present starting in 1.4, so don't go fatal.
1200   compute_optional_offset(_stackSize_offset, k, vmSymbols::stackSize_name(), vmSymbols::long_signature());
1201   // The tid and thread_status fields are only present starting in 1.5, so don't go fatal.
1202   compute_optional_offset(_tid_offset, k, vmSymbols::thread_id_name(), vmSymbols::long_signature());
1203   compute_optional_offset(_thread_status_offset, k, vmSymbols::thread_status_name(), vmSymbols::int_signature());
1204   // The parkBlocker field is only present starting in 1.6, so don't go fatal.
1205   compute_optional_offset(_park_blocker_offset, k, vmSymbols::park_blocker_name(), vmSymbols::object_signature());
1206   compute_optional_offset(_park_event_offset, k, vmSymbols::park_event_name(),
1207  vmSymbols::long_signature());
1208 }
1209 
1210 
1211 JavaThread* java_lang_Thread::thread(oop java_thread) {
1212   return (JavaThread*)java_thread->address_field(_eetop_offset);
1213 }
1214 
1215 
1216 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {
1217   java_thread->address_field_put(_eetop_offset, (address)thread);
1218 }
1219 
1220 
1221 oop java_lang_Thread::name(oop java_thread) {
1222   return java_thread->obj_field(_name_offset);
1223 }
1224 
1225 
1226 void java_lang_Thread::set_name(oop java_thread, oop name) {
1227   java_thread->obj_field_put(_name_offset, name);
1228 }
1229 
1230 
1231 ThreadPriority java_lang_Thread::priority(oop java_thread) {
1232   return (ThreadPriority)java_thread->int_field(_priority_offset);
1233 }
1234 
1235 
1236 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) {
1237   java_thread->int_field_put(_priority_offset, priority);
1238 }
1239 
1240 
1241 oop java_lang_Thread::threadGroup(oop java_thread) {
1242   return java_thread->obj_field(_group_offset);
1243 }
1244 
1245 
1246 bool java_lang_Thread::is_stillborn(oop java_thread) {
1247   return java_thread->bool_field(_stillborn_offset) != 0;
1248 }
1249 
1250 
1251 // We never have reason to turn the stillborn bit off
1252 void java_lang_Thread::set_stillborn(oop java_thread) {
1253   java_thread->bool_field_put(_stillborn_offset, true);
1254 }
1255 
1256 
1257 bool java_lang_Thread::is_alive(oop java_thread) {
1258   JavaThread* thr = java_lang_Thread::thread(java_thread);
1259   return (thr != NULL);
1260 }
1261 
1262 
1263 bool java_lang_Thread::is_daemon(oop java_thread) {
1264   return java_thread->bool_field(_daemon_offset) != 0;
1265 }
1266 
1267 
1268 void java_lang_Thread::set_daemon(oop java_thread) {
1269   java_thread->bool_field_put(_daemon_offset, true);
1270 }
1271 
1272 oop java_lang_Thread::context_class_loader(oop java_thread) {
1273   return java_thread->obj_field(_contextClassLoader_offset);
1274 }
1275 
1276 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
1277   return java_thread->obj_field(_inheritedAccessControlContext_offset);
1278 }
1279 
1280 
1281 jlong java_lang_Thread::stackSize(oop java_thread) {
1282   if (_stackSize_offset > 0) {
1283     return java_thread->long_field(_stackSize_offset);
1284   } else {
1285     return 0;
1286   }
1287 }
1288 
1289 // Write the thread status value to threadStatus field in java.lang.Thread java class.
1290 void java_lang_Thread::set_thread_status(oop java_thread,
1291                                          java_lang_Thread::ThreadStatus status) {
1292   // The threadStatus is only present starting in 1.5
1293   if (_thread_status_offset > 0) {
1294     java_thread->int_field_put(_thread_status_offset, status);
1295   }
1296 }
1297 
1298 // Read thread status value from threadStatus field in java.lang.Thread java class.
1299 java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1300   assert(Thread::current()->is_Watcher_thread() || Thread::current()->is_VM_thread() ||
1301          JavaThread::current()->thread_state() == _thread_in_vm,
1302          "Java Thread is not running in vm");
1303   // The threadStatus is only present starting in 1.5
1304   if (_thread_status_offset > 0) {
1305     return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1306   } else {
1307     // All we can easily figure out is if it is alive, but that is
1308     // enough info for a valid unknown status.
1309     // These aren't restricted to valid set ThreadStatus values, so
1310     // use JVMTI values and cast.
1311     JavaThread* thr = java_lang_Thread::thread(java_thread);
1312     if (thr == NULL) {
1313       // the thread hasn't run yet or is in the process of exiting
1314       return NEW;
1315     }
1316     return (java_lang_Thread::ThreadStatus)JVMTI_THREAD_STATE_ALIVE;
1317   }
1318 }
1319 
1320 
1321 jlong java_lang_Thread::thread_id(oop java_thread) {
1322   // The thread ID field is only present starting in 1.5
1323   if (_tid_offset > 0) {
1324     return java_thread->long_field(_tid_offset);
1325   } else {
1326     return 0;
1327   }
1328 }
1329 
1330 oop java_lang_Thread::park_blocker(oop java_thread) {
1331   assert(JDK_Version::current().supports_thread_park_blocker() &&
1332          _park_blocker_offset != 0, "Must support parkBlocker field");
1333 
1334   if (_park_blocker_offset > 0) {
1335     return java_thread->obj_field(_park_blocker_offset);
1336   }
1337 
1338   return NULL;
1339 }
1340 
1341 jlong java_lang_Thread::park_event(oop java_thread) {
1342   if (_park_event_offset > 0) {
1343     return java_thread->long_field(_park_event_offset);
1344   }
1345   return 0;
1346 }
1347 
1348 bool java_lang_Thread::set_park_event(oop java_thread, jlong ptr) {
1349   if (_park_event_offset > 0) {
1350     java_thread->long_field_put(_park_event_offset, ptr);
1351     return true;
1352   }
1353   return false;
1354 }
1355 
1356 
1357 const char* java_lang_Thread::thread_status_name(oop java_thread) {
1358   assert(_thread_status_offset != 0, "Must have thread status");
1359   ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1360   switch (status) {
1361     case NEW                      : return "NEW";
1362     case RUNNABLE                 : return "RUNNABLE";
1363     case SLEEPING                 : return "TIMED_WAITING (sleeping)";
1364     case IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
1365     case IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
1366     case PARKED                   : return "WAITING (parking)";
1367     case PARKED_TIMED             : return "TIMED_WAITING (parking)";
1368     case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1369     case TERMINATED               : return "TERMINATED";
1370     default                       : return "UNKNOWN";
1371   };
1372 }
1373 int java_lang_ThreadGroup::_parent_offset = 0;
1374 int java_lang_ThreadGroup::_name_offset = 0;
1375 int java_lang_ThreadGroup::_threads_offset = 0;
1376 int java_lang_ThreadGroup::_groups_offset = 0;
1377 int java_lang_ThreadGroup::_maxPriority_offset = 0;
1378 int java_lang_ThreadGroup::_destroyed_offset = 0;
1379 int java_lang_ThreadGroup::_daemon_offset = 0;
1380 int java_lang_ThreadGroup::_vmAllowSuspension_offset = 0;
1381 int java_lang_ThreadGroup::_nthreads_offset = 0;
1382 int java_lang_ThreadGroup::_ngroups_offset = 0;
1383 
1384 oop  java_lang_ThreadGroup::parent(oop java_thread_group) {
1385   assert(java_thread_group->is_oop(), "thread group must be oop");
1386   return java_thread_group->obj_field(_parent_offset);
1387 }
1388 
1389 // ("name as oop" accessor is not necessary)
1390 
1391 const char* java_lang_ThreadGroup::name(oop java_thread_group) {
1392   oop name = java_thread_group->obj_field(_name_offset);
1393   // ThreadGroup.name can be null
1394   if (name != NULL) {
1395     return java_lang_String::as_utf8_string(name);
1396   }
1397   return NULL;
1398 }
1399 
1400 int java_lang_ThreadGroup::nthreads(oop java_thread_group) {
1401   assert(java_thread_group->is_oop(), "thread group must be oop");
1402   return java_thread_group->int_field(_nthreads_offset);
1403 }
1404 
1405 objArrayOop java_lang_ThreadGroup::threads(oop java_thread_group) {
1406   oop threads = java_thread_group->obj_field(_threads_offset);
1407   assert(threads != NULL, "threadgroups should have threads");
1408   assert(threads->is_objArray(), "just checking"); // Todo: Add better type checking code
1409   return objArrayOop(threads);
1410 }
1411 
1412 int java_lang_ThreadGroup::ngroups(oop java_thread_group) {
1413   assert(java_thread_group->is_oop(), "thread group must be oop");
1414   return java_thread_group->int_field(_ngroups_offset);
1415 }
1416 
1417 objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) {
1418   oop groups = java_thread_group->obj_field(_groups_offset);
1419   assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code
1420   return objArrayOop(groups);
1421 }
1422 
1423 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) {
1424   assert(java_thread_group->is_oop(), "thread group must be oop");
1425   return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset);
1426 }
1427 
1428 bool java_lang_ThreadGroup::is_destroyed(oop java_thread_group) {
1429   assert(java_thread_group->is_oop(), "thread group must be oop");
1430   return java_thread_group->bool_field(_destroyed_offset) != 0;
1431 }
1432 
1433 bool java_lang_ThreadGroup::is_daemon(oop java_thread_group) {
1434   assert(java_thread_group->is_oop(), "thread group must be oop");
1435   return java_thread_group->bool_field(_daemon_offset) != 0;
1436 }
1437 
1438 bool java_lang_ThreadGroup::is_vmAllowSuspension(oop java_thread_group) {
1439   assert(java_thread_group->is_oop(), "thread group must be oop");
1440   return java_thread_group->bool_field(_vmAllowSuspension_offset) != 0;
1441 }
1442 
1443 void java_lang_ThreadGroup::compute_offsets() {
1444   assert(_parent_offset == 0, "offsets should be initialized only once");
1445 
1446   Klass* k = SystemDictionary::ThreadGroup_klass();
1447 
1448   compute_offset(_parent_offset,      k, vmSymbols::parent_name(),      vmSymbols::threadgroup_signature());
1449   compute_offset(_name_offset,        k, vmSymbols::name_name(),        vmSymbols::string_signature());
1450   compute_offset(_threads_offset,     k, vmSymbols::threads_name(),     vmSymbols::thread_array_signature());
1451   compute_offset(_groups_offset,      k, vmSymbols::groups_name(),      vmSymbols::threadgroup_array_signature());
1452   compute_offset(_maxPriority_offset, k, vmSymbols::maxPriority_name(), vmSymbols::int_signature());
1453   compute_offset(_destroyed_offset,   k, vmSymbols::destroyed_name(),   vmSymbols::bool_signature());
1454   compute_offset(_daemon_offset,      k, vmSymbols::daemon_name(),      vmSymbols::bool_signature());
1455   compute_offset(_vmAllowSuspension_offset, k, vmSymbols::vmAllowSuspension_name(), vmSymbols::bool_signature());
1456   compute_offset(_nthreads_offset,    k, vmSymbols::nthreads_name(),    vmSymbols::int_signature());
1457   compute_offset(_ngroups_offset,     k, vmSymbols::ngroups_name(),     vmSymbols::int_signature());
1458 }
1459 
1460 
1461 void java_lang_Throwable::compute_offsets() {
1462   Klass* k = SystemDictionary::Throwable_klass();
1463   compute_offset(depth_offset, k, vmSymbols::depth_name(), vmSymbols::int_signature());
1464 }
1465 
1466 oop java_lang_Throwable::unassigned_stacktrace() {
1467   InstanceKlass* ik = SystemDictionary::Throwable_klass();
1468   address addr = ik->static_field_addr(static_unassigned_stacktrace_offset);
1469   if (UseCompressedOops) {
1470     return oopDesc::load_decode_heap_oop((narrowOop *)addr);
1471   } else {
1472     return oopDesc::load_decode_heap_oop((oop*)addr);
1473   }
1474 }
1475 
1476 oop java_lang_Throwable::backtrace(oop throwable) {
1477   return throwable->obj_field_acquire(backtrace_offset);
1478 }
1479 
1480 
1481 void java_lang_Throwable::set_backtrace(oop throwable, oop value) {
1482   throwable->release_obj_field_put(backtrace_offset, value);
1483 }
1484 
1485 int java_lang_Throwable::depth(oop throwable) {
1486   return throwable->int_field(depth_offset);
1487 }
1488 
1489 void java_lang_Throwable::set_depth(oop throwable, int value) {
1490   throwable->int_field_put(depth_offset, value);
1491 }
1492 
1493 oop java_lang_Throwable::message(Handle throwable) {
1494   return throwable->obj_field(detailMessage_offset);
1495 }
1496 
1497 
1498 // Return Symbol for detailed_message or NULL
1499 Symbol* java_lang_Throwable::detail_message(oop throwable) {
1500   PRESERVE_EXCEPTION_MARK;  // Keep original exception
1501   oop detailed_message = java_lang_Throwable::message(throwable);
1502   if (detailed_message != NULL) {
1503     return java_lang_String::as_symbol(detailed_message, THREAD);
1504   }
1505   return NULL;
1506 }
1507 
1508 void java_lang_Throwable::set_message(oop throwable, oop value) {
1509   throwable->obj_field_put(detailMessage_offset, value);
1510 }
1511 
1512 
1513 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
1514   throwable->obj_field_put(stackTrace_offset, st_element_array);
1515 }
1516 
1517 void java_lang_Throwable::clear_stacktrace(oop throwable) {
1518   set_stacktrace(throwable, NULL);
1519 }
1520 
1521 
1522 void java_lang_Throwable::print(Handle throwable, outputStream* st) {
1523   ResourceMark rm;
1524   Klass* k = throwable->klass();
1525   assert(k != NULL, "just checking");
1526   st->print("%s", k->external_name());
1527   oop msg = message(throwable);
1528   if (msg != NULL) {
1529     st->print(": %s", java_lang_String::as_utf8_string(msg));
1530   }
1531 }
1532 
1533 // After this many redefines, the stack trace is unreliable.
1534 const int MAX_VERSION = USHRT_MAX;
1535 
1536 static inline bool version_matches(Method* method, int version) {
1537   assert(version < MAX_VERSION, "version is too big");
1538   return method != NULL && (method->constants()->version() == version);
1539 }
1540 
1541 
1542 // This class provides a simple wrapper over the internal structure of
1543 // exception backtrace to insulate users of the backtrace from needing
1544 // to know what it looks like.
1545 class BacktraceBuilder: public StackObj {
1546  friend class BacktraceIterator;
1547  private:
1548   Handle          _backtrace;
1549   objArrayOop     _head;
1550   typeArrayOop    _methods;
1551   typeArrayOop    _bcis;
1552   objArrayOop     _mirrors;
1553   typeArrayOop    _cprefs; // needed to insulate method name against redefinition
1554   int             _index;
1555   NoSafepointVerifier _nsv;
1556 
1557   enum {
1558     trace_methods_offset = java_lang_Throwable::trace_methods_offset,
1559     trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
1560     trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
1561     trace_cprefs_offset  = java_lang_Throwable::trace_cprefs_offset,
1562     trace_next_offset    = java_lang_Throwable::trace_next_offset,
1563     trace_size           = java_lang_Throwable::trace_size,
1564     trace_chunk_size     = java_lang_Throwable::trace_chunk_size
1565   };
1566 
1567   // get info out of chunks
1568   static typeArrayOop get_methods(objArrayHandle chunk) {
1569     typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
1570     assert(methods != NULL, "method array should be initialized in backtrace");
1571     return methods;
1572   }
1573   static typeArrayOop get_bcis(objArrayHandle chunk) {
1574     typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
1575     assert(bcis != NULL, "bci array should be initialized in backtrace");
1576     return bcis;
1577   }
1578   static objArrayOop get_mirrors(objArrayHandle chunk) {
1579     objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
1580     assert(mirrors != NULL, "mirror array should be initialized in backtrace");
1581     return mirrors;
1582   }
1583   static typeArrayOop get_cprefs(objArrayHandle chunk) {
1584     typeArrayOop cprefs = typeArrayOop(chunk->obj_at(trace_cprefs_offset));
1585     assert(cprefs != NULL, "cprefs array should be initialized in backtrace");
1586     return cprefs;
1587   }
1588 
1589  public:
1590 
1591   // constructor for new backtrace
1592   BacktraceBuilder(TRAPS): _methods(NULL), _bcis(NULL), _head(NULL), _mirrors(NULL), _cprefs(NULL) {
1593     expand(CHECK);
1594     _backtrace = _head;
1595     _index = 0;
1596   }
1597 
1598   BacktraceBuilder(objArrayHandle backtrace) {
1599     _methods = get_methods(backtrace);
1600     _bcis = get_bcis(backtrace);
1601     _mirrors = get_mirrors(backtrace);
1602     _cprefs = get_cprefs(backtrace);
1603     assert(_methods->length() == _bcis->length() &&
1604            _methods->length() == _mirrors->length(),
1605            "method and source information arrays should match");
1606 
1607     // head is the preallocated backtrace
1608     _backtrace = _head = backtrace();
1609     _index = 0;
1610   }
1611 
1612   void expand(TRAPS) {
1613     objArrayHandle old_head(THREAD, _head);
1614     PauseNoSafepointVerifier pnsv(&_nsv);
1615 
1616     objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
1617     objArrayHandle new_head(THREAD, head);
1618 
1619     typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
1620     typeArrayHandle new_methods(THREAD, methods);
1621 
1622     typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
1623     typeArrayHandle new_bcis(THREAD, bcis);
1624 
1625     objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
1626     objArrayHandle new_mirrors(THREAD, mirrors);
1627 
1628     typeArrayOop cprefs = oopFactory::new_shortArray(trace_chunk_size, CHECK);
1629     typeArrayHandle new_cprefs(THREAD, cprefs);
1630 
1631     if (!old_head.is_null()) {
1632       old_head->obj_at_put(trace_next_offset, new_head());
1633     }
1634     new_head->obj_at_put(trace_methods_offset, new_methods());
1635     new_head->obj_at_put(trace_bcis_offset, new_bcis());
1636     new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
1637     new_head->obj_at_put(trace_cprefs_offset, new_cprefs());
1638 
1639     _head    = new_head();
1640     _methods = new_methods();
1641     _bcis = new_bcis();
1642     _mirrors = new_mirrors();
1643     _cprefs  = new_cprefs();
1644     _index = 0;
1645   }
1646 
1647   oop backtrace() {
1648     return _backtrace();
1649   }
1650 
1651   inline void push(Method* method, int bci, TRAPS) {
1652     // Smear the -1 bci to 0 since the array only holds unsigned
1653     // shorts.  The later line number lookup would just smear the -1
1654     // to a 0 even if it could be recorded.
1655     if (bci == SynchronizationEntryBCI) bci = 0;
1656 
1657     if (_index >= trace_chunk_size) {
1658       methodHandle mhandle(THREAD, method);
1659       expand(CHECK);
1660       method = mhandle();
1661     }
1662 
1663     _methods->short_at_put(_index, method->orig_method_idnum());
1664     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
1665     _cprefs->short_at_put(_index, method->name_index());
1666 
1667     // We need to save the mirrors in the backtrace to keep the class
1668     // from being unloaded while we still have this stack trace.
1669     assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
1670     _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());
1671     _index++;
1672   }
1673 
1674 };
1675 
1676 struct BacktraceElement : public StackObj {
1677   int _method_id;
1678   int _bci;
1679   int _version;
1680   int _cpref;
1681   Handle _mirror;
1682   BacktraceElement(Handle mirror, int mid, int version, int bci, int cpref) :
1683                    _mirror(mirror), _method_id(mid), _version(version), _bci(bci), _cpref(cpref) {}
1684 };
1685 
1686 class BacktraceIterator : public StackObj {
1687   int _index;
1688   objArrayHandle  _result;
1689   objArrayHandle  _mirrors;
1690   typeArrayHandle _methods;
1691   typeArrayHandle _bcis;
1692   typeArrayHandle _cprefs;
1693 
1694   void init(objArrayHandle result, Thread* thread) {
1695     // Get method id, bci, version and mirror from chunk
1696     _result = result;
1697     if (_result.not_null()) {
1698       _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result));
1699       _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result));
1700       _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result));
1701       _cprefs = typeArrayHandle(thread, BacktraceBuilder::get_cprefs(_result));
1702       _index = 0;
1703     }
1704   }
1705  public:
1706   BacktraceIterator(objArrayHandle result, Thread* thread) {
1707     init(result, thread);
1708     assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match");
1709   }
1710 
1711   BacktraceElement next(Thread* thread) {
1712     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
1713                         _methods->short_at(_index),
1714                         Backtrace::version_at(_bcis->int_at(_index)),
1715                         Backtrace::bci_at(_bcis->int_at(_index)),
1716                         _cprefs->short_at(_index));
1717     _index++;
1718 
1719     if (_index >= java_lang_Throwable::trace_chunk_size) {
1720       int next_offset = java_lang_Throwable::trace_next_offset;
1721       // Get next chunk
1722       objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset)));
1723       init(result, thread);
1724     }
1725     return e;
1726   }
1727 
1728   bool repeat() {
1729     return _result.not_null() && _mirrors->obj_at(_index) != NULL;
1730   }
1731 };
1732 
1733 
1734 // Print stack trace element to resource allocated buffer
1735 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
1736                                           int version, int bci, int cpref) {
1737   ResourceMark rm;
1738 
1739   // Get strings and string lengths
1740   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
1741   const char* klass_name  = holder->external_name();
1742   int buf_len = (int)strlen(klass_name);
1743 
1744   Method* method = holder->method_with_orig_idnum(method_id, version);
1745 
1746   // The method can be NULL if the requested class version is gone
1747   Symbol* sym = (method != NULL) ? method->name() : holder->constants()->symbol_at(cpref);
1748   char* method_name = sym->as_C_string();
1749   buf_len += (int)strlen(method_name);
1750 
1751   char* source_file_name = NULL;
1752   Symbol* source = Backtrace::get_source_file_name(holder, version);
1753   if (source != NULL) {
1754     source_file_name = source->as_C_string();
1755     buf_len += (int)strlen(source_file_name);
1756   }
1757 
1758   char *module_name = NULL, *module_version = NULL;
1759   ModuleEntry* module = holder->module();
1760   if (module->is_named()) {
1761     module_name = module->name()->as_C_string();
1762     buf_len += (int)strlen(module_name);
1763     if (module->version() != NULL) {
1764       module_version = module->version()->as_C_string();
1765       buf_len += (int)strlen(module_version);
1766     }
1767   }
1768 
1769   // Allocate temporary buffer with extra space for formatting and line number
1770   char* buf = NEW_RESOURCE_ARRAY(char, buf_len + 64);
1771 
1772   // Print stack trace line in buffer
1773   sprintf(buf, "\tat %s.%s(", klass_name, method_name);
1774 
1775   // Print module information
1776   if (module_name != NULL) {
1777     if (module_version != NULL) {
1778       sprintf(buf + (int)strlen(buf), "%s@%s/", module_name, module_version);
1779     } else {
1780       sprintf(buf + (int)strlen(buf), "%s/", module_name);
1781     }
1782   }
1783 
1784   if (!version_matches(method, version)) {
1785     strcat(buf, "Redefined)");
1786   } else {
1787     int line_number = Backtrace::get_line_number(method, bci);
1788     if (line_number == -2) {
1789       strcat(buf, "Native Method)");
1790     } else {
1791       if (source_file_name != NULL && (line_number != -1)) {
1792         // Sourcename and linenumber
1793         sprintf(buf + (int)strlen(buf), "%s:%d)", source_file_name, line_number);
1794       } else if (source_file_name != NULL) {
1795         // Just sourcename
1796         sprintf(buf + (int)strlen(buf), "%s)", source_file_name);
1797       } else {
1798         // Neither sourcename nor linenumber
1799         sprintf(buf + (int)strlen(buf), "Unknown Source)");
1800       }
1801       CompiledMethod* nm = method->code();
1802       if (WizardMode && nm != NULL) {
1803         sprintf(buf + (int)strlen(buf), "(nmethod " INTPTR_FORMAT ")", (intptr_t)nm);
1804       }
1805     }
1806   }
1807 
1808   st->print_cr("%s", buf);
1809 }
1810 
1811 void java_lang_Throwable::print_stack_element(outputStream *st, const methodHandle& method, int bci) {
1812   Handle mirror = method->method_holder()->java_mirror();
1813   int method_id = method->orig_method_idnum();
1814   int version = method->constants()->version();
1815   int cpref = method->name_index();
1816   print_stack_element_to_stream(st, mirror, method_id, version, bci, cpref);
1817 }
1818 
1819 /**
1820  * Print the throwable message and its stack trace plus all causes by walking the
1821  * cause chain.  The output looks the same as of Throwable.printStackTrace().
1822  */
1823 void java_lang_Throwable::print_stack_trace(Handle throwable, outputStream* st) {
1824   // First, print the message.
1825   print(throwable, st);
1826   st->cr();
1827 
1828   // Now print the stack trace.
1829   Thread* THREAD = Thread::current();
1830   while (throwable.not_null()) {
1831     objArrayHandle result (THREAD, objArrayOop(backtrace(throwable())));
1832     if (result.is_null()) {
1833       st->print_raw_cr("\t<<no stack trace available>>");
1834       return;
1835     }
1836     BacktraceIterator iter(result, THREAD);
1837 
1838     while (iter.repeat()) {
1839       BacktraceElement bte = iter.next(THREAD);
1840       print_stack_element_to_stream(st, bte._mirror, bte._method_id, bte._version, bte._bci, bte._cpref);
1841     }
1842     {
1843       // Call getCause() which doesn't necessarily return the _cause field.
1844       EXCEPTION_MARK;
1845       JavaValue cause(T_OBJECT);
1846       JavaCalls::call_virtual(&cause,
1847                               throwable,
1848                               KlassHandle(THREAD, throwable->klass()),
1849                               vmSymbols::getCause_name(),
1850                               vmSymbols::void_throwable_signature(),
1851                               THREAD);
1852       // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM.
1853       if (HAS_PENDING_EXCEPTION) {
1854         CLEAR_PENDING_EXCEPTION;
1855         throwable = Handle();
1856       } else {
1857         throwable = Handle(THREAD, (oop) cause.get_jobject());
1858         if (throwable.not_null()) {
1859           st->print("Caused by: ");
1860           print(throwable, st);
1861           st->cr();
1862         }
1863       }
1864     }
1865   }
1866 }
1867 
1868 /**
1869  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
1870  */
1871 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
1872   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "Throwable instance expected");
1873   JavaValue result(T_VOID);
1874   JavaCalls::call_virtual(&result,
1875                           throwable,
1876                           KlassHandle(THREAD, SystemDictionary::Throwable_klass()),
1877                           vmSymbols::printStackTrace_name(),
1878                           vmSymbols::void_method_signature(),
1879                           THREAD);
1880 }
1881 
1882 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {
1883   if (!StackTraceInThrowable) return;
1884   ResourceMark rm(THREAD);
1885 
1886   // Start out by clearing the backtrace for this object, in case the VM
1887   // runs out of memory while allocating the stack trace
1888   set_backtrace(throwable(), NULL);
1889   // Clear lazily constructed Java level stacktrace if refilling occurs
1890   // This is unnecessary in 1.7+ but harmless
1891   clear_stacktrace(throwable());
1892 
1893   int max_depth = MaxJavaStackTraceDepth;
1894   JavaThread* thread = (JavaThread*)THREAD;
1895 
1896   BacktraceBuilder bt(CHECK);
1897 
1898   // If there is no Java frame just return the method that was being called
1899   // with bci 0
1900   if (!thread->has_last_Java_frame()) {
1901     if (max_depth >= 1 && method() != NULL) {
1902       bt.push(method(), 0, CHECK);
1903       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
1904       set_depth(throwable(), 1);
1905       set_backtrace(throwable(), bt.backtrace());
1906     }
1907     return;
1908   }
1909 
1910   // Instead of using vframe directly, this version of fill_in_stack_trace
1911   // basically handles everything by hand. This significantly improved the
1912   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
1913   // See bug 6333838 for  more details.
1914   // The "ASSERT" here is to verify this method generates the exactly same stack
1915   // trace as utilizing vframe.
1916 #ifdef ASSERT
1917   vframeStream st(thread);
1918   methodHandle st_method(THREAD, st.method());
1919 #endif
1920   int total_count = 0;
1921   RegisterMap map(thread, false);
1922   int decode_offset = 0;
1923   CompiledMethod* nm = NULL;
1924   bool skip_fillInStackTrace_check = false;
1925   bool skip_throwableInit_check = false;
1926   bool skip_hidden = !ShowHiddenFrames;
1927 
1928   for (frame fr = thread->last_frame(); max_depth != total_count;) {
1929     Method* method = NULL;
1930     int bci = 0;
1931 
1932     // Compiled java method case.
1933     if (decode_offset != 0) {
1934       DebugInfoReadStream stream(nm, decode_offset);
1935       decode_offset = stream.read_int();
1936       method = (Method*)nm->metadata_at(stream.read_int());
1937       bci = stream.read_bci();
1938     } else {
1939       if (fr.is_first_frame()) break;
1940       address pc = fr.pc();
1941       if (fr.is_interpreted_frame()) {
1942         address bcp = fr.interpreter_frame_bcp();
1943         method = fr.interpreter_frame_method();
1944         bci =  method->bci_from(bcp);
1945         fr = fr.sender(&map);
1946       } else {
1947         CodeBlob* cb = fr.cb();
1948         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
1949         // but non nmethod
1950         fr = fr.sender(&map);
1951         if (cb == NULL || !cb->is_compiled()) {
1952           continue;
1953         }
1954         nm = cb->as_compiled_method();
1955         if (nm->method()->is_native()) {
1956           method = nm->method();
1957           bci = 0;
1958         } else {
1959           PcDesc* pd = nm->pc_desc_at(pc);
1960           decode_offset = pd->scope_decode_offset();
1961           // if decode_offset is not equal to 0, it will execute the
1962           // "compiled java method case" at the beginning of the loop.
1963           continue;
1964         }
1965       }
1966     }
1967 #ifdef ASSERT
1968     assert(st_method() == method && st.bci() == bci,
1969            "Wrong stack trace");
1970     st.next();
1971     // vframeStream::method isn't GC-safe so store off a copy
1972     // of the Method* in case we GC.
1973     if (!st.at_end()) {
1974       st_method = st.method();
1975     }
1976 #endif
1977 
1978     // the format of the stacktrace will be:
1979     // - 1 or more fillInStackTrace frames for the exception class (skipped)
1980     // - 0 or more <init> methods for the exception class (skipped)
1981     // - rest of the stack
1982 
1983     if (!skip_fillInStackTrace_check) {
1984       if (method->name() == vmSymbols::fillInStackTrace_name() &&
1985           throwable->is_a(method->method_holder())) {
1986         continue;
1987       }
1988       else {
1989         skip_fillInStackTrace_check = true; // gone past them all
1990       }
1991     }
1992     if (!skip_throwableInit_check) {
1993       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
1994 
1995       // skip <init> methods of the exception class and superclasses
1996       // This is simlar to classic VM.
1997       if (method->name() == vmSymbols::object_initializer_name() &&
1998           throwable->is_a(method->method_holder())) {
1999         continue;
2000       } else {
2001         // there are none or we've seen them all - either way stop checking
2002         skip_throwableInit_check = true;
2003       }
2004     }
2005     if (method->is_hidden()) {
2006       if (skip_hidden)  continue;
2007     }
2008     bt.push(method, bci, CHECK);
2009     total_count++;
2010   }
2011 
2012   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2013 
2014   // Put completed stack trace into throwable object
2015   set_backtrace(throwable(), bt.backtrace());
2016   set_depth(throwable(), total_count);
2017 }
2018 
2019 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
2020   // No-op if stack trace is disabled
2021   if (!StackTraceInThrowable) {
2022     return;
2023   }
2024 
2025   // Disable stack traces for some preallocated out of memory errors
2026   if (!Universe::should_fill_in_stack_trace(throwable)) {
2027     return;
2028   }
2029 
2030   PRESERVE_EXCEPTION_MARK;
2031 
2032   JavaThread* thread = JavaThread::active();
2033   fill_in_stack_trace(throwable, method, thread);
2034   // ignore exceptions thrown during stack trace filling
2035   CLEAR_PENDING_EXCEPTION;
2036 }
2037 
2038 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
2039   // Allocate stack trace - backtrace is created but not filled in
2040 
2041   // No-op if stack trace is disabled
2042   if (!StackTraceInThrowable) return;
2043   BacktraceBuilder bt(CHECK);   // creates a backtrace
2044   set_backtrace(throwable(), bt.backtrace());
2045 }
2046 
2047 
2048 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
2049   // Fill in stack trace into preallocated backtrace (no GC)
2050 
2051   // No-op if stack trace is disabled
2052   if (!StackTraceInThrowable) return;
2053 
2054   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "sanity check");
2055 
2056   JavaThread* THREAD = JavaThread::current();
2057 
2058   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2059   assert(backtrace.not_null(), "backtrace should have been preallocated");
2060 
2061   ResourceMark rm(THREAD);
2062   vframeStream st(THREAD);
2063 
2064   BacktraceBuilder bt(backtrace);
2065 
2066   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2067   // methods as preallocated errors aren't created by "java" code.
2068 
2069   // fill in as much stack trace as possible
2070   int chunk_count = 0;
2071   for (;!st.at_end(); st.next()) {
2072     bt.push(st.method(), st.bci(), CHECK);
2073     chunk_count++;
2074 
2075     // Bail-out for deep stacks
2076     if (chunk_count >= trace_chunk_size) break;
2077   }
2078   set_depth(throwable(), chunk_count);
2079   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2080 
2081   // We support the Throwable immutability protocol defined for Java 7.
2082   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
2083   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
2084 }
2085 
2086 void java_lang_Throwable::get_stack_trace_elements(Handle throwable,
2087                                                    objArrayHandle stack_trace_array_h, TRAPS) {
2088 
2089   if (throwable.is_null() || stack_trace_array_h.is_null()) {
2090     THROW(vmSymbols::java_lang_NullPointerException());
2091   }
2092 
2093   assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent");
2094 
2095   if (stack_trace_array_h->length() != depth(throwable())) {
2096     THROW(vmSymbols::java_lang_IndexOutOfBoundsException());
2097   }
2098 
2099   objArrayHandle result(THREAD, objArrayOop(backtrace(throwable())));
2100   BacktraceIterator iter(result, THREAD);
2101 
2102   int index = 0;
2103   while (iter.repeat()) {
2104     BacktraceElement bte = iter.next(THREAD);
2105 
2106     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
2107 
2108     if (stack_trace_element.is_null()) {
2109       THROW(vmSymbols::java_lang_NullPointerException());
2110     }
2111 
2112     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
2113     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
2114 
2115     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
2116                                          method,
2117                                          bte._version,
2118                                          bte._bci,
2119                                          bte._cpref, CHECK);
2120   }
2121 }
2122 
2123 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) {
2124   // Allocate java.lang.StackTraceElement instance
2125   Klass* k = SystemDictionary::StackTraceElement_klass();
2126   assert(k != NULL, "must be loaded in 1.4+");
2127   instanceKlassHandle ik (THREAD, k);
2128   if (ik->should_be_initialized()) {
2129     ik->initialize(CHECK_0);
2130   }
2131 
2132   Handle element = ik->allocate_instance_handle(CHECK_0);
2133 
2134   int cpref = method->name_index();
2135   int version = method->constants()->version();
2136   fill_in(element, method->method_holder(), method, version, bci, cpref, CHECK_0);
2137   return element();
2138 }
2139 
2140 void java_lang_StackTraceElement::fill_in(Handle element,
2141                                           InstanceKlass* holder, const methodHandle& method,
2142                                           int version, int bci, int cpref, TRAPS) {
2143   assert(element->is_a(SystemDictionary::StackTraceElement_klass()), "sanity check");
2144 
2145   // Fill in class name
2146   ResourceMark rm(THREAD);
2147   const char* str = holder->external_name();
2148   oop classname = StringTable::intern((char*) str, CHECK);
2149   java_lang_StackTraceElement::set_declaringClass(element(), classname);
2150 
2151   // The method can be NULL if the requested class version is gone
2152   Symbol* sym = !method.is_null() ? method->name() : holder->constants()->symbol_at(cpref);
2153 
2154   // Fill in method name
2155   oop methodname = StringTable::intern(sym, CHECK);
2156   java_lang_StackTraceElement::set_methodName(element(), methodname);
2157 
2158   // Fill in module name and version
2159   ModuleEntry* module = holder->module();
2160   if (module->is_named()) {
2161     oop module_name = StringTable::intern(module->name(), CHECK);
2162     java_lang_StackTraceElement::set_moduleName(element(), module_name);
2163     oop module_version;
2164     if (module->version() != NULL) {
2165       module_version = StringTable::intern(module->version(), CHECK);
2166     } else {
2167       module_version = NULL;
2168     }
2169     java_lang_StackTraceElement::set_moduleVersion(element(), module_version);
2170   }
2171 
2172   if (!version_matches(method(), version)) {
2173     // The method was redefined, accurate line number information isn't available
2174     java_lang_StackTraceElement::set_fileName(element(), NULL);
2175     java_lang_StackTraceElement::set_lineNumber(element(), -1);
2176   } else {
2177     // Fill in source file name and line number.
2178     Symbol* source = Backtrace::get_source_file_name(holder, version);
2179     if (ShowHiddenFrames && source == NULL)
2180       source = vmSymbols::unknown_class_name();
2181     oop filename = StringTable::intern(source, CHECK);
2182     java_lang_StackTraceElement::set_fileName(element(), filename);
2183 
2184     int line_number = Backtrace::get_line_number(method, bci);
2185     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2186   }
2187 }
2188 
2189 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2190   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2191   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2192   // we should expand MemberName::name when Throwable uses StackTrace
2193   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2194   return method;
2195 }
2196 
2197 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci) {
2198   // set Method* or mid/cpref
2199   oop mname = stackFrame->obj_field(_memberName_offset);
2200   InstanceKlass* ik = method->method_holder();
2201   CallInfo info(method(), ik);
2202   MethodHandles::init_method_MemberName(mname, info);
2203   // set bci
2204   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2205   // method may be redefined; store the version
2206   int version = method->constants()->version();
2207   assert((jushort)version == version, "version should be short");
2208   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);
2209 }
2210 
2211 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
2212   ResourceMark rm(THREAD);
2213   Handle k (THREAD, stackFrame->obj_field(_declaringClass_offset));
2214   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(k()));
2215   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);
2216 
2217   short version = stackFrame->short_field(_version_offset);
2218   short bci = stackFrame->short_field(_bci_offset);
2219   int cpref = method->name_index();
2220   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, method, version, bci, cpref, CHECK);
2221 }
2222 
2223 void java_lang_StackFrameInfo::compute_offsets() {
2224   Klass* k = SystemDictionary::StackFrameInfo_klass();
2225   compute_offset(_declaringClass_offset, k, vmSymbols::declaringClass_name(),  vmSymbols::class_signature());
2226   compute_offset(_memberName_offset,     k, vmSymbols::memberName_name(),  vmSymbols::object_signature());
2227   compute_offset(_bci_offset,            k, vmSymbols::bci_name(),         vmSymbols::short_signature());
2228   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2229 }
2230 
2231 void java_lang_LiveStackFrameInfo::compute_offsets() {
2232   Klass* k = SystemDictionary::LiveStackFrameInfo_klass();
2233   compute_offset(_monitors_offset,   k, vmSymbols::monitors_name(),    vmSymbols::object_array_signature());
2234   compute_offset(_locals_offset,     k, vmSymbols::locals_name(),      vmSymbols::object_array_signature());
2235   compute_offset(_operands_offset,   k, vmSymbols::operands_name(),    vmSymbols::object_array_signature());
2236 }
2237 
2238 void java_lang_reflect_AccessibleObject::compute_offsets() {
2239   Klass* k = SystemDictionary::reflect_AccessibleObject_klass();
2240   compute_offset(override_offset, k, vmSymbols::override_name(), vmSymbols::bool_signature());
2241 }
2242 
2243 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
2244   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2245   return (jboolean) reflect->bool_field(override_offset);
2246 }
2247 
2248 void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) {
2249   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2250   reflect->bool_field_put(override_offset, (int) value);
2251 }
2252 
2253 void java_lang_reflect_Method::compute_offsets() {
2254   Klass* k = SystemDictionary::reflect_Method_klass();
2255   compute_offset(clazz_offset,          k, vmSymbols::clazz_name(),          vmSymbols::class_signature());
2256   compute_offset(name_offset,           k, vmSymbols::name_name(),           vmSymbols::string_signature());
2257   compute_offset(returnType_offset,     k, vmSymbols::returnType_name(),     vmSymbols::class_signature());
2258   compute_offset(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), vmSymbols::class_array_signature());
2259   compute_offset(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), vmSymbols::class_array_signature());
2260   compute_offset(slot_offset,           k, vmSymbols::slot_name(),           vmSymbols::int_signature());
2261   compute_offset(modifiers_offset,      k, vmSymbols::modifiers_name(),      vmSymbols::int_signature());
2262   // The generic signature and annotations fields are only present in 1.5
2263   signature_offset = -1;
2264   annotations_offset = -1;
2265   parameter_annotations_offset = -1;
2266   annotation_default_offset = -1;
2267   type_annotations_offset = -1;
2268   compute_optional_offset(signature_offset,             k, vmSymbols::signature_name(),             vmSymbols::string_signature());
2269   compute_optional_offset(annotations_offset,           k, vmSymbols::annotations_name(),           vmSymbols::byte_array_signature());
2270   compute_optional_offset(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), vmSymbols::byte_array_signature());
2271   compute_optional_offset(annotation_default_offset,    k, vmSymbols::annotation_default_name(),    vmSymbols::byte_array_signature());
2272   compute_optional_offset(type_annotations_offset,      k, vmSymbols::type_annotations_name(),      vmSymbols::byte_array_signature());
2273 }
2274 
2275 Handle java_lang_reflect_Method::create(TRAPS) {
2276   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2277   Klass* klass = SystemDictionary::reflect_Method_klass();
2278   // This class is eagerly initialized during VM initialization, since we keep a refence
2279   // to one of the methods
2280   assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized");
2281   return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD);
2282 }
2283 
2284 oop java_lang_reflect_Method::clazz(oop reflect) {
2285   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2286   return reflect->obj_field(clazz_offset);
2287 }
2288 
2289 void java_lang_reflect_Method::set_clazz(oop reflect, oop value) {
2290   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2291    reflect->obj_field_put(clazz_offset, value);
2292 }
2293 
2294 int java_lang_reflect_Method::slot(oop reflect) {
2295   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2296   return reflect->int_field(slot_offset);
2297 }
2298 
2299 void java_lang_reflect_Method::set_slot(oop reflect, int value) {
2300   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2301   reflect->int_field_put(slot_offset, value);
2302 }
2303 
2304 oop java_lang_reflect_Method::name(oop method) {
2305   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2306   return method->obj_field(name_offset);
2307 }
2308 
2309 void java_lang_reflect_Method::set_name(oop method, oop value) {
2310   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2311   method->obj_field_put(name_offset, value);
2312 }
2313 
2314 oop java_lang_reflect_Method::return_type(oop method) {
2315   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2316   return method->obj_field(returnType_offset);
2317 }
2318 
2319 void java_lang_reflect_Method::set_return_type(oop method, oop value) {
2320   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2321   method->obj_field_put(returnType_offset, value);
2322 }
2323 
2324 oop java_lang_reflect_Method::parameter_types(oop method) {
2325   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2326   return method->obj_field(parameterTypes_offset);
2327 }
2328 
2329 void java_lang_reflect_Method::set_parameter_types(oop method, oop value) {
2330   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2331   method->obj_field_put(parameterTypes_offset, value);
2332 }
2333 
2334 oop java_lang_reflect_Method::exception_types(oop method) {
2335   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2336   return method->obj_field(exceptionTypes_offset);
2337 }
2338 
2339 void java_lang_reflect_Method::set_exception_types(oop method, oop value) {
2340   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2341   method->obj_field_put(exceptionTypes_offset, value);
2342 }
2343 
2344 int java_lang_reflect_Method::modifiers(oop method) {
2345   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2346   return method->int_field(modifiers_offset);
2347 }
2348 
2349 void java_lang_reflect_Method::set_modifiers(oop method, int value) {
2350   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2351   method->int_field_put(modifiers_offset, value);
2352 }
2353 
2354 bool java_lang_reflect_Method::has_signature_field() {
2355   return (signature_offset >= 0);
2356 }
2357 
2358 oop java_lang_reflect_Method::signature(oop method) {
2359   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2360   assert(has_signature_field(), "signature field must be present");
2361   return method->obj_field(signature_offset);
2362 }
2363 
2364 void java_lang_reflect_Method::set_signature(oop method, oop value) {
2365   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2366   assert(has_signature_field(), "signature field must be present");
2367   method->obj_field_put(signature_offset, value);
2368 }
2369 
2370 bool java_lang_reflect_Method::has_annotations_field() {
2371   return (annotations_offset >= 0);
2372 }
2373 
2374 oop java_lang_reflect_Method::annotations(oop method) {
2375   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2376   assert(has_annotations_field(), "annotations field must be present");
2377   return method->obj_field(annotations_offset);
2378 }
2379 
2380 void java_lang_reflect_Method::set_annotations(oop method, oop value) {
2381   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2382   assert(has_annotations_field(), "annotations field must be present");
2383   method->obj_field_put(annotations_offset, value);
2384 }
2385 
2386 bool java_lang_reflect_Method::has_parameter_annotations_field() {
2387   return (parameter_annotations_offset >= 0);
2388 }
2389 
2390 oop java_lang_reflect_Method::parameter_annotations(oop method) {
2391   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2392   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2393   return method->obj_field(parameter_annotations_offset);
2394 }
2395 
2396 void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) {
2397   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2398   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2399   method->obj_field_put(parameter_annotations_offset, value);
2400 }
2401 
2402 bool java_lang_reflect_Method::has_annotation_default_field() {
2403   return (annotation_default_offset >= 0);
2404 }
2405 
2406 oop java_lang_reflect_Method::annotation_default(oop method) {
2407   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2408   assert(has_annotation_default_field(), "annotation default field must be present");
2409   return method->obj_field(annotation_default_offset);
2410 }
2411 
2412 void java_lang_reflect_Method::set_annotation_default(oop method, oop value) {
2413   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2414   assert(has_annotation_default_field(), "annotation default field must be present");
2415   method->obj_field_put(annotation_default_offset, value);
2416 }
2417 
2418 bool java_lang_reflect_Method::has_type_annotations_field() {
2419   return (type_annotations_offset >= 0);
2420 }
2421 
2422 oop java_lang_reflect_Method::type_annotations(oop method) {
2423   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2424   assert(has_type_annotations_field(), "type_annotations field must be present");
2425   return method->obj_field(type_annotations_offset);
2426 }
2427 
2428 void java_lang_reflect_Method::set_type_annotations(oop method, oop value) {
2429   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2430   assert(has_type_annotations_field(), "type_annotations field must be present");
2431   method->obj_field_put(type_annotations_offset, value);
2432 }
2433 
2434 void java_lang_reflect_Constructor::compute_offsets() {
2435   Klass* k = SystemDictionary::reflect_Constructor_klass();
2436   compute_offset(clazz_offset,          k, vmSymbols::clazz_name(),          vmSymbols::class_signature());
2437   compute_offset(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), vmSymbols::class_array_signature());
2438   compute_offset(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), vmSymbols::class_array_signature());
2439   compute_offset(slot_offset,           k, vmSymbols::slot_name(),           vmSymbols::int_signature());
2440   compute_offset(modifiers_offset,      k, vmSymbols::modifiers_name(),      vmSymbols::int_signature());
2441   // The generic signature and annotations fields are only present in 1.5
2442   signature_offset = -1;
2443   annotations_offset = -1;
2444   parameter_annotations_offset = -1;
2445   type_annotations_offset = -1;
2446   compute_optional_offset(signature_offset,             k, vmSymbols::signature_name(),             vmSymbols::string_signature());
2447   compute_optional_offset(annotations_offset,           k, vmSymbols::annotations_name(),           vmSymbols::byte_array_signature());
2448   compute_optional_offset(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), vmSymbols::byte_array_signature());
2449   compute_optional_offset(type_annotations_offset,      k, vmSymbols::type_annotations_name(),      vmSymbols::byte_array_signature());
2450 }
2451 
2452 Handle java_lang_reflect_Constructor::create(TRAPS) {
2453   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2454   Symbol* name = vmSymbols::java_lang_reflect_Constructor();
2455   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2456   instanceKlassHandle klass (THREAD, k);
2457   // Ensure it is initialized
2458   klass->initialize(CHECK_NH);
2459   return klass->allocate_instance_handle(THREAD);
2460 }
2461 
2462 oop java_lang_reflect_Constructor::clazz(oop reflect) {
2463   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2464   return reflect->obj_field(clazz_offset);
2465 }
2466 
2467 void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) {
2468   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2469    reflect->obj_field_put(clazz_offset, value);
2470 }
2471 
2472 oop java_lang_reflect_Constructor::parameter_types(oop constructor) {
2473   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2474   return constructor->obj_field(parameterTypes_offset);
2475 }
2476 
2477 void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) {
2478   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2479   constructor->obj_field_put(parameterTypes_offset, value);
2480 }
2481 
2482 oop java_lang_reflect_Constructor::exception_types(oop constructor) {
2483   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2484   return constructor->obj_field(exceptionTypes_offset);
2485 }
2486 
2487 void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) {
2488   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2489   constructor->obj_field_put(exceptionTypes_offset, value);
2490 }
2491 
2492 int java_lang_reflect_Constructor::slot(oop reflect) {
2493   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2494   return reflect->int_field(slot_offset);
2495 }
2496 
2497 void java_lang_reflect_Constructor::set_slot(oop reflect, int value) {
2498   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2499   reflect->int_field_put(slot_offset, value);
2500 }
2501 
2502 int java_lang_reflect_Constructor::modifiers(oop constructor) {
2503   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2504   return constructor->int_field(modifiers_offset);
2505 }
2506 
2507 void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) {
2508   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2509   constructor->int_field_put(modifiers_offset, value);
2510 }
2511 
2512 bool java_lang_reflect_Constructor::has_signature_field() {
2513   return (signature_offset >= 0);
2514 }
2515 
2516 oop java_lang_reflect_Constructor::signature(oop constructor) {
2517   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2518   assert(has_signature_field(), "signature field must be present");
2519   return constructor->obj_field(signature_offset);
2520 }
2521 
2522 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
2523   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2524   assert(has_signature_field(), "signature field must be present");
2525   constructor->obj_field_put(signature_offset, value);
2526 }
2527 
2528 bool java_lang_reflect_Constructor::has_annotations_field() {
2529   return (annotations_offset >= 0);
2530 }
2531 
2532 oop java_lang_reflect_Constructor::annotations(oop constructor) {
2533   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2534   assert(has_annotations_field(), "annotations field must be present");
2535   return constructor->obj_field(annotations_offset);
2536 }
2537 
2538 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
2539   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2540   assert(has_annotations_field(), "annotations field must be present");
2541   constructor->obj_field_put(annotations_offset, value);
2542 }
2543 
2544 bool java_lang_reflect_Constructor::has_parameter_annotations_field() {
2545   return (parameter_annotations_offset >= 0);
2546 }
2547 
2548 oop java_lang_reflect_Constructor::parameter_annotations(oop method) {
2549   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2550   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2551   return method->obj_field(parameter_annotations_offset);
2552 }
2553 
2554 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
2555   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2556   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2557   method->obj_field_put(parameter_annotations_offset, value);
2558 }
2559 
2560 bool java_lang_reflect_Constructor::has_type_annotations_field() {
2561   return (type_annotations_offset >= 0);
2562 }
2563 
2564 oop java_lang_reflect_Constructor::type_annotations(oop constructor) {
2565   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2566   assert(has_type_annotations_field(), "type_annotations field must be present");
2567   return constructor->obj_field(type_annotations_offset);
2568 }
2569 
2570 void java_lang_reflect_Constructor::set_type_annotations(oop constructor, oop value) {
2571   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2572   assert(has_type_annotations_field(), "type_annotations field must be present");
2573   constructor->obj_field_put(type_annotations_offset, value);
2574 }
2575 
2576 void java_lang_reflect_Field::compute_offsets() {
2577   Klass* k = SystemDictionary::reflect_Field_klass();
2578   compute_offset(clazz_offset,     k, vmSymbols::clazz_name(),     vmSymbols::class_signature());
2579   compute_offset(name_offset,      k, vmSymbols::name_name(),      vmSymbols::string_signature());
2580   compute_offset(type_offset,      k, vmSymbols::type_name(),      vmSymbols::class_signature());
2581   compute_offset(slot_offset,      k, vmSymbols::slot_name(),      vmSymbols::int_signature());
2582   compute_offset(modifiers_offset, k, vmSymbols::modifiers_name(), vmSymbols::int_signature());
2583   // The generic signature and annotations fields are only present in 1.5
2584   signature_offset = -1;
2585   annotations_offset = -1;
2586   type_annotations_offset = -1;
2587   compute_optional_offset(signature_offset, k, vmSymbols::signature_name(), vmSymbols::string_signature());
2588   compute_optional_offset(annotations_offset,  k, vmSymbols::annotations_name(),  vmSymbols::byte_array_signature());
2589   compute_optional_offset(type_annotations_offset,  k, vmSymbols::type_annotations_name(),  vmSymbols::byte_array_signature());
2590 }
2591 
2592 Handle java_lang_reflect_Field::create(TRAPS) {
2593   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2594   Symbol* name = vmSymbols::java_lang_reflect_Field();
2595   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2596   instanceKlassHandle klass (THREAD, k);
2597   // Ensure it is initialized
2598   klass->initialize(CHECK_NH);
2599   return klass->allocate_instance_handle(THREAD);
2600 }
2601 
2602 oop java_lang_reflect_Field::clazz(oop reflect) {
2603   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2604   return reflect->obj_field(clazz_offset);
2605 }
2606 
2607 void java_lang_reflect_Field::set_clazz(oop reflect, oop value) {
2608   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2609    reflect->obj_field_put(clazz_offset, value);
2610 }
2611 
2612 oop java_lang_reflect_Field::name(oop field) {
2613   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2614   return field->obj_field(name_offset);
2615 }
2616 
2617 void java_lang_reflect_Field::set_name(oop field, oop value) {
2618   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2619   field->obj_field_put(name_offset, value);
2620 }
2621 
2622 oop java_lang_reflect_Field::type(oop field) {
2623   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2624   return field->obj_field(type_offset);
2625 }
2626 
2627 void java_lang_reflect_Field::set_type(oop field, oop value) {
2628   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2629   field->obj_field_put(type_offset, value);
2630 }
2631 
2632 int java_lang_reflect_Field::slot(oop reflect) {
2633   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2634   return reflect->int_field(slot_offset);
2635 }
2636 
2637 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
2638   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2639   reflect->int_field_put(slot_offset, value);
2640 }
2641 
2642 int java_lang_reflect_Field::modifiers(oop field) {
2643   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2644   return field->int_field(modifiers_offset);
2645 }
2646 
2647 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
2648   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2649   field->int_field_put(modifiers_offset, value);
2650 }
2651 
2652 bool java_lang_reflect_Field::has_signature_field() {
2653   return (signature_offset >= 0);
2654 }
2655 
2656 oop java_lang_reflect_Field::signature(oop field) {
2657   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2658   assert(has_signature_field(), "signature field must be present");
2659   return field->obj_field(signature_offset);
2660 }
2661 
2662 void java_lang_reflect_Field::set_signature(oop field, oop value) {
2663   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2664   assert(has_signature_field(), "signature field must be present");
2665   field->obj_field_put(signature_offset, value);
2666 }
2667 
2668 bool java_lang_reflect_Field::has_annotations_field() {
2669   return (annotations_offset >= 0);
2670 }
2671 
2672 oop java_lang_reflect_Field::annotations(oop field) {
2673   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2674   assert(has_annotations_field(), "annotations field must be present");
2675   return field->obj_field(annotations_offset);
2676 }
2677 
2678 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
2679   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2680   assert(has_annotations_field(), "annotations field must be present");
2681   field->obj_field_put(annotations_offset, value);
2682 }
2683 
2684 bool java_lang_reflect_Field::has_type_annotations_field() {
2685   return (type_annotations_offset >= 0);
2686 }
2687 
2688 oop java_lang_reflect_Field::type_annotations(oop field) {
2689   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2690   assert(has_type_annotations_field(), "type_annotations field must be present");
2691   return field->obj_field(type_annotations_offset);
2692 }
2693 
2694 void java_lang_reflect_Field::set_type_annotations(oop field, oop value) {
2695   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2696   assert(has_type_annotations_field(), "type_annotations field must be present");
2697   field->obj_field_put(type_annotations_offset, value);
2698 }
2699 
2700 void reflect_ConstantPool::compute_offsets() {
2701   Klass* k = SystemDictionary::reflect_ConstantPool_klass();
2702   // This null test can be removed post beta
2703   if (k != NULL) {
2704     // The field is called ConstantPool* in the sun.reflect.ConstantPool class.
2705     compute_offset(_oop_offset, k, vmSymbols::ConstantPool_name(), vmSymbols::object_signature());
2706   }
2707 }
2708 
2709 void java_lang_reflect_Parameter::compute_offsets() {
2710   Klass* k = SystemDictionary::reflect_Parameter_klass();
2711   if(NULL != k) {
2712     compute_offset(name_offset,        k, vmSymbols::name_name(),        vmSymbols::string_signature());
2713     compute_offset(modifiers_offset,   k, vmSymbols::modifiers_name(),   vmSymbols::int_signature());
2714     compute_offset(index_offset,       k, vmSymbols::index_name(),       vmSymbols::int_signature());
2715     compute_offset(executable_offset,  k, vmSymbols::executable_name(),  vmSymbols::executable_signature());
2716   }
2717 }
2718 
2719 Handle java_lang_reflect_Parameter::create(TRAPS) {
2720   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2721   Symbol* name = vmSymbols::java_lang_reflect_Parameter();
2722   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2723   instanceKlassHandle klass (THREAD, k);
2724   // Ensure it is initialized
2725   klass->initialize(CHECK_NH);
2726   return klass->allocate_instance_handle(THREAD);
2727 }
2728 
2729 oop java_lang_reflect_Parameter::name(oop param) {
2730   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2731   return param->obj_field(name_offset);
2732 }
2733 
2734 void java_lang_reflect_Parameter::set_name(oop param, oop value) {
2735   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2736   param->obj_field_put(name_offset, value);
2737 }
2738 
2739 int java_lang_reflect_Parameter::modifiers(oop param) {
2740   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2741   return param->int_field(modifiers_offset);
2742 }
2743 
2744 void java_lang_reflect_Parameter::set_modifiers(oop param, int value) {
2745   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2746   param->int_field_put(modifiers_offset, value);
2747 }
2748 
2749 int java_lang_reflect_Parameter::index(oop param) {
2750   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2751   return param->int_field(index_offset);
2752 }
2753 
2754 void java_lang_reflect_Parameter::set_index(oop param, int value) {
2755   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2756   param->int_field_put(index_offset, value);
2757 }
2758 
2759 oop java_lang_reflect_Parameter::executable(oop param) {
2760   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2761   return param->obj_field(executable_offset);
2762 }
2763 
2764 void java_lang_reflect_Parameter::set_executable(oop param, oop value) {
2765   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2766   param->obj_field_put(executable_offset, value);
2767 }
2768 
2769 
2770 int java_lang_reflect_Module::loader_offset;
2771 int java_lang_reflect_Module::name_offset;
2772 int java_lang_reflect_Module::_module_entry_offset = -1;
2773 
2774 Handle java_lang_reflect_Module::create(Handle loader, Handle module_name, TRAPS) {
2775   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2776 
2777   Symbol* name = vmSymbols::java_lang_reflect_Module();
2778   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2779   instanceKlassHandle klass (THREAD, k);
2780 
2781   Handle jlrmh = klass->allocate_instance_handle(CHECK_NH);
2782   JavaValue result(T_VOID);
2783   JavaCalls::call_special(&result, jlrmh, KlassHandle(THREAD, klass()),
2784                           vmSymbols::object_initializer_name(),
2785                           vmSymbols::java_lang_reflect_module_init_signature(),
2786                           loader, module_name, CHECK_NH);
2787   return jlrmh;
2788 }
2789 
2790 void java_lang_reflect_Module::compute_offsets() {
2791   Klass* k = SystemDictionary::reflect_Module_klass();
2792   if(NULL != k) {
2793     compute_offset(loader_offset,  k, vmSymbols::loader_name(),  vmSymbols::classloader_signature());
2794     compute_offset(name_offset,    k, vmSymbols::name_name(),    vmSymbols::string_signature());
2795     MODULE_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2796   }
2797 }
2798 
2799 
2800 oop java_lang_reflect_Module::loader(oop module) {
2801   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2802   return module->obj_field(loader_offset);
2803 }
2804 
2805 void java_lang_reflect_Module::set_loader(oop module, oop value) {
2806   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2807   module->obj_field_put(loader_offset, value);
2808 }
2809 
2810 oop java_lang_reflect_Module::name(oop module) {
2811   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2812   return module->obj_field(name_offset);
2813 }
2814 
2815 void java_lang_reflect_Module::set_name(oop module, oop value) {
2816   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2817   module->obj_field_put(name_offset, value);
2818 }
2819 
2820 ModuleEntry* java_lang_reflect_Module::module_entry(oop module, TRAPS) {
2821   assert(_module_entry_offset != -1, "Uninitialized module_entry_offset");
2822   assert(module != NULL, "module can't be null");
2823   assert(module->is_oop(), "module must be oop");
2824 
2825   ModuleEntry* module_entry = (ModuleEntry*)module->address_field(_module_entry_offset);
2826   if (module_entry == NULL) {
2827     // If the inject field containing the ModuleEntry* is null then return the
2828     // class loader's unnamed module.
2829     oop loader = java_lang_reflect_Module::loader(module);
2830     Handle h_loader = Handle(THREAD, loader);
2831     ClassLoaderData* loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL);
2832     return loader_cld->modules()->unnamed_module();
2833   }
2834   return module_entry;
2835 }
2836 
2837 void java_lang_reflect_Module::set_module_entry(oop module, ModuleEntry* module_entry) {
2838   assert(_module_entry_offset != -1, "Uninitialized module_entry_offset");
2839   assert(module != NULL, "module can't be null");
2840   assert(module->is_oop(), "module must be oop");
2841   module->address_field_put(_module_entry_offset, (address)module_entry);
2842 }
2843 
2844 Handle reflect_ConstantPool::create(TRAPS) {
2845   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2846   Klass* k = SystemDictionary::reflect_ConstantPool_klass();
2847   instanceKlassHandle klass (THREAD, k);
2848   // Ensure it is initialized
2849   klass->initialize(CHECK_NH);
2850   return klass->allocate_instance_handle(THREAD);
2851 }
2852 
2853 
2854 void reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) {
2855   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2856   oop mirror = value->pool_holder()->java_mirror();
2857   // Save the mirror to get back the constant pool.
2858   reflect->obj_field_put(_oop_offset, mirror);
2859 }
2860 
2861 ConstantPool* reflect_ConstantPool::get_cp(oop reflect) {
2862   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2863 
2864   oop mirror = reflect->obj_field(_oop_offset);
2865   Klass* k = java_lang_Class::as_Klass(mirror);
2866   assert(k->is_instance_klass(), "Must be");
2867 
2868   // Get the constant pool back from the klass.  Since class redefinition
2869   // merges the new constant pool into the old, this is essentially the
2870   // same constant pool as the original.  If constant pool merging is
2871   // no longer done in the future, this will have to change to save
2872   // the original.
2873   return InstanceKlass::cast(k)->constants();
2874 }
2875 
2876 void reflect_UnsafeStaticFieldAccessorImpl::compute_offsets() {
2877   Klass* k = SystemDictionary::reflect_UnsafeStaticFieldAccessorImpl_klass();
2878   // This null test can be removed post beta
2879   if (k != NULL) {
2880     compute_offset(_base_offset, k,
2881                    vmSymbols::base_name(), vmSymbols::object_signature());
2882   }
2883 }
2884 
2885 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
2886   Klass* k = SystemDictionary::box_klass(type);
2887   if (k == NULL)  return NULL;
2888   instanceKlassHandle h (THREAD, k);
2889   if (!h->is_initialized())  h->initialize(CHECK_0);
2890   return h->allocate_instance(THREAD);
2891 }
2892 
2893 
2894 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
2895   oop box = initialize_and_allocate(type, CHECK_0);
2896   if (box == NULL)  return NULL;
2897   switch (type) {
2898     case T_BOOLEAN:
2899       box->bool_field_put(value_offset, value->z);
2900       break;
2901     case T_CHAR:
2902       box->char_field_put(value_offset, value->c);
2903       break;
2904     case T_FLOAT:
2905       box->float_field_put(value_offset, value->f);
2906       break;
2907     case T_DOUBLE:
2908       box->double_field_put(long_value_offset, value->d);
2909       break;
2910     case T_BYTE:
2911       box->byte_field_put(value_offset, value->b);
2912       break;
2913     case T_SHORT:
2914       box->short_field_put(value_offset, value->s);
2915       break;
2916     case T_INT:
2917       box->int_field_put(value_offset, value->i);
2918       break;
2919     case T_LONG:
2920       box->long_field_put(long_value_offset, value->j);
2921       break;
2922     default:
2923       return NULL;
2924   }
2925   return box;
2926 }
2927 
2928 
2929 BasicType java_lang_boxing_object::basic_type(oop box) {
2930   if (box == NULL)  return T_ILLEGAL;
2931   BasicType type = SystemDictionary::box_klass_type(box->klass());
2932   if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
2933     return T_ILLEGAL;
2934   return type;
2935 }
2936 
2937 
2938 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
2939   BasicType type = SystemDictionary::box_klass_type(box->klass());
2940   switch (type) {
2941   case T_BOOLEAN:
2942     value->z = box->bool_field(value_offset);
2943     break;
2944   case T_CHAR:
2945     value->c = box->char_field(value_offset);
2946     break;
2947   case T_FLOAT:
2948     value->f = box->float_field(value_offset);
2949     break;
2950   case T_DOUBLE:
2951     value->d = box->double_field(long_value_offset);
2952     break;
2953   case T_BYTE:
2954     value->b = box->byte_field(value_offset);
2955     break;
2956   case T_SHORT:
2957     value->s = box->short_field(value_offset);
2958     break;
2959   case T_INT:
2960     value->i = box->int_field(value_offset);
2961     break;
2962   case T_LONG:
2963     value->j = box->long_field(long_value_offset);
2964     break;
2965   default:
2966     return T_ILLEGAL;
2967   } // end switch
2968   return type;
2969 }
2970 
2971 
2972 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
2973   BasicType type = SystemDictionary::box_klass_type(box->klass());
2974   switch (type) {
2975   case T_BOOLEAN:
2976     box->bool_field_put(value_offset, value->z);
2977     break;
2978   case T_CHAR:
2979     box->char_field_put(value_offset, value->c);
2980     break;
2981   case T_FLOAT:
2982     box->float_field_put(value_offset, value->f);
2983     break;
2984   case T_DOUBLE:
2985     box->double_field_put(long_value_offset, value->d);
2986     break;
2987   case T_BYTE:
2988     box->byte_field_put(value_offset, value->b);
2989     break;
2990   case T_SHORT:
2991     box->short_field_put(value_offset, value->s);
2992     break;
2993   case T_INT:
2994     box->int_field_put(value_offset, value->i);
2995     break;
2996   case T_LONG:
2997     box->long_field_put(long_value_offset, value->j);
2998     break;
2999   default:
3000     return T_ILLEGAL;
3001   } // end switch
3002   return type;
3003 }
3004 
3005 
3006 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
3007   switch (type) {
3008   case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
3009   case T_CHAR:      st->print("%d", value->c);                      break;
3010   case T_BYTE:      st->print("%d", value->b);                      break;
3011   case T_SHORT:     st->print("%d", value->s);                      break;
3012   case T_INT:       st->print("%d", value->i);                      break;
3013   case T_LONG:      st->print(INT64_FORMAT, value->j);              break;
3014   case T_FLOAT:     st->print("%f", value->f);                      break;
3015   case T_DOUBLE:    st->print("%lf", value->d);                     break;
3016   default:          st->print("type %d?", type);                    break;
3017   }
3018 }
3019 
3020 
3021 // Support for java_lang_ref_Reference
3022 HeapWord *java_lang_ref_Reference::pending_list_lock_addr() {
3023   InstanceKlass* ik = SystemDictionary::Reference_klass();
3024   address addr = ik->static_field_addr(static_lock_offset);
3025   return (HeapWord*) addr;
3026 }
3027 
3028 oop java_lang_ref_Reference::pending_list_lock() {
3029   InstanceKlass* ik = SystemDictionary::Reference_klass();
3030   address addr = ik->static_field_addr(static_lock_offset);
3031   if (UseCompressedOops) {
3032     return oopDesc::load_decode_heap_oop((narrowOop *)addr);
3033   } else {
3034     return oopDesc::load_decode_heap_oop((oop*)addr);
3035   }
3036 }
3037 
3038 HeapWord *java_lang_ref_Reference::pending_list_addr() {
3039   InstanceKlass* ik = SystemDictionary::Reference_klass();
3040   address addr = ik->static_field_addr(static_pending_offset);
3041   // XXX This might not be HeapWord aligned, almost rather be char *.
3042   return (HeapWord*)addr;
3043 }
3044 
3045 oop java_lang_ref_Reference::pending_list() {
3046   char *addr = (char *)pending_list_addr();
3047   if (UseCompressedOops) {
3048     return oopDesc::load_decode_heap_oop((narrowOop *)addr);
3049   } else {
3050     return oopDesc::load_decode_heap_oop((oop*)addr);
3051   }
3052 }
3053 
3054 
3055 // Support for java_lang_ref_SoftReference
3056 
3057 jlong java_lang_ref_SoftReference::timestamp(oop ref) {
3058   return ref->long_field(timestamp_offset);
3059 }
3060 
3061 jlong java_lang_ref_SoftReference::clock() {
3062   InstanceKlass* ik = SystemDictionary::SoftReference_klass();
3063   jlong* offset = (jlong*)ik->static_field_addr(static_clock_offset);
3064   return *offset;
3065 }
3066 
3067 void java_lang_ref_SoftReference::set_clock(jlong value) {
3068   InstanceKlass* ik = SystemDictionary::SoftReference_klass();
3069   jlong* offset = (jlong*)ik->static_field_addr(static_clock_offset);
3070   *offset = value;
3071 }
3072 
3073 // Support for java_lang_invoke_DirectMethodHandle
3074 
3075 int java_lang_invoke_DirectMethodHandle::_member_offset;
3076 
3077 oop java_lang_invoke_DirectMethodHandle::member(oop dmh) {
3078   oop member_name = NULL;
3079   bool is_dmh = dmh->is_oop() && java_lang_invoke_DirectMethodHandle::is_instance(dmh);
3080   assert(is_dmh, "a DirectMethodHandle oop is expected");
3081   if (is_dmh) {
3082     member_name = dmh->obj_field(member_offset_in_bytes());
3083   }
3084   return member_name;
3085 }
3086 
3087 void java_lang_invoke_DirectMethodHandle::compute_offsets() {
3088   Klass* klass_oop = SystemDictionary::DirectMethodHandle_klass();
3089   if (klass_oop != NULL) {
3090     compute_offset(_member_offset, klass_oop, vmSymbols::member_name(), vmSymbols::java_lang_invoke_MemberName_signature());
3091   }
3092 }
3093 
3094 // Support for java_lang_invoke_MethodHandle
3095 
3096 int java_lang_invoke_MethodHandle::_type_offset;
3097 int java_lang_invoke_MethodHandle::_form_offset;
3098 
3099 int java_lang_invoke_MemberName::_clazz_offset;
3100 int java_lang_invoke_MemberName::_name_offset;
3101 int java_lang_invoke_MemberName::_type_offset;
3102 int java_lang_invoke_MemberName::_flags_offset;
3103 int java_lang_invoke_MemberName::_vmtarget_offset;
3104 int java_lang_invoke_MemberName::_vmloader_offset;
3105 int java_lang_invoke_MemberName::_vmindex_offset;
3106 
3107 int java_lang_invoke_LambdaForm::_vmentry_offset;
3108 
3109 void java_lang_invoke_MethodHandle::compute_offsets() {
3110   Klass* klass_oop = SystemDictionary::MethodHandle_klass();
3111   if (klass_oop != NULL) {
3112     compute_offset(_type_offset, klass_oop, vmSymbols::type_name(), vmSymbols::java_lang_invoke_MethodType_signature());
3113     compute_offset(_form_offset, klass_oop, vmSymbols::form_name(), vmSymbols::java_lang_invoke_LambdaForm_signature());
3114   }
3115 }
3116 
3117 void java_lang_invoke_MemberName::compute_offsets() {
3118   Klass* klass_oop = SystemDictionary::MemberName_klass();
3119   if (klass_oop != NULL) {
3120     compute_offset(_clazz_offset,     klass_oop, vmSymbols::clazz_name(),     vmSymbols::class_signature());
3121     compute_offset(_name_offset,      klass_oop, vmSymbols::name_name(),      vmSymbols::string_signature());
3122     compute_offset(_type_offset,      klass_oop, vmSymbols::type_name(),      vmSymbols::object_signature());
3123     compute_offset(_flags_offset,     klass_oop, vmSymbols::flags_name(),     vmSymbols::int_signature());
3124     MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3125   }
3126 }
3127 
3128 void java_lang_invoke_LambdaForm::compute_offsets() {
3129   Klass* klass_oop = SystemDictionary::LambdaForm_klass();
3130   if (klass_oop != NULL) {
3131     compute_offset(_vmentry_offset, klass_oop, vmSymbols::vmentry_name(), vmSymbols::java_lang_invoke_MemberName_signature());
3132   }
3133 }
3134 
3135 bool java_lang_invoke_LambdaForm::is_instance(oop obj) {
3136   return obj != NULL && is_subclass(obj->klass());
3137 }
3138 
3139 
3140 oop java_lang_invoke_MethodHandle::type(oop mh) {
3141   return mh->obj_field(_type_offset);
3142 }
3143 
3144 void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) {
3145   mh->obj_field_put(_type_offset, mtype);
3146 }
3147 
3148 oop java_lang_invoke_MethodHandle::form(oop mh) {
3149   assert(_form_offset != 0, "");
3150   return mh->obj_field(_form_offset);
3151 }
3152 
3153 void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) {
3154   assert(_form_offset != 0, "");
3155   mh->obj_field_put(_form_offset, lform);
3156 }
3157 
3158 /// MemberName accessors
3159 
3160 oop java_lang_invoke_MemberName::clazz(oop mname) {
3161   assert(is_instance(mname), "wrong type");
3162   return mname->obj_field(_clazz_offset);
3163 }
3164 
3165 void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) {
3166   assert(is_instance(mname), "wrong type");
3167   mname->obj_field_put(_clazz_offset, clazz);
3168 }
3169 
3170 oop java_lang_invoke_MemberName::name(oop mname) {
3171   assert(is_instance(mname), "wrong type");
3172   return mname->obj_field(_name_offset);
3173 }
3174 
3175 void java_lang_invoke_MemberName::set_name(oop mname, oop name) {
3176   assert(is_instance(mname), "wrong type");
3177   mname->obj_field_put(_name_offset, name);
3178 }
3179 
3180 oop java_lang_invoke_MemberName::type(oop mname) {
3181   assert(is_instance(mname), "wrong type");
3182   return mname->obj_field(_type_offset);
3183 }
3184 
3185 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
3186   assert(is_instance(mname), "wrong type");
3187   mname->obj_field_put(_type_offset, type);
3188 }
3189 
3190 int java_lang_invoke_MemberName::flags(oop mname) {
3191   assert(is_instance(mname), "wrong type");
3192   return mname->int_field(_flags_offset);
3193 }
3194 
3195 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
3196   assert(is_instance(mname), "wrong type");
3197   mname->int_field_put(_flags_offset, flags);
3198 }
3199 
3200 Metadata* java_lang_invoke_MemberName::vmtarget(oop mname) {
3201   assert(is_instance(mname), "wrong type");
3202   return (Metadata*)mname->address_field(_vmtarget_offset);
3203 }
3204 
3205 bool java_lang_invoke_MemberName::is_method(oop mname) {
3206   assert(is_instance(mname), "must be MemberName");
3207   return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
3208 }
3209 
3210 void java_lang_invoke_MemberName::set_vmtarget(oop mname, Metadata* ref) {
3211   assert(is_instance(mname), "wrong type");
3212   // check the type of the vmtarget
3213   oop dependency = NULL;
3214   if (ref != NULL) {
3215     switch (flags(mname) & (MN_IS_METHOD |
3216                             MN_IS_CONSTRUCTOR |
3217                             MN_IS_FIELD)) {
3218     case MN_IS_METHOD:
3219     case MN_IS_CONSTRUCTOR:
3220       assert(ref->is_method(), "should be a method");
3221       dependency = ((Method*)ref)->method_holder()->java_mirror();
3222       break;
3223     case MN_IS_FIELD:
3224       assert(ref->is_klass(), "should be a class");
3225       dependency = ((Klass*)ref)->java_mirror();
3226       break;
3227     default:
3228       ShouldNotReachHere();
3229     }
3230   }
3231   mname->address_field_put(_vmtarget_offset, (address)ref);
3232   // Add a reference to the loader (actually mirror because anonymous classes will not have
3233   // distinct loaders) to ensure the metadata is kept alive
3234   // This mirror may be different than the one in clazz field.
3235   mname->obj_field_put(_vmloader_offset, dependency);
3236 }
3237 
3238 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
3239   assert(is_instance(mname), "wrong type");
3240   return (intptr_t) mname->address_field(_vmindex_offset);
3241 }
3242 
3243 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
3244   assert(is_instance(mname), "wrong type");
3245   mname->address_field_put(_vmindex_offset, (address) index);
3246 }
3247 
3248 bool java_lang_invoke_MemberName::equals(oop mn1, oop mn2) {
3249   if (mn1 == mn2) {
3250      return true;
3251   }
3252   return (vmtarget(mn1) == vmtarget(mn2) && flags(mn1) == flags(mn2) &&
3253           vmindex(mn1) == vmindex(mn2) &&
3254           clazz(mn1) == clazz(mn2));
3255 }
3256 
3257 oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
3258   assert(is_instance(lform), "wrong type");
3259   return lform->obj_field(_vmentry_offset);
3260 }
3261 
3262 
3263 // Support for java_lang_invoke_MethodType
3264 
3265 int java_lang_invoke_MethodType::_rtype_offset;
3266 int java_lang_invoke_MethodType::_ptypes_offset;
3267 
3268 void java_lang_invoke_MethodType::compute_offsets() {
3269   Klass* k = SystemDictionary::MethodType_klass();
3270   if (k != NULL) {
3271     compute_offset(_rtype_offset,  k, vmSymbols::rtype_name(),  vmSymbols::class_signature());
3272     compute_offset(_ptypes_offset, k, vmSymbols::ptypes_name(), vmSymbols::class_array_signature());
3273   }
3274 }
3275 
3276 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
3277   st->print("(");
3278   objArrayOop pts = ptypes(mt);
3279   for (int i = 0, limit = pts->length(); i < limit; i++) {
3280     java_lang_Class::print_signature(pts->obj_at(i), st);
3281   }
3282   st->print(")");
3283   java_lang_Class::print_signature(rtype(mt), st);
3284 }
3285 
3286 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found, TRAPS) {
3287   ResourceMark rm;
3288   stringStream buffer(128);
3289   print_signature(mt, &buffer);
3290   const char* sigstr =       buffer.base();
3291   int         siglen = (int) buffer.size();
3292   Symbol *name;
3293   if (!intern_if_not_found) {
3294     name = SymbolTable::probe(sigstr, siglen);
3295   } else {
3296     name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
3297   }
3298   return name;
3299 }
3300 
3301 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
3302   if (mt1 == mt2)
3303     return true;
3304   if (rtype(mt1) != rtype(mt2))
3305     return false;
3306   if (ptype_count(mt1) != ptype_count(mt2))
3307     return false;
3308   for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
3309     if (ptype(mt1, i) != ptype(mt2, i))
3310       return false;
3311   }
3312   return true;
3313 }
3314 
3315 oop java_lang_invoke_MethodType::rtype(oop mt) {
3316   assert(is_instance(mt), "must be a MethodType");
3317   return mt->obj_field(_rtype_offset);
3318 }
3319 
3320 objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) {
3321   assert(is_instance(mt), "must be a MethodType");
3322   return (objArrayOop) mt->obj_field(_ptypes_offset);
3323 }
3324 
3325 oop java_lang_invoke_MethodType::ptype(oop mt, int idx) {
3326   return ptypes(mt)->obj_at(idx);
3327 }
3328 
3329 int java_lang_invoke_MethodType::ptype_count(oop mt) {
3330   return ptypes(mt)->length();
3331 }
3332 
3333 int java_lang_invoke_MethodType::ptype_slot_count(oop mt) {
3334   objArrayOop pts = ptypes(mt);
3335   int count = pts->length();
3336   int slots = 0;
3337   for (int i = 0; i < count; i++) {
3338     BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i));
3339     slots += type2size[bt];
3340   }
3341   return slots;
3342 }
3343 
3344 int java_lang_invoke_MethodType::rtype_slot_count(oop mt) {
3345   BasicType bt = java_lang_Class::as_BasicType(rtype(mt));
3346   return type2size[bt];
3347 }
3348 
3349 
3350 // Support for java_lang_invoke_CallSite
3351 
3352 int java_lang_invoke_CallSite::_target_offset;
3353 int java_lang_invoke_CallSite::_context_offset;
3354 
3355 void java_lang_invoke_CallSite::compute_offsets() {
3356   Klass* k = SystemDictionary::CallSite_klass();
3357   if (k != NULL) {
3358     compute_offset(_target_offset, k, vmSymbols::target_name(), vmSymbols::java_lang_invoke_MethodHandle_signature());
3359     compute_offset(_context_offset, k, vmSymbols::context_name(),
3360                    vmSymbols::java_lang_invoke_MethodHandleNatives_CallSiteContext_signature());
3361   }
3362 }
3363 
3364 oop java_lang_invoke_CallSite::context(oop call_site) {
3365   assert(java_lang_invoke_CallSite::is_instance(call_site), "");
3366 
3367   oop dep_oop = call_site->obj_field(_context_offset);
3368   return dep_oop;
3369 }
3370 
3371 // Support for java_lang_invoke_MethodHandleNatives_CallSiteContext
3372 
3373 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_vmdependencies_offset;
3374 
3375 void java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets() {
3376   Klass* k = SystemDictionary::Context_klass();
3377   if (k != NULL) {
3378     CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3379   }
3380 }
3381 
3382 DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) {
3383   assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), "");
3384   intptr_t* vmdeps_addr = (intptr_t*)call_site->address_field_addr(_vmdependencies_offset);
3385   DependencyContext dep_ctx(vmdeps_addr);
3386   return dep_ctx;
3387 }
3388 
3389 // Support for java_security_AccessControlContext
3390 
3391 int java_security_AccessControlContext::_context_offset = 0;
3392 int java_security_AccessControlContext::_privilegedContext_offset = 0;
3393 int java_security_AccessControlContext::_isPrivileged_offset = 0;
3394 int java_security_AccessControlContext::_isAuthorized_offset = -1;
3395 
3396 void java_security_AccessControlContext::compute_offsets() {
3397   assert(_isPrivileged_offset == 0, "offsets should be initialized only once");
3398   fieldDescriptor fd;
3399   InstanceKlass* ik = SystemDictionary::AccessControlContext_klass();
3400 
3401   if (!ik->find_local_field(vmSymbols::context_name(), vmSymbols::protectiondomain_signature(), &fd)) {
3402     fatal("Invalid layout of java.security.AccessControlContext");
3403   }
3404   _context_offset = fd.offset();
3405 
3406   if (!ik->find_local_field(vmSymbols::privilegedContext_name(), vmSymbols::accesscontrolcontext_signature(), &fd)) {
3407     fatal("Invalid layout of java.security.AccessControlContext");
3408   }
3409   _privilegedContext_offset = fd.offset();
3410 
3411   if (!ik->find_local_field(vmSymbols::isPrivileged_name(), vmSymbols::bool_signature(), &fd)) {
3412     fatal("Invalid layout of java.security.AccessControlContext");
3413   }
3414   _isPrivileged_offset = fd.offset();
3415 
3416   // The offset may not be present for bootstrapping with older JDK.
3417   if (ik->find_local_field(vmSymbols::isAuthorized_name(), vmSymbols::bool_signature(), &fd)) {
3418     _isAuthorized_offset = fd.offset();
3419   }
3420 }
3421 
3422 
3423 bool java_security_AccessControlContext::is_authorized(Handle context) {
3424   assert(context.not_null() && context->klass() == SystemDictionary::AccessControlContext_klass(), "Invalid type");
3425   assert(_isAuthorized_offset != -1, "should be set");
3426   return context->bool_field(_isAuthorized_offset) != 0;
3427 }
3428 
3429 oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) {
3430   assert(_isPrivileged_offset != 0, "offsets should have been initialized");
3431   // Ensure klass is initialized
3432   SystemDictionary::AccessControlContext_klass()->initialize(CHECK_0);
3433   // Allocate result
3434   oop result = SystemDictionary::AccessControlContext_klass()->allocate_instance(CHECK_0);
3435   // Fill in values
3436   result->obj_field_put(_context_offset, context());
3437   result->obj_field_put(_privilegedContext_offset, privileged_context());
3438   result->bool_field_put(_isPrivileged_offset, isPrivileged);
3439   // whitelist AccessControlContexts created by the JVM if present
3440   if (_isAuthorized_offset != -1) {
3441     result->bool_field_put(_isAuthorized_offset, true);
3442   }
3443   return result;
3444 }
3445 
3446 
3447 // Support for java_lang_ClassLoader
3448 
3449 bool java_lang_ClassLoader::offsets_computed = false;
3450 int  java_lang_ClassLoader::_loader_data_offset = -1;
3451 int  java_lang_ClassLoader::parallelCapable_offset = -1;
3452 int  java_lang_ClassLoader::unnamedModule_offset = -1;
3453 
3454 ClassLoaderData** java_lang_ClassLoader::loader_data_addr(oop loader) {
3455     assert(loader != NULL && loader->is_oop(), "loader must be oop");
3456     return (ClassLoaderData**) loader->address_field_addr(_loader_data_offset);
3457 }
3458 
3459 ClassLoaderData* java_lang_ClassLoader::loader_data(oop loader) {
3460   return *java_lang_ClassLoader::loader_data_addr(loader);
3461 }
3462 
3463 void java_lang_ClassLoader::compute_offsets() {
3464   assert(!offsets_computed, "offsets should be initialized only once");
3465   offsets_computed = true;
3466 
3467   // The field indicating parallelCapable (parallelLockMap) is only present starting in 7,
3468   Klass* k1 = SystemDictionary::ClassLoader_klass();
3469   compute_optional_offset(parallelCapable_offset,
3470     k1, vmSymbols::parallelCapable_name(), vmSymbols::concurrenthashmap_signature());
3471 
3472   compute_offset(unnamedModule_offset,
3473     k1, vmSymbols::unnamedModule_name(), vmSymbols::module_signature());
3474 
3475   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3476 }
3477 
3478 oop java_lang_ClassLoader::parent(oop loader) {
3479   assert(is_instance(loader), "loader must be oop");
3480   return loader->obj_field(parent_offset);
3481 }
3482 
3483 bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) {
3484   assert(is_instance(loader), "loader must be oop");
3485   assert(cl == NULL || is_instance(cl), "cl argument must be oop");
3486   oop acl = loader;
3487   debug_only(jint loop_count = 0);
3488   // This loop taken verbatim from ClassLoader.java:
3489   do {
3490     acl = parent(acl);
3491     if (cl == acl) {
3492       return true;
3493     }
3494     assert(++loop_count > 0, "loop_count overflow");
3495   } while (acl != NULL);
3496   return false;
3497 }
3498 
3499 bool java_lang_ClassLoader::is_instance(oop obj) {
3500   return obj != NULL && is_subclass(obj->klass());
3501 }
3502 
3503 
3504 // For class loader classes, parallelCapable defined
3505 // based on non-null field
3506 // Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it
3507 bool java_lang_ClassLoader::parallelCapable(oop class_loader) {
3508   if (parallelCapable_offset == -1) {
3509      // Default for backward compatibility is false
3510      return false;
3511   }
3512   return (class_loader->obj_field(parallelCapable_offset) != NULL);
3513 }
3514 
3515 bool java_lang_ClassLoader::is_trusted_loader(oop loader) {
3516   // Fix for 4474172; see evaluation for more details
3517   loader = non_reflection_class_loader(loader);
3518 
3519   oop cl = SystemDictionary::java_system_loader();
3520   while(cl != NULL) {
3521     if (cl == loader) return true;
3522     cl = parent(cl);
3523   }
3524   return false;
3525 }
3526 
3527 oop java_lang_ClassLoader::non_reflection_class_loader(oop loader) {
3528   if (loader != NULL) {
3529     // See whether this is one of the class loaders associated with
3530     // the generated bytecodes for reflection, and if so, "magically"
3531     // delegate to its parent to prevent class loading from occurring
3532     // in places where applications using reflection didn't expect it.
3533     Klass* delegating_cl_class = SystemDictionary::reflect_DelegatingClassLoader_klass();
3534     // This might be null in non-1.4 JDKs
3535     if (delegating_cl_class != NULL && loader->is_a(delegating_cl_class)) {
3536       return parent(loader);
3537     }
3538   }
3539   return loader;
3540 }
3541 
3542 oop java_lang_ClassLoader::unnamedModule(oop loader) {
3543   assert(is_instance(loader), "loader must be oop");
3544   return loader->obj_field(unnamedModule_offset);
3545 }
3546 
3547 // Support for java_lang_System
3548 int java_lang_System::in_offset_in_bytes() {
3549   return (InstanceMirrorKlass::offset_of_static_fields() + static_in_offset);
3550 }
3551 
3552 
3553 int java_lang_System::out_offset_in_bytes() {
3554   return (InstanceMirrorKlass::offset_of_static_fields() + static_out_offset);
3555 }
3556 
3557 
3558 int java_lang_System::err_offset_in_bytes() {
3559   return (InstanceMirrorKlass::offset_of_static_fields() + static_err_offset);
3560 }
3561 
3562 
3563 bool java_lang_System::has_security_manager() {
3564   InstanceKlass* ik = SystemDictionary::System_klass();
3565   address addr = ik->static_field_addr(static_security_offset);
3566   if (UseCompressedOops) {
3567     return oopDesc::load_decode_heap_oop((narrowOop *)addr) != NULL;
3568   } else {
3569     return oopDesc::load_decode_heap_oop((oop*)addr) != NULL;
3570   }
3571 }
3572 
3573 int java_lang_Class::_klass_offset;
3574 int java_lang_Class::_array_klass_offset;
3575 int java_lang_Class::_oop_size_offset;
3576 int java_lang_Class::_static_oop_field_count_offset;
3577 int java_lang_Class::_class_loader_offset;
3578 int java_lang_Class::_module_offset;
3579 int java_lang_Class::_protection_domain_offset;
3580 int java_lang_Class::_component_mirror_offset;
3581 int java_lang_Class::_init_lock_offset;
3582 int java_lang_Class::_signers_offset;
3583 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
3584 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
3585 int java_lang_Throwable::backtrace_offset;
3586 int java_lang_Throwable::detailMessage_offset;
3587 int java_lang_Throwable::stackTrace_offset;
3588 int java_lang_Throwable::depth_offset;
3589 int java_lang_Throwable::static_unassigned_stacktrace_offset;
3590 int java_lang_reflect_AccessibleObject::override_offset;
3591 int java_lang_reflect_Method::clazz_offset;
3592 int java_lang_reflect_Method::name_offset;
3593 int java_lang_reflect_Method::returnType_offset;
3594 int java_lang_reflect_Method::parameterTypes_offset;
3595 int java_lang_reflect_Method::exceptionTypes_offset;
3596 int java_lang_reflect_Method::slot_offset;
3597 int java_lang_reflect_Method::modifiers_offset;
3598 int java_lang_reflect_Method::signature_offset;
3599 int java_lang_reflect_Method::annotations_offset;
3600 int java_lang_reflect_Method::parameter_annotations_offset;
3601 int java_lang_reflect_Method::annotation_default_offset;
3602 int java_lang_reflect_Method::type_annotations_offset;
3603 int java_lang_reflect_Constructor::clazz_offset;
3604 int java_lang_reflect_Constructor::parameterTypes_offset;
3605 int java_lang_reflect_Constructor::exceptionTypes_offset;
3606 int java_lang_reflect_Constructor::slot_offset;
3607 int java_lang_reflect_Constructor::modifiers_offset;
3608 int java_lang_reflect_Constructor::signature_offset;
3609 int java_lang_reflect_Constructor::annotations_offset;
3610 int java_lang_reflect_Constructor::parameter_annotations_offset;
3611 int java_lang_reflect_Constructor::type_annotations_offset;
3612 int java_lang_reflect_Field::clazz_offset;
3613 int java_lang_reflect_Field::name_offset;
3614 int java_lang_reflect_Field::type_offset;
3615 int java_lang_reflect_Field::slot_offset;
3616 int java_lang_reflect_Field::modifiers_offset;
3617 int java_lang_reflect_Field::signature_offset;
3618 int java_lang_reflect_Field::annotations_offset;
3619 int java_lang_reflect_Field::type_annotations_offset;
3620 int java_lang_reflect_Parameter::name_offset;
3621 int java_lang_reflect_Parameter::modifiers_offset;
3622 int java_lang_reflect_Parameter::index_offset;
3623 int java_lang_reflect_Parameter::executable_offset;
3624 int java_lang_boxing_object::value_offset;
3625 int java_lang_boxing_object::long_value_offset;
3626 int java_lang_ref_Reference::referent_offset;
3627 int java_lang_ref_Reference::queue_offset;
3628 int java_lang_ref_Reference::next_offset;
3629 int java_lang_ref_Reference::discovered_offset;
3630 int java_lang_ref_Reference::static_lock_offset;
3631 int java_lang_ref_Reference::static_pending_offset;
3632 int java_lang_ref_Reference::number_of_fake_oop_fields;
3633 int java_lang_ref_SoftReference::timestamp_offset;
3634 int java_lang_ref_SoftReference::static_clock_offset;
3635 int java_lang_ClassLoader::parent_offset;
3636 int java_lang_System::static_in_offset;
3637 int java_lang_System::static_out_offset;
3638 int java_lang_System::static_err_offset;
3639 int java_lang_System::static_security_offset;
3640 int java_lang_StackTraceElement::declaringClass_offset;
3641 int java_lang_StackTraceElement::methodName_offset;
3642 int java_lang_StackTraceElement::fileName_offset;
3643 int java_lang_StackTraceElement::lineNumber_offset;
3644 int java_lang_StackTraceElement::moduleName_offset;
3645 int java_lang_StackTraceElement::moduleVersion_offset;
3646 int java_lang_StackFrameInfo::_declaringClass_offset;
3647 int java_lang_StackFrameInfo::_memberName_offset;
3648 int java_lang_StackFrameInfo::_bci_offset;
3649 int java_lang_StackFrameInfo::_version_offset;
3650 int java_lang_LiveStackFrameInfo::_monitors_offset;
3651 int java_lang_LiveStackFrameInfo::_locals_offset;
3652 int java_lang_LiveStackFrameInfo::_operands_offset;
3653 int java_lang_AssertionStatusDirectives::classes_offset;
3654 int java_lang_AssertionStatusDirectives::classEnabled_offset;
3655 int java_lang_AssertionStatusDirectives::packages_offset;
3656 int java_lang_AssertionStatusDirectives::packageEnabled_offset;
3657 int java_lang_AssertionStatusDirectives::deflt_offset;
3658 int java_nio_Buffer::_limit_offset;
3659 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset = 0;
3660 int reflect_ConstantPool::_oop_offset;
3661 int reflect_UnsafeStaticFieldAccessorImpl::_base_offset;
3662 
3663 
3664 // Support for java_lang_StackTraceElement
3665 
3666 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
3667   element->obj_field_put(fileName_offset, value);
3668 }
3669 
3670 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
3671   element->obj_field_put(declaringClass_offset, value);
3672 }
3673 
3674 void java_lang_StackTraceElement::set_methodName(oop element, oop value) {
3675   element->obj_field_put(methodName_offset, value);
3676 }
3677 
3678 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
3679   element->int_field_put(lineNumber_offset, value);
3680 }
3681 
3682 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) {
3683   element->obj_field_put(moduleName_offset, value);
3684 }
3685 
3686 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) {
3687   element->obj_field_put(moduleVersion_offset, value);
3688 }
3689 
3690 // Support for java_lang_StackFrameInfo
3691 void java_lang_StackFrameInfo::set_declaringClass(oop element, oop value) {
3692   element->obj_field_put(_declaringClass_offset, value);
3693 }
3694 
3695 void java_lang_StackFrameInfo::set_version(oop element, short value) {
3696   element->short_field_put(_version_offset, value);
3697 }
3698 
3699 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
3700   element->int_field_put(_bci_offset, value);
3701 }
3702 
3703 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) {
3704   element->obj_field_put(_monitors_offset, value);
3705 }
3706 
3707 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) {
3708   element->obj_field_put(_locals_offset, value);
3709 }
3710 
3711 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) {
3712   element->obj_field_put(_operands_offset, value);
3713 }
3714 
3715 // Support for java Assertions - java_lang_AssertionStatusDirectives.
3716 
3717 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
3718   o->obj_field_put(classes_offset, val);
3719 }
3720 
3721 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
3722   o->obj_field_put(classEnabled_offset, val);
3723 }
3724 
3725 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
3726   o->obj_field_put(packages_offset, val);
3727 }
3728 
3729 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
3730   o->obj_field_put(packageEnabled_offset, val);
3731 }
3732 
3733 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
3734   o->bool_field_put(deflt_offset, val);
3735 }
3736 
3737 
3738 // Support for intrinsification of java.nio.Buffer.checkIndex
3739 int java_nio_Buffer::limit_offset() {
3740   return _limit_offset;
3741 }
3742 
3743 
3744 void java_nio_Buffer::compute_offsets() {
3745   Klass* k = SystemDictionary::nio_Buffer_klass();
3746   assert(k != NULL, "must be loaded in 1.4+");
3747   compute_offset(_limit_offset, k, vmSymbols::limit_name(), vmSymbols::int_signature());
3748 }
3749 
3750 void java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(TRAPS) {
3751   if (_owner_offset != 0) return;
3752 
3753   SystemDictionary::load_abstract_ownable_synchronizer_klass(CHECK);
3754   Klass* k = SystemDictionary::abstract_ownable_synchronizer_klass();
3755   compute_offset(_owner_offset, k,
3756                  vmSymbols::exclusive_owner_thread_name(), vmSymbols::thread_signature());
3757 }
3758 
3759 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
3760   assert(_owner_offset != 0, "Must be initialized");
3761   return obj->obj_field(_owner_offset);
3762 }
3763 
3764 // Compute hard-coded offsets
3765 // Invoked before SystemDictionary::initialize, so pre-loaded classes
3766 // are not available to determine the offset_of_static_fields.
3767 void JavaClasses::compute_hard_coded_offsets() {
3768   const int x = heapOopSize;
3769   const int header = instanceOopDesc::base_offset_in_bytes();
3770 
3771   // Throwable Class
3772   java_lang_Throwable::backtrace_offset  = java_lang_Throwable::hc_backtrace_offset  * x + header;
3773   java_lang_Throwable::detailMessage_offset = java_lang_Throwable::hc_detailMessage_offset * x + header;
3774   java_lang_Throwable::stackTrace_offset = java_lang_Throwable::hc_stackTrace_offset * x + header;
3775   java_lang_Throwable::static_unassigned_stacktrace_offset = java_lang_Throwable::hc_static_unassigned_stacktrace_offset *  x;
3776 
3777   // java_lang_boxing_object
3778   java_lang_boxing_object::value_offset = java_lang_boxing_object::hc_value_offset + header;
3779   java_lang_boxing_object::long_value_offset = align_size_up((java_lang_boxing_object::hc_value_offset + header), BytesPerLong);
3780 
3781   // java_lang_ref_Reference:
3782   java_lang_ref_Reference::referent_offset = java_lang_ref_Reference::hc_referent_offset * x + header;
3783   java_lang_ref_Reference::queue_offset = java_lang_ref_Reference::hc_queue_offset * x + header;
3784   java_lang_ref_Reference::next_offset  = java_lang_ref_Reference::hc_next_offset * x + header;
3785   java_lang_ref_Reference::discovered_offset  = java_lang_ref_Reference::hc_discovered_offset * x + header;
3786   java_lang_ref_Reference::static_lock_offset = java_lang_ref_Reference::hc_static_lock_offset *  x;
3787   java_lang_ref_Reference::static_pending_offset = java_lang_ref_Reference::hc_static_pending_offset * x;
3788   // Artificial fields for java_lang_ref_Reference
3789   // The first field is for the discovered field added in 1.4
3790   java_lang_ref_Reference::number_of_fake_oop_fields = 1;
3791 
3792   // java_lang_ref_SoftReference Class
3793   java_lang_ref_SoftReference::timestamp_offset = align_size_up((java_lang_ref_SoftReference::hc_timestamp_offset * x + header), BytesPerLong);
3794   // Don't multiply static fields because they are always in wordSize units
3795   java_lang_ref_SoftReference::static_clock_offset = java_lang_ref_SoftReference::hc_static_clock_offset * x;
3796 
3797   // java_lang_ClassLoader
3798   java_lang_ClassLoader::parent_offset = java_lang_ClassLoader::hc_parent_offset * x + header;
3799 
3800   // java_lang_System
3801   java_lang_System::static_in_offset  = java_lang_System::hc_static_in_offset  * x;
3802   java_lang_System::static_out_offset = java_lang_System::hc_static_out_offset * x;
3803   java_lang_System::static_err_offset = java_lang_System::hc_static_err_offset * x;
3804   java_lang_System::static_security_offset = java_lang_System::hc_static_security_offset * x;
3805 
3806   // java_lang_StackTraceElement
3807   java_lang_StackTraceElement::moduleName_offset = java_lang_StackTraceElement::hc_moduleName_offset * x + header;
3808   java_lang_StackTraceElement::moduleVersion_offset = java_lang_StackTraceElement::hc_moduleVersion_offset * x + header;
3809   java_lang_StackTraceElement::declaringClass_offset = java_lang_StackTraceElement::hc_declaringClass_offset  * x + header;
3810   java_lang_StackTraceElement::methodName_offset = java_lang_StackTraceElement::hc_methodName_offset * x + header;
3811   java_lang_StackTraceElement::fileName_offset   = java_lang_StackTraceElement::hc_fileName_offset   * x + header;
3812   java_lang_StackTraceElement::lineNumber_offset = java_lang_StackTraceElement::hc_lineNumber_offset * x + header;
3813   java_lang_AssertionStatusDirectives::classes_offset = java_lang_AssertionStatusDirectives::hc_classes_offset * x + header;
3814   java_lang_AssertionStatusDirectives::classEnabled_offset = java_lang_AssertionStatusDirectives::hc_classEnabled_offset * x + header;
3815   java_lang_AssertionStatusDirectives::packages_offset = java_lang_AssertionStatusDirectives::hc_packages_offset * x + header;
3816   java_lang_AssertionStatusDirectives::packageEnabled_offset = java_lang_AssertionStatusDirectives::hc_packageEnabled_offset * x + header;
3817   java_lang_AssertionStatusDirectives::deflt_offset = java_lang_AssertionStatusDirectives::hc_deflt_offset * x + header;
3818 
3819 }
3820 
3821 
3822 // Compute non-hard-coded field offsets of all the classes in this file
3823 void JavaClasses::compute_offsets() {
3824   // java_lang_Class::compute_offsets was called earlier in bootstrap
3825   java_lang_ClassLoader::compute_offsets();
3826   java_lang_Throwable::compute_offsets();
3827   java_lang_Thread::compute_offsets();
3828   java_lang_ThreadGroup::compute_offsets();
3829   java_lang_invoke_MethodHandle::compute_offsets();
3830   java_lang_invoke_DirectMethodHandle::compute_offsets();
3831   java_lang_invoke_MemberName::compute_offsets();
3832   java_lang_invoke_LambdaForm::compute_offsets();
3833   java_lang_invoke_MethodType::compute_offsets();
3834   java_lang_invoke_CallSite::compute_offsets();
3835   java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets();
3836   java_security_AccessControlContext::compute_offsets();
3837   // Initialize reflection classes. The layouts of these classes
3838   // changed with the new reflection implementation in JDK 1.4, and
3839   // since the Universe doesn't know what JDK version it is until this
3840   // point we defer computation of these offsets until now.
3841   java_lang_reflect_AccessibleObject::compute_offsets();
3842   java_lang_reflect_Method::compute_offsets();
3843   java_lang_reflect_Constructor::compute_offsets();
3844   java_lang_reflect_Field::compute_offsets();
3845   java_nio_Buffer::compute_offsets();
3846   reflect_ConstantPool::compute_offsets();
3847   reflect_UnsafeStaticFieldAccessorImpl::compute_offsets();
3848   java_lang_reflect_Parameter::compute_offsets();
3849   java_lang_reflect_Module::compute_offsets();
3850   java_lang_StackFrameInfo::compute_offsets();
3851   java_lang_LiveStackFrameInfo::compute_offsets();
3852 
3853   // generated interpreter code wants to know about the offsets we just computed:
3854   AbstractAssembler::update_delayed_values();
3855 }
3856 
3857 #ifndef PRODUCT
3858 
3859 // These functions exist to assert the validity of hard-coded field offsets to guard
3860 // against changes in the class files
3861 
3862 bool JavaClasses::check_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
3863   EXCEPTION_MARK;
3864   fieldDescriptor fd;
3865   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3866   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3867   instanceKlassHandle h_klass (THREAD, k);
3868   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3869   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3870   if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
3871     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
3872     return false;
3873   }
3874   if (fd.is_static()) {
3875     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
3876     return false;
3877   }
3878   if (fd.offset() == hardcoded_offset ) {
3879     return true;
3880   } else {
3881     tty->print_cr("Offset of nonstatic field %s.%s is hardcoded as %d but should really be %d.",
3882                   klass_name, field_name, hardcoded_offset, fd.offset());
3883     return false;
3884   }
3885 }
3886 
3887 
3888 bool JavaClasses::check_static_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
3889   EXCEPTION_MARK;
3890   fieldDescriptor fd;
3891   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3892   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3893   instanceKlassHandle h_klass (THREAD, k);
3894   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3895   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3896   if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
3897     tty->print_cr("Static field %s.%s not found", klass_name, field_name);
3898     return false;
3899   }
3900   if (!fd.is_static()) {
3901     tty->print_cr("Static field %s.%s appears to be nonstatic", klass_name, field_name);
3902     return false;
3903   }
3904   if (fd.offset() == hardcoded_offset + InstanceMirrorKlass::offset_of_static_fields()) {
3905     return true;
3906   } else {
3907     tty->print_cr("Offset of static field %s.%s is hardcoded as %d but should really be %d.", klass_name, field_name, hardcoded_offset, fd.offset() - InstanceMirrorKlass::offset_of_static_fields());
3908     return false;
3909   }
3910 }
3911 
3912 
3913 bool JavaClasses::check_constant(const char *klass_name, int hardcoded_constant, const char *field_name, const char* field_sig) {
3914   EXCEPTION_MARK;
3915   fieldDescriptor fd;
3916   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3917   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3918   instanceKlassHandle h_klass (THREAD, k);
3919   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3920   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3921   if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
3922     tty->print_cr("Static field %s.%s not found", klass_name, field_name);
3923     return false;
3924   }
3925   if (!fd.is_static() || !fd.has_initial_value()) {
3926     tty->print_cr("Static field %s.%s appears to be non-constant", klass_name, field_name);
3927     return false;
3928   }
3929   if (!fd.initial_value_tag().is_int()) {
3930     tty->print_cr("Static field %s.%s is not an int", klass_name, field_name);
3931     return false;
3932   }
3933   jint field_value = fd.int_initial_value();
3934   if (field_value == hardcoded_constant) {
3935     return true;
3936   } else {
3937     tty->print_cr("Constant value of static field %s.%s is hardcoded as %d but should really be %d.", klass_name, field_name, hardcoded_constant, field_value);
3938     return false;
3939   }
3940 }
3941 
3942 
3943 // Check the hard-coded field offsets of all the classes in this file
3944 
3945 void JavaClasses::check_offsets() {
3946   bool valid = true;
3947   HandleMark hm;
3948 
3949 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
3950   valid &= check_offset(klass_name, cpp_klass_name :: field_name ## _offset, #field_name, field_sig)
3951 
3952 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
3953   valid &= check_offset(klass_name, cpp_klass_name :: long_ ## field_name ## _offset, #field_name, field_sig)
3954 
3955 #define CHECK_STATIC_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
3956   valid &= check_static_offset(klass_name, cpp_klass_name :: static_ ## field_name ## _offset, #field_name, field_sig)
3957 
3958 #define CHECK_CONSTANT(klass_name, cpp_klass_name, field_name, field_sig) \
3959   valid &= check_constant(klass_name, cpp_klass_name :: field_name, #field_name, field_sig)
3960 
3961   // java.lang.String
3962 
3963   CHECK_OFFSET("java/lang/String", java_lang_String, value, "[B");
3964   if (java_lang_String::has_hash_field()) {
3965     CHECK_OFFSET("java/lang/String", java_lang_String, hash, "I");
3966   }
3967   if (java_lang_String::has_coder_field()) {
3968     CHECK_OFFSET("java/lang/String", java_lang_String, coder, "B");
3969   }
3970 
3971   // java.lang.Class
3972 
3973   // Fake fields
3974   // CHECK_OFFSET("java/lang/Class", java_lang_Class, klass); // %%% this needs to be checked
3975   // CHECK_OFFSET("java/lang/Class", java_lang_Class, array_klass); // %%% this needs to be checked
3976 
3977   // java.lang.Throwable
3978 
3979   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, backtrace, "Ljava/lang/Object;");
3980   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, detailMessage, "Ljava/lang/String;");
3981   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, stackTrace, "[Ljava/lang/StackTraceElement;");
3982   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, depth, "I");
3983 
3984   // Boxed primitive objects (java_lang_boxing_object)
3985 
3986   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
3987   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
3988   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
3989   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
3990   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
3991   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
3992   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
3993   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
3994 
3995   // java.lang.ClassLoader
3996 
3997   CHECK_OFFSET("java/lang/ClassLoader", java_lang_ClassLoader, parent,        "Ljava/lang/ClassLoader;");
3998 
3999   // java.lang.System
4000 
4001   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System,  in, "Ljava/io/InputStream;");
4002   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, out, "Ljava/io/PrintStream;");
4003   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, err, "Ljava/io/PrintStream;");
4004   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, security, "Ljava/lang/SecurityManager;");
4005 
4006   // java.lang.StackTraceElement
4007 
4008   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, declaringClass, "Ljava/lang/String;");
4009   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, methodName, "Ljava/lang/String;");
4010   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement,   fileName, "Ljava/lang/String;");
4011   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, lineNumber, "I");
4012 
4013   // java.lang.ref.Reference
4014 
4015   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, referent, "Ljava/lang/Object;");
4016   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, queue, "Ljava/lang/ref/ReferenceQueue;");
4017   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, next, "Ljava/lang/ref/Reference;");
4018   // Fake field
4019   //CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, discovered, "Ljava/lang/ref/Reference;");
4020   CHECK_STATIC_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, lock, "Ljava/lang/ref/Reference$Lock;");
4021   CHECK_STATIC_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, pending, "Ljava/lang/ref/Reference;");
4022 
4023   // java.lang.ref.SoftReference
4024 
4025   CHECK_OFFSET("java/lang/ref/SoftReference", java_lang_ref_SoftReference, timestamp, "J");
4026   CHECK_STATIC_OFFSET("java/lang/ref/SoftReference", java_lang_ref_SoftReference, clock, "J");
4027 
4028   // java.lang.AssertionStatusDirectives
4029   //
4030   // The CheckAssertionStatusDirectives boolean can be removed from here and
4031   // globals.hpp after the AssertionStatusDirectives class has been integrated
4032   // into merlin "for some time."  Without it, the vm will fail with early
4033   // merlin builds.
4034 
4035   if (CheckAssertionStatusDirectives) {
4036     const char* nm = "java/lang/AssertionStatusDirectives";
4037     const char* sig = "[Ljava/lang/String;";
4038     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, classes, sig);
4039     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, classEnabled, "[Z");
4040     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, packages, sig);
4041     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, packageEnabled, "[Z");
4042     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, deflt, "Z");
4043   }
4044 
4045   if (!valid) vm_exit_during_initialization("Hard-coded field offset verification failed");
4046 }
4047 
4048 #endif // PRODUCT
4049 
4050 int InjectedField::compute_offset() {
4051   InstanceKlass* ik = InstanceKlass::cast(klass());
4052   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4053     if (!may_be_java && !fs.access_flags().is_internal()) {
4054       // Only look at injected fields
4055       continue;
4056     }
4057     if (fs.name() == name() && fs.signature() == signature()) {
4058       return fs.offset();
4059     }
4060   }
4061   ResourceMark rm;
4062   tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
4063 #ifndef PRODUCT
4064   ik->print();
4065   tty->print_cr("all fields:");
4066   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4067     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
4068   }
4069 #endif //PRODUCT
4070   vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
4071   return -1;
4072 }
4073 
4074 void javaClasses_init() {
4075   JavaClasses::compute_offsets();
4076   JavaClasses::check_offsets();
4077   FilteredFieldsMap::initialize();  // must be done after computing offsets.
4078 }
--- EOF ---