Reputation: 39303
Under what circumstance would someEnum.describeConstable()
fail to return an EnumDesc
?
i.e. it would return an empty Optional
.
Upvotes: 2
Views: 3785
Reputation: 20195
The method Optional<? extends ConstantDesc> describeConstable()
stems from the interface Constable
, not from Enum
. From Constable
's documentation:
...
The nominal form of an instance of a constable type is obtained via
describeConstable()
. AConstable
need not be able to (or may choose not to) describe all its instances in the form of aConstantDesc
; this method returns anOptional
that can be empty to indicate that a nominal descriptor could not be created for an instance. (For example,MethodHandle
will produce nominal descriptors for direct method handles, but not necessarily those produced by method handle combinators.)See The Java Virtual Machine Specification:
4.4 The Constant Pool
4.4.10 The CONSTANT_InvokeDynamic_info Structure...
Upvotes: 6
Reputation: 159165
describeConstable()
is a method inherited from interface Constable
, where the description is:
Returns an
Optional
containing the nominal descriptor for this instance, if one can be constructed, or an emptyOptional
if one cannot be constructed.
Sure, in some classes, e.g. Integer
, it can never be empty:
Returns an
Optional
containing the nominal descriptor for this instance, which is the instance itself.
But the return value is an Optional
because that is not true for all implementations of the interface.
The javadoc of Constable
has this to say:
A Constable need not be able to (or may choose not to) describe all its instances in the form of a
ConstantDesc
; this method returns anOptional
that can be empty to indicate that a nominal descriptor could not be created for an instance. (For example,MethodHandle
will produce nominal descriptors for direct method handles, but not necessarily those produced by method handle combinators.)
Upvotes: 7