bind: support casting of Java objects

Generate Cast functions that take a proxy for a Java class or interface,
and return a new proxy with the same reference. The Cast functions
panic if the underlying Java object is not an instance of the expected
type.

Change-Id: I08a5bf9a79139f0fac5dd102c7b028c8c989fc6d
Reviewed-on: https://go-review.googlesource.com/30095
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Elias Naur
2016-10-05 08:40:06 +00:00
parent 69dc8e1c38
commit e76ec53021
7 changed files with 208 additions and 7 deletions
+22
View File
@@ -13,6 +13,7 @@ import java.util.Arrays;
import java.util.Random;
import go.javapkg.Javapkg;
import go.javapkg.I;
import go.javapkg.GoObject;
import go.javapkg.GoRunnable;
import go.javapkg.GoSubset;
@@ -127,4 +128,25 @@ public class ClassesTest extends InstrumentationTestCase {
Integer i = Javapkg.newJavaInteger();
assertEquals("new Integer(42)", 42, i.intValue());
}
private static class InterfaceRunnable implements I, Runnable {
@Override public void run() {
}
}
public void testCast() {
Runnable r1 = new GoRunnable();
Runnable r1c = Javapkg.castRunnable((Object)r1);
assertTrue("Casting Go object", r1c != null);
Runnable r2 = new Runnable() {
@Override public void run() {
}
};
Runnable r2c = Javapkg.castRunnable((Object)r2);
assertTrue("Casting Java object", r2c != null);
Runnable r3c = Javapkg.castInterface(new InterfaceRunnable());
assertTrue("Casting Go interface implementation", r3c != null);
Runnable r4c = Javapkg.castRunnable(new Object());
assertTrue("Invalid cast", r4c == null);
}
}