src/share/vm/oops/instanceKlass.cpp

Print this page




1364   int l = 0;
1365   int h = len - 1;
1366   while (l <= h) {
1367     int mid = (l + h) >> 1;
1368     Method* m = methods->at(mid);
1369     assert(m->is_method(), "must be method");
1370     int res = m->name()->fast_compare(name);
1371     if (res == 0) {
1372       return mid;
1373     } else if (res < 0) {
1374       l = mid + 1;
1375     } else {
1376       h = mid - 1;
1377     }
1378   }
1379   return -1;
1380 }
1381 
1382 // find_method looks up the name/signature in the local methods array
1383 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const {
1384   return find_method_impl(name, signature, find_overpass, find_static);
1385 }
1386 
1387 Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature,
1388                                         OverpassLookupMode overpass_mode, StaticLookupMode static_mode) const {
1389   return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode);


1390 }
1391 
1392 // find_instance_method looks up the name/signature in the local methods array
1393 // and skips over static methods
1394 Method* InstanceKlass::find_instance_method(
1395     Array<Method*>* methods, Symbol* name, Symbol* signature) {
1396   Method* meth = InstanceKlass::find_method_impl(methods, name, signature,
1397                                                  find_overpass, skip_static);
1398   assert(((meth == NULL) || !meth->is_static()), "find_instance_method should have skipped statics");
1399   return meth;
1400 }
1401 
1402 // find_instance_method looks up the name/signature in the local methods array
1403 // and skips over static methods
1404 Method* InstanceKlass::find_instance_method(Symbol* name, Symbol* signature) {
1405     return InstanceKlass::find_instance_method(methods(), name, signature);
1406 }
1407 


























1408 // find_method looks up the name/signature in the local methods array
1409 Method* InstanceKlass::find_method(
1410     Array<Method*>* methods, Symbol* name, Symbol* signature) {
1411   return InstanceKlass::find_method_impl(methods, name, signature, find_overpass, find_static);
1412 }
1413 
1414 Method* InstanceKlass::find_method_impl(
1415     Array<Method*>* methods, Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode, StaticLookupMode static_mode) {
1416   int hit = find_method_index(methods, name, signature, overpass_mode, static_mode);


1417   return hit >= 0 ? methods->at(hit): NULL;
1418 }
1419 
1420 bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static) {
1421     return (m->signature() == signature) &&
1422             (!skipping_overpass || !m->is_overpass()) &&
1423             (!skipping_static || !m->is_static());

1424 }
1425 
1426 // Used directly for default_methods to find the index into the
1427 // default_vtable_indices, and indirectly by find_method
1428 // find_method_index looks in the local methods array to return the index
1429 // of the matching name/signature. If, overpass methods are being ignored,
1430 // the search continues to find a potential non-overpass match.  This capability
1431 // is important during method resolution to prefer a static method, for example,
1432 // over an overpass method.





1433 int InstanceKlass::find_method_index(
1434     Array<Method*>* methods, Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode, StaticLookupMode static_mode) {


1435   bool skipping_overpass = (overpass_mode == skip_overpass);
1436   bool skipping_static = (static_mode == skip_static);

1437   int hit = binary_search(methods, name);
1438   if (hit != -1) {
1439     Method* m = methods->at(hit);
1440 
1441     // Do linear search to find matching signature.  First, quick check
1442     // for common case, ignoring overpasses if requested.
1443     if (method_matches(m, signature, skipping_overpass, skipping_static)) return hit;
1444 
1445     // search downwards through overloaded methods
1446     int i;
1447     for (i = hit - 1; i >= 0; --i) {
1448         Method* m = methods->at(i);
1449         assert(m->is_method(), "must be method");
1450         if (m->name() != name) break;
1451         if (method_matches(m, signature, skipping_overpass, skipping_static)) return i;
1452     }
1453     // search upwards
1454     for (i = hit + 1; i < methods->length(); ++i) {
1455         Method* m = methods->at(i);
1456         assert(m->is_method(), "must be method");
1457         if (m->name() != name) break;
1458         if (method_matches(m, signature, skipping_overpass, skipping_static)) return i;
1459     }
1460     // not found
1461 #ifdef ASSERT
1462     int index = (skipping_overpass || skipping_static) ? -1 : linear_search(methods, name, signature);
1463     assert(index == -1, err_msg("binary search should have found entry %d", index));
1464 #endif
1465   }
1466   return -1;
1467 }
1468 int InstanceKlass::find_method_by_name(Symbol* name, int* end) {
1469   return find_method_by_name(methods(), name, end);
1470 }
1471 
1472 int InstanceKlass::find_method_by_name(
1473     Array<Method*>* methods, Symbol* name, int* end_ptr) {
1474   assert(end_ptr != NULL, "just checking");
1475   int start = binary_search(methods, name);
1476   int end = start + 1;
1477   if (start != -1) {
1478     while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
1479     while (end < methods->length() && (methods->at(end))->name() == name) ++end;
1480     *end_ptr = end;
1481     return start;
1482   }
1483   return -1;
1484 }
1485 
1486 // uncached_lookup_method searches both the local class methods array and all
1487 // superclasses methods arrays, skipping any overpass methods in superclasses.
1488 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
1489   OverpassLookupMode overpass_local_mode = overpass_mode;
1490   Klass* klass = const_cast<InstanceKlass*>(this);
1491   while (klass != NULL) {
1492     Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, overpass_local_mode, find_static);
1493     if (method != NULL) {
1494       return method;
1495     }
1496     klass = InstanceKlass::cast(klass)->super();
1497     overpass_local_mode = skip_overpass;   // Always ignore overpass methods in superclasses
1498   }
1499   return NULL;
1500 }
1501 
1502 #ifdef ASSERT
1503 // search through class hierarchy and return true if this class or
1504 // one of the superclasses was redefined
1505 bool InstanceKlass::has_redefined_this_or_super() const {
1506   const InstanceKlass* klass = this;
1507   while (klass != NULL) {
1508     if (klass->has_been_redefined()) {
1509       return true;
1510     }
1511     klass = InstanceKlass::cast(klass->super());
1512   }




1364   int l = 0;
1365   int h = len - 1;
1366   while (l <= h) {
1367     int mid = (l + h) >> 1;
1368     Method* m = methods->at(mid);
1369     assert(m->is_method(), "must be method");
1370     int res = m->name()->fast_compare(name);
1371     if (res == 0) {
1372       return mid;
1373     } else if (res < 0) {
1374       l = mid + 1;
1375     } else {
1376       h = mid - 1;
1377     }
1378   }
1379   return -1;
1380 }
1381 
1382 // find_method looks up the name/signature in the local methods array
1383 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const {
1384   return find_method_impl(name, signature, find_overpass, find_static, find_private);
1385 }
1386 
1387 Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature,
1388                                         OverpassLookupMode overpass_mode, 
1389                                         StaticLookupMode static_mode,
1390                                         PrivateLookupMode private_mode) const {
1391   return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode);
1392 }
1393 
1394 // find_instance_method looks up the name/signature in the local methods array
1395 // and skips over static methods
1396 Method* InstanceKlass::find_instance_method(
1397     Array<Method*>* methods, Symbol* name, Symbol* signature) {
1398   Method* meth = InstanceKlass::find_method_impl(methods, name, signature,
1399                                                  find_overpass, skip_static, find_private);
1400   assert(((meth == NULL) || !meth->is_static()), "find_instance_method should have skipped statics");
1401   return meth;
1402 }
1403 
1404 // find_instance_method looks up the name/signature in the local methods array
1405 // and skips over static methods
1406 Method* InstanceKlass::find_instance_method(Symbol* name, Symbol* signature) {
1407     return InstanceKlass::find_instance_method(methods(), name, signature);
1408 }
1409 
1410 // Find looks up the name/signature in the local methods array
1411 // and filters on the overpass, static and private flags
1412 // This returns the first one found
1413 // note that the local methods array can have up to one overpass, one static
1414 // and one instance (private or not) with the same name/signature
1415 Method* InstanceKlass::find_local_method(Symbol* name, Symbol* signature,
1416                                         OverpassLookupMode overpass_mode, 
1417                                         StaticLookupMode static_mode,
1418                                         PrivateLookupMode private_mode) const {
1419   return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode);
1420 }
1421 
1422 // Find looks up the name/signature in the local methods array
1423 // and filters on the overpass, static and private flags
1424 // This returns the first one found
1425 // note that the local methods array can have up to one overpass, one static
1426 // and one instance (private or not) with the same name/signature
1427 Method* InstanceKlass::find_local_method(Array<Method*>* methods,
1428                                         Symbol* name, Symbol* signature,
1429                                         OverpassLookupMode overpass_mode, 
1430                                         StaticLookupMode static_mode,
1431                                         PrivateLookupMode private_mode) {
1432   return InstanceKlass::find_method_impl(methods, name, signature, overpass_mode, static_mode, private_mode);
1433 }
1434 
1435 
1436 // find_method looks up the name/signature in the local methods array
1437 Method* InstanceKlass::find_method(
1438     Array<Method*>* methods, Symbol* name, Symbol* signature) {
1439   return InstanceKlass::find_method_impl(methods, name, signature, find_overpass, find_static, find_private);
1440 }
1441 
1442 Method* InstanceKlass::find_method_impl(
1443     Array<Method*>* methods, Symbol* name, Symbol* signature, 
1444     OverpassLookupMode overpass_mode, StaticLookupMode static_mode,
1445     PrivateLookupMode private_mode) {
1446   int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode);
1447   return hit >= 0 ? methods->at(hit): NULL;
1448 }
1449 
1450 bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static, bool skipping_private) {
1451     return  ((m->signature() == signature) &&
1452             (!skipping_overpass || !m->is_overpass()) &&
1453             (!skipping_static || !m->is_static()) &&
1454             (!skipping_private || !m->is_private()));
1455 }
1456 
1457 // Used directly for default_methods to find the index into the
1458 // default_vtable_indices, and indirectly by find_method
1459 // find_method_index looks in the local methods array to return the index
1460 // of the matching name/signature. If, overpass methods are being ignored,
1461 // the search continues to find a potential non-overpass match.  This capability
1462 // is important during method resolution to prefer a static method, for example,
1463 // over an overpass method.
1464 // There is the possibility in any method to have the same name/signature
1465 // for a static method, an overpass method and a local instance method
1466 // To correctly catch a given method, the search criteria may need
1467 // to explicitly skip the other two. For local instance methods, it
1468 // is often necessary to skip private methods 
1469 int InstanceKlass::find_method_index(
1470     Array<Method*>* methods, Symbol* name, Symbol* signature, 
1471     OverpassLookupMode overpass_mode, StaticLookupMode static_mode,
1472     PrivateLookupMode private_mode) {
1473   bool skipping_overpass = (overpass_mode == skip_overpass);
1474   bool skipping_static = (static_mode == skip_static);
1475   bool skipping_private = (private_mode == skip_private);
1476   int hit = binary_search(methods, name);
1477   if (hit != -1) {
1478     Method* m = methods->at(hit);
1479 
1480     // Do linear search to find matching signature.  First, quick check
1481     // for common case, ignoring overpasses if requested.
1482     if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return hit;
1483 
1484     // search downwards through overloaded methods
1485     int i;
1486     for (i = hit - 1; i >= 0; --i) {
1487         Method* m = methods->at(i);
1488         assert(m->is_method(), "must be method");
1489         if (m->name() != name) break;
1490         if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i;
1491     }
1492     // search upwards
1493     for (i = hit + 1; i < methods->length(); ++i) {
1494         Method* m = methods->at(i);
1495         assert(m->is_method(), "must be method");
1496         if (m->name() != name) break;
1497         if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i;
1498     }
1499     // not found
1500 #ifdef ASSERT
1501     int index = (skipping_overpass || skipping_static || skipping_private) ? -1 : linear_search(methods, name, signature);
1502     assert(index == -1, err_msg("binary search should have found entry %d", index));
1503 #endif
1504   }
1505   return -1;
1506 }
1507 int InstanceKlass::find_method_by_name(Symbol* name, int* end) {
1508   return find_method_by_name(methods(), name, end);
1509 }
1510 
1511 int InstanceKlass::find_method_by_name(
1512     Array<Method*>* methods, Symbol* name, int* end_ptr) {
1513   assert(end_ptr != NULL, "just checking");
1514   int start = binary_search(methods, name);
1515   int end = start + 1;
1516   if (start != -1) {
1517     while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
1518     while (end < methods->length() && (methods->at(end))->name() == name) ++end;
1519     *end_ptr = end;
1520     return start;
1521   }
1522   return -1;
1523 }
1524 
1525 // uncached_lookup_method searches both the local class methods array and all
1526 // superclasses methods arrays, skipping any overpass methods in superclasses.
1527 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
1528   OverpassLookupMode overpass_local_mode = overpass_mode;
1529   Klass* klass = const_cast<InstanceKlass*>(this);
1530   while (klass != NULL) {
1531     Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, overpass_local_mode, find_static, find_private);
1532     if (method != NULL) {
1533       return method;
1534     }
1535     klass = InstanceKlass::cast(klass)->super();
1536     overpass_local_mode = skip_overpass;   // Always ignore overpass methods in superclasses
1537   }
1538   return NULL;
1539 }
1540 
1541 #ifdef ASSERT
1542 // search through class hierarchy and return true if this class or
1543 // one of the superclasses was redefined
1544 bool InstanceKlass::has_redefined_this_or_super() const {
1545   const InstanceKlass* klass = this;
1546   while (klass != NULL) {
1547     if (klass->has_been_redefined()) {
1548       return true;
1549     }
1550     klass = InstanceKlass::cast(klass->super());
1551   }