bind,internal/importers: add Unwrap methods to unwrap Java wrappers

For Java classes implemented in Go, it is useful to take a Java instance
and extract its wrapped Go instance. For example, consider the
java.lang.Runnable implementation wrapping a Go function:

package somepkg

type GoRunnable struct {
    lang.Runnable
    f func()
}

Java methods that take a java.lang.Runnable cannot directly take a
*GoRunnable, so this CL adds a Unwrap method:

import gorun "Java/somepkg/GoRunnable"

...

r := gorun.New()
r.Unwrap().(*GoRunnable).f = func() { ... }
javapkg.Run(r)

The extra interface conversion is unfortunately needed to avoid
import cycles.

Change-Id: Ib775a5712cd25aa75a19d364a55d76b1e11dce77
Reviewed-on: https://go-review.googlesource.com/35295
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Elias Naur
2017-01-18 20:31:42 +00:00
parent a0f998b2d8
commit c243211167
13 changed files with 142 additions and 61 deletions
+6
View File
@@ -18,6 +18,7 @@ import javapkg.GoObject;
import javapkg.GoRunnable;
import javapkg.GoSubset;
import javapkg.GoInputStream;
import javapkg.GoArrayList;
public class ClassesTest extends InstrumentationTestCase {
public void testConst() {
@@ -148,4 +149,9 @@ public class ClassesTest extends InstrumentationTestCase {
Runnable r4c = Javapkg.castRunnable(new Object());
assertTrue("Invalid cast", r4c == null);
}
public void testUnwrap() {
GoArrayList l = new GoArrayList();
Javapkg.unwrapGoArrayList(l);
}
}