hotspot/src/share/vm/oops/instanceKlass.cpp
changeset 46369 3bf4544bec14
parent 44326 6c59cca7ff07
parent 46341 4c676683bdb9
child 46387 c46632622b17
equal deleted inserted replaced
44470:d10b1eca0b45 46369:3bf4544bec14
    86 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
    86 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
    87 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
    87 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
    88 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
    88 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
    89 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
    89 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
    90 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
    90 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
    91 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type)          \
    91 #define DTRACE_CLASSINIT_PROBE(type, thread_type)                \
    92   {                                                              \
    92   {                                                              \
    93     char* data = NULL;                                           \
    93     char* data = NULL;                                           \
    94     int len = 0;                                                 \
    94     int len = 0;                                                 \
    95     Symbol* name = (clss)->name();                               \
    95     Symbol* clss_name = name();                                  \
    96     if (name != NULL) {                                          \
    96     if (clss_name != NULL) {                                     \
    97       data = (char*)name->bytes();                               \
    97       data = (char*)clss_name->bytes();                          \
    98       len = name->utf8_length();                                 \
    98       len = clss_name->utf8_length();                            \
    99     }                                                            \
    99     }                                                            \
   100     HOTSPOT_CLASS_INITIALIZATION_##type(                         \
   100     HOTSPOT_CLASS_INITIALIZATION_##type(                         \
   101       data, len, (clss)->class_loader(), thread_type);           \
   101       data, len, class_loader(), thread_type);                   \
   102   }
   102   }
   103 
   103 
   104 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \
   104 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)     \
   105   {                                                              \
   105   {                                                              \
   106     char* data = NULL;                                           \
   106     char* data = NULL;                                           \
   107     int len = 0;                                                 \
   107     int len = 0;                                                 \
   108     Symbol* name = (clss)->name();                               \
   108     Symbol* clss_name = name();                                  \
   109     if (name != NULL) {                                          \
   109     if (clss_name != NULL) {                                     \
   110       data = (char*)name->bytes();                               \
   110       data = (char*)clss_name->bytes();                          \
   111       len = name->utf8_length();                                 \
   111       len = clss_name->utf8_length();                            \
   112     }                                                            \
   112     }                                                            \
   113     HOTSPOT_CLASS_INITIALIZATION_##type(                         \
   113     HOTSPOT_CLASS_INITIALIZATION_##type(                         \
   114       data, len, (clss)->class_loader(), thread_type, wait);     \
   114       data, len, class_loader(), thread_type, wait);             \
   115   }
   115   }
   116 
   116 
   117 #else //  ndef DTRACE_ENABLED
   117 #else //  ndef DTRACE_ENABLED
   118 
   118 
   119 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type)
   119 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
   120 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait)
   120 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
   121 
   121 
   122 #endif //  ndef DTRACE_ENABLED
   122 #endif //  ndef DTRACE_ENABLED
   123 
   123 
   124 volatile int InstanceKlass::_total_instanceKlass_count = 0;
   124 volatile int InstanceKlass::_total_instanceKlass_count = 0;
   125 
   125 
   372 bool InstanceKlass::should_be_initialized() const {
   372 bool InstanceKlass::should_be_initialized() const {
   373   return !is_initialized();
   373   return !is_initialized();
   374 }
   374 }
   375 
   375 
   376 klassItable* InstanceKlass::itable() const {
   376 klassItable* InstanceKlass::itable() const {
   377   return new klassItable(instanceKlassHandle(this));
   377   return new klassItable(const_cast<InstanceKlass*>(this));
   378 }
   378 }
   379 
   379 
   380 void InstanceKlass::eager_initialize(Thread *thread) {
   380 void InstanceKlass::eager_initialize(Thread *thread) {
   381   if (!EagerInitialization) return;
   381   if (!EagerInitialization) return;
   382 
   382 
   383   if (this->is_not_initialized()) {
   383   if (this->is_not_initialized()) {
   384     // abort if the the class has a class initializer
   384     // abort if the the class has a class initializer
   385     if (this->class_initializer() != NULL) return;
   385     if (this->class_initializer() != NULL) return;
   386 
   386 
   387     // abort if it is java.lang.Object (initialization is handled in genesis)
   387     // abort if it is java.lang.Object (initialization is handled in genesis)
   388     Klass* super = this->super();
   388     Klass* super_klass = super();
   389     if (super == NULL) return;
   389     if (super_klass == NULL) return;
   390 
   390 
   391     // abort if the super class should be initialized
   391     // abort if the super class should be initialized
   392     if (!InstanceKlass::cast(super)->is_initialized()) return;
   392     if (!InstanceKlass::cast(super_klass)->is_initialized()) return;
   393 
   393 
   394     // call body to expose the this pointer
   394     // call body to expose the this pointer
   395     instanceKlassHandle this_k(thread, this);
   395     eager_initialize_impl();
   396     eager_initialize_impl(this_k);
       
   397   }
   396   }
   398 }
   397 }
   399 
   398 
   400 // JVMTI spec thinks there are signers and protection domain in the
   399 // JVMTI spec thinks there are signers and protection domain in the
   401 // instanceKlass.  These accessors pretend these fields are there.
   400 // instanceKlass.  These accessors pretend these fields are there.
   430   OrderAccess::storestore();
   429   OrderAccess::storestore();
   431   java_lang_Class::set_init_lock(java_mirror(), NULL);
   430   java_lang_Class::set_init_lock(java_mirror(), NULL);
   432   assert(!is_not_initialized(), "class must be initialized now");
   431   assert(!is_not_initialized(), "class must be initialized now");
   433 }
   432 }
   434 
   433 
   435 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_k) {
   434 void InstanceKlass::eager_initialize_impl() {
   436   EXCEPTION_MARK;
   435   EXCEPTION_MARK;
   437   oop init_lock = this_k->init_lock();
   436   HandleMark hm(THREAD);
   438   ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
   437   Handle h_init_lock(THREAD, init_lock());
       
   438   ObjectLocker ol(h_init_lock, THREAD, init_lock() != NULL);
   439 
   439 
   440   // abort if someone beat us to the initialization
   440   // abort if someone beat us to the initialization
   441   if (!this_k->is_not_initialized()) return;  // note: not equivalent to is_initialized()
   441   if (!is_not_initialized()) return;  // note: not equivalent to is_initialized()
   442 
   442 
   443   ClassState old_state = this_k->init_state();
   443   ClassState old_state = init_state();
   444   link_class_impl(this_k, true, THREAD);
   444   link_class_impl(true, THREAD);
   445   if (HAS_PENDING_EXCEPTION) {
   445   if (HAS_PENDING_EXCEPTION) {
   446     CLEAR_PENDING_EXCEPTION;
   446     CLEAR_PENDING_EXCEPTION;
   447     // Abort if linking the class throws an exception.
   447     // Abort if linking the class throws an exception.
   448 
   448 
   449     // Use a test to avoid redundantly resetting the state if there's
   449     // Use a test to avoid redundantly resetting the state if there's
   450     // no change.  Set_init_state() asserts that state changes make
   450     // no change.  Set_init_state() asserts that state changes make
   451     // progress, whereas here we might just be spinning in place.
   451     // progress, whereas here we might just be spinning in place.
   452     if( old_state != this_k->_init_state )
   452     if (old_state != _init_state)
   453       this_k->set_init_state (old_state);
   453       set_init_state(old_state);
   454   } else {
   454   } else {
   455     // linking successfull, mark class as initialized
   455     // linking successfull, mark class as initialized
   456     this_k->set_init_state (fully_initialized);
   456     set_init_state(fully_initialized);
   457     this_k->fence_and_clear_init_lock();
   457     fence_and_clear_init_lock();
   458     // trace
   458     // trace
   459     if (log_is_enabled(Info, class, init)) {
   459     if (log_is_enabled(Info, class, init)) {
   460       ResourceMark rm(THREAD);
   460       ResourceMark rm(THREAD);
   461       log_info(class, init)("[Initialized %s without side effects]", this_k->external_name());
   461       log_info(class, init)("[Initialized %s without side effects]", external_name());
   462     }
   462     }
   463   }
   463   }
   464 }
   464 }
   465 
   465 
   466 
   466 
   467 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
   467 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
   468 // process. The step comments refers to the procedure described in that section.
   468 // process. The step comments refers to the procedure described in that section.
   469 // Note: implementation moved to static method to expose the this pointer.
   469 // Note: implementation moved to static method to expose the this pointer.
   470 void InstanceKlass::initialize(TRAPS) {
   470 void InstanceKlass::initialize(TRAPS) {
   471   if (this->should_be_initialized()) {
   471   if (this->should_be_initialized()) {
   472     HandleMark hm(THREAD);
   472     initialize_impl(CHECK);
   473     instanceKlassHandle this_k(THREAD, this);
       
   474     initialize_impl(this_k, CHECK);
       
   475     // Note: at this point the class may be initialized
   473     // Note: at this point the class may be initialized
   476     //       OR it may be in the state of being initialized
   474     //       OR it may be in the state of being initialized
   477     //       in case of recursive initialization!
   475     //       in case of recursive initialization!
   478   } else {
   476   } else {
   479     assert(is_initialized(), "sanity check");
   477     assert(is_initialized(), "sanity check");
   480   }
   478   }
   481 }
   479 }
   482 
   480 
   483 
   481 
   484 bool InstanceKlass::verify_code(
   482 bool InstanceKlass::verify_code(bool throw_verifyerror, TRAPS) {
   485     instanceKlassHandle this_k, bool throw_verifyerror, TRAPS) {
       
   486   // 1) Verify the bytecodes
   483   // 1) Verify the bytecodes
   487   Verifier::Mode mode =
   484   Verifier::Mode mode =
   488     throw_verifyerror ? Verifier::ThrowException : Verifier::NoException;
   485     throw_verifyerror ? Verifier::ThrowException : Verifier::NoException;
   489   return Verifier::verify(this_k, mode, this_k->should_verify_class(), THREAD);
   486   return Verifier::verify(this, mode, should_verify_class(), THREAD);
   490 }
   487 }
   491 
   488 
   492 
   489 
   493 // Used exclusively by the shared spaces dump mechanism to prevent
   490 // Used exclusively by the shared spaces dump mechanism to prevent
   494 // classes mapped into the shared regions in new VMs from appearing linked.
   491 // classes mapped into the shared regions in new VMs from appearing linked.
   499 }
   496 }
   500 
   497 
   501 void InstanceKlass::link_class(TRAPS) {
   498 void InstanceKlass::link_class(TRAPS) {
   502   assert(is_loaded(), "must be loaded");
   499   assert(is_loaded(), "must be loaded");
   503   if (!is_linked()) {
   500   if (!is_linked()) {
   504     HandleMark hm(THREAD);
   501     link_class_impl(true, CHECK);
   505     instanceKlassHandle this_k(THREAD, this);
       
   506     link_class_impl(this_k, true, CHECK);
       
   507   }
   502   }
   508 }
   503 }
   509 
   504 
   510 // Called to verify that a class can link during initialization, without
   505 // Called to verify that a class can link during initialization, without
   511 // throwing a VerifyError.
   506 // throwing a VerifyError.
   512 bool InstanceKlass::link_class_or_fail(TRAPS) {
   507 bool InstanceKlass::link_class_or_fail(TRAPS) {
   513   assert(is_loaded(), "must be loaded");
   508   assert(is_loaded(), "must be loaded");
   514   if (!is_linked()) {
   509   if (!is_linked()) {
   515     HandleMark hm(THREAD);
   510     link_class_impl(false, CHECK_false);
   516     instanceKlassHandle this_k(THREAD, this);
       
   517     link_class_impl(this_k, false, CHECK_false);
       
   518   }
   511   }
   519   return is_linked();
   512   return is_linked();
   520 }
   513 }
   521 
   514 
   522 bool InstanceKlass::link_class_impl(
   515 bool InstanceKlass::link_class_impl(bool throw_verifyerror, TRAPS) {
   523     instanceKlassHandle this_k, bool throw_verifyerror, TRAPS) {
   516   if (DumpSharedSpaces && is_in_error_state()) {
   524   if (DumpSharedSpaces && this_k->is_in_error_state()) {
       
   525     // This is for CDS dumping phase only -- we use the in_error_state to indicate that
   517     // This is for CDS dumping phase only -- we use the in_error_state to indicate that
   526     // the class has failed verification. Throwing the NoClassDefFoundError here is just
   518     // the class has failed verification. Throwing the NoClassDefFoundError here is just
   527     // a convenient way to stop repeat attempts to verify the same (bad) class.
   519     // a convenient way to stop repeat attempts to verify the same (bad) class.
   528     //
   520     //
   529     // Note that the NoClassDefFoundError is not part of the JLS, and should not be thrown
   521     // Note that the NoClassDefFoundError is not part of the JLS, and should not be thrown
   530     // if we are executing Java code. This is not a problem for CDS dumping phase since
   522     // if we are executing Java code. This is not a problem for CDS dumping phase since
   531     // it doesn't execute any Java code.
   523     // it doesn't execute any Java code.
   532     ResourceMark rm(THREAD);
   524     ResourceMark rm(THREAD);
   533     THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(),
   525     THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(), external_name(), false);
   534                this_k->external_name(), false);
       
   535   }
   526   }
   536   // return if already verified
   527   // return if already verified
   537   if (this_k->is_linked()) {
   528   if (is_linked()) {
   538     return true;
   529     return true;
   539   }
   530   }
   540 
   531 
   541   // Timing
   532   // Timing
   542   // timer handles recursion
   533   // timer handles recursion
   543   assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl");
   534   assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl");
   544   JavaThread* jt = (JavaThread*)THREAD;
   535   JavaThread* jt = (JavaThread*)THREAD;
   545 
   536 
   546   // link super class before linking this class
   537   // link super class before linking this class
   547   instanceKlassHandle super(THREAD, this_k->super());
   538   Klass* super_klass = super();
   548   if (super.not_null()) {
   539   if (super_klass != NULL) {
   549     if (super->is_interface()) {  // check if super class is an interface
   540     if (super_klass->is_interface()) {  // check if super class is an interface
   550       ResourceMark rm(THREAD);
   541       ResourceMark rm(THREAD);
   551       Exceptions::fthrow(
   542       Exceptions::fthrow(
   552         THREAD_AND_LOCATION,
   543         THREAD_AND_LOCATION,
   553         vmSymbols::java_lang_IncompatibleClassChangeError(),
   544         vmSymbols::java_lang_IncompatibleClassChangeError(),
   554         "class %s has interface %s as super class",
   545         "class %s has interface %s as super class",
   555         this_k->external_name(),
   546         external_name(),
   556         super->external_name()
   547         super_klass->external_name()
   557       );
   548       );
   558       return false;
   549       return false;
   559     }
   550     }
   560 
   551 
   561     link_class_impl(super, throw_verifyerror, CHECK_false);
   552     InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
       
   553     ik_super->link_class_impl(throw_verifyerror, CHECK_false);
   562   }
   554   }
   563 
   555 
   564   // link all interfaces implemented by this class before linking this class
   556   // link all interfaces implemented by this class before linking this class
   565   Array<Klass*>* interfaces = this_k->local_interfaces();
   557   Array<Klass*>* interfaces = local_interfaces();
   566   int num_interfaces = interfaces->length();
   558   int num_interfaces = interfaces->length();
   567   for (int index = 0; index < num_interfaces; index++) {
   559   for (int index = 0; index < num_interfaces; index++) {
   568     HandleMark hm(THREAD);
   560     InstanceKlass* interk = InstanceKlass::cast(interfaces->at(index));
   569     instanceKlassHandle ih(THREAD, interfaces->at(index));
   561     interk->link_class_impl(throw_verifyerror, CHECK_false);
   570     link_class_impl(ih, throw_verifyerror, CHECK_false);
       
   571   }
   562   }
   572 
   563 
   573   // in case the class is linked in the process of linking its superclasses
   564   // in case the class is linked in the process of linking its superclasses
   574   if (this_k->is_linked()) {
   565   if (is_linked()) {
   575     return true;
   566     return true;
   576   }
   567   }
   577 
   568 
   578   // trace only the link time for this klass that includes
   569   // trace only the link time for this klass that includes
   579   // the verification time
   570   // the verification time
   584                              jt->get_thread_stat()->perf_timers_addr(),
   575                              jt->get_thread_stat()->perf_timers_addr(),
   585                              PerfClassTraceTime::CLASS_LINK);
   576                              PerfClassTraceTime::CLASS_LINK);
   586 
   577 
   587   // verification & rewriting
   578   // verification & rewriting
   588   {
   579   {
   589     oop init_lock = this_k->init_lock();
   580     HandleMark hm(THREAD);
   590     ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
   581     Handle h_init_lock(THREAD, init_lock());
       
   582     ObjectLocker ol(h_init_lock, THREAD, init_lock() != NULL);
   591     // rewritten will have been set if loader constraint error found
   583     // rewritten will have been set if loader constraint error found
   592     // on an earlier link attempt
   584     // on an earlier link attempt
   593     // don't verify or rewrite if already rewritten
   585     // don't verify or rewrite if already rewritten
   594 
   586     //
   595     if (!this_k->is_linked()) {
   587 
   596       if (!this_k->is_rewritten()) {
   588     if (!is_linked()) {
       
   589       if (!is_rewritten()) {
   597         {
   590         {
   598           bool verify_ok = verify_code(this_k, throw_verifyerror, THREAD);
   591           bool verify_ok = verify_code(throw_verifyerror, THREAD);
   599           if (!verify_ok) {
   592           if (!verify_ok) {
   600             return false;
   593             return false;
   601           }
   594           }
   602         }
   595         }
   603 
   596 
   604         // Just in case a side-effect of verify linked this class already
   597         // Just in case a side-effect of verify linked this class already
   605         // (which can sometimes happen since the verifier loads classes
   598         // (which can sometimes happen since the verifier loads classes
   606         // using custom class loaders, which are free to initialize things)
   599         // using custom class loaders, which are free to initialize things)
   607         if (this_k->is_linked()) {
   600         if (is_linked()) {
   608           return true;
   601           return true;
   609         }
   602         }
   610 
   603 
   611         // also sets rewritten
   604         // also sets rewritten
   612         this_k->rewrite_class(CHECK_false);
   605         rewrite_class(CHECK_false);
   613       } else if (this_k->is_shared()) {
   606       } else if (is_shared()) {
   614         SystemDictionaryShared::check_verification_constraints(this_k, CHECK_false);
   607         SystemDictionaryShared::check_verification_constraints(this, CHECK_false);
   615       }
   608       }
   616 
   609 
   617       // relocate jsrs and link methods after they are all rewritten
   610       // relocate jsrs and link methods after they are all rewritten
   618       this_k->link_methods(CHECK_false);
   611       link_methods(CHECK_false);
   619 
   612 
   620       // Initialize the vtable and interface table after
   613       // Initialize the vtable and interface table after
   621       // methods have been rewritten since rewrite may
   614       // methods have been rewritten since rewrite may
   622       // fabricate new Method*s.
   615       // fabricate new Method*s.
   623       // also does loader constraint checking
   616       // also does loader constraint checking
   624       //
   617       //
   625       // initialize_vtable and initialize_itable need to be rerun for
   618       // initialize_vtable and initialize_itable need to be rerun for
   626       // a shared class if the class is not loaded by the NULL classloader.
   619       // a shared class if the class is not loaded by the NULL classloader.
   627       ClassLoaderData * loader_data = this_k->class_loader_data();
   620       ClassLoaderData * loader_data = class_loader_data();
   628       if (!(this_k->is_shared() &&
   621       if (!(is_shared() &&
   629             loader_data->is_the_null_class_loader_data())) {
   622             loader_data->is_the_null_class_loader_data())) {
   630         ResourceMark rm(THREAD);
   623         ResourceMark rm(THREAD);
   631         this_k->vtable()->initialize_vtable(true, CHECK_false);
   624         vtable()->initialize_vtable(true, CHECK_false);
   632         this_k->itable()->initialize_itable(true, CHECK_false);
   625         itable()->initialize_itable(true, CHECK_false);
   633       }
   626       }
   634 #ifdef ASSERT
   627 #ifdef ASSERT
   635       else {
   628       else {
   636         ResourceMark rm(THREAD);
   629         ResourceMark rm(THREAD);
   637         this_k->vtable()->verify(tty, true);
   630         vtable()->verify(tty, true);
   638         // In case itable verification is ever added.
   631         // In case itable verification is ever added.
   639         // this_k->itable()->verify(tty, true);
   632         // itable()->verify(tty, true);
   640       }
   633       }
   641 #endif
   634 #endif
   642       this_k->set_init_state(linked);
   635       set_init_state(linked);
   643       if (JvmtiExport::should_post_class_prepare()) {
   636       if (JvmtiExport::should_post_class_prepare()) {
   644         Thread *thread = THREAD;
   637         Thread *thread = THREAD;
   645         assert(thread->is_Java_thread(), "thread->is_Java_thread()");
   638         assert(thread->is_Java_thread(), "thread->is_Java_thread()");
   646         JvmtiExport::post_class_prepare((JavaThread *) thread, this_k());
   639         JvmtiExport::post_class_prepare((JavaThread *) thread, this);
   647       }
   640       }
   648     }
   641     }
   649   }
   642   }
   650   return true;
   643   return true;
   651 }
   644 }
   654 // Rewrite the byte codes of all of the methods of a class.
   647 // Rewrite the byte codes of all of the methods of a class.
   655 // The rewriter must be called exactly once. Rewriting must happen after
   648 // The rewriter must be called exactly once. Rewriting must happen after
   656 // verification but before the first method of the class is executed.
   649 // verification but before the first method of the class is executed.
   657 void InstanceKlass::rewrite_class(TRAPS) {
   650 void InstanceKlass::rewrite_class(TRAPS) {
   658   assert(is_loaded(), "must be loaded");
   651   assert(is_loaded(), "must be loaded");
   659   instanceKlassHandle this_k(THREAD, this);
   652   if (is_rewritten()) {
   660   if (this_k->is_rewritten()) {
   653     assert(is_shared(), "rewriting an unshared class?");
   661     assert(this_k()->is_shared(), "rewriting an unshared class?");
       
   662     return;
   654     return;
   663   }
   655   }
   664   Rewriter::rewrite(this_k, CHECK);
   656   Rewriter::rewrite(this, CHECK);
   665   this_k->set_rewritten();
   657   set_rewritten();
   666 }
   658 }
   667 
   659 
   668 // Now relocate and link method entry points after class is rewritten.
   660 // Now relocate and link method entry points after class is rewritten.
   669 // This is outside is_rewritten flag. In case of an exception, it can be
   661 // This is outside is_rewritten flag. In case of an exception, it can be
   670 // executed more than once.
   662 // executed more than once.
   677     m->link_method(m, CHECK);
   669     m->link_method(m, CHECK);
   678   }
   670   }
   679 }
   671 }
   680 
   672 
   681 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
   673 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
   682 void InstanceKlass::initialize_super_interfaces(instanceKlassHandle this_k, TRAPS) {
   674 void InstanceKlass::initialize_super_interfaces(TRAPS) {
   683   assert (this_k->has_nonstatic_concrete_methods(), "caller should have checked this");
   675   assert (has_nonstatic_concrete_methods(), "caller should have checked this");
   684   for (int i = 0; i < this_k->local_interfaces()->length(); ++i) {
   676   for (int i = 0; i < local_interfaces()->length(); ++i) {
   685     Klass* iface = this_k->local_interfaces()->at(i);
   677     Klass* iface = local_interfaces()->at(i);
   686     InstanceKlass* ik = InstanceKlass::cast(iface);
   678     InstanceKlass* ik = InstanceKlass::cast(iface);
   687 
   679 
   688     // Initialization is depth first search ie. we start with top of the inheritance tree
   680     // Initialization is depth first search ie. we start with top of the inheritance tree
   689     // has_nonstatic_concrete_methods drives searching superinterfaces since it
   681     // has_nonstatic_concrete_methods drives searching superinterfaces since it
   690     // means has_nonstatic_concrete_methods in its superinterface hierarchy
   682     // means has_nonstatic_concrete_methods in its superinterface hierarchy
   691     if (ik->has_nonstatic_concrete_methods()) {
   683     if (ik->has_nonstatic_concrete_methods()) {
   692       ik->initialize_super_interfaces(ik, CHECK);
   684       ik->initialize_super_interfaces(CHECK);
   693     }
   685     }
   694 
   686 
   695     // Only initialize() interfaces that "declare" concrete methods.
   687     // Only initialize() interfaces that "declare" concrete methods.
   696     if (ik->should_be_initialized() && ik->declares_nonstatic_concrete_methods()) {
   688     if (ik->should_be_initialized() && ik->declares_nonstatic_concrete_methods()) {
   697       ik->initialize(CHECK);
   689       ik->initialize(CHECK);
   698     }
   690     }
   699   }
   691   }
   700 }
   692 }
   701 
   693 
   702 void InstanceKlass::initialize_impl(instanceKlassHandle this_k, TRAPS) {
   694 void InstanceKlass::initialize_impl(TRAPS) {
       
   695   HandleMark hm(THREAD);
       
   696 
   703   // Make sure klass is linked (verified) before initialization
   697   // Make sure klass is linked (verified) before initialization
   704   // A class could already be verified, since it has been reflected upon.
   698   // A class could already be verified, since it has been reflected upon.
   705   this_k->link_class(CHECK);
   699   link_class(CHECK);
   706 
   700 
   707   DTRACE_CLASSINIT_PROBE(required, this_k(), -1);
   701   DTRACE_CLASSINIT_PROBE(required, -1);
   708 
   702 
   709   bool wait = false;
   703   bool wait = false;
   710 
   704 
   711   // refer to the JVM book page 47 for description of steps
   705   // refer to the JVM book page 47 for description of steps
   712   // Step 1
   706   // Step 1
   713   {
   707   {
   714     oop init_lock = this_k->init_lock();
   708     Handle h_init_lock(THREAD, init_lock());
   715     ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
   709     ObjectLocker ol(h_init_lock, THREAD, init_lock() != NULL);
   716 
   710 
   717     Thread *self = THREAD; // it's passed the current thread
   711     Thread *self = THREAD; // it's passed the current thread
   718 
   712 
   719     // Step 2
   713     // Step 2
   720     // If we were to use wait() instead of waitInterruptibly() then
   714     // If we were to use wait() instead of waitInterruptibly() then
   721     // we might end up throwing IE from link/symbol resolution sites
   715     // we might end up throwing IE from link/symbol resolution sites
   722     // that aren't expected to throw.  This would wreak havoc.  See 6320309.
   716     // that aren't expected to throw.  This would wreak havoc.  See 6320309.
   723     while(this_k->is_being_initialized() && !this_k->is_reentrant_initialization(self)) {
   717     while(is_being_initialized() && !is_reentrant_initialization(self)) {
   724         wait = true;
   718         wait = true;
   725       ol.waitUninterruptibly(CHECK);
   719       ol.waitUninterruptibly(CHECK);
   726     }
   720     }
   727 
   721 
   728     // Step 3
   722     // Step 3
   729     if (this_k->is_being_initialized() && this_k->is_reentrant_initialization(self)) {
   723     if (is_being_initialized() && is_reentrant_initialization(self)) {
   730       DTRACE_CLASSINIT_PROBE_WAIT(recursive, this_k(), -1,wait);
   724       DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
   731       return;
   725       return;
   732     }
   726     }
   733 
   727 
   734     // Step 4
   728     // Step 4
   735     if (this_k->is_initialized()) {
   729     if (is_initialized()) {
   736       DTRACE_CLASSINIT_PROBE_WAIT(concurrent, this_k(), -1,wait);
   730       DTRACE_CLASSINIT_PROBE_WAIT(concurrent, -1, wait);
   737       return;
   731       return;
   738     }
   732     }
   739 
   733 
   740     // Step 5
   734     // Step 5
   741     if (this_k->is_in_error_state()) {
   735     if (is_in_error_state()) {
   742       DTRACE_CLASSINIT_PROBE_WAIT(erroneous, this_k(), -1,wait);
   736       DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
   743       ResourceMark rm(THREAD);
   737       ResourceMark rm(THREAD);
   744       const char* desc = "Could not initialize class ";
   738       const char* desc = "Could not initialize class ";
   745       const char* className = this_k->external_name();
   739       const char* className = external_name();
   746       size_t msglen = strlen(desc) + strlen(className) + 1;
   740       size_t msglen = strlen(desc) + strlen(className) + 1;
   747       char* message = NEW_RESOURCE_ARRAY(char, msglen);
   741       char* message = NEW_RESOURCE_ARRAY(char, msglen);
   748       if (NULL == message) {
   742       if (NULL == message) {
   749         // Out of memory: can't create detailed error message
   743         // Out of memory: can't create detailed error message
   750         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
   744         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
   753         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
   747         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
   754       }
   748       }
   755     }
   749     }
   756 
   750 
   757     // Step 6
   751     // Step 6
   758     this_k->set_init_state(being_initialized);
   752     set_init_state(being_initialized);
   759     this_k->set_init_thread(self);
   753     set_init_thread(self);
   760   }
   754   }
   761 
   755 
   762   // Step 7
   756   // Step 7
   763   // Next, if C is a class rather than an interface, initialize it's super class and super
   757   // Next, if C is a class rather than an interface, initialize it's super class and super
   764   // interfaces.
   758   // interfaces.
   765   if (!this_k->is_interface()) {
   759   if (!is_interface()) {
   766     Klass* super_klass = this_k->super();
   760     Klass* super_klass = super();
   767     if (super_klass != NULL && super_klass->should_be_initialized()) {
   761     if (super_klass != NULL && super_klass->should_be_initialized()) {
   768       super_klass->initialize(THREAD);
   762       super_klass->initialize(THREAD);
   769     }
   763     }
   770     // If C implements any interface that declares a non-static, concrete method,
   764     // If C implements any interface that declares a non-static, concrete method,
   771     // the initialization of C triggers initialization of its super interfaces.
   765     // the initialization of C triggers initialization of its super interfaces.
   772     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
   766     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
   773     // having a superinterface that declares, non-static, concrete methods
   767     // having a superinterface that declares, non-static, concrete methods
   774     if (!HAS_PENDING_EXCEPTION && this_k->has_nonstatic_concrete_methods()) {
   768     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
   775       this_k->initialize_super_interfaces(this_k, THREAD);
   769       initialize_super_interfaces(THREAD);
   776     }
   770     }
   777 
   771 
   778     // If any exceptions, complete abruptly, throwing the same exception as above.
   772     // If any exceptions, complete abruptly, throwing the same exception as above.
   779     if (HAS_PENDING_EXCEPTION) {
   773     if (HAS_PENDING_EXCEPTION) {
   780       Handle e(THREAD, PENDING_EXCEPTION);
   774       Handle e(THREAD, PENDING_EXCEPTION);
   781       CLEAR_PENDING_EXCEPTION;
   775       CLEAR_PENDING_EXCEPTION;
   782       {
   776       {
   783         EXCEPTION_MARK;
   777         EXCEPTION_MARK;
   784         // Locks object, set state, and notify all waiting threads
   778         // Locks object, set state, and notify all waiting threads
   785         this_k->set_initialization_state_and_notify(initialization_error, THREAD);
   779         set_initialization_state_and_notify(initialization_error, THREAD);
   786         CLEAR_PENDING_EXCEPTION;
   780         CLEAR_PENDING_EXCEPTION;
   787       }
   781       }
   788       DTRACE_CLASSINIT_PROBE_WAIT(super__failed, this_k(), -1,wait);
   782       DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
   789       THROW_OOP(e());
   783       THROW_OOP(e());
   790     }
   784     }
   791   }
   785   }
   792 
   786 
   793 
   787 
   794   // Look for aot compiled methods for this klass, including class initializer.
   788   // Look for aot compiled methods for this klass, including class initializer.
   795   AOTLoader::load_for_klass(this_k, THREAD);
   789   AOTLoader::load_for_klass(this, THREAD);
   796 
   790 
   797   // Step 8
   791   // Step 8
   798   {
   792   {
   799     assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
   793     assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
   800     JavaThread* jt = (JavaThread*)THREAD;
   794     JavaThread* jt = (JavaThread*)THREAD;
   801     DTRACE_CLASSINIT_PROBE_WAIT(clinit, this_k(), -1,wait);
   795     DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
   802     // Timer includes any side effects of class initialization (resolution,
   796     // Timer includes any side effects of class initialization (resolution,
   803     // etc), but not recursive entry into call_class_initializer().
   797     // etc), but not recursive entry into call_class_initializer().
   804     PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
   798     PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
   805                              ClassLoader::perf_class_init_selftime(),
   799                              ClassLoader::perf_class_init_selftime(),
   806                              ClassLoader::perf_classes_inited(),
   800                              ClassLoader::perf_classes_inited(),
   807                              jt->get_thread_stat()->perf_recursion_counts_addr(),
   801                              jt->get_thread_stat()->perf_recursion_counts_addr(),
   808                              jt->get_thread_stat()->perf_timers_addr(),
   802                              jt->get_thread_stat()->perf_timers_addr(),
   809                              PerfClassTraceTime::CLASS_CLINIT);
   803                              PerfClassTraceTime::CLASS_CLINIT);
   810     this_k->call_class_initializer(THREAD);
   804     call_class_initializer(THREAD);
   811   }
   805   }
   812 
   806 
   813   // Step 9
   807   // Step 9
   814   if (!HAS_PENDING_EXCEPTION) {
   808   if (!HAS_PENDING_EXCEPTION) {
   815     this_k->set_initialization_state_and_notify(fully_initialized, CHECK);
   809     set_initialization_state_and_notify(fully_initialized, CHECK);
   816     { ResourceMark rm(THREAD);
   810     { ResourceMark rm(THREAD);
   817       debug_only(this_k->vtable()->verify(tty, true);)
   811       debug_only(vtable()->verify(tty, true);)
   818     }
   812     }
   819   }
   813   }
   820   else {
   814   else {
   821     // Step 10 and 11
   815     // Step 10 and 11
   822     Handle e(THREAD, PENDING_EXCEPTION);
   816     Handle e(THREAD, PENDING_EXCEPTION);
   824     // JVMTI has already reported the pending exception
   818     // JVMTI has already reported the pending exception
   825     // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
   819     // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
   826     JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
   820     JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
   827     {
   821     {
   828       EXCEPTION_MARK;
   822       EXCEPTION_MARK;
   829       this_k->set_initialization_state_and_notify(initialization_error, THREAD);
   823       set_initialization_state_and_notify(initialization_error, THREAD);
   830       CLEAR_PENDING_EXCEPTION;   // ignore any exception thrown, class initialization error is thrown below
   824       CLEAR_PENDING_EXCEPTION;   // ignore any exception thrown, class initialization error is thrown below
   831       // JVMTI has already reported the pending exception
   825       // JVMTI has already reported the pending exception
   832       // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
   826       // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
   833       JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
   827       JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
   834     }
   828     }
   835     DTRACE_CLASSINIT_PROBE_WAIT(error, this_k(), -1,wait);
   829     DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
   836     if (e->is_a(SystemDictionary::Error_klass())) {
   830     if (e->is_a(SystemDictionary::Error_klass())) {
   837       THROW_OOP(e());
   831       THROW_OOP(e());
   838     } else {
   832     } else {
   839       JavaCallArguments args(e);
   833       JavaCallArguments args(e);
   840       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
   834       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
   841                 vmSymbols::throwable_void_signature(),
   835                 vmSymbols::throwable_void_signature(),
   842                 &args);
   836                 &args);
   843     }
   837     }
   844   }
   838   }
   845   DTRACE_CLASSINIT_PROBE_WAIT(end, this_k(), -1,wait);
   839   DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
   846 }
   840 }
   847 
   841 
   848 
   842 
   849 // Note: implementation moved to static method to expose the this pointer.
       
   850 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
   843 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
   851   instanceKlassHandle kh(THREAD, this);
   844   Handle h_init_lock(THREAD, init_lock());
   852   set_initialization_state_and_notify_impl(kh, state, CHECK);
   845   if (init_lock() != NULL) {
   853 }
   846     ObjectLocker ol(h_init_lock, THREAD);
   854 
   847     set_init_state(state);
   855 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_k, ClassState state, TRAPS) {
   848     fence_and_clear_init_lock();
   856   oop init_lock = this_k->init_lock();
       
   857   if (init_lock != NULL) {
       
   858     ObjectLocker ol(init_lock, THREAD);
       
   859     this_k->set_init_state(state);
       
   860     this_k->fence_and_clear_init_lock();
       
   861     ol.notify_all(CHECK);
   849     ol.notify_all(CHECK);
   862   } else {
   850   } else {
   863     assert(init_lock != NULL, "The initialization state should never be set twice");
   851     assert(init_lock() != NULL, "The initialization state should never be set twice");
   864     this_k->set_init_state(state);
   852     set_init_state(state);
   865   }
   853   }
   866 }
   854 }
   867 
   855 
   868 // The embedded _implementor field can only record one implementor.
   856 // The embedded _implementor field can only record one implementor.
   869 // When there are more than one implementors, the _implementor field
   857 // When there are more than one implementors, the _implementor field
   992     JvmtiExport::post_array_size_exhausted();
   980     JvmtiExport::post_array_size_exhausted();
   993     THROW_OOP_0(Universe::out_of_memory_error_array_size());
   981     THROW_OOP_0(Universe::out_of_memory_error_array_size());
   994   }
   982   }
   995   int size = objArrayOopDesc::object_size(length);
   983   int size = objArrayOopDesc::object_size(length);
   996   Klass* ak = array_klass(n, CHECK_NULL);
   984   Klass* ak = array_klass(n, CHECK_NULL);
   997   KlassHandle h_ak (THREAD, ak);
       
   998   objArrayOop o =
   985   objArrayOop o =
   999     (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL);
   986     (objArrayOop)CollectedHeap::array_allocate(ak, size, length, CHECK_NULL);
  1000   return o;
   987   return o;
  1001 }
   988 }
  1002 
   989 
  1003 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
   990 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
  1004   if (TraceFinalizerRegistration) {
   991   if (TraceFinalizerRegistration) {
  1017 
  1004 
  1018 instanceOop InstanceKlass::allocate_instance(TRAPS) {
  1005 instanceOop InstanceKlass::allocate_instance(TRAPS) {
  1019   bool has_finalizer_flag = has_finalizer(); // Query before possible GC
  1006   bool has_finalizer_flag = has_finalizer(); // Query before possible GC
  1020   int size = size_helper();  // Query before forming handle.
  1007   int size = size_helper();  // Query before forming handle.
  1021 
  1008 
  1022   KlassHandle h_k(THREAD, this);
       
  1023 
       
  1024   instanceOop i;
  1009   instanceOop i;
  1025 
  1010 
  1026   i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL);
  1011   i = (instanceOop)CollectedHeap::obj_allocate(this, size, CHECK_NULL);
  1027   if (has_finalizer_flag && !RegisterFinalizersAtInit) {
  1012   if (has_finalizer_flag && !RegisterFinalizersAtInit) {
  1028     i = register_finalizer(i, CHECK_NULL);
  1013     i = register_finalizer(i, CHECK_NULL);
  1029   }
  1014   }
  1030   return i;
  1015   return i;
  1031 }
  1016 }
  1042               : vmSymbols::java_lang_IllegalAccessException(), external_name());
  1027               : vmSymbols::java_lang_IllegalAccessException(), external_name());
  1043   }
  1028   }
  1044 }
  1029 }
  1045 
  1030 
  1046 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) {
  1031 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) {
  1047   instanceKlassHandle this_k(THREAD, this);
       
  1048   return array_klass_impl(this_k, or_null, n, THREAD);
       
  1049 }
       
  1050 
       
  1051 Klass* InstanceKlass::array_klass_impl(instanceKlassHandle this_k, bool or_null, int n, TRAPS) {
       
  1052   // Need load-acquire for lock-free read
  1032   // Need load-acquire for lock-free read
  1053   if (this_k->array_klasses_acquire() == NULL) {
  1033   if (array_klasses_acquire() == NULL) {
  1054     if (or_null) return NULL;
  1034     if (or_null) return NULL;
  1055 
  1035 
  1056     ResourceMark rm;
  1036     ResourceMark rm;
  1057     JavaThread *jt = (JavaThread *)THREAD;
  1037     JavaThread *jt = (JavaThread *)THREAD;
  1058     {
  1038     {
  1059       // Atomic creation of array_klasses
  1039       // Atomic creation of array_klasses
  1060       MutexLocker mc(Compile_lock, THREAD);   // for vtables
  1040       MutexLocker mc(Compile_lock, THREAD);   // for vtables
  1061       MutexLocker ma(MultiArray_lock, THREAD);
  1041       MutexLocker ma(MultiArray_lock, THREAD);
  1062 
  1042 
  1063       // Check if update has already taken place
  1043       // Check if update has already taken place
  1064       if (this_k->array_klasses() == NULL) {
  1044       if (array_klasses() == NULL) {
  1065         Klass*    k = ObjArrayKlass::allocate_objArray_klass(this_k->class_loader_data(), 1, this_k, CHECK_NULL);
  1045         Klass*    k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
  1066         // use 'release' to pair with lock-free load
  1046         // use 'release' to pair with lock-free load
  1067         this_k->release_set_array_klasses(k);
  1047         release_set_array_klasses(k);
  1068       }
  1048       }
  1069     }
  1049     }
  1070   }
  1050   }
  1071   // _this will always be set at this point
  1051   // _this will always be set at this point
  1072   ObjArrayKlass* oak = (ObjArrayKlass*)this_k->array_klasses();
  1052   ObjArrayKlass* oak = (ObjArrayKlass*)array_klasses();
  1073   if (or_null) {
  1053   if (or_null) {
  1074     return oak->array_klass_or_null(n);
  1054     return oak->array_klass_or_null(n);
  1075   }
  1055   }
  1076   return oak->array_klass(n, THREAD);
  1056   return oak->array_klass(n, THREAD);
  1077 }
  1057 }
  1078 
  1058 
  1079 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) {
  1059 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) {
  1080   return array_klass_impl(or_null, 1, THREAD);
  1060   return array_klass_impl(or_null, 1, THREAD);
  1081 }
  1061 }
  1082 
  1062 
  1083 void InstanceKlass::call_class_initializer(TRAPS) {
  1063 static int call_class_initializer_counter = 0;   // for debugging
  1084   instanceKlassHandle ik (THREAD, this);
  1064 
  1085   call_class_initializer_impl(ik, THREAD);
  1065 Method* InstanceKlass::class_initializer() const {
  1086 }
       
  1087 
       
  1088 static int call_class_initializer_impl_counter = 0;   // for debugging
       
  1089 
       
  1090 Method* InstanceKlass::class_initializer() {
       
  1091   Method* clinit = find_method(
  1066   Method* clinit = find_method(
  1092       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
  1067       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
  1093   if (clinit != NULL && clinit->has_valid_initializer_flags()) {
  1068   if (clinit != NULL && clinit->has_valid_initializer_flags()) {
  1094     return clinit;
  1069     return clinit;
  1095   }
  1070   }
  1096   return NULL;
  1071   return NULL;
  1097 }
  1072 }
  1098 
  1073 
  1099 void InstanceKlass::call_class_initializer_impl(instanceKlassHandle this_k, TRAPS) {
  1074 void InstanceKlass::call_class_initializer(TRAPS) {
  1100   if (ReplayCompiles &&
  1075   if (ReplayCompiles &&
  1101       (ReplaySuppressInitializers == 1 ||
  1076       (ReplaySuppressInitializers == 1 ||
  1102        ReplaySuppressInitializers >= 2 && this_k->class_loader() != NULL)) {
  1077        ReplaySuppressInitializers >= 2 && class_loader() != NULL)) {
  1103     // Hide the existence of the initializer for the purpose of replaying the compile
  1078     // Hide the existence of the initializer for the purpose of replaying the compile
  1104     return;
  1079     return;
  1105   }
  1080   }
  1106 
  1081 
  1107   methodHandle h_method(THREAD, this_k->class_initializer());
  1082   methodHandle h_method(THREAD, class_initializer());
  1108   assert(!this_k->is_initialized(), "we cannot initialize twice");
  1083   assert(!is_initialized(), "we cannot initialize twice");
  1109   if (log_is_enabled(Info, class, init)) {
  1084   if (log_is_enabled(Info, class, init)) {
  1110     ResourceMark rm;
  1085     ResourceMark rm;
  1111     outputStream* log = Log(class, init)::info_stream();
  1086     outputStream* log = Log(class, init)::info_stream();
  1112     log->print("%d Initializing ", call_class_initializer_impl_counter++);
  1087     log->print("%d Initializing ", call_class_initializer_counter++);
  1113     this_k->name()->print_value_on(log);
  1088     name()->print_value_on(log);
  1114     log->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this_k()));
  1089     log->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
  1115   }
  1090   }
  1116   if (h_method() != NULL) {
  1091   if (h_method() != NULL) {
  1117     JavaCallArguments args; // No arguments
  1092     JavaCallArguments args; // No arguments
  1118     JavaValue result(T_VOID);
  1093     JavaValue result(T_VOID);
  1119     JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
  1094     JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
  1260   }
  1235   }
  1261 }
  1236 }
  1262 
  1237 
  1263 
  1238 
  1264 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) {
  1239 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) {
  1265   instanceKlassHandle h_this(THREAD, this);
  1240   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
  1266   do_local_static_fields_impl(h_this, f, mirror, CHECK);
       
  1267 }
       
  1268 
       
  1269 
       
  1270 void InstanceKlass::do_local_static_fields_impl(instanceKlassHandle this_k,
       
  1271                              void f(fieldDescriptor* fd, Handle, TRAPS), Handle mirror, TRAPS) {
       
  1272   for (JavaFieldStream fs(this_k()); !fs.done(); fs.next()) {
       
  1273     if (fs.access_flags().is_static()) {
  1241     if (fs.access_flags().is_static()) {
  1274       fieldDescriptor& fd = fs.field_descriptor();
  1242       fieldDescriptor& fd = fs.field_descriptor();
  1275       f(&fd, mirror, CHECK);
  1243       f(&fd, mirror, CHECK);
  1276     }
  1244     }
  1277   }
  1245   }
  1626   }
  1594   }
  1627   return NULL;
  1595   return NULL;
  1628 }
  1596 }
  1629 
  1597 
  1630 /* jni_id_for_impl for jfieldIds only */
  1598 /* jni_id_for_impl for jfieldIds only */
  1631 JNIid* InstanceKlass::jni_id_for_impl(instanceKlassHandle this_k, int offset) {
  1599 JNIid* InstanceKlass::jni_id_for_impl(int offset) {
  1632   MutexLocker ml(JfieldIdCreation_lock);
  1600   MutexLocker ml(JfieldIdCreation_lock);
  1633   // Retry lookup after we got the lock
  1601   // Retry lookup after we got the lock
  1634   JNIid* probe = this_k->jni_ids() == NULL ? NULL : this_k->jni_ids()->find(offset);
  1602   JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
  1635   if (probe == NULL) {
  1603   if (probe == NULL) {
  1636     // Slow case, allocate new static field identifier
  1604     // Slow case, allocate new static field identifier
  1637     probe = new JNIid(this_k(), offset, this_k->jni_ids());
  1605     probe = new JNIid(this, offset, jni_ids());
  1638     this_k->set_jni_ids(probe);
  1606     set_jni_ids(probe);
  1639   }
  1607   }
  1640   return probe;
  1608   return probe;
  1641 }
  1609 }
  1642 
  1610 
  1643 
  1611 
  1644 /* jni_id_for for jfieldIds only */
  1612 /* jni_id_for for jfieldIds only */
  1645 JNIid* InstanceKlass::jni_id_for(int offset) {
  1613 JNIid* InstanceKlass::jni_id_for(int offset) {
  1646   JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
  1614   JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
  1647   if (probe == NULL) {
  1615   if (probe == NULL) {
  1648     probe = jni_id_for_impl(this, offset);
  1616     probe = jni_id_for_impl(offset);
  1649   }
  1617   }
  1650   return probe;
  1618   return probe;
  1651 }
  1619 }
  1652 
  1620 
  1653 u2 InstanceKlass::enclosing_method_data(int offset) const {
  1621 u2 InstanceKlass::enclosing_method_data(int offset) const {
  1681 // Lookup or create a jmethodID.
  1649 // Lookup or create a jmethodID.
  1682 // This code is called by the VMThread and JavaThreads so the
  1650 // This code is called by the VMThread and JavaThreads so the
  1683 // locking has to be done very carefully to avoid deadlocks
  1651 // locking has to be done very carefully to avoid deadlocks
  1684 // and/or other cache consistency problems.
  1652 // and/or other cache consistency problems.
  1685 //
  1653 //
  1686 jmethodID InstanceKlass::get_jmethod_id(instanceKlassHandle ik_h, const methodHandle& method_h) {
  1654 jmethodID InstanceKlass::get_jmethod_id(const methodHandle& method_h) {
  1687   size_t idnum = (size_t)method_h->method_idnum();
  1655   size_t idnum = (size_t)method_h->method_idnum();
  1688   jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
  1656   jmethodID* jmeths = methods_jmethod_ids_acquire();
  1689   size_t length = 0;
  1657   size_t length = 0;
  1690   jmethodID id = NULL;
  1658   jmethodID id = NULL;
  1691 
  1659 
  1692   // We use a double-check locking idiom here because this cache is
  1660   // We use a double-check locking idiom here because this cache is
  1693   // performance sensitive. In the normal system, this cache only
  1661   // performance sensitive. In the normal system, this cache only
  1707   // cache accesses and freeing of the old cache so a lock is generally
  1675   // cache accesses and freeing of the old cache so a lock is generally
  1708   // acquired when the RedefineClasses() API has been used.
  1676   // acquired when the RedefineClasses() API has been used.
  1709 
  1677 
  1710   if (jmeths != NULL) {
  1678   if (jmeths != NULL) {
  1711     // the cache already exists
  1679     // the cache already exists
  1712     if (!ik_h->idnum_can_increment()) {
  1680     if (!idnum_can_increment()) {
  1713       // the cache can't grow so we can just get the current values
  1681       // the cache can't grow so we can just get the current values
  1714       get_jmethod_id_length_value(jmeths, idnum, &length, &id);
  1682       get_jmethod_id_length_value(jmeths, idnum, &length, &id);
  1715     } else {
  1683     } else {
  1716       // cache can grow so we have to be more careful
  1684       // cache can grow so we have to be more careful
  1717       if (Threads::number_of_threads() == 0 ||
  1685       if (Threads::number_of_threads() == 0 ||
  1741 
  1709 
  1742     // may not allocate new_jmeths or use it if we allocate it
  1710     // may not allocate new_jmeths or use it if we allocate it
  1743     jmethodID* new_jmeths = NULL;
  1711     jmethodID* new_jmeths = NULL;
  1744     if (length <= idnum) {
  1712     if (length <= idnum) {
  1745       // allocate a new cache that might be used
  1713       // allocate a new cache that might be used
  1746       size_t size = MAX2(idnum+1, (size_t)ik_h->idnum_allocated_count());
  1714       size_t size = MAX2(idnum+1, (size_t)idnum_allocated_count());
  1747       new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1, mtClass);
  1715       new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1, mtClass);
  1748       memset(new_jmeths, 0, (size+1)*sizeof(jmethodID));
  1716       memset(new_jmeths, 0, (size+1)*sizeof(jmethodID));
  1749       // cache size is stored in element[0], other elements offset by one
  1717       // cache size is stored in element[0], other elements offset by one
  1750       new_jmeths[0] = (jmethodID)size;
  1718       new_jmeths[0] = (jmethodID)size;
  1751     }
  1719     }
  1752 
  1720 
  1753     // allocate a new jmethodID that might be used
  1721     // allocate a new jmethodID that might be used
  1754     jmethodID new_id = NULL;
  1722     jmethodID new_id = NULL;
  1755     if (method_h->is_old() && !method_h->is_obsolete()) {
  1723     if (method_h->is_old() && !method_h->is_obsolete()) {
  1756       // The method passed in is old (but not obsolete), we need to use the current version
  1724       // The method passed in is old (but not obsolete), we need to use the current version
  1757       Method* current_method = ik_h->method_with_idnum((int)idnum);
  1725       Method* current_method = method_with_idnum((int)idnum);
  1758       assert(current_method != NULL, "old and but not obsolete, so should exist");
  1726       assert(current_method != NULL, "old and but not obsolete, so should exist");
  1759       new_id = Method::make_jmethod_id(ik_h->class_loader_data(), current_method);
  1727       new_id = Method::make_jmethod_id(class_loader_data(), current_method);
  1760     } else {
  1728     } else {
  1761       // It is the current version of the method or an obsolete method,
  1729       // It is the current version of the method or an obsolete method,
  1762       // use the version passed in
  1730       // use the version passed in
  1763       new_id = Method::make_jmethod_id(ik_h->class_loader_data(), method_h());
  1731       new_id = Method::make_jmethod_id(class_loader_data(), method_h());
  1764     }
  1732     }
  1765 
  1733 
  1766     if (Threads::number_of_threads() == 0 ||
  1734     if (Threads::number_of_threads() == 0 ||
  1767         SafepointSynchronize::is_at_safepoint()) {
  1735         SafepointSynchronize::is_at_safepoint()) {
  1768       // we're single threaded or at a safepoint - no locking needed
  1736       // we're single threaded or at a safepoint - no locking needed
  1769       id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
  1737       id = get_jmethod_id_fetch_or_update(idnum, new_id, new_jmeths,
  1770                                           &to_dealloc_id, &to_dealloc_jmeths);
  1738                                           &to_dealloc_id, &to_dealloc_jmeths);
  1771     } else {
  1739     } else {
  1772       MutexLocker ml(JmethodIdCreation_lock);
  1740       MutexLocker ml(JmethodIdCreation_lock);
  1773       id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
  1741       id = get_jmethod_id_fetch_or_update(idnum, new_id, new_jmeths,
  1774                                           &to_dealloc_id, &to_dealloc_jmeths);
  1742                                           &to_dealloc_id, &to_dealloc_jmeths);
  1775     }
  1743     }
  1776 
  1744 
  1777     // The lock has been dropped so we can free resources.
  1745     // The lock has been dropped so we can free resources.
  1778     // Free up either the old cache or the new cache if we allocated one.
  1746     // Free up either the old cache or the new cache if we allocated one.
  1779     if (to_dealloc_jmeths != NULL) {
  1747     if (to_dealloc_jmeths != NULL) {
  1780       FreeHeap(to_dealloc_jmeths);
  1748       FreeHeap(to_dealloc_jmeths);
  1781     }
  1749     }
  1782     // free up the new ID since it wasn't needed
  1750     // free up the new ID since it wasn't needed
  1783     if (to_dealloc_id != NULL) {
  1751     if (to_dealloc_id != NULL) {
  1784       Method::destroy_jmethod_id(ik_h->class_loader_data(), to_dealloc_id);
  1752       Method::destroy_jmethod_id(class_loader_data(), to_dealloc_id);
  1785     }
  1753     }
  1786   }
  1754   }
  1787   return id;
  1755   return id;
  1788 }
  1756 }
  1789 
  1757 
  1811 // cache with the new jmethodID. This function should never do anything
  1779 // cache with the new jmethodID. This function should never do anything
  1812 // that causes the caller to go to a safepoint or we can deadlock with
  1780 // that causes the caller to go to a safepoint or we can deadlock with
  1813 // the VMThread or have cache consistency issues.
  1781 // the VMThread or have cache consistency issues.
  1814 //
  1782 //
  1815 jmethodID InstanceKlass::get_jmethod_id_fetch_or_update(
  1783 jmethodID InstanceKlass::get_jmethod_id_fetch_or_update(
  1816             instanceKlassHandle ik_h, size_t idnum, jmethodID new_id,
  1784             size_t idnum, jmethodID new_id,
  1817             jmethodID* new_jmeths, jmethodID* to_dealloc_id_p,
  1785             jmethodID* new_jmeths, jmethodID* to_dealloc_id_p,
  1818             jmethodID** to_dealloc_jmeths_p) {
  1786             jmethodID** to_dealloc_jmeths_p) {
  1819   assert(new_id != NULL, "sanity check");
  1787   assert(new_id != NULL, "sanity check");
  1820   assert(to_dealloc_id_p != NULL, "sanity check");
  1788   assert(to_dealloc_id_p != NULL, "sanity check");
  1821   assert(to_dealloc_jmeths_p != NULL, "sanity check");
  1789   assert(to_dealloc_jmeths_p != NULL, "sanity check");
  1822   assert(Threads::number_of_threads() == 0 ||
  1790   assert(Threads::number_of_threads() == 0 ||
  1823          SafepointSynchronize::is_at_safepoint() ||
  1791          SafepointSynchronize::is_at_safepoint() ||
  1824          JmethodIdCreation_lock->owned_by_self(), "sanity check");
  1792          JmethodIdCreation_lock->owned_by_self(), "sanity check");
  1825 
  1793 
  1826   // reacquire the cache - we are locked, single threaded or at a safepoint
  1794   // reacquire the cache - we are locked, single threaded or at a safepoint
  1827   jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
  1795   jmethodID* jmeths = methods_jmethod_ids_acquire();
  1828   jmethodID  id     = NULL;
  1796   jmethodID  id     = NULL;
  1829   size_t     length = 0;
  1797   size_t     length = 0;
  1830 
  1798 
  1831   if (jmeths == NULL ||                         // no cache yet
  1799   if (jmeths == NULL ||                         // no cache yet
  1832       (length = (size_t)jmeths[0]) <= idnum) {  // cache is too short
  1800       (length = (size_t)jmeths[0]) <= idnum) {  // cache is too short
  1835       for (size_t index = 0; index < length; index++) {
  1803       for (size_t index = 0; index < length; index++) {
  1836         new_jmeths[index+1] = jmeths[index+1];
  1804         new_jmeths[index+1] = jmeths[index+1];
  1837       }
  1805       }
  1838       *to_dealloc_jmeths_p = jmeths;  // save old cache for later delete
  1806       *to_dealloc_jmeths_p = jmeths;  // save old cache for later delete
  1839     }
  1807     }
  1840     ik_h->release_set_methods_jmethod_ids(jmeths = new_jmeths);
  1808     release_set_methods_jmethod_ids(jmeths = new_jmeths);
  1841   } else {
  1809   } else {
  1842     // fetch jmethodID (if any) from the existing cache
  1810     // fetch jmethodID (if any) from the existing cache
  1843     id = jmeths[idnum+1];
  1811     id = jmeths[idnum+1];
  1844     *to_dealloc_jmeths_p = new_jmeths;  // save new cache for later delete
  1812     *to_dealloc_jmeths_p = new_jmeths;  // save new cache for later delete
  1845   }
  1813   }
  2055   // --> see ArrayKlass::complete_create_array_klass()
  2023   // --> see ArrayKlass::complete_create_array_klass()
  2056   k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
  2024   k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
  2057 }
  2025 }
  2058 
  2026 
  2059 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
  2027 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
  2060   instanceKlassHandle ik(THREAD, this);
  2028   set_package(loader_data, CHECK);
  2061   ik->set_package(loader_data, CHECK);
       
  2062   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
  2029   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
  2063 
  2030 
  2064   Array<Method*>* methods = ik->methods();
  2031   Array<Method*>* methods = this->methods();
  2065   int num_methods = methods->length();
  2032   int num_methods = methods->length();
  2066   for (int index2 = 0; index2 < num_methods; ++index2) {
  2033   for (int index2 = 0; index2 < num_methods; ++index2) {
  2067     methodHandle m(THREAD, methods->at(index2));
  2034     methodHandle m(THREAD, methods->at(index2));
  2068     m->restore_unshareable_info(CHECK);
  2035     m->restore_unshareable_info(CHECK);
  2069   }
  2036   }
  2072     // entries in this vtable for super classes so the CDS vtable might
  2039     // entries in this vtable for super classes so the CDS vtable might
  2073     // point to old or obsolete entries.  RedefineClasses doesn't fix up
  2040     // point to old or obsolete entries.  RedefineClasses doesn't fix up
  2074     // vtables in the shared system dictionary, only the main one.
  2041     // vtables in the shared system dictionary, only the main one.
  2075     // It also redefines the itable too so fix that too.
  2042     // It also redefines the itable too so fix that too.
  2076     ResourceMark rm(THREAD);
  2043     ResourceMark rm(THREAD);
  2077     ik->vtable()->initialize_vtable(false, CHECK);
  2044     vtable()->initialize_vtable(false, CHECK);
  2078     ik->itable()->initialize_itable(false, CHECK);
  2045     itable()->initialize_itable(false, CHECK);
  2079   }
  2046   }
  2080 
  2047 
  2081   // restore constant pool resolved references
  2048   // restore constant pool resolved references
  2082   ik->constants()->restore_unshareable_info(CHECK);
  2049   constants()->restore_unshareable_info(CHECK);
  2083 
  2050 
  2084   ik->array_klasses_do(restore_unshareable_in_class, CHECK);
  2051   array_klasses_do(restore_unshareable_in_class, CHECK);
  2085 }
  2052 }
  2086 
  2053 
  2087 // returns true IFF is_in_error_state() has been changed as a result of this call.
  2054 // returns true IFF is_in_error_state() has been changed as a result of this call.
  2088 bool InstanceKlass::check_sharing_error_state() {
  2055 bool InstanceKlass::check_sharing_error_state() {
  2089   assert(DumpSharedSpaces, "should only be called during dumping");
  2056   assert(DumpSharedSpaces, "should only be called during dumping");
  2296 }
  2263 }
  2297 
  2264 
  2298 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
  2265 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
  2299 
  2266 
  2300   // ensure java/ packages only loaded by boot or platform builtin loaders
  2267   // ensure java/ packages only loaded by boot or platform builtin loaders
  2301   check_prohibited_package(name(), loader_data->class_loader(), CHECK);
  2268   Handle class_loader(THREAD, loader_data->class_loader());
       
  2269   check_prohibited_package(name(), class_loader, CHECK);
  2302 
  2270 
  2303   TempNewSymbol pkg_name = package_from_name(name(), CHECK);
  2271   TempNewSymbol pkg_name = package_from_name(name(), CHECK);
  2304 
  2272 
  2305   if (pkg_name != NULL && loader_data != NULL) {
  2273   if (pkg_name != NULL && loader_data != NULL) {
  2306 
  2274 
  2443    // Package-private methods are not inherited outside of package
  2411    // Package-private methods are not inherited outside of package
  2444    assert(super_method->is_package_private(), "must be package private");
  2412    assert(super_method->is_package_private(), "must be package private");
  2445    return(is_same_class_package(targetclassloader(), targetclassname));
  2413    return(is_same_class_package(targetclassloader(), targetclassname));
  2446 }
  2414 }
  2447 
  2415 
  2448 /* defined for now in jvm.cpp, for historical reasons *--
       
  2449 Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle self,
       
  2450                                                      Symbol*& simple_name_result, TRAPS) {
       
  2451   ...
       
  2452 }
       
  2453 */
       
  2454 
       
  2455 // Only boot and platform class loaders can define classes in "java/" packages.
  2416 // Only boot and platform class loaders can define classes in "java/" packages.
  2456 void InstanceKlass::check_prohibited_package(Symbol* class_name,
  2417 void InstanceKlass::check_prohibited_package(Symbol* class_name,
  2457                                              Handle class_loader,
  2418                                              Handle class_loader,
  2458                                              TRAPS) {
  2419                                              TRAPS) {
  2459   if (!class_loader.is_null() &&
  2420   if (!class_loader.is_null() &&
  2460       !SystemDictionary::is_platform_class_loader(class_loader) &&
  2421       !SystemDictionary::is_platform_class_loader(class_loader()) &&
  2461       class_name != NULL) {
  2422       class_name != NULL) {
  2462     ResourceMark rm(THREAD);
  2423     ResourceMark rm(THREAD);
  2463     char* name = class_name->as_C_string();
  2424     char* name = class_name->as_C_string();
  2464     if (strncmp(name, JAVAPKG, JAVAPKG_LEN) == 0 && name[JAVAPKG_LEN] == '/') {
  2425     if (strncmp(name, JAVAPKG, JAVAPKG_LEN) == 0 && name[JAVAPKG_LEN] == '/') {
  2465       TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK);
  2426       TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK);
  2477   }
  2438   }
  2478   return;
  2439   return;
  2479 }
  2440 }
  2480 
  2441 
  2481 // tell if two classes have the same enclosing class (at package level)
  2442 // tell if two classes have the same enclosing class (at package level)
  2482 bool InstanceKlass::is_same_package_member_impl(const InstanceKlass* class1,
  2443 bool InstanceKlass::is_same_package_member(const Klass* class2, TRAPS) const {
  2483                                                 const Klass* class2,
  2444   if (class2 == this) return true;
  2484                                                 TRAPS) {
       
  2485   if (class2 == class1) return true;
       
  2486   if (!class2->is_instance_klass())  return false;
  2445   if (!class2->is_instance_klass())  return false;
  2487 
  2446 
  2488   // must be in same package before we try anything else
  2447   // must be in same package before we try anything else
  2489   if (!class1->is_same_class_package(class2))
  2448   if (!is_same_class_package(class2))
  2490     return false;
  2449     return false;
  2491 
  2450 
  2492   // As long as there is an outer1.getEnclosingClass,
  2451   // As long as there is an outer_this.getEnclosingClass,
  2493   // shift the search outward.
  2452   // shift the search outward.
  2494   const InstanceKlass* outer1 = class1;
  2453   const InstanceKlass* outer_this = this;
  2495   for (;;) {
  2454   for (;;) {
  2496     // As we walk along, look for equalities between outer1 and class2.
  2455     // As we walk along, look for equalities between outer_this and class2.
  2497     // Eventually, the walks will terminate as outer1 stops
  2456     // Eventually, the walks will terminate as outer_this stops
  2498     // at the top-level class around the original class.
  2457     // at the top-level class around the original class.
  2499     bool ignore_inner_is_member;
  2458     bool ignore_inner_is_member;
  2500     const Klass* next = outer1->compute_enclosing_class(&ignore_inner_is_member,
  2459     const Klass* next = outer_this->compute_enclosing_class(&ignore_inner_is_member,
  2501                                                   CHECK_false);
  2460                                                             CHECK_false);
  2502     if (next == NULL)  break;
  2461     if (next == NULL)  break;
  2503     if (next == class2)  return true;
  2462     if (next == class2)  return true;
  2504     outer1 = InstanceKlass::cast(next);
  2463     outer_this = InstanceKlass::cast(next);
  2505   }
  2464   }
  2506 
  2465 
  2507   // Now do the same for class2.
  2466   // Now do the same for class2.
  2508   const InstanceKlass* outer2 = InstanceKlass::cast(class2);
  2467   const InstanceKlass* outer2 = InstanceKlass::cast(class2);
  2509   for (;;) {
  2468   for (;;) {
  2510     bool ignore_inner_is_member;
  2469     bool ignore_inner_is_member;
  2511     Klass* next = outer2->compute_enclosing_class(&ignore_inner_is_member,
  2470     Klass* next = outer2->compute_enclosing_class(&ignore_inner_is_member,
  2512                                                     CHECK_false);
  2471                                                     CHECK_false);
  2513     if (next == NULL)  break;
  2472     if (next == NULL)  break;
  2514     // Might as well check the new outer against all available values.
  2473     // Might as well check the new outer against all available values.
  2515     if (next == class1)  return true;
  2474     if (next == this)  return true;
  2516     if (next == outer1)  return true;
  2475     if (next == outer_this)  return true;
  2517     outer2 = InstanceKlass::cast(next);
  2476     outer2 = InstanceKlass::cast(next);
  2518   }
  2477   }
  2519 
  2478 
  2520   // If by this point we have not found an equality between the
  2479   // If by this point we have not found an equality between the
  2521   // two classes, we know they are in separate package members.
  2480   // two classes, we know they are in separate package members.
  2522   return false;
  2481   return false;
  2523 }
  2482 }
  2524 
  2483 
  2525 bool InstanceKlass::find_inner_classes_attr(instanceKlassHandle k, int* ooff, int* noff, TRAPS) {
  2484 bool InstanceKlass::find_inner_classes_attr(int* ooff, int* noff, TRAPS) const {
  2526   constantPoolHandle i_cp(THREAD, k->constants());
  2485   constantPoolHandle i_cp(THREAD, constants());
  2527   for (InnerClassesIterator iter(k); !iter.done(); iter.next()) {
  2486   for (InnerClassesIterator iter(this); !iter.done(); iter.next()) {
  2528     int ioff = iter.inner_class_info_index();
  2487     int ioff = iter.inner_class_info_index();
  2529     if (ioff != 0) {
  2488     if (ioff != 0) {
  2530       // Check to see if the name matches the class we're looking for
  2489       // Check to see if the name matches the class we're looking for
  2531       // before attempting to find the class.
  2490       // before attempting to find the class.
  2532       if (i_cp->klass_name_at_matches(k, ioff)) {
  2491       if (i_cp->klass_name_at_matches(this, ioff)) {
  2533         Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
  2492         Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
  2534         if (k() == inner_klass) {
  2493         if (this == inner_klass) {
  2535           *ooff = iter.outer_class_info_index();
  2494           *ooff = iter.outer_class_info_index();
  2536           *noff = iter.inner_name_index();
  2495           *noff = iter.inner_name_index();
  2537           return true;
  2496           return true;
  2538         }
  2497         }
  2539       }
  2498       }
  2540     }
  2499     }
  2541   }
  2500   }
  2542   return false;
  2501   return false;
  2543 }
  2502 }
  2544 
  2503 
  2545 InstanceKlass* InstanceKlass::compute_enclosing_class_impl(const InstanceKlass* k,
  2504 InstanceKlass* InstanceKlass::compute_enclosing_class(bool* inner_is_member, TRAPS) const {
  2546                                                            bool* inner_is_member,
       
  2547                                                            TRAPS) {
       
  2548   InstanceKlass* outer_klass = NULL;
  2505   InstanceKlass* outer_klass = NULL;
  2549   *inner_is_member = false;
  2506   *inner_is_member = false;
  2550   int ooff = 0, noff = 0;
  2507   int ooff = 0, noff = 0;
  2551   if (find_inner_classes_attr(k, &ooff, &noff, THREAD)) {
  2508   bool has_inner_classes_attr = find_inner_classes_attr(&ooff, &noff, THREAD);
  2552     constantPoolHandle i_cp(THREAD, k->constants());
  2509   if (has_inner_classes_attr) {
       
  2510     constantPoolHandle i_cp(THREAD, constants());
  2553     if (ooff != 0) {
  2511     if (ooff != 0) {
  2554       Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
  2512       Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
  2555       outer_klass = InstanceKlass::cast(ok);
  2513       outer_klass = InstanceKlass::cast(ok);
  2556       *inner_is_member = true;
  2514       *inner_is_member = true;
  2557     }
  2515     }
  2558     if (NULL == outer_klass) {
  2516     if (NULL == outer_klass) {
  2559       // It may be anonymous; try for that.
  2517       // It may be anonymous; try for that.
  2560       int encl_method_class_idx = k->enclosing_method_class_index();
  2518       int encl_method_class_idx = enclosing_method_class_index();
  2561       if (encl_method_class_idx != 0) {
  2519       if (encl_method_class_idx != 0) {
  2562         Klass* ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
  2520         Klass* ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
  2563         outer_klass = InstanceKlass::cast(ok);
  2521         outer_klass = InstanceKlass::cast(ok);
  2564         *inner_is_member = false;
  2522         *inner_is_member = false;
  2565       }
  2523       }
  2570   if (NULL == outer_klass) return NULL;
  2528   if (NULL == outer_klass) return NULL;
  2571 
  2529 
  2572   // Throws an exception if outer klass has not declared k as an inner klass
  2530   // Throws an exception if outer klass has not declared k as an inner klass
  2573   // We need evidence that each klass knows about the other, or else
  2531   // We need evidence that each klass knows about the other, or else
  2574   // the system could allow a spoof of an inner class to gain access rights.
  2532   // the system could allow a spoof of an inner class to gain access rights.
  2575   Reflection::check_for_inner_class(outer_klass, k, *inner_is_member, CHECK_NULL);
  2533   Reflection::check_for_inner_class(outer_klass, this, *inner_is_member, CHECK_NULL);
  2576   return outer_klass;
  2534   return outer_klass;
  2577 }
  2535 }
  2578 
  2536 
  2579 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
  2537 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
  2580   jint access = access_flags().as_int();
  2538   jint access = access_flags().as_int();
  2581 
  2539 
  2582   // But check if it happens to be member class.
  2540   // But check if it happens to be member class.
  2583   instanceKlassHandle ik(THREAD, this);
  2541   InnerClassesIterator iter(this);
  2584   InnerClassesIterator iter(ik);
       
  2585   for (; !iter.done(); iter.next()) {
  2542   for (; !iter.done(); iter.next()) {
  2586     int ioff = iter.inner_class_info_index();
  2543     int ioff = iter.inner_class_info_index();
  2587     // Inner class attribute can be zero, skip it.
  2544     // Inner class attribute can be zero, skip it.
  2588     // Strange but true:  JVM spec. allows null inner class refs.
  2545     // Strange but true:  JVM spec. allows null inner class refs.
  2589     if (ioff == 0) continue;
  2546     if (ioff == 0) continue;
  2590 
  2547 
  2591     // only look at classes that are already loaded
  2548     // only look at classes that are already loaded
  2592     // since we are looking for the flags for our self.
  2549     // since we are looking for the flags for our self.
  2593     Symbol* inner_name = ik->constants()->klass_name_at(ioff);
  2550     Symbol* inner_name = constants()->klass_name_at(ioff);
  2594     if ((ik->name() == inner_name)) {
  2551     if (name() == inner_name) {
  2595       // This is really a member class.
  2552       // This is really a member class.
  2596       access = iter.inner_access_flags();
  2553       access = iter.inner_access_flags();
  2597       break;
  2554       break;
  2598     }
  2555     }
  2599   }
  2556   }
  3141         log->print(" source: instance of %s", caller->external_name());
  3098         log->print(" source: instance of %s", caller->external_name());
  3142       } else {
  3099       } else {
  3143         // source is unknown
  3100         // source is unknown
  3144       }
  3101       }
  3145     } else {
  3102     } else {
  3146       Handle class_loader(loader_data->class_loader());
  3103       oop class_loader = loader_data->class_loader();
  3147       log->print(" source: %s", class_loader->klass()->external_name());
  3104       log->print(" source: %s", class_loader->klass()->external_name());
  3148     }
  3105     }
  3149   } else {
  3106   } else {
  3150     log->print(" source: shared objects file");
  3107     log->print(" source: shared objects file");
  3151   }
  3108   }
  3610 }
  3567 }
  3611 
  3568 
  3612 // Save the scratch_class as the previous version if any of the methods are running.
  3569 // Save the scratch_class as the previous version if any of the methods are running.
  3613 // The previous_versions are used to set breakpoints in EMCP methods and they are
  3570 // The previous_versions are used to set breakpoints in EMCP methods and they are
  3614 // also used to clean MethodData links to redefined methods that are no longer running.
  3571 // also used to clean MethodData links to redefined methods that are no longer running.
  3615 void InstanceKlass::add_previous_version(instanceKlassHandle scratch_class,
  3572 void InstanceKlass::add_previous_version(InstanceKlass* scratch_class,
  3616                                          int emcp_method_count) {
  3573                                          int emcp_method_count) {
  3617   assert(Thread::current()->is_VM_thread(),
  3574   assert(Thread::current()->is_VM_thread(),
  3618          "only VMThread can add previous versions");
  3575          "only VMThread can add previous versions");
  3619 
  3576 
  3620   ResourceMark rm;
  3577   ResourceMark rm;
  3636   ConstantPool* cp_ref = scratch_class->constants();
  3593   ConstantPool* cp_ref = scratch_class->constants();
  3637   if (!cp_ref->on_stack()) {
  3594   if (!cp_ref->on_stack()) {
  3638     log_trace(redefine, class, iklass, add)("scratch class not added; no methods are running");
  3595     log_trace(redefine, class, iklass, add)("scratch class not added; no methods are running");
  3639     // For debugging purposes.
  3596     // For debugging purposes.
  3640     scratch_class->set_is_scratch_class();
  3597     scratch_class->set_is_scratch_class();
  3641     scratch_class->class_loader_data()->add_to_deallocate_list(scratch_class());
  3598     scratch_class->class_loader_data()->add_to_deallocate_list(scratch_class);
  3642     return;
  3599     return;
  3643   }
  3600   }
  3644 
  3601 
  3645   if (emcp_method_count != 0) {
  3602   if (emcp_method_count != 0) {
  3646     // At least one method is still running, check for EMCP methods
  3603     // At least one method is still running, check for EMCP methods
  3669   // Set has_previous_version flag for processing during class unloading.
  3626   // Set has_previous_version flag for processing during class unloading.
  3670   _has_previous_versions = true;
  3627   _has_previous_versions = true;
  3671   log_trace(redefine, class, iklass, add) ("scratch class added; one of its methods is on_stack.");
  3628   log_trace(redefine, class, iklass, add) ("scratch class added; one of its methods is on_stack.");
  3672   assert(scratch_class->previous_versions() == NULL, "shouldn't have a previous version");
  3629   assert(scratch_class->previous_versions() == NULL, "shouldn't have a previous version");
  3673   scratch_class->link_previous_versions(previous_versions());
  3630   scratch_class->link_previous_versions(previous_versions());
  3674   link_previous_versions(scratch_class());
  3631   link_previous_versions(scratch_class);
  3675 } // end add_previous_version()
  3632 } // end add_previous_version()
  3676 
  3633 
  3677 #endif // INCLUDE_JVMTI
  3634 #endif // INCLUDE_JVMTI
  3678 
  3635 
  3679 Method* InstanceKlass::method_with_idnum(int idnum) {
  3636 Method* InstanceKlass::method_with_idnum(int idnum) {