comparison src/share/vm/classfile/defaultMethods.cpp @ 11104:50257d6f5aaa

8013635: VM should no longer create bridges for generic signatures. Summary: Requires: 8013789: Compiler bridges, 8015402: metafactory Reviewed-by: sspitsyn, coleenp, bharadwaj
author acorn
date Tue, 09 Jul 2013 14:02:28 -0400
parents fe00365c8f31
children d416f67d6f91 91b93f523ec6
comparison
equal deleted inserted replaced
11101:e79a9f26ba2e 11104:50257d6f5aaa
316 _registrar->register_class(current_class()); 316 _registrar->register_class(current_class());
317 return true; 317 return true;
318 } 318 }
319 }; 319 };
320 320
321
321 // A method family contains a set of all methods that implement a single 322 // A method family contains a set of all methods that implement a single
322 // language-level method. Because of erasure, these methods may have different 323 // erased method. As members of the set are collected while walking over the
323 // signatures. As members of the set are collected while walking over the
324 // hierarchy, they are tagged with a qualification state. The qualification 324 // hierarchy, they are tagged with a qualification state. The qualification
325 // state for an erased method is set to disqualified if there exists a path 325 // state for an erased method is set to disqualified if there exists a path
326 // from the root of hierarchy to the method that contains an interleaving 326 // from the root of hierarchy to the method that contains an interleaving
327 // language-equivalent method defined in an interface. 327 // erased method defined in an interface.
328
328 class MethodFamily : public ResourceObj { 329 class MethodFamily : public ResourceObj {
329 private: 330 private:
330 331
331 generic::MethodDescriptor* _descriptor; // language-level description
332 GrowableArray<Pair<Method*,QualifiedState> > _members; 332 GrowableArray<Pair<Method*,QualifiedState> > _members;
333 ResourceHashtable<Method*, int> _member_index; 333 ResourceHashtable<Method*, int> _member_index;
334 334
335 Method* _selected_target; // Filled in later, if a unique target exists 335 Method* _selected_target; // Filled in later, if a unique target exists
336 Symbol* _exception_message; // If no unique target is found 336 Symbol* _exception_message; // If no unique target is found
356 Symbol* generate_abstract_method_message(Method* method, TRAPS) const; 356 Symbol* generate_abstract_method_message(Method* method, TRAPS) const;
357 Symbol* generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const; 357 Symbol* generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const;
358 358
359 public: 359 public:
360 360
361 MethodFamily(generic::MethodDescriptor* canonical_desc) 361 MethodFamily()
362 : _descriptor(canonical_desc), _selected_target(NULL), 362 : _selected_target(NULL), _exception_message(NULL) {}
363 _exception_message(NULL) {}
364
365 generic::MethodDescriptor* descriptor() const { return _descriptor; }
366
367 bool descriptor_matches(generic::MethodDescriptor* md, generic::Context* ctx) {
368 return descriptor()->covariant_match(md, ctx);
369 }
370 363
371 void set_target_if_empty(Method* m) { 364 void set_target_if_empty(Method* m) {
372 if (_selected_target == NULL && !m->is_overpass()) { 365 if (_selected_target == NULL && !m->is_overpass()) {
373 _selected_target = m; 366 _selected_target = m;
374 } 367 }
439 } 432 }
440 return false; 433 return false;
441 } 434 }
442 435
443 #ifndef PRODUCT 436 #ifndef PRODUCT
444 void print_on(outputStream* str) const { 437 void print_sig_on(outputStream* str, Symbol* signature, int indent) const {
445 print_on(str, 0);
446 }
447
448 void print_on(outputStream* str, int indent) const {
449 streamIndentor si(str, indent * 2); 438 streamIndentor si(str, indent * 2);
450 439
451 generic::Context ctx(NULL); // empty, as _descriptor already canonicalized 440 str->indent().print_cr("Logical Method %s:", signature->as_C_string());
452 TempNewSymbol family = descriptor()->reify_signature(&ctx, Thread::current());
453 str->indent().print_cr("Logical Method %s:", family->as_C_string());
454 441
455 streamIndentor si2(str); 442 streamIndentor si2(str);
456 for (int i = 0; i < _members.length(); ++i) { 443 for (int i = 0; i < _members.length(); ++i) {
457 str->indent(); 444 str->indent();
458 print_method(str, _members.at(i).first); 445 print_method(str, _members.at(i).first);
514 ss.write((const char*)name->bytes(), name->utf8_length()); 501 ss.write((const char*)name->bytes(), name->utf8_length());
515 } 502 }
516 return SymbolTable::new_symbol(ss.base(), (int)ss.size(), CHECK_NULL); 503 return SymbolTable::new_symbol(ss.base(), (int)ss.size(), CHECK_NULL);
517 } 504 }
518 505
506 // A generic method family contains a set of all methods that implement a single
507 // language-level method. Because of erasure, these methods may have different
508 // signatures. As members of the set are collected while walking over the
509 // hierarchy, they are tagged with a qualification state. The qualification
510 // state for an erased method is set to disqualified if there exists a path
511 // from the root of hierarchy to the method that contains an interleaving
512 // language-equivalent method defined in an interface.
513 class GenericMethodFamily : public MethodFamily {
514 private:
515
516 generic::MethodDescriptor* _descriptor; // language-level description
517
518 public:
519
520 GenericMethodFamily(generic::MethodDescriptor* canonical_desc)
521 : _descriptor(canonical_desc) {}
522
523 generic::MethodDescriptor* descriptor() const { return _descriptor; }
524
525 bool descriptor_matches(generic::MethodDescriptor* md, generic::Context* ctx) {
526 return descriptor()->covariant_match(md, ctx);
527 }
528
529 #ifndef PRODUCT
530 Symbol* get_generic_sig() const {
531
532 generic::Context ctx(NULL); // empty, as _descriptor already canonicalized
533 TempNewSymbol sig = descriptor()->reify_signature(&ctx, Thread::current());
534 return sig;
535 }
536 #endif // ndef PRODUCT
537 };
538
519 class StateRestorer; 539 class StateRestorer;
520 540
521 // StatefulMethodFamily is a wrapper around MethodFamily that maintains the 541 // StatefulMethodFamily is a wrapper around a MethodFamily that maintains the
522 // qualification state during hierarchy visitation, and applies that state 542 // qualification state during hierarchy visitation, and applies that state
523 // when adding members to the MethodFamily. 543 // when adding members to the MethodFamily
524 class StatefulMethodFamily : public ResourceObj { 544 class StatefulMethodFamily : public ResourceObj {
525 friend class StateRestorer; 545 friend class StateRestorer;
526 private: 546 private:
527 MethodFamily* _method;
528 QualifiedState _qualification_state; 547 QualifiedState _qualification_state;
529 548
530 void set_qualification_state(QualifiedState state) { 549 void set_qualification_state(QualifiedState state) {
531 _qualification_state = state; 550 _qualification_state = state;
532 } 551 }
533 552
534 public: 553 protected:
535 StatefulMethodFamily(generic::MethodDescriptor* md, generic::Context* ctx) { 554 MethodFamily* _method_family;
536 _method = new MethodFamily(md->canonicalize(ctx)); 555
537 _qualification_state = QUALIFIED; 556 public:
538 } 557 StatefulMethodFamily() {
539 558 _method_family = new MethodFamily();
540 void set_target_if_empty(Method* m) { _method->set_target_if_empty(m); } 559 _qualification_state = QUALIFIED;
541 560 }
542 MethodFamily* get_method_family() { return _method; } 561
562 StatefulMethodFamily(MethodFamily* mf) {
563 _method_family = mf;
564 _qualification_state = QUALIFIED;
565 }
566
567 void set_target_if_empty(Method* m) { _method_family->set_target_if_empty(m); }
568
569 MethodFamily* get_method_family() { return _method_family; }
570
571 StateRestorer* record_method_and_dq_further(Method* mo);
572 };
573
574
575 // StatefulGenericMethodFamily is a wrapper around GenericMethodFamily that maintains the
576 // qualification state during hierarchy visitation, and applies that state
577 // when adding members to the GenericMethodFamily.
578 class StatefulGenericMethodFamily : public StatefulMethodFamily {
579
580 public:
581 StatefulGenericMethodFamily(generic::MethodDescriptor* md, generic::Context* ctx)
582 : StatefulMethodFamily(new GenericMethodFamily(md->canonicalize(ctx))) {
583
584 }
585 GenericMethodFamily* get_method_family() {
586 return (GenericMethodFamily*)_method_family;
587 }
543 588
544 bool descriptor_matches(generic::MethodDescriptor* md, generic::Context* ctx) { 589 bool descriptor_matches(generic::MethodDescriptor* md, generic::Context* ctx) {
545 return _method->descriptor_matches(md, ctx); 590 return get_method_family()->descriptor_matches(md, ctx);
546 } 591 }
547
548 StateRestorer* record_method_and_dq_further(Method* mo);
549 }; 592 };
550 593
551 class StateRestorer : public PseudoScopeMark { 594 class StateRestorer : public PseudoScopeMark {
552 private: 595 private:
553 StatefulMethodFamily* _method; 596 StatefulMethodFamily* _method;
561 }; 604 };
562 605
563 StateRestorer* StatefulMethodFamily::record_method_and_dq_further(Method* mo) { 606 StateRestorer* StatefulMethodFamily::record_method_and_dq_further(Method* mo) {
564 StateRestorer* mark = new StateRestorer(this, _qualification_state); 607 StateRestorer* mark = new StateRestorer(this, _qualification_state);
565 if (_qualification_state == QUALIFIED) { 608 if (_qualification_state == QUALIFIED) {
566 _method->record_qualified_method(mo); 609 _method_family->record_qualified_method(mo);
567 } else { 610 } else {
568 _method->record_disqualified_method(mo); 611 _method_family->record_disqualified_method(mo);
569 } 612 }
570 // Everything found "above"??? this method in the hierarchy walk is set to 613 // Everything found "above"??? this method in the hierarchy walk is set to
571 // disqualified 614 // disqualified
572 set_qualification_state(DISQUALIFIED); 615 set_qualification_state(DISQUALIFIED);
573 return mark; 616 return mark;
574 } 617 }
575 618
576 class StatefulMethodFamilies : public ResourceObj { 619 class StatefulGenericMethodFamilies : public ResourceObj {
577 private: 620 private:
578 GrowableArray<StatefulMethodFamily*> _methods; 621 GrowableArray<StatefulGenericMethodFamily*> _methods;
579 622
580 public: 623 public:
581 StatefulMethodFamily* find_matching( 624 StatefulGenericMethodFamily* find_matching(
582 generic::MethodDescriptor* md, generic::Context* ctx) { 625 generic::MethodDescriptor* md, generic::Context* ctx) {
583 for (int i = 0; i < _methods.length(); ++i) { 626 for (int i = 0; i < _methods.length(); ++i) {
584 StatefulMethodFamily* existing = _methods.at(i); 627 StatefulGenericMethodFamily* existing = _methods.at(i);
585 if (existing->descriptor_matches(md, ctx)) { 628 if (existing->descriptor_matches(md, ctx)) {
586 return existing; 629 return existing;
587 } 630 }
588 } 631 }
589 return NULL; 632 return NULL;
590 } 633 }
591 634
592 StatefulMethodFamily* find_matching_or_create( 635 StatefulGenericMethodFamily* find_matching_or_create(
593 generic::MethodDescriptor* md, generic::Context* ctx) { 636 generic::MethodDescriptor* md, generic::Context* ctx) {
594 StatefulMethodFamily* method = find_matching(md, ctx); 637 StatefulGenericMethodFamily* method = find_matching(md, ctx);
595 if (method == NULL) { 638 if (method == NULL) {
596 method = new StatefulMethodFamily(md, ctx); 639 method = new StatefulGenericMethodFamily(md, ctx);
597 _methods.append(method); 640 _methods.append(method);
598 } 641 }
599 return method; 642 return method;
600 } 643 }
601 644
602 void extract_families_into(GrowableArray<MethodFamily*>* array) { 645 void extract_families_into(GrowableArray<GenericMethodFamily*>* array) {
603 for (int i = 0; i < _methods.length(); ++i) { 646 for (int i = 0; i < _methods.length(); ++i) {
604 array->append(_methods.at(i)->get_method_family()); 647 array->append(_methods.at(i)->get_method_family());
605 } 648 }
606 } 649 }
607 }; 650 };
681 } 724 }
682 #endif // ndef PRODUCT 725 #endif // ndef PRODUCT
683 return slots; 726 return slots;
684 } 727 }
685 728
729 // Iterates over the superinterface type hierarchy looking for all methods
730 // with a specific erased signature.
731 class FindMethodsByErasedSig : public HierarchyVisitor<FindMethodsByErasedSig> {
732 private:
733 // Context data
734 Symbol* _method_name;
735 Symbol* _method_signature;
736 StatefulMethodFamily* _family;
737
738 public:
739 FindMethodsByErasedSig(Symbol* name, Symbol* signature) :
740 _method_name(name), _method_signature(signature),
741 _family(NULL) {}
742
743 void get_discovered_family(MethodFamily** family) {
744 if (_family != NULL) {
745 *family = _family->get_method_family();
746 } else {
747 *family = NULL;
748 }
749 }
750
751 void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); }
752 void free_node_data(void* node_data) {
753 PseudoScope::cast(node_data)->destroy();
754 }
755
756 // Find all methods on this hierarchy that match this
757 // method's erased (name, signature)
758 bool visit() {
759 PseudoScope* scope = PseudoScope::cast(current_data());
760 InstanceKlass* iklass = current_class();
761
762 Method* m = iklass->find_method(_method_name, _method_signature);
763 if (m != NULL) {
764 if (_family == NULL) {
765 _family = new StatefulMethodFamily();
766 }
767
768 if (iklass->is_interface()) {
769 StateRestorer* restorer = _family->record_method_and_dq_further(m);
770 scope->add_mark(restorer);
771 } else {
772 // This is the rule that methods in classes "win" (bad word) over
773 // methods in interfaces. This works because of single inheritance
774 _family->set_target_if_empty(m);
775 }
776 }
777 return true;
778 }
779
780 };
781
686 // Iterates over the type hierarchy looking for all methods with a specific 782 // Iterates over the type hierarchy looking for all methods with a specific
687 // method name. The result of this is a set of method families each of 783 // method name. The result of this is a set of method families each of
688 // which is populated with a set of methods that implement the same 784 // which is populated with a set of methods that implement the same
689 // language-level signature. 785 // language-level signature.
690 class FindMethodsByName : public HierarchyVisitor<FindMethodsByName> { 786 class FindMethodsByGenericSig : public HierarchyVisitor<FindMethodsByGenericSig> {
691 private: 787 private:
692 // Context data 788 // Context data
693 Thread* THREAD; 789 Thread* THREAD;
694 generic::DescriptorCache* _cache; 790 generic::DescriptorCache* _cache;
695 Symbol* _method_name; 791 Symbol* _method_name;
696 generic::Context* _ctx; 792 generic::Context* _ctx;
697 StatefulMethodFamilies _families; 793 StatefulGenericMethodFamilies _families;
698 794
699 public: 795 public:
700 796
701 FindMethodsByName(generic::DescriptorCache* cache, Symbol* name, 797 FindMethodsByGenericSig(generic::DescriptorCache* cache, Symbol* name,
702 generic::Context* ctx, Thread* thread) : 798 generic::Context* ctx, Thread* thread) :
703 _cache(cache), _method_name(name), _ctx(ctx), THREAD(thread) {} 799 _cache(cache), _method_name(name), _ctx(ctx), THREAD(thread) {}
704 800
705 void get_discovered_families(GrowableArray<MethodFamily*>* methods) { 801 void get_discovered_families(GrowableArray<GenericMethodFamily*>* methods) {
706 _families.extract_families_into(methods); 802 _families.extract_families_into(methods);
707 } 803 }
708 804
709 void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); } 805 void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); }
710 void free_node_data(void* node_data) { 806 void free_node_data(void* node_data) {
731 generic::MethodDescriptor* md = _cache->descriptor_for(m, THREAD); 827 generic::MethodDescriptor* md = _cache->descriptor_for(m, THREAD);
732 828
733 // Find all methods on this hierarchy that match this method 829 // Find all methods on this hierarchy that match this method
734 // (name, signature). This class collects other families of this 830 // (name, signature). This class collects other families of this
735 // method name. 831 // method name.
736 StatefulMethodFamily* family = 832 StatefulGenericMethodFamily* family =
737 _families.find_matching_or_create(md, _ctx); 833 _families.find_matching_or_create(md, _ctx);
738 834
739 if (klass->is_interface()) { 835 if (klass->is_interface()) {
740 // ??? 836 // ???
741 StateRestorer* restorer = family->record_method_and_dq_further(m); 837 StateRestorer* restorer = family->record_method_and_dq_further(m);
750 return true; 846 return true;
751 } 847 }
752 }; 848 };
753 849
754 #ifndef PRODUCT 850 #ifndef PRODUCT
755 static void print_families( 851 static void print_generic_families(
756 GrowableArray<MethodFamily*>* methods, Symbol* match) { 852 GrowableArray<GenericMethodFamily*>* methods, Symbol* match) {
757 streamIndentor si(tty, 4); 853 streamIndentor si(tty, 4);
758 if (methods->length() == 0) { 854 if (methods->length() == 0) {
759 tty->indent(); 855 tty->indent();
760 tty->print_cr("No Logical Method found"); 856 tty->print_cr("No Logical Method found");
761 } 857 }
762 for (int i = 0; i < methods->length(); ++i) { 858 for (int i = 0; i < methods->length(); ++i) {
763 tty->indent(); 859 tty->indent();
764 MethodFamily* lm = methods->at(i); 860 GenericMethodFamily* lm = methods->at(i);
765 if (lm->contains_signature(match)) { 861 if (lm->contains_signature(match)) {
766 tty->print_cr("<Matching>"); 862 tty->print_cr("<Matching>");
767 } else { 863 } else {
768 tty->print_cr("<Non-Matching>"); 864 tty->print_cr("<Non-Matching>");
769 } 865 }
770 lm->print_on(tty, 1); 866 lm->print_sig_on(tty, lm->get_generic_sig(), 1);
771 } 867 }
772 } 868 }
773 #endif // ndef PRODUCT 869 #endif // ndef PRODUCT
870
871 static void create_overpasses(
872 GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS);
873
874 static void generate_generic_defaults(
875 InstanceKlass* klass, GrowableArray<EmptyVtableSlot*>* empty_slots,
876 EmptyVtableSlot* slot, int current_slot_index, TRAPS) {
877
878 if (slot->is_bound()) {
879 #ifndef PRODUCT
880 if (TraceDefaultMethods) {
881 streamIndentor si(tty, 4);
882 tty->indent().print_cr("Already bound to logical method:");
883 GenericMethodFamily* lm = (GenericMethodFamily*)(slot->get_binding());
884 lm->print_sig_on(tty, lm->get_generic_sig(), 1);
885 }
886 #endif // ndef PRODUCT
887 return; // covered by previous processing
888 }
889
890 generic::DescriptorCache cache;
891
892 generic::Context ctx(&cache);
893 FindMethodsByGenericSig visitor(&cache, slot->name(), &ctx, CHECK);
894 visitor.run(klass);
895
896 GrowableArray<GenericMethodFamily*> discovered_families;
897 visitor.get_discovered_families(&discovered_families);
898
899 #ifndef PRODUCT
900 if (TraceDefaultMethods) {
901 print_generic_families(&discovered_families, slot->signature());
902 }
903 #endif // ndef PRODUCT
904
905 // Find and populate any other slots that match the discovered families
906 for (int j = current_slot_index; j < empty_slots->length(); ++j) {
907 EmptyVtableSlot* open_slot = empty_slots->at(j);
908
909 if (slot->name() == open_slot->name()) {
910 for (int k = 0; k < discovered_families.length(); ++k) {
911 GenericMethodFamily* lm = discovered_families.at(k);
912
913 if (lm->contains_signature(open_slot->signature())) {
914 lm->determine_target(klass, CHECK);
915 open_slot->bind_family(lm);
916 }
917 }
918 }
919 }
920 }
921
922 static void generate_erased_defaults(
923 InstanceKlass* klass, GrowableArray<EmptyVtableSlot*>* empty_slots,
924 EmptyVtableSlot* slot, TRAPS) {
925
926 // sets up a set of methods with the same exact erased signature
927 FindMethodsByErasedSig visitor(slot->name(), slot->signature());
928 visitor.run(klass);
929
930 MethodFamily* family;
931 visitor.get_discovered_family(&family);
932 if (family != NULL) {
933 family->determine_target(klass, CHECK);
934 slot->bind_family(family);
935 }
936 }
774 937
775 static void merge_in_new_methods(InstanceKlass* klass, 938 static void merge_in_new_methods(InstanceKlass* klass,
776 GrowableArray<Method*>* new_methods, TRAPS); 939 GrowableArray<Method*>* new_methods, TRAPS);
777 static void create_overpasses(
778 GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS);
779 940
780 // This is the guts of the default methods implementation. This is called just 941 // This is the guts of the default methods implementation. This is called just
781 // after the classfile has been parsed if some ancestor has default methods. 942 // after the classfile has been parsed if some ancestor has default methods.
782 // 943 //
783 // First if finds any name/signature slots that need any implementation (either 944 // First if finds any name/signature slots that need any implementation (either
805 // all (Resource) objects' memory will be reclaimed. Be careful if adding an 966 // all (Resource) objects' memory will be reclaimed. Be careful if adding an
806 // embedded resource mark under here as that memory can't be used outside 967 // embedded resource mark under here as that memory can't be used outside
807 // whatever scope it's in. 968 // whatever scope it's in.
808 ResourceMark rm(THREAD); 969 ResourceMark rm(THREAD);
809 970
810 generic::DescriptorCache cache;
811
812 // Keep entire hierarchy alive for the duration of the computation 971 // Keep entire hierarchy alive for the duration of the computation
813 KeepAliveRegistrar keepAlive(THREAD); 972 KeepAliveRegistrar keepAlive(THREAD);
814 KeepAliveVisitor loadKeepAlive(&keepAlive); 973 KeepAliveVisitor loadKeepAlive(&keepAlive);
815 loadKeepAlive.run(klass); 974 loadKeepAlive.run(klass);
816 975
835 tty->indent().print("Looking for default methods for slot "); 994 tty->indent().print("Looking for default methods for slot ");
836 slot->print_on(tty); 995 slot->print_on(tty);
837 tty->print_cr(""); 996 tty->print_cr("");
838 } 997 }
839 #endif // ndef PRODUCT 998 #endif // ndef PRODUCT
840 if (slot->is_bound()) { 999
841 #ifndef PRODUCT 1000 if (ParseGenericDefaults) {
842 if (TraceDefaultMethods) { 1001 generate_generic_defaults(klass, empty_slots, slot, i, CHECK);
843 streamIndentor si(tty, 4); 1002 } else {
844 tty->indent().print_cr("Already bound to logical method:"); 1003 generate_erased_defaults(klass, empty_slots, slot, CHECK);
845 slot->get_binding()->print_on(tty, 1); 1004 }
846 } 1005 }
847 #endif // ndef PRODUCT
848 continue; // covered by previous processing
849 }
850
851 generic::Context ctx(&cache);
852 FindMethodsByName visitor(&cache, slot->name(), &ctx, CHECK);
853 visitor.run(klass);
854
855 GrowableArray<MethodFamily*> discovered_families;
856 visitor.get_discovered_families(&discovered_families);
857
858 #ifndef PRODUCT
859 if (TraceDefaultMethods) {
860 print_families(&discovered_families, slot->signature());
861 }
862 #endif // ndef PRODUCT
863
864 // Find and populate any other slots that match the discovered families
865 for (int j = i; j < empty_slots->length(); ++j) {
866 EmptyVtableSlot* open_slot = empty_slots->at(j);
867
868 if (slot->name() == open_slot->name()) {
869 for (int k = 0; k < discovered_families.length(); ++k) {
870 MethodFamily* lm = discovered_families.at(k);
871
872 if (lm->contains_signature(open_slot->signature())) {
873 lm->determine_target(klass, CHECK);
874 open_slot->bind_family(lm);
875 }
876 }
877 }
878 }
879 }
880
881 #ifndef PRODUCT 1006 #ifndef PRODUCT
882 if (TraceDefaultMethods) { 1007 if (TraceDefaultMethods) {
883 tty->print_cr("Creating overpasses..."); 1008 tty->print_cr("Creating overpasses...");
884 } 1009 }
885 #endif // ndef PRODUCT 1010 #endif // ndef PRODUCT
890 if (TraceDefaultMethods) { 1015 if (TraceDefaultMethods) {
891 tty->print_cr("Default method processing complete"); 1016 tty->print_cr("Default method processing complete");
892 } 1017 }
893 #endif // ndef PRODUCT 1018 #endif // ndef PRODUCT
894 } 1019 }
895
896 1020
897 /** 1021 /**
898 * Generic analysis was used upon interface '_target' and found a unique 1022 * Generic analysis was used upon interface '_target' and found a unique
899 * default method candidate with generic signature '_method_desc'. This 1023 * default method candidate with generic signature '_method_desc'. This
900 * method is only viable if it would also be in the set of default method 1024 * method is only viable if it would also be in the set of default method
910 * interfaces. If we find path that leads to the '_target' interface, then 1034 * interfaces. If we find path that leads to the '_target' interface, then
911 * we examine that path to see if there are any methods that would shadow 1035 * we examine that path to see if there are any methods that would shadow
912 * the selected method along that path. 1036 * the selected method along that path.
913 */ 1037 */
914 class ShadowChecker : public HierarchyVisitor<ShadowChecker> { 1038 class ShadowChecker : public HierarchyVisitor<ShadowChecker> {
1039 protected:
1040 Thread* THREAD;
1041
1042 InstanceKlass* _target;
1043
1044 Symbol* _method_name;
1045 InstanceKlass* _method_holder;
1046 bool _found_shadow;
1047
1048
1049 public:
1050
1051 ShadowChecker(Thread* thread, Symbol* name, InstanceKlass* holder,
1052 InstanceKlass* target)
1053 : THREAD(thread), _method_name(name), _method_holder(holder),
1054 _target(target), _found_shadow(false) {}
1055
1056 void* new_node_data(InstanceKlass* cls) { return NULL; }
1057 void free_node_data(void* data) { return; }
1058
1059 bool visit() {
1060 InstanceKlass* ik = current_class();
1061 if (ik == _target && current_depth() == 1) {
1062 return false; // This was the specified super -- no need to search it
1063 }
1064 if (ik == _method_holder || ik == _target) {
1065 // We found a path that should be examined to see if it shadows _method
1066 if (path_has_shadow()) {
1067 _found_shadow = true;
1068 cancel_iteration();
1069 }
1070 return false; // no need to continue up hierarchy
1071 }
1072 return true;
1073 }
1074
1075 virtual bool path_has_shadow() = 0;
1076 bool found_shadow() { return _found_shadow; }
1077 };
1078
1079 // Used for Invokespecial.
1080 // Invokespecial is allowed to invoke a concrete interface method
1081 // and can be used to disambuiguate among qualified candidates,
1082 // which are methods in immediate superinterfaces,
1083 // but may not be used to invoke a candidate that would be shadowed
1084 // from the perspective of the caller.
1085 // Invokespecial is also used in the overpass generation today
1086 // We re-run the shadowchecker because we can't distinguish this case,
1087 // but it should return the same answer, since the overpass target
1088 // is now the invokespecial caller.
1089 class ErasedShadowChecker : public ShadowChecker {
1090 private:
1091 bool path_has_shadow() {
1092
1093 for (int i = current_depth() - 1; i > 0; --i) {
1094 InstanceKlass* ik = class_at_depth(i);
1095
1096 if (ik->is_interface()) {
1097 int end;
1098 int start = ik->find_method_by_name(_method_name, &end);
1099 if (start != -1) {
1100 return true;
1101 }
1102 }
1103 }
1104 return false;
1105 }
1106 public:
1107
1108 ErasedShadowChecker(Thread* thread, Symbol* name, InstanceKlass* holder,
1109 InstanceKlass* target)
1110 : ShadowChecker(thread, name, holder, target) {}
1111 };
1112
1113 class GenericShadowChecker : public ShadowChecker {
915 private: 1114 private:
916 generic::DescriptorCache* _cache; 1115 generic::DescriptorCache* _cache;
917 Thread* THREAD;
918
919 InstanceKlass* _target;
920
921 Symbol* _method_name;
922 InstanceKlass* _method_holder;
923 generic::MethodDescriptor* _method_desc; 1116 generic::MethodDescriptor* _method_desc;
924 bool _found_shadow;
925 1117
926 bool path_has_shadow() { 1118 bool path_has_shadow() {
927 generic::Context ctx(_cache); 1119 generic::Context ctx(_cache);
928 1120
929 for (int i = current_depth() - 1; i > 0; --i) { 1121 for (int i = current_depth() - 1; i > 0; --i) {
948 return false; 1140 return false;
949 } 1141 }
950 1142
951 public: 1143 public:
952 1144
953 ShadowChecker(generic::DescriptorCache* cache, Thread* thread, 1145 GenericShadowChecker(generic::DescriptorCache* cache, Thread* thread,
954 Symbol* name, InstanceKlass* holder, generic::MethodDescriptor* desc, 1146 Symbol* name, InstanceKlass* holder, generic::MethodDescriptor* desc,
955 InstanceKlass* target) 1147 InstanceKlass* target)
956 : _cache(cache), THREAD(thread), _method_name(name), _method_holder(holder), 1148 : ShadowChecker(thread, name, holder, target) {
957 _method_desc(desc), _target(target), _found_shadow(false) {} 1149 _cache = cache;
958 1150 _method_desc = desc;
959 void* new_node_data(InstanceKlass* cls) { return NULL; } 1151 }
960 void free_node_data(void* data) { return; } 1152 };
961 1153
962 bool visit() { 1154
963 InstanceKlass* ik = current_class(); 1155
964 if (ik == _target && current_depth() == 1) { 1156 // Find the unique qualified candidate from the perspective of the super_class
965 return false; // This was the specified super -- no need to search it 1157 // which is the resolved_klass, which must be an immediate superinterface
966 } 1158 // of klass
967 if (ik == _method_holder || ik == _target) { 1159 Method* find_erased_super_default(InstanceKlass* current_class, InstanceKlass* super_class, Symbol* method_name, Symbol* sig, TRAPS) {
968 // We found a path that should be examined to see if it shadows _method 1160
969 if (path_has_shadow()) { 1161 FindMethodsByErasedSig visitor(method_name, sig);
970 _found_shadow = true; 1162 visitor.run(super_class); // find candidates from resolved_klass
971 cancel_iteration(); 1163
972 } 1164 MethodFamily* family;
973 return false; // no need to continue up hierarchy 1165 visitor.get_discovered_family(&family);
974 } 1166
975 return true; 1167 if (family != NULL) {
976 } 1168 family->determine_target(current_class, CHECK_NULL); // get target from current_class
977 1169 }
978 bool found_shadow() { return _found_shadow; } 1170
979 }; 1171 if (family->has_target()) {
1172 Method* target = family->get_selected_target();
1173 InstanceKlass* holder = InstanceKlass::cast(target->method_holder());
1174
1175 // Verify that the identified method is valid from the context of
1176 // the current class, which is the caller class for invokespecial
1177 // link resolution, i.e. ensure there it is not shadowed.
1178 // You can use invokespecial to disambiguate interface methods, but
1179 // you can not use it to skip over an interface method that would shadow it.
1180 ErasedShadowChecker checker(THREAD, target->name(), holder, super_class);
1181 checker.run(current_class);
1182
1183 if (checker.found_shadow()) {
1184 #ifndef PRODUCT
1185 if (TraceDefaultMethods) {
1186 tty->print_cr(" Only candidate found was shadowed.");
1187 }
1188 #endif // ndef PRODUCT
1189 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
1190 "Accessible default method not found", NULL);
1191 } else {
1192 #ifndef PRODUCT
1193 if (TraceDefaultMethods) {
1194 family->print_sig_on(tty, target->signature(), 1);
1195 }
1196 #endif // ndef PRODUCT
1197 return target;
1198 }
1199 } else {
1200 assert(family->throws_exception(), "must have target or throw");
1201 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
1202 family->get_exception_message()->as_C_string(), NULL);
1203 }
1204 }
1205
1206 // super_class is assumed to be the direct super of current_class
1207 Method* find_generic_super_default( InstanceKlass* current_class,
1208 InstanceKlass* super_class,
1209 Symbol* method_name, Symbol* sig, TRAPS) {
1210 generic::DescriptorCache cache;
1211 generic::Context ctx(&cache);
1212
1213 // Prime the initial generic context for current -> super_class
1214 ctx.apply_type_arguments(current_class, super_class, CHECK_NULL);
1215
1216 FindMethodsByGenericSig visitor(&cache, method_name, &ctx, CHECK_NULL);
1217 visitor.run(super_class);
1218
1219 GrowableArray<GenericMethodFamily*> families;
1220 visitor.get_discovered_families(&families);
1221
1222 #ifndef PRODUCT
1223 if (TraceDefaultMethods) {
1224 print_generic_families(&families, sig);
1225 }
1226 #endif // ndef PRODUCT
1227
1228 GenericMethodFamily* selected_family = NULL;
1229
1230 for (int i = 0; i < families.length(); ++i) {
1231 GenericMethodFamily* lm = families.at(i);
1232 if (lm->contains_signature(sig)) {
1233 lm->determine_target(current_class, CHECK_NULL);
1234 selected_family = lm;
1235 }
1236 }
1237
1238 if (selected_family->has_target()) {
1239 Method* target = selected_family->get_selected_target();
1240 InstanceKlass* holder = InstanceKlass::cast(target->method_holder());
1241
1242 // Verify that the identified method is valid from the context of
1243 // the current class
1244 GenericShadowChecker checker(&cache, THREAD, target->name(),
1245 holder, selected_family->descriptor(), super_class);
1246 checker.run(current_class);
1247
1248 if (checker.found_shadow()) {
1249 #ifndef PRODUCT
1250 if (TraceDefaultMethods) {
1251 tty->print_cr(" Only candidate found was shadowed.");
1252 }
1253 #endif // ndef PRODUCT
1254 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
1255 "Accessible default method not found", NULL);
1256 } else {
1257 return target;
1258 }
1259 } else {
1260 assert(selected_family->throws_exception(), "must have target or throw");
1261 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
1262 selected_family->get_exception_message()->as_C_string(), NULL);
1263 }
1264 }
980 1265
981 // This is called during linktime when we find an invokespecial call that 1266 // This is called during linktime when we find an invokespecial call that
982 // refers to a direct superinterface. It indicates that we should find the 1267 // refers to a direct superinterface. It indicates that we should find the
983 // default method in the hierarchy of that superinterface, and if that method 1268 // default method in the hierarchy of that superinterface, and if that method
984 // would have been a candidate from the point of view of 'this' class, then we 1269 // would have been a candidate from the point of view of 'this' class, then we
985 // return that method. 1270 // return that method.
1271 // This logic assumes that the super is a direct superclass of the caller
986 Method* DefaultMethods::find_super_default( 1272 Method* DefaultMethods::find_super_default(
987 Klass* cls, Klass* super, Symbol* method_name, Symbol* sig, TRAPS) { 1273 Klass* cls, Klass* super, Symbol* method_name, Symbol* sig, TRAPS) {
988 1274
989 ResourceMark rm(THREAD); 1275 ResourceMark rm(THREAD);
990 1276
991 assert(cls != NULL && super != NULL, "Need real classes"); 1277 assert(cls != NULL && super != NULL, "Need real classes");
992 1278
993 InstanceKlass* current_class = InstanceKlass::cast(cls); 1279 InstanceKlass* current_class = InstanceKlass::cast(cls);
994 InstanceKlass* direction = InstanceKlass::cast(super); 1280 InstanceKlass* super_class = InstanceKlass::cast(super);
995 1281
996 // Keep entire hierarchy alive for the duration of the computation 1282 // Keep entire hierarchy alive for the duration of the computation
997 KeepAliveRegistrar keepAlive(THREAD); 1283 KeepAliveRegistrar keepAlive(THREAD);
998 KeepAliveVisitor loadKeepAlive(&keepAlive); 1284 KeepAliveVisitor loadKeepAlive(&keepAlive);
999 loadKeepAlive.run(current_class); 1285 loadKeepAlive.run(current_class); // get hierarchy from current class
1000 1286
1001 #ifndef PRODUCT 1287 #ifndef PRODUCT
1002 if (TraceDefaultMethods) { 1288 if (TraceDefaultMethods) {
1003 tty->print_cr("Finding super default method %s.%s%s from %s", 1289 tty->print_cr("Finding super default method %s.%s%s from %s",
1004 direction->name()->as_C_string(), 1290 super_class->name()->as_C_string(),
1005 method_name->as_C_string(), sig->as_C_string(), 1291 method_name->as_C_string(), sig->as_C_string(),
1006 current_class->name()->as_C_string()); 1292 current_class->name()->as_C_string());
1007 } 1293 }
1008 #endif // ndef PRODUCT 1294 #endif // ndef PRODUCT
1009 1295
1010 if (!direction->is_interface()) { 1296 assert(super_class->is_interface(), "only call for default methods");
1011 // We should not be here 1297
1012 return NULL; 1298 Method* target = NULL;
1013 } 1299 if (ParseGenericDefaults) {
1014 1300 target = find_generic_super_default(current_class, super_class,
1015 generic::DescriptorCache cache; 1301 method_name, sig, CHECK_NULL);
1016 generic::Context ctx(&cache);
1017
1018 // Prime the initial generic context for current -> direction
1019 ctx.apply_type_arguments(current_class, direction, CHECK_NULL);
1020
1021 FindMethodsByName visitor(&cache, method_name, &ctx, CHECK_NULL);
1022 visitor.run(direction);
1023
1024 GrowableArray<MethodFamily*> families;
1025 visitor.get_discovered_families(&families);
1026
1027 #ifndef PRODUCT
1028 if (TraceDefaultMethods) {
1029 print_families(&families, sig);
1030 }
1031 #endif // ndef PRODUCT
1032
1033 MethodFamily* selected_family = NULL;
1034
1035 for (int i = 0; i < families.length(); ++i) {
1036 MethodFamily* lm = families.at(i);
1037 if (lm->contains_signature(sig)) {
1038 lm->determine_target(current_class, CHECK_NULL);
1039 selected_family = lm;
1040 }
1041 }
1042
1043 if (selected_family->has_target()) {
1044 Method* target = selected_family->get_selected_target();
1045 InstanceKlass* holder = InstanceKlass::cast(target->method_holder());
1046
1047 // Verify that the identified method is valid from the context of
1048 // the current class
1049 ShadowChecker checker(&cache, THREAD, target->name(),
1050 holder, selected_family->descriptor(), direction);
1051 checker.run(current_class);
1052
1053 if (checker.found_shadow()) {
1054 #ifndef PRODUCT
1055 if (TraceDefaultMethods) {
1056 tty->print_cr(" Only candidate found was shadowed.");
1057 }
1058 #endif // ndef PRODUCT
1059 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
1060 "Accessible default method not found", NULL);
1061 } else {
1062 #ifndef PRODUCT
1063 if (TraceDefaultMethods) {
1064 tty->print(" Returning ");
1065 print_method(tty, target, true);
1066 tty->print_cr("");
1067 }
1068 #endif // ndef PRODUCT
1069 return target;
1070 }
1071 } else { 1302 } else {
1072 assert(selected_family->throws_exception(), "must have target or throw"); 1303 target = find_erased_super_default(current_class, super_class,
1073 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(), 1304 method_name, sig, CHECK_NULL);
1074 selected_family->get_exception_message()->as_C_string(), NULL); 1305 }
1075 } 1306
1076 } 1307 #ifndef PRODUCT
1077 1308 if (target != NULL) {
1309 if (TraceDefaultMethods) {
1310 tty->print(" Returning ");
1311 print_method(tty, target, true);
1312 tty->print_cr("");
1313 }
1314 }
1315 #endif // ndef PRODUCT
1316 return target;
1317 }
1318
1319 #ifndef PRODUCT
1320 // Return true is broad type is a covariant return of narrow type
1321 static bool covariant_return_type(BasicType narrow, BasicType broad) {
1322 if (narrow == broad) {
1323 return true;
1324 }
1325 if (broad == T_OBJECT) {
1326 return true;
1327 }
1328 return false;
1329 }
1330 #endif // ndef PRODUCT
1078 1331
1079 static int assemble_redirect( 1332 static int assemble_redirect(
1080 BytecodeConstantPool* cp, BytecodeBuffer* buffer, 1333 BytecodeConstantPool* cp, BytecodeBuffer* buffer,
1081 Symbol* incoming, Method* target, TRAPS) { 1334 Symbol* incoming, Method* target, TRAPS) {
1082 1335
1101 ++parameter_count; 1354 ++parameter_count;
1102 in.next(); 1355 in.next();
1103 out.next(); 1356 out.next();
1104 } 1357 }
1105 assert(out.at_return_type(), "Parameter counts do not match"); 1358 assert(out.at_return_type(), "Parameter counts do not match");
1106 assert(in.type() == out.type(), "Return types are not compatible"); 1359 assert(covariant_return_type(out.type(), in.type()), "Return types are not compatible");
1107 1360
1108 if (parameter_count == 1 && (in.type() == T_LONG || in.type() == T_DOUBLE)) { 1361 if (parameter_count == 1 && (in.type() == T_LONG || in.type() == T_DOUBLE)) {
1109 ++parameter_count; // need room for return value 1362 ++parameter_count; // need room for return value
1110 } 1363 }
1111 if (target->method_holder()->is_interface()) { 1364 if (target->method_holder()->is_interface()) {
1142 static Method* new_method( 1395 static Method* new_method(
1143 BytecodeConstantPool* cp, BytecodeBuffer* bytecodes, Symbol* name, 1396 BytecodeConstantPool* cp, BytecodeBuffer* bytecodes, Symbol* name,
1144 Symbol* sig, AccessFlags flags, int max_stack, int params, 1397 Symbol* sig, AccessFlags flags, int max_stack, int params,
1145 ConstMethod::MethodType mt, TRAPS) { 1398 ConstMethod::MethodType mt, TRAPS) {
1146 1399
1147 address code_start = static_cast<address>(bytecodes->adr_at(0)); 1400 address code_start = 0;
1148 int code_length = bytecodes->length(); 1401 int code_length = 0;
1149 InlineTableSizes sizes; 1402 InlineTableSizes sizes;
1403
1404 if (bytecodes != NULL && bytecodes->length() > 0) {
1405 code_start = static_cast<address>(bytecodes->adr_at(0));
1406 code_length = bytecodes->length();
1407 }
1150 1408
1151 Method* m = Method::allocate(cp->pool_holder()->class_loader_data(), 1409 Method* m = Method::allocate(cp->pool_holder()->class_loader_data(),
1152 code_length, flags, &sizes, 1410 code_length, flags, &sizes,
1153 mt, CHECK_NULL); 1411 mt, CHECK_NULL);
1154 1412