< prev index next >

src/hotspot/share/classfile/defaultMethods.cpp

Print this page




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/bytecodeAssembler.hpp"
  27 #include "classfile/defaultMethods.hpp"
  28 #include "classfile/symbolTable.hpp"

  29 #include "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/metadataFactory.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "runtime/handles.inline.hpp"
  35 #include "runtime/signature.hpp"
  36 #include "runtime/thread.hpp"
  37 #include "oops/instanceKlass.hpp"
  38 #include "oops/klass.hpp"
  39 #include "oops/method.hpp"
  40 #include "utilities/accessFlags.hpp"
  41 #include "utilities/exceptions.hpp"
  42 #include "utilities/ostream.hpp"
  43 #include "utilities/pair.hpp"
  44 #include "utilities/resourceHash.hpp"
  45 
  46 typedef enum { QUALIFIED, DISQUALIFIED } QualifiedState;
  47 
  48 // Because we use an iterative algorithm when iterating over the type


 666     LogStream ls(lt);
 667     streamIndentor si(&ls);
 668     for (int i = 0; i < slots->length(); ++i) {
 669       ls.indent();
 670       slots->at(i)->print_on(&ls);
 671       ls.cr();
 672     }
 673   }
 674 
 675   return slots;
 676 }
 677 
 678 // Iterates over the superinterface type hierarchy looking for all methods
 679 // with a specific erased signature.
 680 class FindMethodsByErasedSig : public HierarchyVisitor<FindMethodsByErasedSig> {
 681  private:
 682   // Context data
 683   Symbol* _method_name;
 684   Symbol* _method_signature;
 685   StatefulMethodFamily*  _family;

 686 
 687  public:
 688   FindMethodsByErasedSig(Symbol* name, Symbol* signature) :
 689       _method_name(name), _method_signature(signature),
 690       _family(NULL) {}
 691 
 692   void get_discovered_family(MethodFamily** family) {
 693       if (_family != NULL) {
 694         *family = _family->get_method_family();
 695       } else {
 696         *family = NULL;
 697       }
 698   }
 699 
 700   void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); }
 701   void free_node_data(void* node_data) {
 702     PseudoScope::cast(node_data)->destroy();
 703   }
 704 
 705   // Find all methods on this hierarchy that match this
 706   // method's erased (name, signature)
 707   bool visit() {
 708     PseudoScope* scope = PseudoScope::cast(current_data());
 709     InstanceKlass* iklass = current_class();
 710 
 711     Method* m = iklass->find_method(_method_name, _method_signature);
 712     // private interface methods are not candidates for default methods
 713     // invokespecial to private interface methods doesn't use default method logic
 714     // private class methods are not candidates for default methods,
 715     // private methods do not override default methods, so need to perform
 716     // default method inheritance without including private methods
 717     // The overpasses are your supertypes' errors, we do not include them
 718     // future: take access controls into account for superclass methods
 719     if (m != NULL && !m->is_static() && !m->is_overpass() && !m->is_private()) {



 720       if (_family == NULL) {
 721         _family = new StatefulMethodFamily();
 722       }
 723 
 724       if (iklass->is_interface()) {
 725         StateRestorer* restorer = _family->record_method_and_dq_further(m);
 726         scope->add_mark(restorer);
 727       } else {
 728         // This is the rule that methods in classes "win" (bad word) over
 729         // methods in interfaces. This works because of single inheritance
 730         // private methods in classes do not "win", they will be found
 731         // first on searching, but overriding for invokevirtual needs
 732         // to find default method candidates for the same signature
 733         _family->set_target_if_empty(m);
 734       }
 735     }
 736     return true;
 737   }
 738 
 739 };
 740 
 741 
 742 
 743 static void create_defaults_and_exceptions(
 744     GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS);
 745 
 746 static void generate_erased_defaults(
 747      InstanceKlass* klass, GrowableArray<EmptyVtableSlot*>* empty_slots,
 748      EmptyVtableSlot* slot, TRAPS) {
 749 
 750   // sets up a set of methods with the same exact erased signature
 751   FindMethodsByErasedSig visitor(slot->name(), slot->signature());
 752   visitor.run(klass);
 753 
 754   MethodFamily* family;
 755   visitor.get_discovered_family(&family);
 756   if (family != NULL) {
 757     family->determine_target(klass, CHECK);
 758     slot->bind_family(family);
 759   }
 760 }
 761 
 762 static void merge_in_new_methods(InstanceKlass* klass,
 763     GrowableArray<Method*>* new_methods, TRAPS);
 764 static void create_default_methods( InstanceKlass* klass,
 765     GrowableArray<Method*>* new_methods, TRAPS);
 766 
 767 // This is the guts of the default methods implementation.  This is called just
 768 // after the classfile has been parsed if some ancestor has default methods.
 769 //
 770 // First it finds any name/signature slots that need any implementation (either
 771 // because they are miranda or a superclass's implementation is an overpass


 800              klass->is_interface() ? "Interface" : "Class",
 801              klass->name()->as_klass_external_name());
 802     LogStream ls(lt);
 803     PrintHierarchy printer(&ls);
 804     printer.run(klass);
 805   }
 806 
 807   GrowableArray<EmptyVtableSlot*>* empty_slots =
 808       find_empty_vtable_slots(klass, mirandas, CHECK);
 809 
 810   for (int i = 0; i < empty_slots->length(); ++i) {
 811     EmptyVtableSlot* slot = empty_slots->at(i);
 812     LogTarget(Debug, defaultmethods) lt;
 813     if (lt.is_enabled()) {
 814       LogStream ls(lt);
 815       streamIndentor si(&ls, 2);
 816       ls.indent().print("Looking for default methods for slot ");
 817       slot->print_on(&ls);
 818       ls.cr();
 819     }
 820     generate_erased_defaults(klass, empty_slots, slot, CHECK);
 821   }
 822   log_debug(defaultmethods)("Creating defaults and overpasses...");
 823   create_defaults_and_exceptions(empty_slots, klass, CHECK);
 824   log_debug(defaultmethods)("Default method processing complete");
 825 }
 826 
 827 static int assemble_method_error(
 828     BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* errorName, Symbol* message, TRAPS) {
 829 
 830   Symbol* init = vmSymbols::object_initializer_name();
 831   Symbol* sig = vmSymbols::string_void_signature();
 832 
 833   BytecodeAssembler assem(buffer, cp);
 834 
 835   assem._new(errorName);
 836   assem.dup();
 837   assem.load_string(message);
 838   assem.invokespecial(errorName, init, sig);
 839   assem.athrow();
 840 




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/bytecodeAssembler.hpp"
  27 #include "classfile/defaultMethods.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "logging/log.hpp"
  31 #include "logging/logStream.hpp"
  32 #include "memory/allocation.hpp"
  33 #include "memory/metadataFactory.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "runtime/handles.inline.hpp"
  36 #include "runtime/signature.hpp"
  37 #include "runtime/thread.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/klass.hpp"
  40 #include "oops/method.hpp"
  41 #include "utilities/accessFlags.hpp"
  42 #include "utilities/exceptions.hpp"
  43 #include "utilities/ostream.hpp"
  44 #include "utilities/pair.hpp"
  45 #include "utilities/resourceHash.hpp"
  46 
  47 typedef enum { QUALIFIED, DISQUALIFIED } QualifiedState;
  48 
  49 // Because we use an iterative algorithm when iterating over the type


 667     LogStream ls(lt);
 668     streamIndentor si(&ls);
 669     for (int i = 0; i < slots->length(); ++i) {
 670       ls.indent();
 671       slots->at(i)->print_on(&ls);
 672       ls.cr();
 673     }
 674   }
 675 
 676   return slots;
 677 }
 678 
 679 // Iterates over the superinterface type hierarchy looking for all methods
 680 // with a specific erased signature.
 681 class FindMethodsByErasedSig : public HierarchyVisitor<FindMethodsByErasedSig> {
 682  private:
 683   // Context data
 684   Symbol* _method_name;
 685   Symbol* _method_signature;
 686   StatefulMethodFamily*  _family;
 687   bool _cur_class_is_interface;
 688 
 689  public:
 690   FindMethodsByErasedSig(Symbol* name, Symbol* signature, bool is_interf) :
 691       _method_name(name), _method_signature(signature), _cur_class_is_interface(is_interf),
 692       _family(NULL) {}
 693 
 694   void get_discovered_family(MethodFamily** family) {
 695       if (_family != NULL) {
 696         *family = _family->get_method_family();
 697       } else {
 698         *family = NULL;
 699       }
 700   }
 701 
 702   void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); }
 703   void free_node_data(void* node_data) {
 704     PseudoScope::cast(node_data)->destroy();
 705   }
 706 
 707   // Find all methods on this hierarchy that match this
 708   // method's erased (name, signature)
 709   bool visit() {
 710     PseudoScope* scope = PseudoScope::cast(current_data());
 711     InstanceKlass* iklass = current_class();
 712 
 713     Method* m = iklass->find_method(_method_name, _method_signature);
 714     // Private interface methods are not candidates for default methods.
 715     // invokespecial to private interface methods doesn't use default method logic.
 716     // Private class methods are not candidates for default methods.
 717     // Private methods do not override default methods, so need to perform
 718     // default method inheritance without including private methods.
 719     // The overpasses are your supertypes' errors, we do not include them.
 720     // Non-public methods in java.lang.Object are not candidates for default
 721     // methods.
 722     // Future: take access controls into account for superclass methods
 723     if (m != NULL && !m->is_static() && !m->is_overpass() && !m->is_private() &&
 724      (!_cur_class_is_interface || !SystemDictionary::is_nonpublic_Object_method(m))) {
 725       if (_family == NULL) {
 726         _family = new StatefulMethodFamily();
 727       }
 728 
 729       if (iklass->is_interface()) {
 730         StateRestorer* restorer = _family->record_method_and_dq_further(m);
 731         scope->add_mark(restorer);
 732       } else {
 733         // This is the rule that methods in classes "win" (bad word) over
 734         // methods in interfaces. This works because of single inheritance.
 735         // Private methods in classes do not "win", they will be found
 736         // first on searching, but overriding for invokevirtual needs
 737         // to find default method candidates for the same signature
 738         _family->set_target_if_empty(m);
 739       }
 740     }
 741     return true;
 742   }
 743 
 744 };
 745 
 746 
 747 
 748 static void create_defaults_and_exceptions(
 749     GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS);
 750 
 751 static void generate_erased_defaults(
 752      InstanceKlass* klass, GrowableArray<EmptyVtableSlot*>* empty_slots,
 753      EmptyVtableSlot* slot, bool is_intf, TRAPS) {
 754 
 755   // sets up a set of methods with the same exact erased signature
 756   FindMethodsByErasedSig visitor(slot->name(), slot->signature(), is_intf);
 757   visitor.run(klass);
 758 
 759   MethodFamily* family;
 760   visitor.get_discovered_family(&family);
 761   if (family != NULL) {
 762     family->determine_target(klass, CHECK);
 763     slot->bind_family(family);
 764   }
 765 }
 766 
 767 static void merge_in_new_methods(InstanceKlass* klass,
 768     GrowableArray<Method*>* new_methods, TRAPS);
 769 static void create_default_methods( InstanceKlass* klass,
 770     GrowableArray<Method*>* new_methods, TRAPS);
 771 
 772 // This is the guts of the default methods implementation.  This is called just
 773 // after the classfile has been parsed if some ancestor has default methods.
 774 //
 775 // First it finds any name/signature slots that need any implementation (either
 776 // because they are miranda or a superclass's implementation is an overpass


 805              klass->is_interface() ? "Interface" : "Class",
 806              klass->name()->as_klass_external_name());
 807     LogStream ls(lt);
 808     PrintHierarchy printer(&ls);
 809     printer.run(klass);
 810   }
 811 
 812   GrowableArray<EmptyVtableSlot*>* empty_slots =
 813       find_empty_vtable_slots(klass, mirandas, CHECK);
 814 
 815   for (int i = 0; i < empty_slots->length(); ++i) {
 816     EmptyVtableSlot* slot = empty_slots->at(i);
 817     LogTarget(Debug, defaultmethods) lt;
 818     if (lt.is_enabled()) {
 819       LogStream ls(lt);
 820       streamIndentor si(&ls, 2);
 821       ls.indent().print("Looking for default methods for slot ");
 822       slot->print_on(&ls);
 823       ls.cr();
 824     }
 825     generate_erased_defaults(klass, empty_slots, slot, klass->is_interface(), CHECK);
 826   }
 827   log_debug(defaultmethods)("Creating defaults and overpasses...");
 828   create_defaults_and_exceptions(empty_slots, klass, CHECK);
 829   log_debug(defaultmethods)("Default method processing complete");
 830 }
 831 
 832 static int assemble_method_error(
 833     BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* errorName, Symbol* message, TRAPS) {
 834 
 835   Symbol* init = vmSymbols::object_initializer_name();
 836   Symbol* sig = vmSymbols::string_void_signature();
 837 
 838   BytecodeAssembler assem(buffer, cp);
 839 
 840   assem._new(errorName);
 841   assem.dup();
 842   assem.load_string(message);
 843   assem.invokespecial(errorName, init, sig);
 844   assem.athrow();
 845 


< prev index next >