From 5147de90b810918cbff11c84fd041175b71beea0 Mon Sep 17 00:00:00 2001 From: Alexander Bezzubov Date: Sun, 21 Oct 2018 15:40:25 +0200 Subject: [PATCH] java: retrofit bindings for JNA generated from Cgo 1.10 Go 1.10 improved the way of passing strings between C and Go with https://go-review.googlesource.com/c/go/+/70890 This change adapts API/helpers to a new convention, without changing existing API surface and without benefiting from a new way of passing strings. Another, perferable on my readind of `go doc cgo`, but more invasive approach would be to change ALL enry C API to always return *C.char \w C.CString() Signed-off-by: Alexander Bezzubov --- java/build.sbt | 1 + .../main/java/tech/sourced/enry/GoUtils.java | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/java/build.sbt b/java/build.sbt index b93d032..201a451 100644 --- a/java/build.sbt +++ b/java/build.sbt @@ -35,6 +35,7 @@ pgpSecretRing := baseDirectory.value / "project" / ".gnupg" / "secring.gpg" pgpPublicRing := baseDirectory.value / "project" / ".gnupg" / "pubring.gpg" pgpPassphrase := Some(SONATYPE_PASSPHRASE.toArray) +libraryDependencies += "com.nativelibs4java" % "jnaerator-runtime" % "0.12" libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test unmanagedBase := baseDirectory.value / "lib" diff --git a/java/src/main/java/tech/sourced/enry/GoUtils.java b/java/src/main/java/tech/sourced/enry/GoUtils.java index 4168aca..54d66d4 100644 --- a/java/src/main/java/tech/sourced/enry/GoUtils.java +++ b/java/src/main/java/tech/sourced/enry/GoUtils.java @@ -3,13 +3,14 @@ package tech.sourced.enry; import com.sun.jna.Memory; import com.sun.jna.Pointer; import tech.sourced.enry.nativelib.GoSlice; -import tech.sourced.enry.nativelib.GoString; +import tech.sourced.enry.nativelib._GoString_; +import com.ochafik.lang.jnaerator.runtime.NativeSize; import java.io.UnsupportedEncodingException; class GoUtils { - static GoString.ByValue toGoString(String str) { + static _GoString_.ByValue toGoString(String str) { byte[] bytes; try { bytes = str.getBytes("utf-8"); @@ -26,19 +27,19 @@ class GoUtils { ptr = ptrFromBytes(bytes); } - GoString.ByValue val = new GoString.ByValue(); - val.n = length; + _GoString_.ByValue val = new _GoString_.ByValue(); + val.n = new NativeSize(length); val.p = ptr; return val; } - static String toJavaString(GoString str) { - if (str.n == 0) { + static String toJavaString(_GoString_ str) { + if (str.n.intValue() == 0) { return ""; } - byte[] bytes = new byte[(int) str.n]; - str.p.read(0, bytes, 0, (int) str.n); + byte[] bytes = new byte[(int) str.n.intValue()]; + str.p.read(0, bytes, 0, (int) str.n.intValue()); try { return new String(bytes, "utf-8"); } catch (UnsupportedEncodingException e) {