JEPs proposed to target JDK 9 (2015/7/16)

David M. Lloyd david.lloyd at redhat.com
Thu Jul 16 22:21:02 UTC 2015


On 07/16/2015 04:56 PM, mark.reinhold at oracle.com wrote:
> The following JEPs have been placed into the "Proposed to Target"
> state by their owners after discussion and review:
>
>    193: Variable Handles
>         http://openjdk.java.net/jeps/193

Given how frequently I expect the VarHandle API will be used (in my 
code, at least), I think it would be nice to require that there be a 
simpler/friendlier API for constructing the things.  In particular, the 
example proposed API usage is particularly unfriendly:

  class Bar {
      static final VarHandle VH_FOO_FIELD_I;

      static {
          try {
              VH_FOO_FIELD_I = MethodHandles.lookup().
                  in(Foo.class).
                  findFieldVarHandle(Foo.class, "i", int.class);
          } catch (Exception e) {
              throw new Error(e);
          }
      }
  }

Ideally, VarHandle construction should be possible on one line, and thus 
should already throw Error directly.  To me, the *worst case* API looks 
something like this:

      private static final VarHandle BLAH = 
MethodHandles.lookup().in(Foo.class).findFieldVarHandle(Foo.class, 
"blah", int.class);

But even that is arguably fairly ridiculous complexity considering that 
we're talking about the ability to update a variable in a way that 
conceptually is not dissimilar to the simple assignments that we have at 
the language level today.  It is my belief that having a "syntax" which 
is highly complex relative to the more traditional operations that these 
are very strongly related to acts to perpetuate a mode of thinking that 
these kinds of operations belong to a different "strata" of engineering, 
which I think is unwarranted.

I do not think that requiring a simpler syntax for realizing VarHandles 
in the initial implementation is an unreasonable additional requirement.

-- 
- DML


More information about the jdk9-dev mailing list