Convenience methods for `loadConstant`
David Lloyd
david.lloyd at redhat.com
Thu Aug 29 16:04:35 UTC 2024
Today I noticed 8339217 (`loadConstant` overloads for `int`, `long`, etc.)
come across, which is great (I was just thinking of writing an email to
complain about a lack of this and possibly propose a PR for it).
However I have one other frequent headache around loading constants, and
that is the fact that I am very frequently finding myself loading constants
for `Constable` objects which do not also implement `ConstantDesc`. Thus I
find myself frequently typing the sequence
`.describeConstable().orElseThrow()`. The most common offenders are
`Class`, `MethodType`, and `MethodHandle`.
I find this problematic not only because of the repetitive typing, but also
because if I want to actually provide some consistent exception message I
have to write a utility method, which I would then likely need to replicate
since I use this pattern quite often.
I would like to propose a `loadConstant(Constable)` variant, which throws a
reasonably descriptive exception when the `Constable` cannot be described.
One problem with such a proposal is that if there are overloads for both
`ClassDesc` and `Constable`, then anything implementing both would cause a
compilation issue. So maybe this could be done as
`loadConstable(Constable)` or `loadConstantOrElseThrow(Constable)` to avoid
the overload ambiguity?
If this proposal makes sense in any form, then I could open a bug and
propose a PR, if that is OK.
--
- DML • he/him
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20240829/4ee93484/attachment-0001.htm>
More information about the classfile-api-dev
mailing list