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 "memory/allocation.hpp"
31 #include "memory/metadataFactory.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "runtime/handles.inline.hpp"
34 #include "runtime/signature.hpp"
35 #include "runtime/thread.hpp"
36 #include "oops/instanceKlass.hpp"
37 #include "oops/klass.hpp"
38 #include "oops/method.hpp"
39 #include "utilities/accessFlags.hpp"
40 #include "utilities/exceptions.hpp"
41 #include "utilities/ostream.hpp"
42 #include "utilities/pair.hpp"
43 #include "utilities/resourceHash.hpp"
44
45 typedef enum { QUALIFIED, DISQUALIFIED } QualifiedState;
46
47 // Because we use an iterative algorithm when iterating over the type
48 // hierarchy, we can't use traditional scoped objects which automatically do
49 // cleanup in the destructor when the scope is exited. PseudoScope (and
416
417 if (num_defaults == 0) {
418 // If the root klass has a static method with matching name and signature
419 // then do not generate an overpass method because it will hide the
420 // static method during resolution.
421 if (qualified_methods.length() == 0) {
422 _exception_message = generate_no_defaults_message(CHECK);
423 } else {
424 assert(root != NULL, "Null root class");
425 _exception_message = generate_method_message(root->name(), qualified_methods.at(0), CHECK);
426 }
427 _exception_name = vmSymbols::java_lang_AbstractMethodError();
428
429 // If only one qualified method is default, select that
430 } else if (num_defaults == 1) {
431 _selected_target = qualified_methods.at(default_index);
432
433 } else if (num_defaults > 1) {
434 _exception_message = generate_conflicts_message(&qualified_methods,CHECK);
435 _exception_name = vmSymbols::java_lang_IncompatibleClassChangeError();
436 if (log_is_enabled(Debug, defaultmethods)) {
437 ResourceMark rm;
438 outputStream* logstream = Log(defaultmethods)::debug_stream();
439 _exception_message->print_value_on(logstream);
440 logstream->cr();
441 }
442 }
443 }
444
445 bool contains_signature(Symbol* query) {
446 for (int i = 0; i < _members.length(); ++i) {
447 if (query == _members.at(i).first->signature()) {
448 return true;
449 }
450 }
451 return false;
452 }
453
454 void print_selected(outputStream* str, int indent) const {
455 assert(has_target(), "Should be called otherwise");
456 streamIndentor si(str, indent * 2);
457 str->indent().print("Selected method: ");
458 print_method(str, _selected_target);
459 Klass* method_holder = _selected_target->method_holder();
460 if (!method_holder->is_interface()) {
641
642 // also any default methods in our superclasses
643 if (super->default_methods() != NULL) {
644 for (int i = 0; i < super->default_methods()->length(); ++i) {
645 Method* m = super->default_methods()->at(i);
646 // m is a method that would have been a miranda if not for the
647 // default method processing that occurred on behalf of our superclass,
648 // so it's a method we want to re-examine in this new context. That is,
649 // unless we have a real implementation of it in the current class.
650 Method* impl = klass->lookup_method(m->name(), m->signature());
651 if (impl == NULL || impl->is_overpass() || impl->is_static()) {
652 if (!already_in_vtable_slots(slots, m)) {
653 slots->append(new EmptyVtableSlot(m));
654 }
655 }
656 }
657 }
658 super = super->java_super();
659 }
660
661 if (log_is_enabled(Debug, defaultmethods)) {
662 log_debug(defaultmethods)("Slots that need filling:");
663 ResourceMark rm;
664 outputStream* logstream = Log(defaultmethods)::debug_stream();
665 streamIndentor si(logstream);
666 for (int i = 0; i < slots->length(); ++i) {
667 logstream->indent();
668 slots->at(i)->print_on(logstream);
669 logstream->cr();
670 }
671 }
672
673 return slots;
674 }
675
676 // Iterates over the superinterface type hierarchy looking for all methods
677 // with a specific erased signature.
678 class FindMethodsByErasedSig : public HierarchyVisitor<FindMethodsByErasedSig> {
679 private:
680 // Context data
681 Symbol* _method_name;
682 Symbol* _method_signature;
683 StatefulMethodFamily* _family;
684
685 public:
686 FindMethodsByErasedSig(Symbol* name, Symbol* signature) :
687 _method_name(name), _method_signature(signature),
688 _family(NULL) {}
689
775 // (in the case of no default method candidates, or more than one valid
776 // candidate). These methods are then added to the class's method list.
777 // The JVM does not create bridges nor handle generic signatures here.
778 void DefaultMethods::generate_default_methods(
779 InstanceKlass* klass, const GrowableArray<Method*>* mirandas, TRAPS) {
780 assert(klass != NULL, "invariant");
781
782 // This resource mark is the bound for all memory allocation that takes
783 // place during default method processing. After this goes out of scope,
784 // all (Resource) objects' memory will be reclaimed. Be careful if adding an
785 // embedded resource mark under here as that memory can't be used outside
786 // whatever scope it's in.
787 ResourceMark rm(THREAD);
788
789 // Keep entire hierarchy alive for the duration of the computation
790 constantPoolHandle cp(THREAD, klass->constants());
791 KeepAliveRegistrar keepAlive(THREAD);
792 KeepAliveVisitor loadKeepAlive(&keepAlive);
793 loadKeepAlive.run(klass);
794
795 if (log_is_enabled(Debug, defaultmethods)) {
796 ResourceMark rm;
797 log_debug(defaultmethods)("%s %s requires default method processing",
798 klass->is_interface() ? "Interface" : "Class",
799 klass->name()->as_klass_external_name());
800 PrintHierarchy printer(Log(defaultmethods)::debug_stream());
801 printer.run(klass);
802 }
803
804 GrowableArray<EmptyVtableSlot*>* empty_slots =
805 find_empty_vtable_slots(klass, mirandas, CHECK);
806
807 for (int i = 0; i < empty_slots->length(); ++i) {
808 EmptyVtableSlot* slot = empty_slots->at(i);
809 if (log_is_enabled(Debug, defaultmethods)) {
810 outputStream* logstream = Log(defaultmethods)::debug_stream();
811 streamIndentor si(logstream, 2);
812 logstream->indent().print("Looking for default methods for slot ");
813 slot->print_on(logstream);
814 logstream->cr();
815 }
816 generate_erased_defaults(klass, empty_slots, slot, CHECK);
817 }
818 log_debug(defaultmethods)("Creating defaults and overpasses...");
819 create_defaults_and_exceptions(empty_slots, klass, CHECK);
820 log_debug(defaultmethods)("Default method processing complete");
821 }
822
823 static int assemble_method_error(
824 BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* errorName, Symbol* message, TRAPS) {
825
826 Symbol* init = vmSymbols::object_initializer_name();
827 Symbol* sig = vmSymbols::string_void_signature();
828
829 BytecodeAssembler assem(buffer, cp);
830
831 assem._new(errorName);
832 assem.dup();
833 assem.load_string(message);
834 assem.invokespecial(errorName, init, sig);
894 // With the VM only processing erased signatures, the VM only
895 // creates an overpass in a conflict case or a case with no candidates.
896 // This allows virtual methods to override the overpass, but ensures
897 // that a local method search will find the exception rather than an abstract
898 // or default method that is not a valid candidate.
899 static void create_defaults_and_exceptions(
900 GrowableArray<EmptyVtableSlot*>* slots,
901 InstanceKlass* klass, TRAPS) {
902
903 GrowableArray<Method*> overpasses;
904 GrowableArray<Method*> defaults;
905 BytecodeConstantPool bpool(klass->constants());
906
907 for (int i = 0; i < slots->length(); ++i) {
908 EmptyVtableSlot* slot = slots->at(i);
909
910 if (slot->is_bound()) {
911 MethodFamily* method = slot->get_binding();
912 BytecodeBuffer buffer;
913
914 if (log_is_enabled(Debug, defaultmethods)) {
915 ResourceMark rm(THREAD);
916 outputStream* logstream = Log(defaultmethods)::debug_stream();
917 logstream->print("for slot: ");
918 slot->print_on(logstream);
919 logstream->cr();
920 if (method->has_target()) {
921 method->print_selected(logstream, 1);
922 } else if (method->throws_exception()) {
923 method->print_exception(logstream, 1);
924 }
925 }
926
927 if (method->has_target()) {
928 Method* selected = method->get_selected_target();
929 if (selected->method_holder()->is_interface()) {
930 assert(!selected->is_private(), "pushing private interface method as default");
931 defaults.push(selected);
932 }
933 } else if (method->throws_exception()) {
934 int max_stack = assemble_method_error(&bpool, &buffer,
935 method->get_exception_name(), method->get_exception_message(), CHECK);
936 AccessFlags flags = accessFlags_from(
937 JVM_ACC_PUBLIC | JVM_ACC_SYNTHETIC | JVM_ACC_BRIDGE);
938 Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(),
939 flags, max_stack, slot->size_of_parameters(),
940 ConstMethod::OVERPASS, CHECK);
941 // We push to the methods list:
942 // overpass methods which are exception throwing methods
943 if (m != NULL) {
|
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
49 // hierarchy, we can't use traditional scoped objects which automatically do
50 // cleanup in the destructor when the scope is exited. PseudoScope (and
417
418 if (num_defaults == 0) {
419 // If the root klass has a static method with matching name and signature
420 // then do not generate an overpass method because it will hide the
421 // static method during resolution.
422 if (qualified_methods.length() == 0) {
423 _exception_message = generate_no_defaults_message(CHECK);
424 } else {
425 assert(root != NULL, "Null root class");
426 _exception_message = generate_method_message(root->name(), qualified_methods.at(0), CHECK);
427 }
428 _exception_name = vmSymbols::java_lang_AbstractMethodError();
429
430 // If only one qualified method is default, select that
431 } else if (num_defaults == 1) {
432 _selected_target = qualified_methods.at(default_index);
433
434 } else if (num_defaults > 1) {
435 _exception_message = generate_conflicts_message(&qualified_methods,CHECK);
436 _exception_name = vmSymbols::java_lang_IncompatibleClassChangeError();
437 LogTarget(Debug, defaultmethods) lt;
438 if (lt.is_enabled()) {
439 LogStream ls(lt);
440 _exception_message->print_value_on(&ls);
441 ls.cr();
442 }
443 }
444 }
445
446 bool contains_signature(Symbol* query) {
447 for (int i = 0; i < _members.length(); ++i) {
448 if (query == _members.at(i).first->signature()) {
449 return true;
450 }
451 }
452 return false;
453 }
454
455 void print_selected(outputStream* str, int indent) const {
456 assert(has_target(), "Should be called otherwise");
457 streamIndentor si(str, indent * 2);
458 str->indent().print("Selected method: ");
459 print_method(str, _selected_target);
460 Klass* method_holder = _selected_target->method_holder();
461 if (!method_holder->is_interface()) {
642
643 // also any default methods in our superclasses
644 if (super->default_methods() != NULL) {
645 for (int i = 0; i < super->default_methods()->length(); ++i) {
646 Method* m = super->default_methods()->at(i);
647 // m is a method that would have been a miranda if not for the
648 // default method processing that occurred on behalf of our superclass,
649 // so it's a method we want to re-examine in this new context. That is,
650 // unless we have a real implementation of it in the current class.
651 Method* impl = klass->lookup_method(m->name(), m->signature());
652 if (impl == NULL || impl->is_overpass() || impl->is_static()) {
653 if (!already_in_vtable_slots(slots, m)) {
654 slots->append(new EmptyVtableSlot(m));
655 }
656 }
657 }
658 }
659 super = super->java_super();
660 }
661
662 LogTarget(Debug, defaultmethods) lt;
663 if (lt.is_enabled()) {
664 lt.print("Slots that need filling:");
665 ResourceMark rm;
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
777 // (in the case of no default method candidates, or more than one valid
778 // candidate). These methods are then added to the class's method list.
779 // The JVM does not create bridges nor handle generic signatures here.
780 void DefaultMethods::generate_default_methods(
781 InstanceKlass* klass, const GrowableArray<Method*>* mirandas, TRAPS) {
782 assert(klass != NULL, "invariant");
783
784 // This resource mark is the bound for all memory allocation that takes
785 // place during default method processing. After this goes out of scope,
786 // all (Resource) objects' memory will be reclaimed. Be careful if adding an
787 // embedded resource mark under here as that memory can't be used outside
788 // whatever scope it's in.
789 ResourceMark rm(THREAD);
790
791 // Keep entire hierarchy alive for the duration of the computation
792 constantPoolHandle cp(THREAD, klass->constants());
793 KeepAliveRegistrar keepAlive(THREAD);
794 KeepAliveVisitor loadKeepAlive(&keepAlive);
795 loadKeepAlive.run(klass);
796
797 LogTarget(Debug, defaultmethods) lt;
798 if (lt.is_enabled()) {
799 ResourceMark rm;
800 lt.print("%s %s requires default method processing",
801 klass->is_interface() ? "Interface" : "Class",
802 klass->name()->as_klass_external_name());
803 LogStream ls(lt);
804 PrintHierarchy printer(&ls);
805 printer.run(klass);
806 }
807
808 GrowableArray<EmptyVtableSlot*>* empty_slots =
809 find_empty_vtable_slots(klass, mirandas, CHECK);
810
811 for (int i = 0; i < empty_slots->length(); ++i) {
812 EmptyVtableSlot* slot = empty_slots->at(i);
813 LogTarget(Debug, defaultmethods) lt;
814 if (lt.is_enabled()) {
815 LogStream ls(lt);
816 streamIndentor si(&ls, 2);
817 ls.indent().print("Looking for default methods for slot ");
818 slot->print_on(&ls);
819 ls.cr();
820 }
821 generate_erased_defaults(klass, empty_slots, slot, CHECK);
822 }
823 log_debug(defaultmethods)("Creating defaults and overpasses...");
824 create_defaults_and_exceptions(empty_slots, klass, CHECK);
825 log_debug(defaultmethods)("Default method processing complete");
826 }
827
828 static int assemble_method_error(
829 BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* errorName, Symbol* message, TRAPS) {
830
831 Symbol* init = vmSymbols::object_initializer_name();
832 Symbol* sig = vmSymbols::string_void_signature();
833
834 BytecodeAssembler assem(buffer, cp);
835
836 assem._new(errorName);
837 assem.dup();
838 assem.load_string(message);
839 assem.invokespecial(errorName, init, sig);
899 // With the VM only processing erased signatures, the VM only
900 // creates an overpass in a conflict case or a case with no candidates.
901 // This allows virtual methods to override the overpass, but ensures
902 // that a local method search will find the exception rather than an abstract
903 // or default method that is not a valid candidate.
904 static void create_defaults_and_exceptions(
905 GrowableArray<EmptyVtableSlot*>* slots,
906 InstanceKlass* klass, TRAPS) {
907
908 GrowableArray<Method*> overpasses;
909 GrowableArray<Method*> defaults;
910 BytecodeConstantPool bpool(klass->constants());
911
912 for (int i = 0; i < slots->length(); ++i) {
913 EmptyVtableSlot* slot = slots->at(i);
914
915 if (slot->is_bound()) {
916 MethodFamily* method = slot->get_binding();
917 BytecodeBuffer buffer;
918
919 LogTarget(Debug, defaultmethods) lt;
920 if (lt.is_enabled()) {
921 ResourceMark rm(THREAD);
922 LogStream ls(lt);
923 ls.print("for slot: ");
924 slot->print_on(&ls);
925 ls.cr();
926 if (method->has_target()) {
927 method->print_selected(&ls, 1);
928 } else if (method->throws_exception()) {
929 method->print_exception(&ls, 1);
930 }
931 }
932
933 if (method->has_target()) {
934 Method* selected = method->get_selected_target();
935 if (selected->method_holder()->is_interface()) {
936 assert(!selected->is_private(), "pushing private interface method as default");
937 defaults.push(selected);
938 }
939 } else if (method->throws_exception()) {
940 int max_stack = assemble_method_error(&bpool, &buffer,
941 method->get_exception_name(), method->get_exception_message(), CHECK);
942 AccessFlags flags = accessFlags_from(
943 JVM_ACC_PUBLIC | JVM_ACC_SYNTHETIC | JVM_ACC_BRIDGE);
944 Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(),
945 flags, max_stack, slot->size_of_parameters(),
946 ConstMethod::OVERPASS, CHECK);
947 // We push to the methods list:
948 // overpass methods which are exception throwing methods
949 if (m != NULL) {
|