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 <bzz@apache.org>
This commit is contained in:
Alexander Bezzubov 2018-10-21 15:40:25 +02:00
parent 115a7bdc64
commit 5147de90b8
No known key found for this signature in database
GPG Key ID: 8039F5787EFCD05D
2 changed files with 10 additions and 8 deletions

View File

@ -35,6 +35,7 @@ pgpSecretRing := baseDirectory.value / "project" / ".gnupg" / "secring.gpg"
pgpPublicRing := baseDirectory.value / "project" / ".gnupg" / "pubring.gpg" pgpPublicRing := baseDirectory.value / "project" / ".gnupg" / "pubring.gpg"
pgpPassphrase := Some(SONATYPE_PASSPHRASE.toArray) pgpPassphrase := Some(SONATYPE_PASSPHRASE.toArray)
libraryDependencies += "com.nativelibs4java" % "jnaerator-runtime" % "0.12"
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
unmanagedBase := baseDirectory.value / "lib" unmanagedBase := baseDirectory.value / "lib"

View File

@ -3,13 +3,14 @@ package tech.sourced.enry;
import com.sun.jna.Memory; import com.sun.jna.Memory;
import com.sun.jna.Pointer; import com.sun.jna.Pointer;
import tech.sourced.enry.nativelib.GoSlice; 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; import java.io.UnsupportedEncodingException;
class GoUtils { class GoUtils {
static GoString.ByValue toGoString(String str) { static _GoString_.ByValue toGoString(String str) {
byte[] bytes; byte[] bytes;
try { try {
bytes = str.getBytes("utf-8"); bytes = str.getBytes("utf-8");
@ -26,19 +27,19 @@ class GoUtils {
ptr = ptrFromBytes(bytes); ptr = ptrFromBytes(bytes);
} }
GoString.ByValue val = new GoString.ByValue(); _GoString_.ByValue val = new _GoString_.ByValue();
val.n = length; val.n = new NativeSize(length);
val.p = ptr; val.p = ptr;
return val; return val;
} }
static String toJavaString(GoString str) { static String toJavaString(_GoString_ str) {
if (str.n == 0) { if (str.n.intValue() == 0) {
return ""; return "";
} }
byte[] bytes = new byte[(int) str.n]; byte[] bytes = new byte[(int) str.n.intValue()];
str.p.read(0, bytes, 0, (int) str.n); str.p.read(0, bytes, 0, (int) str.n.intValue());
try { try {
return new String(bytes, "utf-8"); return new String(bytes, "utf-8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {