< prev index next >

src/hotspot/share/oops/klassVtable.cpp

Print this page




  49   return InstanceKlass::cast(_klass);
  50 }
  51 
  52 bool klassVtable::is_preinitialized_vtable() {
  53   return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
  54 }
  55 
  56 
  57 // this function computes the vtable size (including the size needed for miranda
  58 // methods) and the number of miranda methods in this class.
  59 // Note on Miranda methods: Let's say there is a class C that implements
  60 // interface I, and none of C's superclasses implements I.
  61 // Let's say there is an abstract method m in I that neither C
  62 // nor any of its super classes implement (i.e there is no method of any access,
  63 // with the same name and signature as m), then m is a Miranda method which is
  64 // entered as a public abstract method in C's vtable.  From then on it should
  65 // treated as any other public method in C for method over-ride purposes.
  66 void klassVtable::compute_vtable_size_and_num_mirandas(
  67     int* vtable_length_ret, int* num_new_mirandas,
  68     GrowableArray<Method*>* all_mirandas, const Klass* super,
  69     Array<Method*>* methods, AccessFlags class_flags, u2 major_version,
  70     Handle classloader, Symbol* classname, Array<InstanceKlass*>* local_interfaces,
  71     TRAPS) {
  72   NoSafepointVerifier nsv;
  73 
  74   // set up default result values
  75   int vtable_length = 0;
  76 
  77   // start off with super's vtable length
  78   vtable_length = super == NULL ? 0 : super->vtable_length();
  79 
  80   // go thru each method in the methods table to see if it needs a new entry
  81   int len = methods->length();
  82   for (int i = 0; i < len; i++) {
  83     assert(methods->at(i)->is_method(), "must be a Method*");
  84     methodHandle mh(THREAD, methods->at(i));
  85 
  86     if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, major_version, THREAD)) {
  87       assert(!methods->at(i)->is_private(), "private methods should not need a vtable entry");
  88       vtable_length += vtableEntry::size(); // we need a new entry
  89     }
  90   }
  91 
  92   GrowableArray<Method*> new_mirandas(20);
  93   // compute the number of mirandas methods that must be added to the end
  94   get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces,
  95                class_flags.is_interface());
  96   *num_new_mirandas = new_mirandas.length();
  97 
  98   // Interfaces do not need interface methods in their vtables
  99   // This includes miranda methods and during later processing, default methods
 100   if (!class_flags.is_interface()) {
 101      vtable_length += *num_new_mirandas * vtableEntry::size();
 102   }
 103 
 104   if (Universe::is_bootstrapping() && vtable_length == 0) {
 105     // array classes don't have their superclass set correctly during
 106     // bootstrapping


 380   // default_vtable_indices stores the vtable value relative to this inheritor
 381   if (default_index >= 0 ) {
 382     is_default = true;
 383     def_vtable_indices = klass->default_vtable_indices();
 384     assert(!target_method()->is_private(), "private interface method flagged as default");
 385     assert(def_vtable_indices != NULL, "def vtable alloc?");
 386     assert(default_index <= def_vtable_indices->length(), "def vtable len?");
 387   } else {
 388     assert(klass == target_method()->method_holder(), "caller resp.");
 389     // Initialize the method's vtable index to "nonvirtual".
 390     // If we allocate a vtable entry, we will update it to a non-negative number.
 391     target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
 392   }
 393 
 394   // Private, static and <init> methods are never in
 395   if (target_method()->is_private() || target_method()->is_static() ||
 396       (target_method()->name()->fast_compare(vmSymbols::object_initializer_name()) == 0)) {
 397     return false;
 398   }
 399 
 400   if (target_method->is_final_method(klass->access_flags())) {
 401     // a final method never needs a new entry; final methods can be statically
 402     // resolved and they have to be present in the vtable only if they override
 403     // a super's method, in which case they re-use its entry
 404     allocate_new = false;
 405   } else if (klass->is_interface()) {
 406     allocate_new = false;  // see note below in needs_new_vtable_entry
 407     // An interface never allocates new vtable slots, only inherits old ones.
 408     // This method will either be assigned its own itable index later,
 409     // or be assigned an inherited vtable index in the loop below.
 410     // default methods inherited by classes store their vtable indices
 411     // in the inheritor's default_vtable_indices.
 412     // default methods inherited by interfaces may already have a
 413     // valid itable index, if so, don't change it.
 414     // Overpass methods in an interface will be assigned an itable index later
 415     // by an inheriting class.
 416     if ((!is_default || !target_method()->has_itable_index())) {
 417       target_method()->set_vtable_index(Method::pending_itable_index);
 418     }
 419   }
 420 


 567       ls.cr();
 568     }
 569     table()[index].set(m);
 570   }
 571 }
 572 
 573 // Find out if a method "m" with superclass "super", loader "classloader" and
 574 // name "classname" needs a new vtable entry.  Let P be a class package defined
 575 // by "classloader" and "classname".
 576 // NOTE: The logic used here is very similar to the one used for computing
 577 // the vtables indices for a method. We cannot directly use that function because,
 578 // we allocate the InstanceKlass at load time, and that requires that the
 579 // superclass has been loaded.
 580 // However, the vtable entries are filled in at link time, and therefore
 581 // the superclass' vtable may not yet have been filled in.
 582 bool klassVtable::needs_new_vtable_entry(const methodHandle& target_method,
 583                                          const Klass* super,
 584                                          Handle classloader,
 585                                          Symbol* classname,
 586                                          AccessFlags class_flags,

 587                                          u2 major_version,
 588                                          TRAPS) {
 589   if (class_flags.is_interface()) {
 590     // Interfaces do not use vtables, except for java.lang.Object methods,
 591     // so there is no point to assigning
 592     // a vtable index to any of their local methods.  If we refrain from doing this,
 593     // we can use Method::_vtable_index to hold the itable index
 594     return false;
 595   }
 596 
 597   if (target_method->is_final_method(class_flags) ||
 598       // a final method never needs a new entry; final methods can be statically
 599       // resolved and they have to be present in the vtable only if they override
 600       // a super's method, in which case they re-use its entry
 601       (target_method()->is_private()) ||
 602       // private methods don't need to be in vtable
 603       (target_method()->is_static()) ||
 604       // static methods don't need to be in vtable
 605       (target_method()->name()->fast_compare(vmSymbols::object_initializer_name()) == 0)
 606       // <init> is never called dynamically-bound
 607       ) {
 608     return false;
 609   }
 610 
 611   // Concrete interface methods do not need new entries, they override
 612   // abstract method entries using default inheritance rules
 613   if (target_method()->method_holder() != NULL &&
 614       target_method()->method_holder()->is_interface()  &&
 615       !target_method()->is_abstract()) {
 616     assert(target_method()->is_default_method(),
 617            "unexpected interface method type");




  49   return InstanceKlass::cast(_klass);
  50 }
  51 
  52 bool klassVtable::is_preinitialized_vtable() {
  53   return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
  54 }
  55 
  56 
  57 // this function computes the vtable size (including the size needed for miranda
  58 // methods) and the number of miranda methods in this class.
  59 // Note on Miranda methods: Let's say there is a class C that implements
  60 // interface I, and none of C's superclasses implements I.
  61 // Let's say there is an abstract method m in I that neither C
  62 // nor any of its super classes implement (i.e there is no method of any access,
  63 // with the same name and signature as m), then m is a Miranda method which is
  64 // entered as a public abstract method in C's vtable.  From then on it should
  65 // treated as any other public method in C for method over-ride purposes.
  66 void klassVtable::compute_vtable_size_and_num_mirandas(
  67     int* vtable_length_ret, int* num_new_mirandas,
  68     GrowableArray<Method*>* all_mirandas, const Klass* super,
  69     Array<Method*>* methods, AccessFlags class_flags, bool is_class_sealed, u2 major_version,
  70     Handle classloader, Symbol* classname, Array<InstanceKlass*>* local_interfaces,
  71     TRAPS) {
  72   NoSafepointVerifier nsv;
  73 
  74   // set up default result values
  75   int vtable_length = 0;
  76 
  77   // start off with super's vtable length
  78   vtable_length = super == NULL ? 0 : super->vtable_length();
  79 
  80   // go thru each method in the methods table to see if it needs a new entry
  81   int len = methods->length();
  82   for (int i = 0; i < len; i++) {
  83     assert(methods->at(i)->is_method(), "must be a Method*");
  84     methodHandle mh(THREAD, methods->at(i));
  85 
  86     if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, is_class_sealed, major_version, THREAD)) {
  87       assert(!methods->at(i)->is_private(), "private methods should not need a vtable entry");
  88       vtable_length += vtableEntry::size(); // we need a new entry
  89     }
  90   }
  91 
  92   GrowableArray<Method*> new_mirandas(20);
  93   // compute the number of mirandas methods that must be added to the end
  94   get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces,
  95                class_flags.is_interface());
  96   *num_new_mirandas = new_mirandas.length();
  97 
  98   // Interfaces do not need interface methods in their vtables
  99   // This includes miranda methods and during later processing, default methods
 100   if (!class_flags.is_interface()) {
 101      vtable_length += *num_new_mirandas * vtableEntry::size();
 102   }
 103 
 104   if (Universe::is_bootstrapping() && vtable_length == 0) {
 105     // array classes don't have their superclass set correctly during
 106     // bootstrapping


 380   // default_vtable_indices stores the vtable value relative to this inheritor
 381   if (default_index >= 0 ) {
 382     is_default = true;
 383     def_vtable_indices = klass->default_vtable_indices();
 384     assert(!target_method()->is_private(), "private interface method flagged as default");
 385     assert(def_vtable_indices != NULL, "def vtable alloc?");
 386     assert(default_index <= def_vtable_indices->length(), "def vtable len?");
 387   } else {
 388     assert(klass == target_method()->method_holder(), "caller resp.");
 389     // Initialize the method's vtable index to "nonvirtual".
 390     // If we allocate a vtable entry, we will update it to a non-negative number.
 391     target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
 392   }
 393 
 394   // Private, static and <init> methods are never in
 395   if (target_method()->is_private() || target_method()->is_static() ||
 396       (target_method()->name()->fast_compare(vmSymbols::object_initializer_name()) == 0)) {
 397     return false;
 398   }
 399 
 400   if (target_method->is_final_method(klass->access_flags(), klass->is_sealed())) {
 401     // a final method never needs a new entry; final methods can be statically
 402     // resolved and they have to be present in the vtable only if they override
 403     // a super's method, in which case they re-use its entry
 404     allocate_new = false;
 405   } else if (klass->is_interface()) {
 406     allocate_new = false;  // see note below in needs_new_vtable_entry
 407     // An interface never allocates new vtable slots, only inherits old ones.
 408     // This method will either be assigned its own itable index later,
 409     // or be assigned an inherited vtable index in the loop below.
 410     // default methods inherited by classes store their vtable indices
 411     // in the inheritor's default_vtable_indices.
 412     // default methods inherited by interfaces may already have a
 413     // valid itable index, if so, don't change it.
 414     // Overpass methods in an interface will be assigned an itable index later
 415     // by an inheriting class.
 416     if ((!is_default || !target_method()->has_itable_index())) {
 417       target_method()->set_vtable_index(Method::pending_itable_index);
 418     }
 419   }
 420 


 567       ls.cr();
 568     }
 569     table()[index].set(m);
 570   }
 571 }
 572 
 573 // Find out if a method "m" with superclass "super", loader "classloader" and
 574 // name "classname" needs a new vtable entry.  Let P be a class package defined
 575 // by "classloader" and "classname".
 576 // NOTE: The logic used here is very similar to the one used for computing
 577 // the vtables indices for a method. We cannot directly use that function because,
 578 // we allocate the InstanceKlass at load time, and that requires that the
 579 // superclass has been loaded.
 580 // However, the vtable entries are filled in at link time, and therefore
 581 // the superclass' vtable may not yet have been filled in.
 582 bool klassVtable::needs_new_vtable_entry(const methodHandle& target_method,
 583                                          const Klass* super,
 584                                          Handle classloader,
 585                                          Symbol* classname,
 586                                          AccessFlags class_flags,
 587                                          bool is_sealed,
 588                                          u2 major_version,
 589                                          TRAPS) {
 590   if (class_flags.is_interface()) {
 591     // Interfaces do not use vtables, except for java.lang.Object methods,
 592     // so there is no point to assigning
 593     // a vtable index to any of their local methods.  If we refrain from doing this,
 594     // we can use Method::_vtable_index to hold the itable index
 595     return false;
 596   }
 597 
 598   if (target_method->is_final_method(class_flags, is_sealed) ||
 599       // a final method never needs a new entry; final methods can be statically
 600       // resolved and they have to be present in the vtable only if they override
 601       // a super's method, in which case they re-use its entry
 602       (target_method()->is_private()) ||
 603       // private methods don't need to be in vtable
 604       (target_method()->is_static()) ||
 605       // static methods don't need to be in vtable
 606       (target_method()->name()->fast_compare(vmSymbols::object_initializer_name()) == 0)
 607       // <init> is never called dynamically-bound
 608       ) {
 609     return false;
 610   }
 611 
 612   // Concrete interface methods do not need new entries, they override
 613   // abstract method entries using default inheritance rules
 614   if (target_method()->method_holder() != NULL &&
 615       target_method()->method_holder()->is_interface()  &&
 616       !target_method()->is_abstract()) {
 617     assert(target_method()->is_default_method(),
 618            "unexpected interface method type");


< prev index next >