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

John Rose john.r.rose at oracle.com
Fri Feb 28 20:05:23 UTC 2020


On Feb 28, 2020, at 6:10 AM, Roger Riggs <Roger.Riggs at oracle.com> wrote:
> 
> I agree shorter is better, but I hope no one gets attached to the name of the implementation class.
> It should be forgettable, I even wondered if it should be a hidden class.

A hidden class!  Good idea.  That’s even more anonymous than an AIC.
There are four ways I see to do this.  I’d be happy with any one, but I
prefer a way that doesn’t have the attractive nuisance of a stable name
that users may come to rely on, like AbstractList$SubList.

public static Object newIdentity1() {
    return new Object() { };  // Object$0
}

public static Object newIdentity2() {
    class Id { }
    return new Id();  // Object$0$Id
}

private static class Id { }
public static Object newIdentity3() {
    return new Id();  // Object$Id
}

private static final Class<?> HC_Id;
static {
    HC_Id = //defineHiddenClass...
            null;  //for argument sake
}
public static Object newIdentity4() {
    try {
        return HC_Id.newInstance();  // Object$Id
    } catch (ReflectiveOperationException ex) {
        throw new AssertionError(ex);
    }
}

> Also, Object$Identity is too strong a name for this purpose. (and Brian has another purpose for that name.)
> 
> How about either Object$Id or Object$Instance?

I also thought of Object$Id.  Short and sweet.

I also like Brian’s commandeering of Object.Identity as an interface name!

— John



More information about the valhalla-dev mailing list