src/share/vm/oops/instanceKlass.cpp

changeset 8982
8f1acbb637e3
parent 8951
0612a789929b
parent 8721
575f637864df
child 9041
95a08233f46c
child 9183
f95c67788f18
child 9291
a2c8195708cc
equal deleted inserted replaced
8962:8aa5e0006ee3 8982:8f1acbb637e3
1 /* 1 /*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
614 return is_linked(); 614 return is_linked();
615 } 615 }
616 616
617 bool InstanceKlass::link_class_impl( 617 bool InstanceKlass::link_class_impl(
618 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) { 618 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
619 // check for error state 619 // check for error state.
620 // This is checking for the wrong state. If the state is initialization_error,
621 // then this class *was* linked. The CDS code does a try_link_class and uses
622 // initialization_error to mark classes to not include in the archive during
623 // DumpSharedSpaces. This should be removed when the CDS bug is fixed.
620 if (this_oop->is_in_error_state()) { 624 if (this_oop->is_in_error_state()) {
621 ResourceMark rm(THREAD); 625 ResourceMark rm(THREAD);
622 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(), 626 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(),
623 this_oop->external_name(), false); 627 this_oop->external_name(), false);
624 } 628 }
799 #endif //ASSERT 803 #endif //ASSERT
800 } 804 }
801 } 805 }
802 806
803 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access) 807 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
804 void InstanceKlass::initialize_super_interfaces(instanceKlassHandle this_oop, TRAPS) { 808 void InstanceKlass::initialize_super_interfaces(instanceKlassHandle this_k, TRAPS) {
805 if (this_oop->has_default_methods()) { 809 assert (this_k->has_default_methods(), "caller should have checked this");
806 for (int i = 0; i < this_oop->local_interfaces()->length(); ++i) { 810 for (int i = 0; i < this_k->local_interfaces()->length(); ++i) {
807 Klass* iface = this_oop->local_interfaces()->at(i); 811 Klass* iface = this_k->local_interfaces()->at(i);
808 InstanceKlass* ik = InstanceKlass::cast(iface); 812 InstanceKlass* ik = InstanceKlass::cast(iface);
809 if (ik->should_be_initialized()) { 813
810 if (ik->has_default_methods()) { 814 // Initialization is depth first search ie. we start with top of the inheritance tree
811 ik->initialize_super_interfaces(ik, THREAD); 815 // has_default_methods drives searching superinterfaces since it
812 } 816 // means has_default_methods in its superinterface hierarchy
813 // Only initialize() interfaces that "declare" concrete methods. 817 if (ik->has_default_methods()) {
814 // has_default_methods drives searching superinterfaces since it 818 ik->initialize_super_interfaces(ik, CHECK);
815 // means has_default_methods in its superinterface hierarchy 819 }
816 if (!HAS_PENDING_EXCEPTION && ik->declares_default_methods()) { 820
817 ik->initialize(THREAD); 821 // Only initialize() interfaces that "declare" concrete methods.
818 } 822 if (ik->should_be_initialized() && ik->declares_default_methods()) {
819 if (HAS_PENDING_EXCEPTION) { 823 ik->initialize(CHECK);
820 Handle e(THREAD, PENDING_EXCEPTION);
821 CLEAR_PENDING_EXCEPTION;
822 {
823 EXCEPTION_MARK;
824 // Locks object, set state, and notify all waiting threads
825 this_oop->set_initialization_state_and_notify(
826 initialization_error, THREAD);
827
828 // ignore any exception thrown, superclass initialization error is
829 // thrown below
830 CLEAR_PENDING_EXCEPTION;
831 }
832 THROW_OOP(e());
833 }
834 }
835 } 824 }
836 } 825 }
837 } 826 }
838 827
839 void InstanceKlass::initialize_impl(instanceKlassHandle this_oop, TRAPS) { 828 void InstanceKlass::initialize_impl(instanceKlassHandle this_oop, TRAPS) {
895 this_oop->set_init_state(being_initialized); 884 this_oop->set_init_state(being_initialized);
896 this_oop->set_init_thread(self); 885 this_oop->set_init_thread(self);
897 } 886 }
898 887
899 // Step 7 888 // Step 7
900 Klass* super_klass = this_oop->super(); 889 // Next, if C is a class rather than an interface, initialize its super class and super
901 if (super_klass != NULL && !this_oop->is_interface() && super_klass->should_be_initialized()) { 890 // interfaces.
902 super_klass->initialize(THREAD); 891 if (!this_oop->is_interface()) {
903 892 Klass* super_klass = this_oop->super();
893 if (super_klass != NULL && super_klass->should_be_initialized()) {
894 super_klass->initialize(THREAD);
895 }
896 // If C implements any interfaces that declares a non-abstract, non-static method,
897 // the initialization of C triggers initialization of its super interfaces.
898 // Only need to recurse if has_default_methods which includes declaring and
899 // inheriting default methods
900 if (!HAS_PENDING_EXCEPTION && this_oop->has_default_methods()) {
901 this_oop->initialize_super_interfaces(this_oop, THREAD);
902 }
903
904 // If any exceptions, complete abruptly, throwing the same exception as above.
904 if (HAS_PENDING_EXCEPTION) { 905 if (HAS_PENDING_EXCEPTION) {
905 Handle e(THREAD, PENDING_EXCEPTION); 906 Handle e(THREAD, PENDING_EXCEPTION);
906 CLEAR_PENDING_EXCEPTION; 907 CLEAR_PENDING_EXCEPTION;
907 { 908 {
908 EXCEPTION_MARK; 909 EXCEPTION_MARK;
909 this_oop->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads 910 // Locks object, set state, and notify all waiting threads
910 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, superclass initialization error is thrown below 911 this_oop->set_initialization_state_and_notify(initialization_error, THREAD);
912 CLEAR_PENDING_EXCEPTION;
911 } 913 }
912 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, InstanceKlass::cast(this_oop()), -1,wait); 914 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, InstanceKlass::cast(this_oop()), -1,wait);
913 THROW_OOP(e()); 915 THROW_OOP(e());
914 } 916 }
915 }
916
917 // Recursively initialize any superinterfaces that declare default methods
918 // Only need to recurse if has_default_methods which includes declaring and
919 // inheriting default methods
920 if (this_oop->has_default_methods()) {
921 this_oop->initialize_super_interfaces(this_oop, CHECK);
922 } 917 }
923 918
924 // Step 8 919 // Step 8
925 { 920 {
926 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl"); 921 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
979 set_initialization_state_and_notify_impl(kh, state, CHECK); 974 set_initialization_state_and_notify_impl(kh, state, CHECK);
980 } 975 }
981 976
982 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) { 977 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) {
983 oop init_lock = this_oop->init_lock(); 978 oop init_lock = this_oop->init_lock();
984 ObjectLocker ol(init_lock, THREAD, init_lock != NULL); 979 if (init_lock != NULL) {
985 this_oop->set_init_state(state); 980 ObjectLocker ol(init_lock, THREAD);
986 this_oop->fence_and_clear_init_lock(); 981 this_oop->set_init_state(state);
987 ol.notify_all(CHECK); 982 this_oop->fence_and_clear_init_lock();
983 ol.notify_all(CHECK);
984 } else {
985 assert(init_lock != NULL, "The initialization state should never be set twice");
986 this_oop->set_init_state(state);
987 }
988 } 988 }
989 989
990 // The embedded _implementor field can only record one implementor. 990 // The embedded _implementor field can only record one implementor.
991 // When there are more than one implementors, the _implementor field 991 // When there are more than one implementors, the _implementor field
992 // is set to the interface Klass* itself. Following are the possible 992 // is set to the interface Klass* itself. Following are the possible
1473 return -1; 1473 return -1;
1474 } 1474 }
1475 1475
1476 // find_method looks up the name/signature in the local methods array 1476 // find_method looks up the name/signature in the local methods array
1477 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const { 1477 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const {
1478 return find_method_impl(name, signature, false); 1478 return find_method_impl(name, signature, find_overpass, find_static, find_private);
1479 } 1479 }
1480 1480
1481 Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature, bool skipping_overpass) const { 1481 Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature,
1482 return InstanceKlass::find_method_impl(methods(), name, signature, skipping_overpass, false); 1482 OverpassLookupMode overpass_mode,
1483 StaticLookupMode static_mode,
1484 PrivateLookupMode private_mode) const {
1485 return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode);
1483 } 1486 }
1484 1487
1485 // find_instance_method looks up the name/signature in the local methods array 1488 // find_instance_method looks up the name/signature in the local methods array
1486 // and skips over static methods 1489 // and skips over static methods
1487 Method* InstanceKlass::find_instance_method( 1490 Method* InstanceKlass::find_instance_method(
1488 Array<Method*>* methods, Symbol* name, Symbol* signature) { 1491 Array<Method*>* methods, Symbol* name, Symbol* signature) {
1489 Method* meth = InstanceKlass::find_method_impl(methods, name, signature, false, true); 1492 Method* meth = InstanceKlass::find_method_impl(methods, name, signature,
1493 find_overpass, skip_static, find_private);
1494 assert(((meth == NULL) || !meth->is_static()), "find_instance_method should have skipped statics");
1490 return meth; 1495 return meth;
1491 } 1496 }
1492 1497
1493 // find_instance_method looks up the name/signature in the local methods array 1498 // find_instance_method looks up the name/signature in the local methods array
1494 // and skips over static methods 1499 // and skips over static methods
1495 Method* InstanceKlass::find_instance_method(Symbol* name, Symbol* signature) { 1500 Method* InstanceKlass::find_instance_method(Symbol* name, Symbol* signature) {
1496 return InstanceKlass::find_instance_method(methods(), name, signature); 1501 return InstanceKlass::find_instance_method(methods(), name, signature);
1497 } 1502 }
1498 1503
1504 // Find looks up the name/signature in the local methods array
1505 // and filters on the overpass, static and private flags
1506 // This returns the first one found
1507 // note that the local methods array can have up to one overpass, one static
1508 // and one instance (private or not) with the same name/signature
1509 Method* InstanceKlass::find_local_method(Symbol* name, Symbol* signature,
1510 OverpassLookupMode overpass_mode,
1511 StaticLookupMode static_mode,
1512 PrivateLookupMode private_mode) const {
1513 return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode);
1514 }
1515
1516 // Find looks up the name/signature in the local methods array
1517 // and filters on the overpass, static and private flags
1518 // This returns the first one found
1519 // note that the local methods array can have up to one overpass, one static
1520 // and one instance (private or not) with the same name/signature
1521 Method* InstanceKlass::find_local_method(Array<Method*>* methods,
1522 Symbol* name, Symbol* signature,
1523 OverpassLookupMode overpass_mode,
1524 StaticLookupMode static_mode,
1525 PrivateLookupMode private_mode) {
1526 return InstanceKlass::find_method_impl(methods, name, signature, overpass_mode, static_mode, private_mode);
1527 }
1528
1529
1499 // find_method looks up the name/signature in the local methods array 1530 // find_method looks up the name/signature in the local methods array
1500 Method* InstanceKlass::find_method( 1531 Method* InstanceKlass::find_method(
1501 Array<Method*>* methods, Symbol* name, Symbol* signature) { 1532 Array<Method*>* methods, Symbol* name, Symbol* signature) {
1502 return InstanceKlass::find_method_impl(methods, name, signature, false, false); 1533 return InstanceKlass::find_method_impl(methods, name, signature, find_overpass, find_static, find_private);
1503 } 1534 }
1504 1535
1505 Method* InstanceKlass::find_method_impl( 1536 Method* InstanceKlass::find_method_impl(
1506 Array<Method*>* methods, Symbol* name, Symbol* signature, bool skipping_overpass, bool skipping_static) { 1537 Array<Method*>* methods, Symbol* name, Symbol* signature,
1507 int hit = find_method_index(methods, name, signature, skipping_overpass, skipping_static); 1538 OverpassLookupMode overpass_mode, StaticLookupMode static_mode,
1539 PrivateLookupMode private_mode) {
1540 int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode);
1508 return hit >= 0 ? methods->at(hit): NULL; 1541 return hit >= 0 ? methods->at(hit): NULL;
1509 } 1542 }
1510 1543
1511 bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static) { 1544 bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static, bool skipping_private) {
1512 return (m->signature() == signature) && 1545 return ((m->signature() == signature) &&
1513 (!skipping_overpass || !m->is_overpass()) && 1546 (!skipping_overpass || !m->is_overpass()) &&
1514 (!skipping_static || !m->is_static()); 1547 (!skipping_static || !m->is_static()) &&
1548 (!skipping_private || !m->is_private()));
1515 } 1549 }
1516 1550
1517 // Used directly for default_methods to find the index into the 1551 // Used directly for default_methods to find the index into the
1518 // default_vtable_indices, and indirectly by find_method 1552 // default_vtable_indices, and indirectly by find_method
1519 // find_method_index looks in the local methods array to return the index 1553 // find_method_index looks in the local methods array to return the index
1520 // of the matching name/signature. If, overpass methods are being ignored, 1554 // of the matching name/signature. If, overpass methods are being ignored,
1521 // the search continues to find a potential non-overpass match. This capability 1555 // the search continues to find a potential non-overpass match. This capability
1522 // is important during method resolution to prefer a static method, for example, 1556 // is important during method resolution to prefer a static method, for example,
1523 // over an overpass method. 1557 // over an overpass method.
1558 // There is the possibility in any _method's array to have the same name/signature
1559 // for a static method, an overpass method and a local instance method
1560 // To correctly catch a given method, the search criteria may need
1561 // to explicitly skip the other two. For local instance methods, it
1562 // is often necessary to skip private methods
1524 int InstanceKlass::find_method_index( 1563 int InstanceKlass::find_method_index(
1525 Array<Method*>* methods, Symbol* name, Symbol* signature, bool skipping_overpass, bool skipping_static) { 1564 Array<Method*>* methods, Symbol* name, Symbol* signature,
1565 OverpassLookupMode overpass_mode, StaticLookupMode static_mode,
1566 PrivateLookupMode private_mode) {
1567 bool skipping_overpass = (overpass_mode == skip_overpass);
1568 bool skipping_static = (static_mode == skip_static);
1569 bool skipping_private = (private_mode == skip_private);
1526 int hit = binary_search(methods, name); 1570 int hit = binary_search(methods, name);
1527 if (hit != -1) { 1571 if (hit != -1) {
1528 Method* m = methods->at(hit); 1572 Method* m = methods->at(hit);
1529 1573
1530 // Do linear search to find matching signature. First, quick check 1574 // Do linear search to find matching signature. First, quick check
1531 // for common case, ignoring overpasses if requested. 1575 // for common case, ignoring overpasses if requested.
1532 if (method_matches(m, signature, skipping_overpass, skipping_static)) return hit; 1576 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return hit;
1533 1577
1534 // search downwards through overloaded methods 1578 // search downwards through overloaded methods
1535 int i; 1579 int i;
1536 for (i = hit - 1; i >= 0; --i) { 1580 for (i = hit - 1; i >= 0; --i) {
1537 Method* m = methods->at(i); 1581 Method* m = methods->at(i);
1538 assert(m->is_method(), "must be method"); 1582 assert(m->is_method(), "must be method");
1539 if (m->name() != name) break; 1583 if (m->name() != name) break;
1540 if (method_matches(m, signature, skipping_overpass, skipping_static)) return i; 1584 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i;
1541 } 1585 }
1542 // search upwards 1586 // search upwards
1543 for (i = hit + 1; i < methods->length(); ++i) { 1587 for (i = hit + 1; i < methods->length(); ++i) {
1544 Method* m = methods->at(i); 1588 Method* m = methods->at(i);
1545 assert(m->is_method(), "must be method"); 1589 assert(m->is_method(), "must be method");
1546 if (m->name() != name) break; 1590 if (m->name() != name) break;
1547 if (method_matches(m, signature, skipping_overpass, skipping_static)) return i; 1591 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i;
1548 } 1592 }
1549 // not found 1593 // not found
1550 #ifdef ASSERT 1594 #ifdef ASSERT
1551 int index = skipping_overpass || skipping_static ? -1 : linear_search(methods, name, signature); 1595 int index = (skipping_overpass || skipping_static || skipping_private) ? -1 : linear_search(methods, name, signature);
1552 assert(index == -1, err_msg("binary search should have found entry %d", index)); 1596 assert(index == -1, err_msg("binary search should have found entry %d", index));
1553 #endif 1597 #endif
1554 } 1598 }
1555 return -1; 1599 return -1;
1556 } 1600 }
1572 return -1; 1616 return -1;
1573 } 1617 }
1574 1618
1575 // uncached_lookup_method searches both the local class methods array and all 1619 // uncached_lookup_method searches both the local class methods array and all
1576 // superclasses methods arrays, skipping any overpass methods in superclasses. 1620 // superclasses methods arrays, skipping any overpass methods in superclasses.
1577 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, MethodLookupMode mode) const { 1621 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
1578 MethodLookupMode lookup_mode = mode; 1622 OverpassLookupMode overpass_local_mode = overpass_mode;
1579 Klass* klass = const_cast<InstanceKlass*>(this); 1623 Klass* klass = const_cast<InstanceKlass*>(this);
1580 while (klass != NULL) { 1624 while (klass != NULL) {
1581 Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, (lookup_mode == skip_overpass)); 1625 Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, overpass_local_mode, find_static, find_private);
1582 if (method != NULL) { 1626 if (method != NULL) {
1583 return method; 1627 return method;
1584 } 1628 }
1585 klass = InstanceKlass::cast(klass)->super(); 1629 klass = InstanceKlass::cast(klass)->super();
1586 lookup_mode = skip_overpass; // Always ignore overpass methods in superclasses 1630 overpass_local_mode = skip_overpass; // Always ignore overpass methods in superclasses
1587 } 1631 }
1588 return NULL; 1632 return NULL;
1589 } 1633 }
1590 1634
1591 #ifdef ASSERT 1635 #ifdef ASSERT
1611 if (default_methods() != NULL) { 1655 if (default_methods() != NULL) {
1612 m = find_method(default_methods(), name, signature); 1656 m = find_method(default_methods(), name, signature);
1613 } 1657 }
1614 // Look up interfaces 1658 // Look up interfaces
1615 if (m == NULL) { 1659 if (m == NULL) {
1616 m = lookup_method_in_all_interfaces(name, signature, normal); 1660 m = lookup_method_in_all_interfaces(name, signature, find_defaults);
1617 } 1661 }
1618 return m; 1662 return m;
1619 } 1663 }
1620 1664
1621 // lookup a method in all the interfaces that this class implements 1665 // lookup a method in all the interfaces that this class implements
1622 // Do NOT return private or static methods, new in JDK8 which are not externally visible 1666 // Do NOT return private or static methods, new in JDK8 which are not externally visible
1623 // They should only be found in the initial InterfaceMethodRef 1667 // They should only be found in the initial InterfaceMethodRef
1624 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name, 1668 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name,
1625 Symbol* signature, 1669 Symbol* signature,
1626 MethodLookupMode mode) const { 1670 DefaultsLookupMode defaults_mode) const {
1627 Array<Klass*>* all_ifs = transitive_interfaces(); 1671 Array<Klass*>* all_ifs = transitive_interfaces();
1628 int num_ifs = all_ifs->length(); 1672 int num_ifs = all_ifs->length();
1629 InstanceKlass *ik = NULL; 1673 InstanceKlass *ik = NULL;
1630 for (int i = 0; i < num_ifs; i++) { 1674 for (int i = 0; i < num_ifs; i++) {
1631 ik = InstanceKlass::cast(all_ifs->at(i)); 1675 ik = InstanceKlass::cast(all_ifs->at(i));
1632 Method* m = ik->lookup_method(name, signature); 1676 Method* m = ik->lookup_method(name, signature);
1633 if (m != NULL && m->is_public() && !m->is_static() && 1677 if (m != NULL && m->is_public() && !m->is_static() &&
1634 ((mode != skip_defaults) || !m->is_default_method())) { 1678 ((defaults_mode != skip_defaults) || !m->is_default_method())) {
1635 return m; 1679 return m;
1636 } 1680 }
1637 } 1681 }
1638 return NULL; 1682 return NULL;
1639 } 1683 }

mercurial