JEP 370 - text example leads to exception

Chris T tech.meshter at gmail.com
Sat Feb 8 03:23:26 UTC 2020


I tried to build an example on top of this code snippet (from the JEP text):

VarHandle intHandle = MemoryHandles.varHandle(int.class);
VarHandle intElemHandle = MemoryHandles.withStride(intHandle, 4);

try (MemorySegment segment = MemorySegment.allocateNative(100)) {
   MemoryAddress base = segment.baseAddress();
   for (int i = 0 ; i < 25 ; i++) {
        intElemHandle.set(base, (long)i);
   }
}

The first issue was that the API for the first line need to get the
ByteOrder parameter (that's not a big deal). I ended up with this:

    VarHandle intHandle = MemoryHandles.varHandle(int.class, order);
    VarHandle intElemHandle = MemoryHandles.withStride(intHandle, 4);

    try (MemorySegment segment = MemorySegment.allocateNative(100)) {
      MemoryAddress base = segment.baseAddress();
      for (int i = 0; i < 25; i++) {
        // this is the line where the app crashes:
        intElemHandle.set(base, (long) i);
      }
    }

The issue that I have is that the code crashes with:
java.lang.invoke.WrongMethodTypeException: cannot convert
MethodHandle(VarHandle,MemoryAddressProxy,long,int)void to
(VarHandle,MemoryAddress,long)void
at
java.base/java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:880)
at java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:865)
at
java.base/java.lang.invoke.VarHandleGuards.guard_LJ_V(VarHandleGuards.java:184)
at
com.github.kbnt.java14.fma.ForeignMemoryAccessExamples.exampleXXStrides(ForeignMemoryAccessExamples.java:65)
at
com.github.kbnt.java14.fma.ForeignMemoryAccessExamples.main(ForeignMemoryAccessExamples.java:25)

Any idea why this happens (and more importantly how the code can be
changed)?

Thanks!
  Chris T


More information about the core-libs-dev mailing list