Skip to content
Permalink
Browse files
8280168: Add Objects.toIdentityString
Reviewed-by: alanb, mchung, rriggs, smarks
  • Loading branch information
jddarcy committed Jan 25, 2022
1 parent f4575e4 commit cbe8395ace3230dc599c7f082e3524a861b2da8e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
@@ -292,7 +292,7 @@ private static boolean isObjectMethod(Method m) {
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;
@@ -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 static String toString(Object o, String nullDefault) {
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.
@@ -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 static void main(String... args) {
errors += testHash();
errors += testToString();
errors += testToString2();
errors += testToIdentityString();
errors += testCompare();
errors += testRequireNonNull();
errors += testIsNull();
@@ -134,6 +134,37 @@ private static int testToString2() {
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"};

1 comment on commit cbe8395

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.