src/hotspot/share/oops/klassVtable.cpp
changeset 48608 1dab70e20292
parent 48557 2e867226b914
parent 48463 474cec233fb2
child 48819 ee513596f3ee
equal deleted inserted replaced
48607:7fc3d62481ba 48608:1dab70e20292
    84     }
    84     }
    85   }
    85   }
    86 
    86 
    87   GrowableArray<Method*> new_mirandas(20);
    87   GrowableArray<Method*> new_mirandas(20);
    88   // compute the number of mirandas methods that must be added to the end
    88   // compute the number of mirandas methods that must be added to the end
    89   get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces);
    89   get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces,
       
    90                class_flags.is_interface());
    90   *num_new_mirandas = new_mirandas.length();
    91   *num_new_mirandas = new_mirandas.length();
    91 
    92 
    92   // Interfaces do not need interface methods in their vtables
    93   // Interfaces do not need interface methods in their vtables
    93   // This includes miranda methods and during later processing, default methods
    94   // This includes miranda methods and during later processing, default methods
    94   if (!class_flags.is_interface()) {
    95   if (!class_flags.is_interface()) {
    95     vtable_length += *num_new_mirandas * vtableEntry::size();
    96      vtable_length += *num_new_mirandas * vtableEntry::size();
    96   }
    97   }
    97 
    98 
    98   if (Universe::is_bootstrapping() && vtable_length == 0) {
    99   if (Universe::is_bootstrapping() && vtable_length == 0) {
    99     // array classes don't have their superclass set correctly during
   100     // array classes don't have their superclass set correctly during
   100     // bootstrapping
   101     // bootstrapping
   452       klassVtable superVtable = super->vtable();
   453       klassVtable superVtable = super->vtable();
   453       super_method = superVtable.method_at(i);
   454       super_method = superVtable.method_at(i);
   454     } else {
   455     } else {
   455       super_method = method_at(i);
   456       super_method = method_at(i);
   456     }
   457     }
   457     // Check if method name matches
   458     // Check if method name matches.  Ignore match if klass is an interface and the
   458     if (super_method->name() == name && super_method->signature() == signature) {
   459     // matching method is a non-public java.lang.Object method.  (See JVMS 5.4.3.4)
       
   460     // This is safe because the method at this slot should never get invoked.
       
   461     // (TBD: put in a method to throw NoSuchMethodError if this slot is ever used.)
       
   462     if (super_method->name() == name && super_method->signature() == signature &&
       
   463         (!_klass->is_interface() ||
       
   464          !SystemDictionary::is_nonpublic_Object_method(super_method))) {
   459 
   465 
   460       // get super_klass for method_holder for the found method
   466       // get super_klass for method_holder for the found method
   461       InstanceKlass* super_klass =  super_method->method_holder();
   467       InstanceKlass* super_klass =  super_method->method_holder();
   462 
   468 
   463       // Whether the method is being overridden
   469       // Whether the method is being overridden
   711 
   717 
   712   // miranda methods are public abstract instance interface methods in a class's vtable
   718   // miranda methods are public abstract instance interface methods in a class's vtable
   713   if (mhk->is_interface()) {
   719   if (mhk->is_interface()) {
   714     assert(m->is_public(), "should be public");
   720     assert(m->is_public(), "should be public");
   715     assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
   721     assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
   716     if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) {
   722     if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super(), klass()->is_interface())) {
   717       return true;
   723       return true;
   718     }
   724     }
   719   }
   725   }
   720   return false;
   726   return false;
   721 }
   727 }
   736 // include superinterface abstract and default methods (non-private instance).
   742 // include superinterface abstract and default methods (non-private instance).
   737 // We include potential default methods to give them space in the vtable.
   743 // We include potential default methods to give them space in the vtable.
   738 // During the first run, the current instanceKlass has not yet been
   744 // During the first run, the current instanceKlass has not yet been
   739 // created, the superclasses and superinterfaces do have instanceKlasses
   745 // created, the superclasses and superinterfaces do have instanceKlasses
   740 // but may not have vtables, the default_methods list is empty, no overpasses.
   746 // but may not have vtables, the default_methods list is empty, no overpasses.
   741 // This is seen by default method creation.
   747 // Default method generation uses the all_mirandas array as the starter set for
       
   748 // maximally-specific default method calculation.  So, for both classes and
       
   749 // interfaces, it is necessary that the first pass will find all non-private
       
   750 // interface instance methods, whether or not they are concrete.
   742 //
   751 //
   743 // Pass 2: recalculated during vtable initialization: only include abstract methods.
   752 // Pass 2: recalculated during vtable initialization: only include abstract methods.
   744 // The goal of pass 2 is to walk through the superinterfaces to see if any of
   753 // The goal of pass 2 is to walk through the superinterfaces to see if any of
   745 // the superinterface methods (which were all abstract pre-default methods)
   754 // the superinterface methods (which were all abstract pre-default methods)
   746 // need to be added to the vtable.
   755 // need to be added to the vtable.
   770 // the vtable index for the miranda method.
   779 // the vtable index for the miranda method.
   771 //
   780 //
   772 // Part of the Miranda Rights in the US mean that if you do not have
   781 // Part of the Miranda Rights in the US mean that if you do not have
   773 // an attorney one will be appointed for you.
   782 // an attorney one will be appointed for you.
   774 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
   783 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
   775                              Array<Method*>* default_methods, const Klass* super) {
   784                              Array<Method*>* default_methods, const Klass* super,
       
   785                              bool is_interface) {
   776   if (m->is_static() || m->is_private() || m->is_overpass()) {
   786   if (m->is_static() || m->is_private() || m->is_overpass()) {
   777     return false;
   787     return false;
   778   }
   788   }
   779   Symbol* name = m->name();
   789   Symbol* name = m->name();
   780   Symbol* signature = m->signature();
   790   Symbol* signature = m->signature();
   798   // Overpasses may or may not exist for supers for pass 1,
   808   // Overpasses may or may not exist for supers for pass 1,
   799   // they should have been created for pass 2 and later.
   809   // they should have been created for pass 2 and later.
   800 
   810 
   801   for (const Klass* cursuper = super; cursuper != NULL; cursuper = cursuper->super())
   811   for (const Klass* cursuper = super; cursuper != NULL; cursuper = cursuper->super())
   802   {
   812   {
   803      if (InstanceKlass::cast(cursuper)->find_local_method(name, signature,
   813      Method* found_mth = InstanceKlass::cast(cursuper)->find_local_method(name, signature,
   804            Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL) {
   814        Klass::find_overpass, Klass::skip_static, Klass::skip_private);
       
   815      // Ignore non-public methods in java.lang.Object if klass is an interface.
       
   816      if (found_mth != NULL && (!is_interface ||
       
   817          !SystemDictionary::is_nonpublic_Object_method(found_mth))) {
   805        return false;
   818        return false;
   806      }
   819      }
   807   }
   820   }
   808 
   821 
   809   return true;
   822   return true;
   818 // all_mirandas will be the set of all mirandas applicable to this class
   831 // all_mirandas will be the set of all mirandas applicable to this class
   819 // including all defined in superclasses.
   832 // including all defined in superclasses.
   820 void klassVtable::add_new_mirandas_to_lists(
   833 void klassVtable::add_new_mirandas_to_lists(
   821     GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,
   834     GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,
   822     Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
   835     Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
   823     Array<Method*>* default_methods, const Klass* super) {
   836     Array<Method*>* default_methods, const Klass* super, bool is_interface) {
   824 
   837 
   825   // iterate thru the current interface's method to see if it a miranda
   838   // iterate thru the current interface's method to see if it a miranda
   826   int num_methods = current_interface_methods->length();
   839   int num_methods = current_interface_methods->length();
   827   for (int i = 0; i < num_methods; i++) {
   840   for (int i = 0; i < num_methods; i++) {
   828     Method* im = current_interface_methods->at(i);
   841     Method* im = current_interface_methods->at(i);
   837         break;
   850         break;
   838       }
   851       }
   839     }
   852     }
   840 
   853 
   841     if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
   854     if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
   842       if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all?
   855       if (is_miranda(im, class_methods, default_methods, super, is_interface)) { // is it a miranda at all?
   843         const InstanceKlass *sk = InstanceKlass::cast(super);
   856         const InstanceKlass *sk = InstanceKlass::cast(super);
   844         // check if it is a duplicate of a super's miranda
   857         // check if it is a duplicate of a super's miranda
   845         if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::find_defaults) == NULL) {
   858         if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::find_defaults) == NULL) {
   846           new_mirandas->append(im);
   859           new_mirandas->append(im);
   847         }
   860         }
   856 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
   869 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
   857                                GrowableArray<Method*>* all_mirandas,
   870                                GrowableArray<Method*>* all_mirandas,
   858                                const Klass* super,
   871                                const Klass* super,
   859                                Array<Method*>* class_methods,
   872                                Array<Method*>* class_methods,
   860                                Array<Method*>* default_methods,
   873                                Array<Method*>* default_methods,
   861                                Array<Klass*>* local_interfaces) {
   874                                Array<Klass*>* local_interfaces,
       
   875                                bool is_interface) {
   862   assert((new_mirandas->length() == 0) , "current mirandas must be 0");
   876   assert((new_mirandas->length() == 0) , "current mirandas must be 0");
   863 
   877 
   864   // iterate thru the local interfaces looking for a miranda
   878   // iterate thru the local interfaces looking for a miranda
   865   int num_local_ifs = local_interfaces->length();
   879   int num_local_ifs = local_interfaces->length();
   866   for (int i = 0; i < num_local_ifs; i++) {
   880   for (int i = 0; i < num_local_ifs; i++) {
   867     InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
   881     InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
   868     add_new_mirandas_to_lists(new_mirandas, all_mirandas,
   882     add_new_mirandas_to_lists(new_mirandas, all_mirandas,
   869                               ik->methods(), class_methods,
   883                               ik->methods(), class_methods,
   870                               default_methods, super);
   884                               default_methods, super, is_interface);
   871     // iterate thru each local's super interfaces
   885     // iterate thru each local's super interfaces
   872     Array<Klass*>* super_ifs = ik->transitive_interfaces();
   886     Array<Klass*>* super_ifs = ik->transitive_interfaces();
   873     int num_super_ifs = super_ifs->length();
   887     int num_super_ifs = super_ifs->length();
   874     for (int j = 0; j < num_super_ifs; j++) {
   888     for (int j = 0; j < num_super_ifs; j++) {
   875       InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
   889       InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
   876       add_new_mirandas_to_lists(new_mirandas, all_mirandas,
   890       add_new_mirandas_to_lists(new_mirandas, all_mirandas,
   877                                 sik->methods(), class_methods,
   891                                 sik->methods(), class_methods,
   878                                 default_methods, super);
   892                                 default_methods, super, is_interface);
   879     }
   893     }
   880   }
   894   }
   881 }
   895 }
   882 
   896 
   883 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
   897 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
   886 // Miranda methods use vtable entries, but do not get assigned a vtable_index
   900 // Miranda methods use vtable entries, but do not get assigned a vtable_index
   887 // The vtable_index is discovered by searching from the end of the vtable
   901 // The vtable_index is discovered by searching from the end of the vtable
   888 int klassVtable::fill_in_mirandas(int initialized) {
   902 int klassVtable::fill_in_mirandas(int initialized) {
   889   GrowableArray<Method*> mirandas(20);
   903   GrowableArray<Method*> mirandas(20);
   890   get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
   904   get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
   891                ik()->default_methods(), ik()->local_interfaces());
   905                ik()->default_methods(), ik()->local_interfaces(),
       
   906                klass()->is_interface());
   892   for (int i = 0; i < mirandas.length(); i++) {
   907   for (int i = 0; i < mirandas.length(); i++) {
   893     if (log_develop_is_enabled(Trace, vtables)) {
   908     if (log_develop_is_enabled(Trace, vtables)) {
   894       Method* meth = mirandas.at(i);
   909       Method* meth = mirandas.at(i);
   895       ResourceMark rm(Thread::current());
   910       ResourceMark rm(Thread::current());
   896       LogTarget(Trace, vtables) lt;
   911       LogTarget(Trace, vtables) lt;