src/share/vm/classfile/defaultMethods.cpp

changeset 5599
91b93f523ec6
parent 5377
50257d6f5aaa
child 5608
915cc4f3fb15
equal deleted inserted replaced
5577:faf2631b9334 5599:91b93f523ec6
23 */ 23 */
24 24
25 #include "precompiled.hpp" 25 #include "precompiled.hpp"
26 #include "classfile/bytecodeAssembler.hpp" 26 #include "classfile/bytecodeAssembler.hpp"
27 #include "classfile/defaultMethods.hpp" 27 #include "classfile/defaultMethods.hpp"
28 #include "classfile/genericSignatures.hpp"
29 #include "classfile/symbolTable.hpp" 28 #include "classfile/symbolTable.hpp"
30 #include "memory/allocation.hpp" 29 #include "memory/allocation.hpp"
31 #include "memory/metadataFactory.hpp" 30 #include "memory/metadataFactory.hpp"
32 #include "memory/resourceArea.hpp" 31 #include "memory/resourceArea.hpp"
33 #include "runtime/signature.hpp" 32 #include "runtime/signature.hpp"
71 void destroy() { 70 void destroy() {
72 for (int i = 0; i < _marks.length(); ++i) { 71 for (int i = 0; i < _marks.length(); ++i) {
73 _marks.at(i)->destroy(); 72 _marks.at(i)->destroy();
74 } 73 }
75 } 74 }
76 };
77
78 class ContextMark : public PseudoScopeMark {
79 private:
80 generic::Context::Mark _mark;
81 public:
82 ContextMark(const generic::Context::Mark& cm) : _mark(cm) {}
83 virtual void destroy() { _mark.destroy(); }
84 }; 75 };
85 76
86 #ifndef PRODUCT 77 #ifndef PRODUCT
87 static void print_slot(outputStream* str, Symbol* name, Symbol* signature) { 78 static void print_slot(outputStream* str, Symbol* name, Symbol* signature) {
88 ResourceMark rm; 79 ResourceMark rm;
501 ss.write((const char*)name->bytes(), name->utf8_length()); 492 ss.write((const char*)name->bytes(), name->utf8_length());
502 } 493 }
503 return SymbolTable::new_symbol(ss.base(), (int)ss.size(), CHECK_NULL); 494 return SymbolTable::new_symbol(ss.base(), (int)ss.size(), CHECK_NULL);
504 } 495 }
505 496
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 497
539 class StateRestorer; 498 class StateRestorer;
540 499
541 // StatefulMethodFamily is a wrapper around a MethodFamily that maintains the 500 // StatefulMethodFamily is a wrapper around a MethodFamily that maintains the
542 // qualification state during hierarchy visitation, and applies that state 501 // qualification state during hierarchy visitation, and applies that state
567 void set_target_if_empty(Method* m) { _method_family->set_target_if_empty(m); } 526 void set_target_if_empty(Method* m) { _method_family->set_target_if_empty(m); }
568 527
569 MethodFamily* get_method_family() { return _method_family; } 528 MethodFamily* get_method_family() { return _method_family; }
570 529
571 StateRestorer* record_method_and_dq_further(Method* mo); 530 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 }
588
589 bool descriptor_matches(generic::MethodDescriptor* md, generic::Context* ctx) {
590 return get_method_family()->descriptor_matches(md, ctx);
591 }
592 }; 531 };
593 532
594 class StateRestorer : public PseudoScopeMark { 533 class StateRestorer : public PseudoScopeMark {
595 private: 534 private:
596 StatefulMethodFamily* _method; 535 StatefulMethodFamily* _method;
613 // Everything found "above"??? this method in the hierarchy walk is set to 552 // Everything found "above"??? this method in the hierarchy walk is set to
614 // disqualified 553 // disqualified
615 set_qualification_state(DISQUALIFIED); 554 set_qualification_state(DISQUALIFIED);
616 return mark; 555 return mark;
617 } 556 }
618
619 class StatefulGenericMethodFamilies : public ResourceObj {
620 private:
621 GrowableArray<StatefulGenericMethodFamily*> _methods;
622
623 public:
624 StatefulGenericMethodFamily* find_matching(
625 generic::MethodDescriptor* md, generic::Context* ctx) {
626 for (int i = 0; i < _methods.length(); ++i) {
627 StatefulGenericMethodFamily* existing = _methods.at(i);
628 if (existing->descriptor_matches(md, ctx)) {
629 return existing;
630 }
631 }
632 return NULL;
633 }
634
635 StatefulGenericMethodFamily* find_matching_or_create(
636 generic::MethodDescriptor* md, generic::Context* ctx) {
637 StatefulGenericMethodFamily* method = find_matching(md, ctx);
638 if (method == NULL) {
639 method = new StatefulGenericMethodFamily(md, ctx);
640 _methods.append(method);
641 }
642 return method;
643 }
644
645 void extract_families_into(GrowableArray<GenericMethodFamily*>* array) {
646 for (int i = 0; i < _methods.length(); ++i) {
647 array->append(_methods.at(i)->get_method_family());
648 }
649 }
650 };
651 557
652 // Represents a location corresponding to a vtable slot for methods that 558 // Represents a location corresponding to a vtable slot for methods that
653 // neither the class nor any of it's ancestors provide an implementaion. 559 // neither the class nor any of it's ancestors provide an implementaion.
654 // Default methods may be present to fill this slot. 560 // Default methods may be present to fill this slot.
655 class EmptyVtableSlot : public ResourceObj { 561 class EmptyVtableSlot : public ResourceObj {
777 return true; 683 return true;
778 } 684 }
779 685
780 }; 686 };
781 687
782 // Iterates over the type hierarchy looking for all methods with a specific 688
783 // method name. The result of this is a set of method families each of
784 // which is populated with a set of methods that implement the same
785 // language-level signature.
786 class FindMethodsByGenericSig : public HierarchyVisitor<FindMethodsByGenericSig> {
787 private:
788 // Context data
789 Thread* THREAD;
790 generic::DescriptorCache* _cache;
791 Symbol* _method_name;
792 generic::Context* _ctx;
793 StatefulGenericMethodFamilies _families;
794
795 public:
796
797 FindMethodsByGenericSig(generic::DescriptorCache* cache, Symbol* name,
798 generic::Context* ctx, Thread* thread) :
799 _cache(cache), _method_name(name), _ctx(ctx), THREAD(thread) {}
800
801 void get_discovered_families(GrowableArray<GenericMethodFamily*>* methods) {
802 _families.extract_families_into(methods);
803 }
804
805 void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); }
806 void free_node_data(void* node_data) {
807 PseudoScope::cast(node_data)->destroy();
808 }
809
810 bool visit() {
811 PseudoScope* scope = PseudoScope::cast(current_data());
812 InstanceKlass* klass = current_class();
813 InstanceKlass* sub = current_depth() > 0 ? class_at_depth(1) : NULL;
814
815 ContextMark* cm = new ContextMark(_ctx->mark());
816 scope->add_mark(cm); // will restore context when scope is freed
817
818 _ctx->apply_type_arguments(sub, klass, THREAD);
819
820 int start, end = 0;
821 start = klass->find_method_by_name(_method_name, &end);
822 if (start != -1) {
823 for (int i = start; i < end; ++i) {
824 Method* m = klass->methods()->at(i);
825 // This gets the method's parameter list with its generic type
826 // parameters resolved
827 generic::MethodDescriptor* md = _cache->descriptor_for(m, THREAD);
828
829 // Find all methods on this hierarchy that match this method
830 // (name, signature). This class collects other families of this
831 // method name.
832 StatefulGenericMethodFamily* family =
833 _families.find_matching_or_create(md, _ctx);
834
835 if (klass->is_interface()) {
836 // ???
837 StateRestorer* restorer = family->record_method_and_dq_further(m);
838 scope->add_mark(restorer);
839 } else {
840 // This is the rule that methods in classes "win" (bad word) over
841 // methods in interfaces. This works because of single inheritance
842 family->set_target_if_empty(m);
843 }
844 }
845 }
846 return true;
847 }
848 };
849
850 #ifndef PRODUCT
851 static void print_generic_families(
852 GrowableArray<GenericMethodFamily*>* methods, Symbol* match) {
853 streamIndentor si(tty, 4);
854 if (methods->length() == 0) {
855 tty->indent();
856 tty->print_cr("No Logical Method found");
857 }
858 for (int i = 0; i < methods->length(); ++i) {
859 tty->indent();
860 GenericMethodFamily* lm = methods->at(i);
861 if (lm->contains_signature(match)) {
862 tty->print_cr("<Matching>");
863 } else {
864 tty->print_cr("<Non-Matching>");
865 }
866 lm->print_sig_on(tty, lm->get_generic_sig(), 1);
867 }
868 }
869 #endif // ndef PRODUCT
870 689
871 static void create_overpasses( 690 static void create_overpasses(
872 GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS); 691 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 692
922 static void generate_erased_defaults( 693 static void generate_erased_defaults(
923 InstanceKlass* klass, GrowableArray<EmptyVtableSlot*>* empty_slots, 694 InstanceKlass* klass, GrowableArray<EmptyVtableSlot*>* empty_slots,
924 EmptyVtableSlot* slot, TRAPS) { 695 EmptyVtableSlot* slot, TRAPS) {
925 696
941 // This is the guts of the default methods implementation. This is called just 712 // This is the guts of the default methods implementation. This is called just
942 // after the classfile has been parsed if some ancestor has default methods. 713 // after the classfile has been parsed if some ancestor has default methods.
943 // 714 //
944 // First if finds any name/signature slots that need any implementation (either 715 // First if finds any name/signature slots that need any implementation (either
945 // because they are miranda or a superclass's implementation is an overpass 716 // because they are miranda or a superclass's implementation is an overpass
946 // itself). For each slot, iterate over the hierarchy, using generic signature 717 // itself). For each slot, iterate over the hierarchy, to see if they contain a
947 // information to partition any methods that match the name into method families 718 // signature that matches the slot we are looking at.
948 // where each family contains methods whose signatures are equivalent at the
949 // language level (i.e., their reified parameters match and return values are
950 // covariant). Check those sets to see if they contain a signature that matches
951 // the slot we're looking at (if we're lucky, there might be other empty slots
952 // that we can fill using the same analysis).
953 // 719 //
954 // For each slot filled, we generate an overpass method that either calls the 720 // For each slot filled, we generate an overpass method that either calls the
955 // unique default method candidate using invokespecial, or throws an exception 721 // unique default method candidate using invokespecial, or throws an exception
956 // (in the case of no default method candidates, or more than one valid 722 // (in the case of no default method candidates, or more than one valid
957 // candidate). These methods are then added to the class's method list. If 723 // candidate). These methods are then added to the class's method list.
958 // the method set we're using contains methods (qualified or not) with a 724 // The JVM does not create bridges nor handle generic signatures here.
959 // different runtime signature than the method we're creating, then we have to
960 // create bridges with those signatures too.
961 void DefaultMethods::generate_default_methods( 725 void DefaultMethods::generate_default_methods(
962 InstanceKlass* klass, GrowableArray<Method*>* mirandas, TRAPS) { 726 InstanceKlass* klass, GrowableArray<Method*>* mirandas, TRAPS) {
963 727
964 // This resource mark is the bound for all memory allocation that takes 728 // This resource mark is the bound for all memory allocation that takes
965 // place during default method processing. After this goes out of scope, 729 // place during default method processing. After this goes out of scope,
995 slot->print_on(tty); 759 slot->print_on(tty);
996 tty->print_cr(""); 760 tty->print_cr("");
997 } 761 }
998 #endif // ndef PRODUCT 762 #endif // ndef PRODUCT
999 763
1000 if (ParseGenericDefaults) { 764 generate_erased_defaults(klass, empty_slots, slot, CHECK);
1001 generate_generic_defaults(klass, empty_slots, slot, i, CHECK);
1002 } else {
1003 generate_erased_defaults(klass, empty_slots, slot, CHECK);
1004 }
1005 } 765 }
1006 #ifndef PRODUCT 766 #ifndef PRODUCT
1007 if (TraceDefaultMethods) { 767 if (TraceDefaultMethods) {
1008 tty->print_cr("Creating overpasses..."); 768 tty->print_cr("Creating overpasses...");
1009 } 769 }
1017 } 777 }
1018 #endif // ndef PRODUCT 778 #endif // ndef PRODUCT
1019 } 779 }
1020 780
1021 /** 781 /**
1022 * Generic analysis was used upon interface '_target' and found a unique 782 * Interface inheritance rules were used to find a unique default method
1023 * default method candidate with generic signature '_method_desc'. This 783 * candidate for the resolved class. This
1024 * method is only viable if it would also be in the set of default method 784 * method is only viable if it would also be in the set of default method
1025 * candidates if we ran a full analysis on the current class. 785 * candidates if we ran a full analysis on the current class.
1026 * 786 *
1027 * The only reason that the method would not be in the set of candidates for 787 * The only reason that the method would not be in the set of candidates for
1028 * the current class is if that there's another covariantly matching method 788 * the current class is if that there's another matching method
1029 * which is "more specific" than the found method -- i.e., one could find a 789 * which is "more specific" than the found method -- i.e., one could find a
1030 * path in the interface hierarchy in which the matching method appears 790 * path in the interface hierarchy in which the matching method appears
1031 * before we get to '_target'. 791 * before we get to '_target'.
1032 * 792 *
1033 * In order to determine this, we examine all of the implemented 793 * In order to determine this, we examine all of the implemented
1108 ErasedShadowChecker(Thread* thread, Symbol* name, InstanceKlass* holder, 868 ErasedShadowChecker(Thread* thread, Symbol* name, InstanceKlass* holder,
1109 InstanceKlass* target) 869 InstanceKlass* target)
1110 : ShadowChecker(thread, name, holder, target) {} 870 : ShadowChecker(thread, name, holder, target) {}
1111 }; 871 };
1112 872
1113 class GenericShadowChecker : public ShadowChecker {
1114 private:
1115 generic::DescriptorCache* _cache;
1116 generic::MethodDescriptor* _method_desc;
1117
1118 bool path_has_shadow() {
1119 generic::Context ctx(_cache);
1120
1121 for (int i = current_depth() - 1; i > 0; --i) {
1122 InstanceKlass* ik = class_at_depth(i);
1123 InstanceKlass* sub = class_at_depth(i + 1);
1124 ctx.apply_type_arguments(sub, ik, THREAD);
1125
1126 if (ik->is_interface()) {
1127 int end;
1128 int start = ik->find_method_by_name(_method_name, &end);
1129 if (start != -1) {
1130 for (int j = start; j < end; ++j) {
1131 Method* mo = ik->methods()->at(j);
1132 generic::MethodDescriptor* md = _cache->descriptor_for(mo, THREAD);
1133 if (_method_desc->covariant_match(md, &ctx)) {
1134 return true;
1135 }
1136 }
1137 }
1138 }
1139 }
1140 return false;
1141 }
1142
1143 public:
1144
1145 GenericShadowChecker(generic::DescriptorCache* cache, Thread* thread,
1146 Symbol* name, InstanceKlass* holder, generic::MethodDescriptor* desc,
1147 InstanceKlass* target)
1148 : ShadowChecker(thread, name, holder, target) {
1149 _cache = cache;
1150 _method_desc = desc;
1151 }
1152 };
1153
1154
1155 873
1156 // Find the unique qualified candidate from the perspective of the super_class 874 // Find the unique qualified candidate from the perspective of the super_class
1157 // which is the resolved_klass, which must be an immediate superinterface 875 // which is the resolved_klass, which must be an immediate superinterface
1158 // of klass 876 // of klass
1159 Method* find_erased_super_default(InstanceKlass* current_class, InstanceKlass* super_class, Symbol* method_name, Symbol* sig, TRAPS) { 877 Method* find_erased_super_default(InstanceKlass* current_class, InstanceKlass* super_class, Symbol* method_name, Symbol* sig, TRAPS) {
1198 } 916 }
1199 } else { 917 } else {
1200 assert(family->throws_exception(), "must have target or throw"); 918 assert(family->throws_exception(), "must have target or throw");
1201 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(), 919 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
1202 family->get_exception_message()->as_C_string(), NULL); 920 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 } 921 }
1264 } 922 }
1265 923
1266 // This is called during linktime when we find an invokespecial call that 924 // This is called during linktime when we find an invokespecial call that
1267 // refers to a direct superinterface. It indicates that we should find the 925 // refers to a direct superinterface. It indicates that we should find the
1294 #endif // ndef PRODUCT 952 #endif // ndef PRODUCT
1295 953
1296 assert(super_class->is_interface(), "only call for default methods"); 954 assert(super_class->is_interface(), "only call for default methods");
1297 955
1298 Method* target = NULL; 956 Method* target = NULL;
1299 if (ParseGenericDefaults) { 957 target = find_erased_super_default(current_class, super_class,
1300 target = find_generic_super_default(current_class, super_class, 958 method_name, sig, CHECK_NULL);
1301 method_name, sig, CHECK_NULL);
1302 } else {
1303 target = find_erased_super_default(current_class, super_class,
1304 method_name, sig, CHECK_NULL);
1305 }
1306 959
1307 #ifndef PRODUCT 960 #ifndef PRODUCT
1308 if (target != NULL) { 961 if (target != NULL) {
1309 if (TraceDefaultMethods) { 962 if (TraceDefaultMethods) {
1310 tty->print(" Returning "); 963 tty->print(" Returning ");

mercurial