RFR: implementation for JEP 334: JVM Constants API
jbvernee
jbvernee at xs4all.nl
Sun May 27 09:57:46 UTC 2018
Hello,
1. After looking around some more, I also noticed that you can make the
return type of `FieldTypeDescriptor::arrayType` in `Class` more
specific:
```
@Override
@SuppressWarnings("unchecked")
public Class<T[]> arrayType() {
return (Class<T[]>) Array.newInstance(this, 0).getClass();
}
```
I don't think I'm wrong in assuming that this always returns
`Class<T[]>`? I imagine that could be very useful.
---
2. It's unfortunate that the return type of `describeConstable` in
`EnumDesc` and `VarhandleDesc` can't be simplified any more currently.
The cause seems to be that `DynamicConstantDesc` implements `Constable`
on it's own, and does so with a less specific type parameter than is
needed for the simplification. I can think of 2 ways to work around
this. The first is to not let `DynamicConstantDesc` inherit from
`Constantable` itself, but leave that up to the subclass, so it can use
a more specific type parameter. For `EnumDesc` for instance that would
be `Constable<EnumDesc<E>>`, then the return type of `describeConstable`
could be `Optional<ConstantDesc<EnumDesc<E>>>`. I'm not sure how
important it is for `DynamicConstantcDesc` to be a sub type of
`Constable` though.
The other solution, which does keep it as a sub type, would be to add an
additional type parameter to `DynamicConstantDesc` for the type with
which to implement `Constable` i.e.:
```
public abstract class DynamicConstantDesc<T, C extends ConstantDesc<?
super T>>
implements ConstantDesc<T>, Constable<C> {
```
Then `EnumDesc` would extend `DynamicConstantDesc<E, EnumDesc<E>>`, and
return `Optional<ConstantDesc<EnumDesc<E>>>` (similarly for
`VarhandleDesc`). Basically `C` is the type implementing
`DynamicConstantDesc` here. The default implementation of
`describeConstable` in `DynamicConstantDesc` would have to be removed
(making it abstract), or change it's implementation to return
`Optional.empty()`, since there is no easy way (afaik) to return
something that satisfies the new return type. So that pretty much forces
subclasses to implement `describeConstable` themselves. Right now, both
subclasses of `DynamicConstantDesc` override it any ways, and with the
help of the factory methods `DynamicConstantDesc::of` an implementation
looks trivial, so that shouldn't be too much of a problem for future
subclasses (?).
It's the question if doing this is worth it for the sake of simpler
return types for the subclasses. I think it is, since the abstract base
is just a template to build classes on top of, but the subclasses and
interfaces are what you end up using mostly (how often do you see
`AbstractList` in code?) and you want their types to be specific and not
some erased version.
(I hope I didn't make any mistakes, I can't build the JDK right now, so
it's mostly been copy-and-pasting code into my own IDE)
Best regards,
Jorn Vernee
More information about the amber-dev
mailing list