mirror of
https://github.com/netbirdio/gomobile-tvos-fork.git
synced 2026-05-22 18:43:29 -07:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,5 +59,6 @@ extern void go_seq_pop_local_frame(JNIEnv *env);
|
||||
extern jclass go_seq_find_class(const char *name);
|
||||
extern jmethodID go_seq_get_static_method_id(jclass clazz, const char *name, const char *sig);
|
||||
extern jmethodID go_seq_get_method_id(jclass clazz, const char *name, const char *sig);
|
||||
extern int go_seq_isinstanceof(jint refnum, jclass clazz);
|
||||
|
||||
#endif // __GO_SEQ_HDR__
|
||||
|
||||
@@ -382,3 +382,11 @@ void go_seq_release_byte_array(JNIEnv *env, jbyteArray arr, jbyte* ptr) {
|
||||
(*env)->ReleaseByteArrayElements(env, arr, ptr, 0);
|
||||
}
|
||||
}
|
||||
|
||||
int go_seq_isinstanceof(jint refnum, jclass clazz) {
|
||||
JNIEnv *env = go_seq_push_local_frame(0);
|
||||
jobject obj = go_seq_from_refnum(env, refnum, NULL, NULL);
|
||||
jboolean isinst = (*env)->IsInstanceOf(env, obj, clazz);
|
||||
go_seq_pop_local_frame(env);
|
||||
return isinst;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user