< prev index next >

src/share/vm/oops/klassVtable.cpp

Print this page




 209         initialized++;
 210       }
 211     }
 212 
 213     // update vtable with default_methods
 214     Array<Method*>* default_methods = ik()->default_methods();
 215     if (default_methods != NULL) {
 216       len = default_methods->length();
 217       if (len > 0) {
 218         Array<int>* def_vtable_indices = NULL;
 219         if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) {
 220           assert(!is_shared, "shared class def_vtable_indices does not exist");
 221           def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK);
 222         } else {
 223           assert(def_vtable_indices->length() == len, "reinit vtable len?");
 224         }
 225         for (int i = 0; i < len; i++) {
 226           HandleMark hm(THREAD);
 227           assert(default_methods->at(i)->is_method(), "must be a Method*");
 228           methodHandle mh(THREAD, default_methods->at(i));
 229 
 230           bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK);
 231 
 232           // needs new entry
 233           if (needs_new_entry) {
 234             put_method_at(mh(), initialized);
 235             if (is_preinitialized_vtable()) {
 236               // At runtime initialize_vtable is rerun for a shared class
 237               // (loaded by the non-boot loader) as part of link_class_impl().
 238               // The dumptime vtable index should be the same as the runtime index.
 239               assert(def_vtable_indices->at(i) == initialized,
 240                      "dump time vtable index is different from runtime index");
 241             } else {
 242               def_vtable_indices->at_put(i, initialized); //set vtable index
 243             }
 244             initialized++;
 245           }
 246         }
 247       }
 248     }
 249 


 345     logst->print("overriders flags: ");
 346     target_method->print_linkage_flags(logst);
 347     logst->cr();
 348   }
 349 #endif
 350 }
 351 
 352 // Update child's copy of super vtable for overrides
 353 // OR return true if a new vtable entry is required.
 354 // Only called for InstanceKlass's, i.e. not for arrays
 355 // If that changed, could not use _klass as handle for klass
 356 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
 357                                           int super_vtable_len, int default_index,
 358                                           bool checkconstraints, TRAPS) {
 359   ResourceMark rm;
 360   bool allocate_new = true;
 361   assert(klass->is_instance_klass(), "must be InstanceKlass");
 362 
 363   Array<int>* def_vtable_indices = NULL;
 364   bool is_default = false;
 365   // default methods are concrete methods in superinterfaces which are added to the vtable
 366   // with their real method_holder

 367   // Since vtable and itable indices share the same storage, don't touch
 368   // the default method's real vtable/itable index
 369   // default_vtable_indices stores the vtable value relative to this inheritor
 370   if (default_index >= 0 ) {
 371     is_default = true;
 372     def_vtable_indices = klass->default_vtable_indices();

 373     assert(def_vtable_indices != NULL, "def vtable alloc?");
 374     assert(default_index <= def_vtable_indices->length(), "def vtable len?");
 375   } else {
 376     assert(klass == target_method()->method_holder(), "caller resp.");
 377     // Initialize the method's vtable index to "nonvirtual".
 378     // If we allocate a vtable entry, we will update it to a non-negative number.
 379     target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
 380   }
 381 
 382   // Static and <init> methods are never in
 383   if (target_method()->is_static() || target_method()->name() ==  vmSymbols::object_initializer_name()) {
 384     return false;
 385   }
 386 
 387   if (target_method->is_final_method(klass->access_flags())) {
 388     // a final method never needs a new entry; final methods can be statically
 389     // resolved and they have to be present in the vtable only if they override
 390     // a super's method, in which case they re-use its entry
 391     allocate_new = false;
 392   } else if (klass->is_interface()) {
 393     allocate_new = false;  // see note below in needs_new_vtable_entry
 394     // An interface never allocates new vtable slots, only inherits old ones.
 395     // This method will either be assigned its own itable index later,
 396     // or be assigned an inherited vtable index in the loop below.
 397     // default methods inherited by classes store their vtable indices
 398     // in the inheritor's default_vtable_indices
 399     // default methods inherited by interfaces may already have a
 400     // valid itable index, if so, don't change it
 401     // overpass methods in an interface will be assigned an itable index later
 402     // by an inheriting class
 403     if (!is_default || !target_method()->has_itable_index()) {



 404       target_method()->set_vtable_index(Method::pending_itable_index);
 405     }
 406   }
 407 
 408   // we need a new entry if there is no superclass
 409   Klass* super = klass->super();
 410   if (super == NULL) {
 411     return allocate_new;
 412   }
 413 
 414   // private methods in classes always have a new entry in the vtable
 415   // specification interpretation since classic has
 416   // private methods not overriding
 417   // JDK8 adds private methods in interfaces which require invokespecial
 418   if (target_method()->is_private()) {
 419     return allocate_new;
 420   }
 421 
 422   // search through the vtable and update overridden entries
 423   // Since check_signature_loaders acquires SystemDictionary_lock


 580     // we can use Method::_vtable_index to hold the itable index
 581     return false;
 582   }
 583 
 584   if (target_method->is_final_method(class_flags) ||
 585       // a final method never needs a new entry; final methods can be statically
 586       // resolved and they have to be present in the vtable only if they override
 587       // a super's method, in which case they re-use its entry
 588       (target_method()->is_static()) ||
 589       // static methods don't need to be in vtable
 590       (target_method()->name() ==  vmSymbols::object_initializer_name())
 591       // <init> is never called dynamically-bound
 592       ) {
 593     return false;
 594   }
 595 
 596   // Concrete interface methods do not need new entries, they override
 597   // abstract method entries using default inheritance rules
 598   if (target_method()->method_holder() != NULL &&
 599       target_method()->method_holder()->is_interface()  &&
 600       !target_method()->is_abstract() ) {


 601     return false;
 602   }
 603 
 604   // we need a new entry if there is no superclass
 605   if (super == NULL) {
 606     return true;
 607   }
 608 
 609   // private methods in classes always have a new entry in the vtable
 610   // specification interpretation since classic has
 611   // private methods not overriding
 612   // JDK8 adds private  methods in interfaces which require invokespecial
 613   if (target_method()->is_private()) {
 614     return true;
 615   }
 616 
 617   // Package private methods always need a new entry to root their own
 618   // overriding. This allows transitive overriding to work.
 619   if (target_method()->is_package_private()) {
 620     return true;
 621   }
 622 
 623   // search through the super class hierarchy to see if we need
 624   // a new entry
 625   ResourceMark rm;
 626   Symbol* name = target_method()->name();
 627   Symbol* signature = target_method()->signature();
 628   const Klass* k = super;
 629   Method* super_method = NULL;
 630   InstanceKlass *holder = NULL;
 631   Method* recheck_method =  NULL;
 632   while (k != NULL) {


1071     // Iterate through all interfaces
1072     int i;
1073     for(i = 0; i < num_interfaces; i++) {
1074       itableOffsetEntry* ioe = offset_entry(i);
1075       HandleMark hm(THREAD);
1076       KlassHandle interf_h (THREAD, ioe->interface_klass());
1077       assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1078       initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1079     }
1080 
1081   }
1082   // Check that the last entry is empty
1083   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1084   guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1085 }
1086 
1087 
1088 inline bool interface_method_needs_itable_index(Method* m) {
1089   if (m->is_static())           return false;   // e.g., Stream.empty
1090   if (m->is_initializer())      return false;   // <init> or <clinit>

1091   // If an interface redeclares a method from java.lang.Object,
1092   // it should already have a vtable index, don't touch it.
1093   // e.g., CharSequence.toString (from initialize_vtable)
1094   // if (m->has_vtable_index())  return false; // NO!
1095   return true;
1096 }
1097 
1098 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1099   // an interface does not have an itable, but its methods need to be numbered
1100   log_develop_debug(itables)("%3d: Initializing itable indices for interface %s",
1101                              ++initialize_count, klass->name()->as_C_string());
1102   Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1103   int nof_methods = methods->length();
1104   int ime_num = 0;
1105   for (int i = 0; i < nof_methods; i++) {
1106     Method* m = methods->at(i);
1107     if (interface_method_needs_itable_index(m)) {
1108       assert(!m->is_final_method(), "no final interface methods");
1109       // If m is already assigned a vtable index, do not disturb it.
1110       if (log_develop_is_enabled(Trace, itables)) {




 209         initialized++;
 210       }
 211     }
 212 
 213     // update vtable with default_methods
 214     Array<Method*>* default_methods = ik()->default_methods();
 215     if (default_methods != NULL) {
 216       len = default_methods->length();
 217       if (len > 0) {
 218         Array<int>* def_vtable_indices = NULL;
 219         if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) {
 220           assert(!is_shared, "shared class def_vtable_indices does not exist");
 221           def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK);
 222         } else {
 223           assert(def_vtable_indices->length() == len, "reinit vtable len?");
 224         }
 225         for (int i = 0; i < len; i++) {
 226           HandleMark hm(THREAD);
 227           assert(default_methods->at(i)->is_method(), "must be a Method*");
 228           methodHandle mh(THREAD, default_methods->at(i));
 229           assert(!mh()->is_private(), "private interface method in the default method list");
 230           bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK);
 231 
 232           // needs new entry
 233           if (needs_new_entry) {
 234             put_method_at(mh(), initialized);
 235             if (is_preinitialized_vtable()) {
 236               // At runtime initialize_vtable is rerun for a shared class
 237               // (loaded by the non-boot loader) as part of link_class_impl().
 238               // The dumptime vtable index should be the same as the runtime index.
 239               assert(def_vtable_indices->at(i) == initialized,
 240                      "dump time vtable index is different from runtime index");
 241             } else {
 242               def_vtable_indices->at_put(i, initialized); //set vtable index
 243             }
 244             initialized++;
 245           }
 246         }
 247       }
 248     }
 249 


 345     logst->print("overriders flags: ");
 346     target_method->print_linkage_flags(logst);
 347     logst->cr();
 348   }
 349 #endif
 350 }
 351 
 352 // Update child's copy of super vtable for overrides
 353 // OR return true if a new vtable entry is required.
 354 // Only called for InstanceKlass's, i.e. not for arrays
 355 // If that changed, could not use _klass as handle for klass
 356 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
 357                                           int super_vtable_len, int default_index,
 358                                           bool checkconstraints, TRAPS) {
 359   ResourceMark rm;
 360   bool allocate_new = true;
 361   assert(klass->is_instance_klass(), "must be InstanceKlass");
 362 
 363   Array<int>* def_vtable_indices = NULL;
 364   bool is_default = false;
 365 
 366   // default methods are non-private concrete methods in superinterfaces which are added
 367   // to the vtable with their real method_holder.
 368   // Since vtable and itable indices share the same storage, don't touch
 369   // the default method's real vtable/itable index.
 370   // default_vtable_indices stores the vtable value relative to this inheritor
 371   if (default_index >= 0 ) {
 372     is_default = true;
 373     def_vtable_indices = klass->default_vtable_indices();
 374     assert(!target_method()->is_private(), "private interface method flagged as default");
 375     assert(def_vtable_indices != NULL, "def vtable alloc?");
 376     assert(default_index <= def_vtable_indices->length(), "def vtable len?");
 377   } else {
 378     assert(klass == target_method()->method_holder(), "caller resp.");
 379     // Initialize the method's vtable index to "nonvirtual".
 380     // If we allocate a vtable entry, we will update it to a non-negative number.
 381     target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
 382   }
 383 
 384   // Static and <init> methods are never in
 385   if (target_method()->is_static() || target_method()->name() ==  vmSymbols::object_initializer_name()) {
 386     return false;
 387   }
 388 
 389   if (target_method->is_final_method(klass->access_flags())) {
 390     // a final method never needs a new entry; final methods can be statically
 391     // resolved and they have to be present in the vtable only if they override
 392     // a super's method, in which case they re-use its entry
 393     allocate_new = false;
 394   } else if (klass->is_interface()) {
 395     allocate_new = false;  // see note below in needs_new_vtable_entry
 396     // An interface never allocates new vtable slots, only inherits old ones.
 397     // This method will either be assigned its own itable index later,
 398     // or be assigned an inherited vtable index in the loop below.
 399     // default methods inherited by classes store their vtable indices
 400     // in the inheritor's default_vtable_indices.
 401     // default methods inherited by interfaces may already have a
 402     // valid itable index, if so, don't change it.
 403     // Overpass methods in an interface will be assigned an itable index later
 404     // by an inheriting class.
 405     // Private interface methods have no itable index and are always invoked nonvirtually,
 406     // so they retain their nonvirtual_vtable_index value, and therefore can_be_statically_bound()
 407     // will return true.
 408     if ((!is_default || !target_method()->has_itable_index()) && !target_method()->is_private()) {
 409       target_method()->set_vtable_index(Method::pending_itable_index);
 410     }
 411   }
 412 
 413   // we need a new entry if there is no superclass
 414   Klass* super = klass->super();
 415   if (super == NULL) {
 416     return allocate_new;
 417   }
 418 
 419   // private methods in classes always have a new entry in the vtable
 420   // specification interpretation since classic has
 421   // private methods not overriding
 422   // JDK8 adds private methods in interfaces which require invokespecial
 423   if (target_method()->is_private()) {
 424     return allocate_new;
 425   }
 426 
 427   // search through the vtable and update overridden entries
 428   // Since check_signature_loaders acquires SystemDictionary_lock


 585     // we can use Method::_vtable_index to hold the itable index
 586     return false;
 587   }
 588 
 589   if (target_method->is_final_method(class_flags) ||
 590       // a final method never needs a new entry; final methods can be statically
 591       // resolved and they have to be present in the vtable only if they override
 592       // a super's method, in which case they re-use its entry
 593       (target_method()->is_static()) ||
 594       // static methods don't need to be in vtable
 595       (target_method()->name() ==  vmSymbols::object_initializer_name())
 596       // <init> is never called dynamically-bound
 597       ) {
 598     return false;
 599   }
 600 
 601   // Concrete interface methods do not need new entries, they override
 602   // abstract method entries using default inheritance rules
 603   if (target_method()->method_holder() != NULL &&
 604       target_method()->method_holder()->is_interface()  &&
 605       !target_method()->is_abstract()) {
 606     assert(target_method()->is_default_method() || target_method()->is_private(),
 607            "unexpected interface method type");
 608     return false;
 609   }
 610 
 611   // we need a new entry if there is no superclass
 612   if (super == NULL) {
 613     return true;
 614   }
 615 
 616   // private methods in classes always have a new entry in the vtable.
 617   // Specification interpretation since classic has private methods not overriding.


 618   if (target_method()->is_private()) {
 619     return true;
 620   }
 621 
 622   // Package private methods always need a new entry to root their own
 623   // overriding. This allows transitive overriding to work.
 624   if (target_method()->is_package_private()) {
 625     return true;
 626   }
 627 
 628   // search through the super class hierarchy to see if we need
 629   // a new entry
 630   ResourceMark rm;
 631   Symbol* name = target_method()->name();
 632   Symbol* signature = target_method()->signature();
 633   const Klass* k = super;
 634   Method* super_method = NULL;
 635   InstanceKlass *holder = NULL;
 636   Method* recheck_method =  NULL;
 637   while (k != NULL) {


1076     // Iterate through all interfaces
1077     int i;
1078     for(i = 0; i < num_interfaces; i++) {
1079       itableOffsetEntry* ioe = offset_entry(i);
1080       HandleMark hm(THREAD);
1081       KlassHandle interf_h (THREAD, ioe->interface_klass());
1082       assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1083       initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1084     }
1085 
1086   }
1087   // Check that the last entry is empty
1088   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1089   guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1090 }
1091 
1092 
1093 inline bool interface_method_needs_itable_index(Method* m) {
1094   if (m->is_static())           return false;   // e.g., Stream.empty
1095   if (m->is_initializer())      return false;   // <init> or <clinit>
1096   if (m->is_private())          return false;   // requires invokeSpecial
1097   // If an interface redeclares a method from java.lang.Object,
1098   // it should already have a vtable index, don't touch it.
1099   // e.g., CharSequence.toString (from initialize_vtable)
1100   // if (m->has_vtable_index())  return false; // NO!
1101   return true;
1102 }
1103 
1104 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1105   // an interface does not have an itable, but its methods need to be numbered
1106   log_develop_debug(itables)("%3d: Initializing itable indices for interface %s",
1107                              ++initialize_count, klass->name()->as_C_string());
1108   Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1109   int nof_methods = methods->length();
1110   int ime_num = 0;
1111   for (int i = 0; i < nof_methods; i++) {
1112     Method* m = methods->at(i);
1113     if (interface_method_needs_itable_index(m)) {
1114       assert(!m->is_final_method(), "no final interface methods");
1115       // If m is already assigned a vtable index, do not disturb it.
1116       if (log_develop_is_enabled(Trace, itables)) {


< prev index next >