Thanks for pointing out the inconsistencies. I modified the JEP with updated code snippets that compile against the latest API in JDK 14 [*].
The handle created “withStride” requires an additional coordinate the is an offset from the base address. You may find Maurizio’s recent talk at Fosdem 2020 helpful and informative: https://fosdem.org/2020/schedule/event/bytebuffers/ <https://fosdem.org/2020/schedule/event/bytebuffers/> Hth, Paul. [*] I wish there was a way to automate the compile and test of such snippets without duplication. > On Feb 7, 2020, at 7:23 PM, Chris T <tech.mesh...@gmail.com> wrote: > > 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