Clarifying record reflective support

forax at univ-mlv.fr forax at univ-mlv.fr
Wed Dec 4 15:53:02 UTC 2019


> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Maurizio Cimadamore" <maurizio.cimadamore at oracle.com>, "amber-spec-experts"
> <amber-spec-experts at openjdk.java.net>, "joe darcy" <joe.darcy at oracle.com>
> Envoyé: Mercredi 4 Décembre 2019 15:46:01
> Objet: Re: Clarifying record reflective support

>> I pretty sure there is an item in Effective Java saying that It's bad practice
>> to have a method that return null instead of an empty array.

> This is true, but remember where this method lives — java.lang.Class. All the
> other methods return null here.

methods that returns an array in java.lang.Class: 
getAnnotatedInterfaces(), getAnnotations(), getAnnotationsByType(), getClasses(), getConstructors(), getDeclaredAnnotations(), getDeclaredAnnotationsBytType(), getDeclaredClasses(), getDeclaredConstructors(), getDeclaredFields(), getDeclaredMethods(), getEnumConstants(), getFields(), getGenericInterfaces(), getInterfaces(), getMethods(), getNestMembers(), getSigners(), getTypeParameters(). 

Two of them can return null, getEnumConstants() and getSigners(). 

> So if we were to do the new one “right”, now the poor users are in an _even
> worse_ situation — some of the methods do the wrong thing — but not all of
> them, and so the user has to keep it in their head to remember which is which!

You are already describing the current situation, we already have methods that returns null (wrong), 
but not all of them so why add a new one ? 

> The lesser of evils here is to follow the convention established by previous
> reflection methods, especially — as Maurizio pointed out — the name of the
> method screams “don’t call me if you’re not a record.”

Like getEnumConstants(), this is an argument i can agree upon. 

But during the scanning of the methods of java.lang.Class, i've discovered that describeConstable() already returns an Optional. 

So i vote for returning an Optional. I'm sure there is a quote from you somewhere saying that Optional is better than null. 

regards, 
Rémi 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20191204/977e361e/attachment.html>


More information about the amber-spec-experts mailing list