1 /* |
1 /* |
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2015, 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. |
1414 return -1; |
1414 return -1; |
1415 } |
1415 } |
1416 |
1416 |
1417 // find_method looks up the name/signature in the local methods array |
1417 // find_method looks up the name/signature in the local methods array |
1418 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const { |
1418 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const { |
1419 return find_method_impl(name, signature, false); |
1419 return find_method_impl(name, signature, find_overpass, find_static); |
1420 } |
1420 } |
1421 |
1421 |
1422 Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature, bool skipping_overpass) const { |
1422 Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature, |
1423 return InstanceKlass::find_method_impl(methods(), name, signature, skipping_overpass); |
1423 OverpassLookupMode overpass_mode, StaticLookupMode static_mode) const { |
|
1424 return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode); |
1424 } |
1425 } |
1425 |
1426 |
1426 // find_instance_method looks up the name/signature in the local methods array |
1427 // find_instance_method looks up the name/signature in the local methods array |
1427 // and skips over static methods |
1428 // and skips over static methods |
1428 Method* InstanceKlass::find_instance_method( |
1429 Method* InstanceKlass::find_instance_method( |
1429 Array<Method*>* methods, Symbol* name, Symbol* signature) { |
1430 Array<Method*>* methods, Symbol* name, Symbol* signature) { |
1430 Method* meth = InstanceKlass::find_method(methods, name, signature); |
1431 Method* meth = InstanceKlass::find_method_impl(methods, name, signature, |
1431 if (meth != NULL && meth->is_static()) { |
1432 find_overpass, skip_static); |
1432 meth = NULL; |
1433 assert(((meth == NULL) || !meth->is_static()), "find_instance_method should have skipped statics"); |
1433 } |
|
1434 return meth; |
1434 return meth; |
1435 } |
1435 } |
1436 |
1436 |
1437 // find_method looks up the name/signature in the local methods array |
1437 // find_method looks up the name/signature in the local methods array |
1438 Method* InstanceKlass::find_method( |
1438 Method* InstanceKlass::find_method( |
1439 Array<Method*>* methods, Symbol* name, Symbol* signature) { |
1439 Array<Method*>* methods, Symbol* name, Symbol* signature) { |
1440 return InstanceKlass::find_method_impl(methods, name, signature, false); |
1440 return InstanceKlass::find_method_impl(methods, name, signature, find_overpass, find_static); |
1441 } |
1441 } |
1442 |
1442 |
1443 Method* InstanceKlass::find_method_impl( |
1443 Method* InstanceKlass::find_method_impl( |
1444 Array<Method*>* methods, Symbol* name, Symbol* signature, bool skipping_overpass) { |
1444 Array<Method*>* methods, Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode, StaticLookupMode static_mode) { |
1445 int hit = find_method_index(methods, name, signature, skipping_overpass); |
1445 int hit = find_method_index(methods, name, signature, overpass_mode, static_mode); |
1446 return hit >= 0 ? methods->at(hit): NULL; |
1446 return hit >= 0 ? methods->at(hit): NULL; |
1447 } |
1447 } |
1448 |
1448 |
1449 // Used directly for default_methods to find the index into the |
1449 // Used directly for default_methods to find the index into the |
1450 // default_vtable_indices, and indirectly by find_method |
1450 // default_vtable_indices, and indirectly by find_method |
1452 // of the matching name/signature. If, overpass methods are being ignored, |
1452 // of the matching name/signature. If, overpass methods are being ignored, |
1453 // the search continues to find a potential non-overpass match. This capability |
1453 // the search continues to find a potential non-overpass match. This capability |
1454 // is important during method resolution to prefer a static method, for example, |
1454 // is important during method resolution to prefer a static method, for example, |
1455 // over an overpass method. |
1455 // over an overpass method. |
1456 int InstanceKlass::find_method_index( |
1456 int InstanceKlass::find_method_index( |
1457 Array<Method*>* methods, Symbol* name, Symbol* signature, bool skipping_overpass) { |
1457 Array<Method*>* methods, Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode, StaticLookupMode static_mode) { |
|
1458 bool skipping_overpass = (overpass_mode == skip_overpass); |
|
1459 bool skipping_static = (static_mode == skip_static); |
1458 int hit = binary_search(methods, name); |
1460 int hit = binary_search(methods, name); |
1459 if (hit != -1) { |
1461 if (hit != -1) { |
1460 Method* m = methods->at(hit); |
1462 Method* m = methods->at(hit); |
1461 // Do linear search to find matching signature. First, quick check |
1463 // Do linear search to find matching signature. First, quick check |
1462 // for common case, ignoring overpasses if requested. |
1464 // for common case, ignoring overpasses if requested. |
1463 if ((m->signature() == signature) && (!skipping_overpass || !m->is_overpass())) return hit; |
1465 if ((m->signature() == signature) && |
|
1466 (!skipping_overpass || !m->is_overpass()) && |
|
1467 (!skipping_static || !m->is_static())) { |
|
1468 return hit; |
|
1469 } |
1464 |
1470 |
1465 // search downwards through overloaded methods |
1471 // search downwards through overloaded methods |
1466 int i; |
1472 int i; |
1467 for (i = hit - 1; i >= 0; --i) { |
1473 for (i = hit - 1; i >= 0; --i) { |
1468 Method* m = methods->at(i); |
1474 Method* m = methods->at(i); |
1469 assert(m->is_method(), "must be method"); |
1475 assert(m->is_method(), "must be method"); |
1470 if (m->name() != name) break; |
1476 if (m->name() != name) break; |
1471 if ((m->signature() == signature) && (!skipping_overpass || !m->is_overpass())) return i; |
1477 if ((m->signature() == signature) && |
|
1478 (!skipping_overpass || !m->is_overpass()) && |
|
1479 (!skipping_static || !m->is_static())) { |
|
1480 return i; |
|
1481 } |
1472 } |
1482 } |
1473 // search upwards |
1483 // search upwards |
1474 for (i = hit + 1; i < methods->length(); ++i) { |
1484 for (i = hit + 1; i < methods->length(); ++i) { |
1475 Method* m = methods->at(i); |
1485 Method* m = methods->at(i); |
1476 assert(m->is_method(), "must be method"); |
1486 assert(m->is_method(), "must be method"); |
1477 if (m->name() != name) break; |
1487 if (m->name() != name) break; |
1478 if ((m->signature() == signature) && (!skipping_overpass || !m->is_overpass())) return i; |
1488 if ((m->signature() == signature) && |
|
1489 (!skipping_overpass || !m->is_overpass()) && |
|
1490 (!skipping_static || !m->is_static())) { |
|
1491 return i; |
|
1492 } |
1479 } |
1493 } |
1480 // not found |
1494 // not found |
1481 #ifdef ASSERT |
1495 #ifdef ASSERT |
1482 int index = skipping_overpass ? -1 : linear_search(methods, name, signature); |
1496 int index = (skipping_overpass || skipping_static) ? -1 : linear_search(methods, name, signature); |
1483 assert(index == -1, err_msg("binary search should have found entry %d", index)); |
1497 assert(index == -1, err_msg("binary search should have found entry %d", index)); |
1484 #endif |
1498 #endif |
1485 } |
1499 } |
1486 return -1; |
1500 return -1; |
1487 } |
1501 } |
1503 return -1; |
1517 return -1; |
1504 } |
1518 } |
1505 |
1519 |
1506 // uncached_lookup_method searches both the local class methods array and all |
1520 // uncached_lookup_method searches both the local class methods array and all |
1507 // superclasses methods arrays, skipping any overpass methods in superclasses. |
1521 // superclasses methods arrays, skipping any overpass methods in superclasses. |
1508 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, MethodLookupMode mode) const { |
1522 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const { |
1509 MethodLookupMode lookup_mode = mode; |
1523 OverpassLookupMode overpass_local_mode = overpass_mode; |
1510 Klass* klass = const_cast<InstanceKlass*>(this); |
1524 Klass* klass = const_cast<InstanceKlass*>(this); |
1511 while (klass != NULL) { |
1525 while (klass != NULL) { |
1512 Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, (lookup_mode == skip_overpass)); |
1526 Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, overpass_local_mode, find_static); |
1513 if (method != NULL) { |
1527 if (method != NULL) { |
1514 return method; |
1528 return method; |
1515 } |
1529 } |
1516 klass = InstanceKlass::cast(klass)->super(); |
1530 klass = InstanceKlass::cast(klass)->super(); |
1517 lookup_mode = skip_overpass; // Always ignore overpass methods in superclasses |
1531 overpass_local_mode = skip_overpass; // Always ignore overpass methods in superclasses |
1518 } |
1532 } |
1519 return NULL; |
1533 return NULL; |
1520 } |
1534 } |
1521 |
1535 |
1522 #ifdef ASSERT |
1536 #ifdef ASSERT |
1542 if (default_methods() != NULL) { |
1556 if (default_methods() != NULL) { |
1543 m = find_method(default_methods(), name, signature); |
1557 m = find_method(default_methods(), name, signature); |
1544 } |
1558 } |
1545 // Look up interfaces |
1559 // Look up interfaces |
1546 if (m == NULL) { |
1560 if (m == NULL) { |
1547 m = lookup_method_in_all_interfaces(name, signature, normal); |
1561 m = lookup_method_in_all_interfaces(name, signature, find_defaults); |
1548 } |
1562 } |
1549 return m; |
1563 return m; |
1550 } |
1564 } |
1551 |
1565 |
1552 // lookup a method in all the interfaces that this class implements |
1566 // lookup a method in all the interfaces that this class implements |
1553 // Do NOT return private or static methods, new in JDK8 which are not externally visible |
1567 // Do NOT return private or static methods, new in JDK8 which are not externally visible |
1554 // They should only be found in the initial InterfaceMethodRef |
1568 // They should only be found in the initial InterfaceMethodRef |
1555 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name, |
1569 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name, |
1556 Symbol* signature, |
1570 Symbol* signature, |
1557 MethodLookupMode mode) const { |
1571 DefaultsLookupMode defaults_mode) const { |
1558 Array<Klass*>* all_ifs = transitive_interfaces(); |
1572 Array<Klass*>* all_ifs = transitive_interfaces(); |
1559 int num_ifs = all_ifs->length(); |
1573 int num_ifs = all_ifs->length(); |
1560 InstanceKlass *ik = NULL; |
1574 InstanceKlass *ik = NULL; |
1561 for (int i = 0; i < num_ifs; i++) { |
1575 for (int i = 0; i < num_ifs; i++) { |
1562 ik = InstanceKlass::cast(all_ifs->at(i)); |
1576 ik = InstanceKlass::cast(all_ifs->at(i)); |
1563 Method* m = ik->lookup_method(name, signature); |
1577 Method* m = ik->lookup_method(name, signature); |
1564 if (m != NULL && m->is_public() && !m->is_static() && |
1578 if (m != NULL && m->is_public() && !m->is_static() && |
1565 ((mode != skip_defaults) || !m->is_default_method())) { |
1579 ((defaults_mode != skip_defaults) || !m->is_default_method())) { |
1566 return m; |
1580 return m; |
1567 } |
1581 } |
1568 } |
1582 } |
1569 return NULL; |
1583 return NULL; |
1570 } |
1584 } |