[concurrency-interest] MethodHandles.lookup().findVarHandle alternative not throwing checked exceptions?

Dávid Karnok akarnokd at gmail.com
Tue Aug 15 10:18:26 EDT 2017


I've been implementing a lot of concurrent code with VarHandles lately and
there is a repeated inconvenience of using


because I can't use it to initialize static fields directly but have to use
a static initialization block and have try-catch around them:

static {
   try {
       UPSTREAM = MethodHandles.lookup().findVarHandle(A.class, "upstream",
   } catch (Throwable ex) {
      throw new InternalError(ex);

Apart from writing the above by hand all the time, there is a slight
annoyance that a well parameterized findVarHandle will never throw
thus the throw
new InternalError remains uncovered by code-coverage.

Factoring out the lookup part doesn't work if the target class and/or field
is non accessible from the point of the common routine, plus I lose the
nice IntelliJ feature of validating the findVarHandle parameters in the IDE

Would it be possible to have some versions of findVarHandle and co in Java
10+ that don't throw checked exceptions?

(Tools such as Lombok may (eventually) help with hiding the VarHandle setup
via annotation, but the generated catch code would still remain uncovered.)

Best regards,
David Karnok
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20170815/9d7efbcc/attachment.html>

More information about the Concurrency-interest mailing list