RFR [lworld] 8239929: Object.newIdentity() replacement for new Object()

Roger Riggs Roger.Riggs at oracle.com
Mon Mar 2 20:18:50 UTC 2020


To keep it simple and localized, the factory method is in the 
IdentityObject interface.
The direct replacement is:

         "new Object();" -> "IdentityObject.newIdentity();"

It may be slightly less convenient than Object.newIdentity but will 
avoid bleading
identity based operations into Object.

If/when it is moved to an nested class of object, it won't take much 
editing and will look like:
         "Identity.newIdentity();" // maybe with a specific import

Making the change is mostly smooth but there will be issues with 
bootstrapping in javac and asm.

And there's a runtime error to find/fix also:
Caused by: java.lang.ClassCastException: Cannot cast 
java.lang.IdentityObject$1 to java.lang.invoke.BoundMethodHandle$SpeciesData

Also spotted are uses with Annonymous inner classes:
      new Object() {...};

When deprecated, this will be part of the source incompatibile migration.

BTW, are there any restrictions on AICs being inline classes or does 
that just work?

Patch below or webrev: 
http://cr.openjdk.java.net/~rriggs/webrev-object-newinstance-8239929-1/

Thanks, Roger


diff a/src/java.base/share/classes/java/lang/IdentityObject.java b/src/java.base/share/classes/java/lang/IdentityObject.java
--- a/src/java.base/share/classes/java/lang/IdentityObject.java
+++ b/src/java.base/share/classes/java/lang/IdentityObject.java

@@ -25,9 +25,22 @@
  
  package java.lang;
  
  /**
   * A restricted interface implemented by all identity objects.
- * @since 1.14
+ * @since Valhalla
  */
  public interface IdentityObject {
+    /**
+     * Returns a new Object implementing the {@code IdentityObject} interface.
+     * The object is a unique {@link IdentityObject} suitable for all purposes
+     * for which {@code new Object{}} was used including synchronization,
+     * mutexes and unique placeholders.
+     *
+     * @return a new Object implementing the IdentityObject interface
+     * @since Valhalla
+     */
+    static IdentityObject newIdentity() {
+        // Return a new instance of an anonymous inner class.
+        return new IdentityObject() { };
+    }
  }
diff a/src/java.base/share/classes/java/lang/Object.java b/src/java.base/share/classes/java/lang/Object.java
--- a/src/java.base/share/classes/java/lang/Object.java
+++ b/src/java.base/share/classes/java/lang/Object.java
@@ -38,10 +38,12 @@
   */
  public class Object {
  
      /**
       * Constructs a new object.
+     * @apiNote {@link IdentityObject#newIdentity new IdentityObject.newIdentity()}
+     * should be used instead of {@code new Object()}.
       */
      @HotSpotIntrinsicCandidate
      public Object() {}



More information about the valhalla-dev mailing list