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.