Skip to content

Commit 0bca291

Browse files
author
Roger Riggs
committed
Added IdentityObject.newIdentity()
1 parent 092f642 commit 0bca291

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: src/java.base/share/classes/java/lang/Object.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,8 @@
2727

2828
import jdk.internal.HotSpotIntrinsicCandidate;
2929

30+
import java.util.Objects;
31+
3032
/**
3133
* Class {@code Object} is the root of the class hierarchy.
3234
* Every class has {@code Object} as a superclass. All objects,
@@ -40,6 +42,9 @@ public class Object {
4042

4143
/**
4244
* Constructs a new object.
45+
*
46+
* @apiNote {@link Objects#newIdentity java.util.Objects.newIdentity()}
47+
* should be used instead of {@code new Object()}.
4348
*/
4449
@HotSpotIntrinsicCandidate
4550
public Object() {}

Diff for: src/java.base/share/classes/java/util/Objects.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -436,4 +436,18 @@ int checkFromIndexSize(int fromIndex, int size, int length) {
436436
public static long getObjectSize(Object o) {
437437
return Unsafe.getUnsafe().getObjectSize(o);
438438
}
439+
440+
/**
441+
* Returns a new Object implementing the {@code IdentityObject} interface.
442+
* The object is a unique {@link IdentityObject} suitable for all purposes
443+
* for which {@code new Object{}} was used including synchronization,
444+
* mutexes and unique placeholders.
445+
*
446+
* @return a new Object implementing the IdentityObject interface
447+
* @since Valhalla
448+
*/
449+
public static IdentityObject newIdentity() {
450+
// Return a new instance of an anonymous inner class.
451+
return new IdentityObject() { };
452+
}
439453
}

0 commit comments

Comments
 (0)