bind,cmd: accept Java API in bound packages

Accept Java API interface types as arguments and return values from
bound Go package functions and methods. Also, allow Go structs
to extend Java classes and implement Java interfaces as well as override
and implement methods.

This is the third and final part of the implementation of the golang/go#16876
proposal.

Fixes golang/go#16876

Change-Id: I6951dd87235553ce09abe5117a39a503466163c0
Reviewed-on: https://go-review.googlesource.com/28597
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Elias Naur
2016-09-22 10:16:33 +00:00
parent 08c3b2f4a5
commit bdf873ed8f
42 changed files with 2036 additions and 370 deletions
+27 -8
View File
@@ -67,16 +67,24 @@ public class Seq {
tracker.incRefnum(refnum);
}
// incRef increments the reference count of Java objects.
// For proxies for Go objects, it calls into the Proxy method
// incRefnum() to make sure the Go reference count is positive
// even if the Proxy is garbage collected and its Ref is finalized.
public static int incRef(Object o) {
return tracker.inc(o);
}
public static int incGoObjectRef(GoObject o) {
return o.incRefnum();
}
public static Ref getRef(int refnum) {
return tracker.get(refnum);
}
// Increment the Go reference count before sending over a refnum.
static native void incGoRef(int refnum);
public static native void incGoRef(int refnum);
// Informs the Go ref tracker that Java is done with this ref.
static native void destroyRef(int refnum);
@@ -86,20 +94,31 @@ public class Seq {
tracker.dec(refnum);
}
// A Proxy is a Java object that proxies a Go object.
public static abstract class Proxy {
// A GoObject is a Java class implemented in Go. When a GoObject
// is passed to Go, it is wrapped in a Go proxy, to make it behave
// the same as passing a regular Java class.
public interface GoObject {
// Increment refcount and return the refnum of the proxy.
//
// The Go reference count need to be bumped while the
// refnum is passed to Go, to avoid finalizing and
// invalidating it before being translated on the Go side.
int incRefnum();
}
// A Proxy is a Java object that proxies a Go object. Proxies, unlike
// GoObjects, are unwrapped to their Go counterpart when deserialized
// in Go.
public static abstract class Proxy implements GoObject {
private final Ref ref;
protected Proxy(Ref ref) {
this.ref = ref;
}
public final int incRefnum() {
// The Go reference count need to be bumped while the
// refnum is passed to Go, to avoid finalizing and
// invalidating it before being translated on the Go side.
@Override public final int incRefnum() {
int refnum = ref.refnum;
incGoRef(refnum);
Seq.incGoRef(refnum);
return refnum;
}
}