mirror of
https://github.com/netbirdio/gomobile-tvos-fork.git
synced 2026-05-22 18:43:29 -07:00
bind,cmd/gomobile: add a new generator for Java API wrappers
Using the new Java class analyzer API, scan the bound packages for references to Java classes and interfaces and generate Go wrappers for them. This is the second part of the implementation of proposal golang/go#16876. For golang/go#16876 Change-Id: I59ec0ebdae0081a615dc34d450f344c20c03f871 Reviewed-on: https://go-review.googlesource.com/28596 Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package go;
|
||||
|
||||
import android.test.InstrumentationTestCase;
|
||||
import android.test.MoreAsserts;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import go.javapkg.Javapkg;
|
||||
|
||||
public class ClassesTest extends InstrumentationTestCase {
|
||||
public void testConst() {
|
||||
assertEquals("const Float", Float.MIN_VALUE, Javapkg.floatMin());
|
||||
assertEquals("const String", java.util.jar.JarFile.MANIFEST_NAME, Javapkg.manifestName());
|
||||
assertEquals("const Int", 7, Integer.SIZE, Javapkg.integerBytes());
|
||||
}
|
||||
|
||||
public void testFunction() {
|
||||
Javapkg.systemCurrentTimeMillis();
|
||||
}
|
||||
|
||||
public void testMethod() {
|
||||
try {
|
||||
assertEquals("Integer.decode", 0xff, Javapkg.integerDecode("0xff"));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Exception exc = null;
|
||||
try {
|
||||
Javapkg.integerDecode("obviously wrong");
|
||||
} catch (Exception e) {
|
||||
exc = e;
|
||||
}
|
||||
assertNotNull("IntegerDecode Exception", exc);
|
||||
}
|
||||
|
||||
public void testOverloadedMethod() {
|
||||
try {
|
||||
assertEquals("Integer.parseInt", 0xc4, Javapkg.integerParseInt("c4", 16));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Exception exc = null;
|
||||
try {
|
||||
Javapkg.integerParseInt("wrong", 16);
|
||||
} catch (Exception e) {
|
||||
exc = e;
|
||||
}
|
||||
assertNotNull("integerParseInt Exception", exc);
|
||||
assertEquals("Integer.valueOf", 42, Javapkg.integerValueOf(42));
|
||||
}
|
||||
|
||||
public void testException() {
|
||||
Exception exc = null;
|
||||
try {
|
||||
Javapkg.provokeRuntimeException();
|
||||
} catch (Exception e) {
|
||||
exc = e;
|
||||
}
|
||||
assertNotNull("RuntimeException", exc);
|
||||
}
|
||||
}
|
||||
@@ -54,4 +54,9 @@ extern JNIEnv *go_seq_push_local_frame(jint cap);
|
||||
// Pop the current local frame, releasing all JNI local references in it
|
||||
extern void go_seq_pop_local_frame(JNIEnv *env);
|
||||
|
||||
// Return a global reference to the given class. Return NULL and clear exception if not found.
|
||||
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);
|
||||
|
||||
#endif // __GO_SEQ_HDR__
|
||||
|
||||
@@ -332,6 +332,7 @@ Java_go_Seq_init(JNIEnv *env, jclass clazz) {
|
||||
if (ref_objField == NULL) {
|
||||
LOG_FATAL("failed to find the Seq.Ref.obj field");
|
||||
}
|
||||
initClasses();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
@@ -343,3 +344,35 @@ JNIEXPORT void JNICALL
|
||||
Java_go_Seq_incGoRef(JNIEnv *env, jclass clazz, jint refnum) {
|
||||
IncGoRef(refnum);
|
||||
}
|
||||
|
||||
jclass go_seq_find_class(const char *name) {
|
||||
JNIEnv *env = go_seq_push_local_frame(0);
|
||||
jclass clazz = (*env)->FindClass(env, name);
|
||||
if (clazz == NULL) {
|
||||
(*env)->ExceptionClear(env);
|
||||
} else {
|
||||
clazz = (*env)->NewGlobalRef(env, clazz);
|
||||
}
|
||||
go_seq_pop_local_frame(env);
|
||||
return clazz;
|
||||
}
|
||||
|
||||
jmethodID go_seq_get_static_method_id(jclass clazz, const char *name, const char *sig) {
|
||||
JNIEnv *env = go_seq_push_local_frame(0);
|
||||
jmethodID m = (*env)->GetStaticMethodID(env, clazz, name, sig);
|
||||
if (m == NULL) {
|
||||
(*env)->ExceptionClear(env);
|
||||
}
|
||||
go_seq_pop_local_frame(env);
|
||||
return m;
|
||||
}
|
||||
|
||||
jmethodID go_seq_get_method_id(jclass clazz, const char *name, const char *sig) {
|
||||
JNIEnv *env = go_seq_push_local_frame(0);
|
||||
jmethodID m = (*env)->GetMethodID(env, clazz, name, sig);
|
||||
if (m == NULL) {
|
||||
(*env)->ExceptionClear(env);
|
||||
}
|
||||
go_seq_pop_local_frame(env);
|
||||
return m;
|
||||
}
|
||||
|
||||
@@ -14,8 +14,19 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/mobile/internal/importers/java"
|
||||
)
|
||||
|
||||
func TestClasses(t *testing.T) {
|
||||
if !java.IsAvailable() {
|
||||
t.Skipf("java importer is not available")
|
||||
}
|
||||
runTest(t, []string{
|
||||
"golang.org/x/mobile/bind/testpkg/javapkg",
|
||||
}, "", "ClassesTest")
|
||||
}
|
||||
|
||||
func TestCustomPkg(t *testing.T) {
|
||||
runTest(t, []string{
|
||||
"golang.org/x/mobile/bind/testpkg",
|
||||
|
||||
Reference in New Issue
Block a user