1752 // methods are sorted by ascending addresses of their names, so do binary search 1753 while (l <= h) { 1754 int mid = (l + h) >> 1; 1755 Method* m = methods->at(mid); 1756 assert(m->is_method(), "must be method"); 1757 int res = m->name()->fast_compare(name); 1758 if (res == 0) { 1759 return mid; 1760 } else if (res < 0) { 1761 l = mid + 1; 1762 } else { 1763 h = mid - 1; 1764 } 1765 } 1766 return -1; 1767 } 1768 1769 // find_method looks up the name/signature in the local methods array 1770 Method* InstanceKlass::find_method(const Symbol* name, 1771 const Symbol* signature) const { 1772 return find_method_impl(name, signature, find_overpass, find_static, find_private); 1773 } 1774 1775 Method* InstanceKlass::find_method_impl(const Symbol* name, 1776 const Symbol* signature, 1777 OverpassLookupMode overpass_mode, 1778 StaticLookupMode static_mode, 1779 PrivateLookupMode private_mode) const { 1780 return InstanceKlass::find_method_impl(methods(), 1781 name, 1782 signature, 1783 overpass_mode, 1784 static_mode, 1785 private_mode); 1786 } 1787 1788 // find_instance_method looks up the name/signature in the local methods array 1789 // and skips over static methods 1790 Method* InstanceKlass::find_instance_method(const Array<Method*>* methods, 1791 const Symbol* name, 1792 const Symbol* signature, 1793 PrivateLookupMode private_mode) { 1794 Method* const meth = InstanceKlass::find_method_impl(methods, 1795 name, 1796 signature, 1797 find_overpass, 1798 skip_static, 1799 private_mode); 1800 assert(((meth == NULL) || !meth->is_static()), 1801 "find_instance_method should have skipped statics"); 1802 return meth; 1803 } 1804 1805 // find_instance_method looks up the name/signature in the local methods array 1806 // and skips over static methods 1807 Method* InstanceKlass::find_instance_method(const Symbol* name, 1808 const Symbol* signature, 1809 PrivateLookupMode private_mode) const { 1810 return InstanceKlass::find_instance_method(methods(), name, signature, private_mode); 1811 } 1812 1813 // Find looks up the name/signature in the local methods array 1814 // and filters on the overpass, static and private flags 1815 // This returns the first one found 1816 // note that the local methods array can have up to one overpass, one static 1817 // and one instance (private or not) with the same name/signature 1818 Method* InstanceKlass::find_local_method(const Symbol* name, 1836 Method* InstanceKlass::find_local_method(const Array<Method*>* methods, 1837 const Symbol* name, 1838 const Symbol* signature, 1839 OverpassLookupMode overpass_mode, 1840 StaticLookupMode static_mode, 1841 PrivateLookupMode private_mode) { 1842 return InstanceKlass::find_method_impl(methods, 1843 name, 1844 signature, 1845 overpass_mode, 1846 static_mode, 1847 private_mode); 1848 } 1849 1850 Method* InstanceKlass::find_method(const Array<Method*>* methods, 1851 const Symbol* name, 1852 const Symbol* signature) { 1853 return InstanceKlass::find_method_impl(methods, 1854 name, 1855 signature, 1856 find_overpass, 1857 find_static, 1858 find_private); 1859 } 1860 1861 Method* InstanceKlass::find_method_impl(const Array<Method*>* methods, 1862 const Symbol* name, 1863 const Symbol* signature, 1864 OverpassLookupMode overpass_mode, 1865 StaticLookupMode static_mode, 1866 PrivateLookupMode private_mode) { 1867 int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode); 1868 return hit >= 0 ? methods->at(hit): NULL; 1869 } 1870 1871 // true if method matches signature and conforms to skipping_X conditions. 1872 static bool method_matches(const Method* m, 1873 const Symbol* signature, 1874 bool skipping_overpass, 1875 bool skipping_static, 1876 bool skipping_private) { 1877 return ((m->signature() == signature) && 1878 (!skipping_overpass || !m->is_overpass()) && 1881 } 1882 1883 // Used directly for default_methods to find the index into the 1884 // default_vtable_indices, and indirectly by find_method 1885 // find_method_index looks in the local methods array to return the index 1886 // of the matching name/signature. If, overpass methods are being ignored, 1887 // the search continues to find a potential non-overpass match. This capability 1888 // is important during method resolution to prefer a static method, for example, 1889 // over an overpass method. 1890 // There is the possibility in any _method's array to have the same name/signature 1891 // for a static method, an overpass method and a local instance method 1892 // To correctly catch a given method, the search criteria may need 1893 // to explicitly skip the other two. For local instance methods, it 1894 // is often necessary to skip private methods 1895 int InstanceKlass::find_method_index(const Array<Method*>* methods, 1896 const Symbol* name, 1897 const Symbol* signature, 1898 OverpassLookupMode overpass_mode, 1899 StaticLookupMode static_mode, 1900 PrivateLookupMode private_mode) { 1901 const bool skipping_overpass = (overpass_mode == skip_overpass); 1902 const bool skipping_static = (static_mode == skip_static); 1903 const bool skipping_private = (private_mode == skip_private); 1904 const int hit = quick_search(methods, name); 1905 if (hit != -1) { 1906 const Method* const m = methods->at(hit); 1907 1908 // Do linear search to find matching signature. First, quick check 1909 // for common case, ignoring overpasses if requested. 1910 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) { 1911 return hit; 1912 } 1913 1914 // search downwards through overloaded methods 1915 int i; 1916 for (i = hit - 1; i >= 0; --i) { 1917 const Method* const m = methods->at(i); 1918 assert(m->is_method(), "must be method"); 1919 if (m->name() != name) { 1920 break; 1921 } 1922 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) { 1923 return i; 1959 while (end < methods->length() && (methods->at(end))->name() == name) ++end; 1960 *end_ptr = end; 1961 return start; 1962 } 1963 return -1; 1964 } 1965 1966 // uncached_lookup_method searches both the local class methods array and all 1967 // superclasses methods arrays, skipping any overpass methods in superclasses, 1968 // and possibly skipping private methods. 1969 Method* InstanceKlass::uncached_lookup_method(const Symbol* name, 1970 const Symbol* signature, 1971 OverpassLookupMode overpass_mode, 1972 PrivateLookupMode private_mode) const { 1973 OverpassLookupMode overpass_local_mode = overpass_mode; 1974 const Klass* klass = this; 1975 while (klass != NULL) { 1976 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name, 1977 signature, 1978 overpass_local_mode, 1979 find_static, 1980 private_mode); 1981 if (method != NULL) { 1982 return method; 1983 } 1984 klass = klass->super(); 1985 overpass_local_mode = skip_overpass; // Always ignore overpass methods in superclasses 1986 } 1987 return NULL; 1988 } 1989 1990 #ifdef ASSERT 1991 // search through class hierarchy and return true if this class or 1992 // one of the superclasses was redefined 1993 bool InstanceKlass::has_redefined_this_or_super() const { 1994 const Klass* klass = this; 1995 while (klass != NULL) { 1996 if (InstanceKlass::cast(klass)->has_been_redefined()) { 1997 return true; 1998 } 1999 klass = klass->super(); 2000 } 2001 return false; 2002 } 2003 #endif 2004 2005 // lookup a method in the default methods list then in all transitive interfaces 2006 // Do NOT return private or static methods 2007 Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name, 2008 Symbol* signature) const { 2009 Method* m = NULL; 2010 if (default_methods() != NULL) { 2011 m = find_method(default_methods(), name, signature); 2012 } 2013 // Look up interfaces 2014 if (m == NULL) { 2015 m = lookup_method_in_all_interfaces(name, signature, find_defaults); 2016 } 2017 return m; 2018 } 2019 2020 // lookup a method in all the interfaces that this class implements 2021 // Do NOT return private or static methods, new in JDK8 which are not externally visible 2022 // They should only be found in the initial InterfaceMethodRef 2023 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name, 2024 Symbol* signature, 2025 DefaultsLookupMode defaults_mode) const { 2026 Array<InstanceKlass*>* all_ifs = transitive_interfaces(); 2027 int num_ifs = all_ifs->length(); 2028 InstanceKlass *ik = NULL; 2029 for (int i = 0; i < num_ifs; i++) { 2030 ik = all_ifs->at(i); 2031 Method* m = ik->lookup_method(name, signature); 2032 if (m != NULL && m->is_public() && !m->is_static() && 2033 ((defaults_mode != skip_defaults) || !m->is_default_method())) { 2034 return m; 2035 } 2036 } 2037 return NULL; 2038 } 2039 2040 /* jni_id_for_impl for jfieldIds only */ 2041 JNIid* InstanceKlass::jni_id_for_impl(int offset) { 2042 MutexLocker ml(JfieldIdCreation_lock); 2043 // Retry lookup after we got the lock 2044 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset); 2045 if (probe == NULL) { 2046 // Slow case, allocate new static field identifier 2047 probe = new JNIid(this, offset, jni_ids()); 2048 set_jni_ids(probe); 2049 } 2050 return probe; 2051 } 2052 2053 | 1752 // methods are sorted by ascending addresses of their names, so do binary search 1753 while (l <= h) { 1754 int mid = (l + h) >> 1; 1755 Method* m = methods->at(mid); 1756 assert(m->is_method(), "must be method"); 1757 int res = m->name()->fast_compare(name); 1758 if (res == 0) { 1759 return mid; 1760 } else if (res < 0) { 1761 l = mid + 1; 1762 } else { 1763 h = mid - 1; 1764 } 1765 } 1766 return -1; 1767 } 1768 1769 // find_method looks up the name/signature in the local methods array 1770 Method* InstanceKlass::find_method(const Symbol* name, 1771 const Symbol* signature) const { 1772 return find_method_impl(name, signature, 1773 OverpassLookupMode::find_overpass, 1774 StaticLookupMode::find_static, 1775 PrivateLookupMode::find_private); 1776 } 1777 1778 Method* InstanceKlass::find_method_impl(const Symbol* name, 1779 const Symbol* signature, 1780 OverpassLookupMode overpass_mode, 1781 StaticLookupMode static_mode, 1782 PrivateLookupMode private_mode) const { 1783 return InstanceKlass::find_method_impl(methods(), 1784 name, 1785 signature, 1786 overpass_mode, 1787 static_mode, 1788 private_mode); 1789 } 1790 1791 // find_instance_method looks up the name/signature in the local methods array 1792 // and skips over static methods 1793 Method* InstanceKlass::find_instance_method(const Array<Method*>* methods, 1794 const Symbol* name, 1795 const Symbol* signature, 1796 PrivateLookupMode private_mode) { 1797 Method* const meth = InstanceKlass::find_method_impl(methods, 1798 name, 1799 signature, 1800 OverpassLookupMode::find_overpass, 1801 StaticLookupMode::skip_static, 1802 private_mode); 1803 assert(((meth == NULL) || !meth->is_static()), 1804 "find_instance_method should have skipped statics"); 1805 return meth; 1806 } 1807 1808 // find_instance_method looks up the name/signature in the local methods array 1809 // and skips over static methods 1810 Method* InstanceKlass::find_instance_method(const Symbol* name, 1811 const Symbol* signature, 1812 PrivateLookupMode private_mode) const { 1813 return InstanceKlass::find_instance_method(methods(), name, signature, private_mode); 1814 } 1815 1816 // Find looks up the name/signature in the local methods array 1817 // and filters on the overpass, static and private flags 1818 // This returns the first one found 1819 // note that the local methods array can have up to one overpass, one static 1820 // and one instance (private or not) with the same name/signature 1821 Method* InstanceKlass::find_local_method(const Symbol* name, 1839 Method* InstanceKlass::find_local_method(const Array<Method*>* methods, 1840 const Symbol* name, 1841 const Symbol* signature, 1842 OverpassLookupMode overpass_mode, 1843 StaticLookupMode static_mode, 1844 PrivateLookupMode private_mode) { 1845 return InstanceKlass::find_method_impl(methods, 1846 name, 1847 signature, 1848 overpass_mode, 1849 static_mode, 1850 private_mode); 1851 } 1852 1853 Method* InstanceKlass::find_method(const Array<Method*>* methods, 1854 const Symbol* name, 1855 const Symbol* signature) { 1856 return InstanceKlass::find_method_impl(methods, 1857 name, 1858 signature, 1859 OverpassLookupMode::find_overpass, 1860 StaticLookupMode::find_static, 1861 PrivateLookupMode::find_private); 1862 } 1863 1864 Method* InstanceKlass::find_method_impl(const Array<Method*>* methods, 1865 const Symbol* name, 1866 const Symbol* signature, 1867 OverpassLookupMode overpass_mode, 1868 StaticLookupMode static_mode, 1869 PrivateLookupMode private_mode) { 1870 int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode); 1871 return hit >= 0 ? methods->at(hit): NULL; 1872 } 1873 1874 // true if method matches signature and conforms to skipping_X conditions. 1875 static bool method_matches(const Method* m, 1876 const Symbol* signature, 1877 bool skipping_overpass, 1878 bool skipping_static, 1879 bool skipping_private) { 1880 return ((m->signature() == signature) && 1881 (!skipping_overpass || !m->is_overpass()) && 1884 } 1885 1886 // Used directly for default_methods to find the index into the 1887 // default_vtable_indices, and indirectly by find_method 1888 // find_method_index looks in the local methods array to return the index 1889 // of the matching name/signature. If, overpass methods are being ignored, 1890 // the search continues to find a potential non-overpass match. This capability 1891 // is important during method resolution to prefer a static method, for example, 1892 // over an overpass method. 1893 // There is the possibility in any _method's array to have the same name/signature 1894 // for a static method, an overpass method and a local instance method 1895 // To correctly catch a given method, the search criteria may need 1896 // to explicitly skip the other two. For local instance methods, it 1897 // is often necessary to skip private methods 1898 int InstanceKlass::find_method_index(const Array<Method*>* methods, 1899 const Symbol* name, 1900 const Symbol* signature, 1901 OverpassLookupMode overpass_mode, 1902 StaticLookupMode static_mode, 1903 PrivateLookupMode private_mode) { 1904 const bool skipping_overpass = (overpass_mode == OverpassLookupMode::skip_overpass); 1905 const bool skipping_static = (static_mode == StaticLookupMode::skip_static); 1906 const bool skipping_private = (private_mode == PrivateLookupMode::skip_private); 1907 const int hit = quick_search(methods, name); 1908 if (hit != -1) { 1909 const Method* const m = methods->at(hit); 1910 1911 // Do linear search to find matching signature. First, quick check 1912 // for common case, ignoring overpasses if requested. 1913 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) { 1914 return hit; 1915 } 1916 1917 // search downwards through overloaded methods 1918 int i; 1919 for (i = hit - 1; i >= 0; --i) { 1920 const Method* const m = methods->at(i); 1921 assert(m->is_method(), "must be method"); 1922 if (m->name() != name) { 1923 break; 1924 } 1925 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) { 1926 return i; 1962 while (end < methods->length() && (methods->at(end))->name() == name) ++end; 1963 *end_ptr = end; 1964 return start; 1965 } 1966 return -1; 1967 } 1968 1969 // uncached_lookup_method searches both the local class methods array and all 1970 // superclasses methods arrays, skipping any overpass methods in superclasses, 1971 // and possibly skipping private methods. 1972 Method* InstanceKlass::uncached_lookup_method(const Symbol* name, 1973 const Symbol* signature, 1974 OverpassLookupMode overpass_mode, 1975 PrivateLookupMode private_mode) const { 1976 OverpassLookupMode overpass_local_mode = overpass_mode; 1977 const Klass* klass = this; 1978 while (klass != NULL) { 1979 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name, 1980 signature, 1981 overpass_local_mode, 1982 StaticLookupMode::find_static, 1983 private_mode); 1984 if (method != NULL) { 1985 return method; 1986 } 1987 klass = klass->super(); 1988 overpass_local_mode = OverpassLookupMode::skip_overpass; // Always ignore overpass methods in superclasses 1989 } 1990 return NULL; 1991 } 1992 1993 #ifdef ASSERT 1994 // search through class hierarchy and return true if this class or 1995 // one of the superclasses was redefined 1996 bool InstanceKlass::has_redefined_this_or_super() const { 1997 const Klass* klass = this; 1998 while (klass != NULL) { 1999 if (InstanceKlass::cast(klass)->has_been_redefined()) { 2000 return true; 2001 } 2002 klass = klass->super(); 2003 } 2004 return false; 2005 } 2006 #endif 2007 2008 // lookup a method in the default methods list then in all transitive interfaces 2009 // Do NOT return private or static methods 2010 Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name, 2011 Symbol* signature) const { 2012 Method* m = NULL; 2013 if (default_methods() != NULL) { 2014 m = find_method(default_methods(), name, signature); 2015 } 2016 // Look up interfaces 2017 if (m == NULL) { 2018 m = lookup_method_in_all_interfaces(name, signature, DefaultsLookupMode::find_defaults); 2019 } 2020 return m; 2021 } 2022 2023 // lookup a method in all the interfaces that this class implements 2024 // Do NOT return private or static methods, new in JDK8 which are not externally visible 2025 // They should only be found in the initial InterfaceMethodRef 2026 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name, 2027 Symbol* signature, 2028 DefaultsLookupMode defaults_mode) const { 2029 Array<InstanceKlass*>* all_ifs = transitive_interfaces(); 2030 int num_ifs = all_ifs->length(); 2031 InstanceKlass *ik = NULL; 2032 for (int i = 0; i < num_ifs; i++) { 2033 ik = all_ifs->at(i); 2034 Method* m = ik->lookup_method(name, signature); 2035 if (m != NULL && m->is_public() && !m->is_static() && 2036 ((defaults_mode != DefaultsLookupMode::skip_defaults) || !m->is_default_method())) { 2037 return m; 2038 } 2039 } 2040 return NULL; 2041 } 2042 2043 /* jni_id_for_impl for jfieldIds only */ 2044 JNIid* InstanceKlass::jni_id_for_impl(int offset) { 2045 MutexLocker ml(JfieldIdCreation_lock); 2046 // Retry lookup after we got the lock 2047 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset); 2048 if (probe == NULL) { 2049 // Slow case, allocate new static field identifier 2050 probe = new JNIid(this, offset, jni_ids()); 2051 set_jni_ids(probe); 2052 } 2053 return probe; 2054 } 2055 2056 |