8280168: Add Objects.toIdentityString

Reviewed-by: alanb, mchung, rriggs, smarks
This commit is contained in:
Joe Darcy 2022-01-25 18:15:37 +00:00
parent f4575e4052
commit cbe8395ace
3 changed files with 60 additions and 5 deletions

View File

@ -292,7 +292,7 @@ public class MethodHandleProxies {
private static Object callObjectMethod(Object self, Method m, Object[] args) {
assert(isObjectMethod(m)) : m;
return switch (m.getName()) {
case "toString" -> self.getClass().getName() + "@" + Integer.toHexString(self.hashCode());
case "toString" -> java.util.Objects.toIdentityString(self);
case "hashCode" -> System.identityHashCode(self);
case "equals" -> (self == args[0]);
default -> null;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -164,6 +164,30 @@ public final class Objects {
return (o != null) ? o.toString() : nullDefault;
}
/**
* {@return a string equivalent to the string returned by {@code
* Object.toString} if that method and {@code hashCode} are not
* overridden}
*
* @implNote
* This method constructs a string for an object without calling
* any overridable methods of the object.
*
* @implSpec
* The method returns a string equivalent to:<br>
* {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
*
* @param o an object
* @throws NullPointerException if the argument is null
* @see Object#toString
* @see System#identityHashCode(Object)
* @since 19
*/
public static String toIdentityString(Object o) {
requireNonNull(o);
return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
}
/**
* Returns 0 if the arguments are identical and {@code
* c.compare(a, b)} otherwise.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,9 +23,8 @@
/*
* @test
* @bug 6797535 6889858 6891113 8013712 8011800 8014365
* @bug 6797535 6889858 6891113 8013712 8011800 8014365 8280168
* @summary Basic tests for methods in java.util.Objects
* @author Joseph D. Darcy
*/
import java.util.*;
@ -40,6 +39,7 @@ public class BasicObjectsTest {
errors += testHash();
errors += testToString();
errors += testToString2();
errors += testToIdentityString();
errors += testCompare();
errors += testRequireNonNull();
errors += testIsNull();
@ -134,6 +134,37 @@ public class BasicObjectsTest {
return errors;
}
private static int testToIdentityString() {
int errors = 0;
// Test null behavior
try {
Objects.toIdentityString(null);
errors++;
} catch (NullPointerException npe) {
; // Expected
}
// Behavior on typical objects
Object o = new Object(){};
errors += (Objects.toIdentityString(o).equals(o.toString()))? 0 : 1;
// Verify object's toString *not* called
Object badToString = new Object() {
@Override
public String toString() {
throw new RuntimeException();
}
};
Objects.toIdentityString(badToString);
// Verify object's hashCode *not* called
Object badHashCode = new Object() {
@Override
public int hashCode() {
throw new RuntimeException("0xDEADBEFF");
}
};
Objects.toIdentityString(badHashCode);
return errors;
}
private static int testCompare() {
int errors = 0;
String[] values = {"e. e. cummings", "zzz"};