Compare commits
10
Commits
4a3b173acb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b2c1e0034 | ||
|
|
d142949d34 | ||
|
|
511df960e4 | ||
|
|
34026c7e22 | ||
|
|
f8a0df591f | ||
|
|
ff2a63c99a | ||
|
|
014cd5c7c4 | ||
|
|
396d6d4dec | ||
|
|
3310c43bcf | ||
|
|
15a239885b |
@@ -15,6 +15,7 @@ __attribute__ ((visibility ("hidden"))) jfieldID _jni_GetFieldID(JNIEnv *env, jc
|
||||
__attribute__ ((visibility ("hidden"))) jfieldID _jni_GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
|
||||
__attribute__ ((visibility ("hidden"))) jsize _jni_GetStringLength(JNIEnv *env, jstring str);
|
||||
__attribute__ ((visibility ("hidden"))) const jchar *_jni_GetStringChars(JNIEnv *env, jstring str);
|
||||
__attribute__ ((visibility ("hidden"))) void _jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars);
|
||||
__attribute__ ((visibility ("hidden"))) jstring _jni_NewString(JNIEnv *env, const jchar *unicodeChars, jsize len);
|
||||
__attribute__ ((visibility ("hidden"))) jboolean _jni_IsSameObject(JNIEnv *env, jobject ref1, jobject ref2);
|
||||
__attribute__ ((visibility ("hidden"))) jboolean _jni_IsInstanceOf(JNIEnv *env, jobject obj, jclass cls);
|
||||
@@ -23,6 +24,7 @@ __attribute__ ((visibility ("hidden"))) void _jni_DeleteGlobalRef(JNIEnv *env, j
|
||||
__attribute__ ((visibility ("hidden"))) jobject _jni_NewLocalRef(JNIEnv *env, jobject obj);
|
||||
__attribute__ ((visibility ("hidden"))) void _jni_DeleteLocalRef(JNIEnv *env, jobject obj);
|
||||
__attribute__ ((visibility ("hidden"))) jint _jni_CallStaticIntMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
|
||||
__attribute__ ((visibility ("hidden"))) jboolean _jni_CallStaticBooleanMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
|
||||
__attribute__ ((visibility ("hidden"))) jobject _jni_CallStaticObjectMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
|
||||
__attribute__ ((visibility ("hidden"))) jobject _jni_NewObjectA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
|
||||
__attribute__ ((visibility ("hidden"))) void _jni_CallStaticVoidMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
|
||||
@@ -37,6 +39,9 @@ __attribute__ ((visibility ("hidden"))) jlong _jni_CallLongMethodA(JNIEnv *env,
|
||||
__attribute__ ((visibility ("hidden"))) jfloat _jni_CallFloatMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
|
||||
__attribute__ ((visibility ("hidden"))) jdouble _jni_CallDoubleMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
|
||||
__attribute__ ((visibility ("hidden"))) jbyteArray _jni_NewByteArray(JNIEnv *env, jsize length);
|
||||
__attribute__ ((visibility ("hidden"))) jobjectArray _jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement);
|
||||
__attribute__ ((visibility ("hidden"))) jobject _jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index);
|
||||
__attribute__ ((visibility ("hidden"))) void _jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value);
|
||||
__attribute__ ((visibility ("hidden"))) jbyte *_jni_GetByteArrayElements(JNIEnv *env, jbyteArray arr);
|
||||
__attribute__ ((visibility ("hidden"))) void _jni_ReleaseByteArrayElements(JNIEnv *env, jbyteArray arr, jbyte *elems, jint mode);
|
||||
__attribute__ ((visibility ("hidden"))) jsize _jni_GetArrayLength(JNIEnv *env, jarray arr);
|
||||
|
||||
@@ -78,6 +78,10 @@ const jchar *_jni_GetStringChars(JNIEnv *env, jstring str) {
|
||||
return (*env)->GetStringChars(env, str, NULL);
|
||||
}
|
||||
|
||||
void _jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars) {
|
||||
(*env)->ReleaseStringChars(env, str, chars);
|
||||
}
|
||||
|
||||
jstring _jni_NewString(JNIEnv *env, const jchar *unicodeChars, jsize len) {
|
||||
return (*env)->NewString(env, unicodeChars, len);
|
||||
}
|
||||
@@ -118,6 +122,10 @@ jint _jni_CallStaticIntMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue
|
||||
return (*env)->CallStaticIntMethodA(env, cls, method, args);
|
||||
}
|
||||
|
||||
jboolean _jni_CallStaticBooleanMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args) {
|
||||
return (*env)->CallStaticBooleanMethodA(env, cls, method, args);
|
||||
}
|
||||
|
||||
jobject _jni_CallStaticObjectMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args) {
|
||||
return (*env)->CallStaticObjectMethodA(env, cls, method, args);
|
||||
}
|
||||
@@ -178,6 +186,18 @@ jsize _jni_GetArrayLength(JNIEnv *env, jarray arr) {
|
||||
return (*env)->GetArrayLength(env, arr);
|
||||
}
|
||||
|
||||
jobjectArray _jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement) {
|
||||
return (*env)->NewObjectArray(env, length, elementClass, initialElement);
|
||||
}
|
||||
|
||||
jobject _jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index) {
|
||||
return (*env)->GetObjectArrayElement(env, array, index);
|
||||
}
|
||||
|
||||
void _jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value) {
|
||||
return (*env)->SetObjectArrayElement(env, array, index, value);
|
||||
}
|
||||
|
||||
jobject _jni_GetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID) {
|
||||
return (*env)->GetStaticObjectField(env, clazz, fieldID);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,8 @@ type (
|
||||
FieldID C.jfieldID
|
||||
String C.jstring
|
||||
ByteArray C.jbyteArray
|
||||
ObjectArray C.jobjectArray
|
||||
Size C.jsize
|
||||
Value uint64 // All JNI types fit into 64-bits.
|
||||
)
|
||||
|
||||
@@ -102,6 +104,12 @@ func CallStaticIntMethod(e Env, cls Class, method MethodID, args ...Value) (int,
|
||||
return int(res), exception(e)
|
||||
}
|
||||
|
||||
// CallStaticBooleanMethod calls a static method on a Java class, returning an int.
|
||||
func CallStaticBooleanMethod(e Env, cls Class, method MethodID, args ...Value) (bool, error) {
|
||||
res := C._jni_CallStaticBooleanMethodA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
||||
return res == TRUE, exception(e)
|
||||
}
|
||||
|
||||
// FindClass returns a reference to a Java class with a given name, using the
|
||||
// JVM's default class loader. Any exceptions caused by the underlying JNI call
|
||||
// (for example if the class is not found) will result in a panic.
|
||||
@@ -221,6 +229,28 @@ func NewByteArray(e Env, content []byte) ByteArray {
|
||||
return ByteArray(jarr)
|
||||
}
|
||||
|
||||
func NewObjectArray(e Env, len Size, class Class, elem Object) ObjectArray {
|
||||
jarr := C._jni_NewObjectArray(e.env, C.jsize(len), C.jclass(class), C.jobject(elem))
|
||||
if jarr == 0 {
|
||||
panic(fmt.Errorf("jni: NewObjectArray failed"))
|
||||
}
|
||||
return ObjectArray(jarr)
|
||||
}
|
||||
|
||||
func GetObjectArrayElement(e Env, jarr ObjectArray, index Size) (Object, error) {
|
||||
jobj := C._jni_GetObjectArrayElement(e.env, C.jobjectArray(jarr), C.jsize(index))
|
||||
return Object(jobj), exception(e)
|
||||
}
|
||||
|
||||
func SetObjectArrayElement(e Env, jarr ObjectArray, index Size, value Object) error {
|
||||
C._jni_SetObjectArrayElement(
|
||||
e.env,
|
||||
C.jobjectArray(jarr),
|
||||
C.jsize(index),
|
||||
C.jobject(value))
|
||||
return exception(e)
|
||||
}
|
||||
|
||||
// ClassLoader returns a reference to the Java ClassLoader associated
|
||||
// with obj.
|
||||
func ClassLoaderFor(e Env, obj Object) Object {
|
||||
@@ -381,6 +411,10 @@ func GoString(e Env, str String) string {
|
||||
}
|
||||
strlen := C._jni_GetStringLength(e.env, C.jstring(str))
|
||||
chars := C._jni_GetStringChars(e.env, C.jstring(str))
|
||||
if chars == nil {
|
||||
return ""
|
||||
}
|
||||
defer C._jni_ReleaseStringChars(e.env, C.jstring(str), chars)
|
||||
var utf16Chars []uint16
|
||||
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&utf16Chars))
|
||||
hdr.Data = uintptr(unsafe.Pointer(chars))
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@
|
||||
package jni
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -I/usr/lib/jvm/java-11-openjdk-amd64/include -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux
|
||||
#cgo LDFLAGS: -L/usr/lib/jvm/java-11-openjdk-amd64/lib/server -ljvm
|
||||
#cgo CFLAGS: -I/usr/lib/jvm/java-17-openjdk-amd64/include -I/usr/lib/jvm/java-17-openjdk-amd64/include/linux
|
||||
#cgo LDFLAGS: -L/usr/lib/jvm/java-17-openjdk-amd64/lib/server -ljvm
|
||||
*/
|
||||
import "C"
|
||||
|
||||
+77
-3
@@ -152,7 +152,7 @@ func TestStaticMethod(t *testing.T) {
|
||||
}()
|
||||
Do(vm, func(env Env) error {
|
||||
cls := FindClass(env, "test/AClass")
|
||||
GetStaticMethodID(env, cls, "noSuchMethod","()V")
|
||||
GetStaticMethodID(env, cls, "noSuchMethod", "()V")
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -198,6 +198,44 @@ func TestStaticObjectMethod(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticBooleanMethod(t *testing.T) {
|
||||
err := Do(vm, func(env Env) error {
|
||||
cls := FindClass(env, "test/AClass")
|
||||
mid := GetStaticMethodID(env, cls, "GetStaticBoolean", "()Z")
|
||||
if mid == nil {
|
||||
t.Errorf("MethodID is nil")
|
||||
}
|
||||
setterMid := GetStaticMethodID(env, cls, "SetStaticBoolean", "(Z)V")
|
||||
if setterMid == nil {
|
||||
t.Errorf("Setter MethodID is nil")
|
||||
}
|
||||
res, err := CallStaticBooleanMethod(env, cls, mid)
|
||||
if err != nil {
|
||||
t.Errorf("Method invocation failed")
|
||||
}
|
||||
if !res {
|
||||
t.Errorf("Method returned %v, not expected value of %v.", res, true)
|
||||
}
|
||||
if err := CallStaticVoidMethod(env, cls, setterMid, FALSE); err != nil {
|
||||
t.Errorf("Setter invocation failed")
|
||||
}
|
||||
res, err = CallStaticBooleanMethod(env, cls, mid)
|
||||
if err != nil {
|
||||
t.Errorf("Method invocation failed")
|
||||
}
|
||||
if res {
|
||||
t.Errorf("Method returned %v, not expected value of %v.", res, false)
|
||||
}
|
||||
if err := CallStaticVoidMethod(env, cls, setterMid, TRUE); err != nil {
|
||||
t.Errorf("Setter invocation failed")
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("Error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMethod(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
@@ -208,7 +246,7 @@ func TestMethod(t *testing.T) {
|
||||
}()
|
||||
Do(vm, func(env Env) error {
|
||||
cls := FindClass(env, "test/AClass")
|
||||
GetMethodID(env, cls, "noSuchMethod","()V")
|
||||
GetMethodID(env, cls, "noSuchMethod", "()V")
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -791,7 +829,7 @@ func TestStaticField2(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestByteArray(t *testing.T) {
|
||||
arr := []byte{'a','b','c'}
|
||||
arr := []byte{'a', 'b', 'c'}
|
||||
Do(vm, func(env Env) error {
|
||||
jarr := NewByteArray(env, arr)
|
||||
arr2 := GetByteArrayElements(env, jarr)
|
||||
@@ -805,6 +843,42 @@ func TestByteArray(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestObjectArray(t *testing.T) {
|
||||
Do(vm, func(env Env) error {
|
||||
cls := FindClass(env, "java/lang/Object")
|
||||
if cls == 0 {
|
||||
t.Errorf("Class is nil")
|
||||
}
|
||||
|
||||
strings := []string{
|
||||
"item1",
|
||||
"item2",
|
||||
}
|
||||
|
||||
arr := NewObjectArray(env, Size(len(strings)), cls, 0)
|
||||
for i, s := range strings {
|
||||
jstring := JavaString(env, s)
|
||||
err := SetObjectArrayElement(env, arr, Size(i), Object(jstring))
|
||||
if err != nil {
|
||||
t.Errorf("SetObjectArrayElement at index %d raised exception", i)
|
||||
}
|
||||
}
|
||||
|
||||
for i, s := range strings {
|
||||
r, err := GetObjectArrayElement(env, arr, Size(i))
|
||||
if err != nil {
|
||||
t.Errorf("GetObjectArrayElement at index %d raised exception", i)
|
||||
}
|
||||
|
||||
retrieved := GoString(env, String(r))
|
||||
if s != retrieved {
|
||||
t.Errorf("Expected item %d to be '%s' but instead was '%s'", i, s, retrieved)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func TestClassLoaderFor(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
JAVA_HOME=$(dirname $(readlink -f $(which java) | sed 's^jre/bin/^^'))
|
||||
if [ -z ${JAVA_HOME+x} ]
|
||||
then
|
||||
# JAVA_HOME not set. Figure out what to use for it.
|
||||
JAVA_HOME=$(dirname $(readlink -f $(which java) | sed 's^jre/bin/^^'))
|
||||
# Handle the case where java is being run from the jdk itself, not from within the jre
|
||||
JAVA_HOME=$(echo $JAVA_HOME | sed 's^/bin^^')
|
||||
fi
|
||||
|
||||
echo // +build linux > jni_linux.tmp
|
||||
echo // +build linux,!android > jni_linux.tmp
|
||||
echo //Generated by scripts/find_linux_jnih >> jni_linux.tmp
|
||||
echo >> jni_linux.tmp
|
||||
echo package jni >> jni_linux.tmp
|
||||
|
||||
Binary file not shown.
+5
-1
@@ -47,7 +47,7 @@ public class AClass {
|
||||
private float pfl = (float)2.2;
|
||||
private double pdb = 1001.1001;
|
||||
|
||||
public static Boolean GetStaticBoolean() {
|
||||
public static boolean GetStaticBoolean() {
|
||||
return staticbl;
|
||||
}
|
||||
|
||||
@@ -75,6 +75,10 @@ public class AClass {
|
||||
return staticobj;
|
||||
}
|
||||
|
||||
public static void SetStaticBoolean(boolean val) {
|
||||
staticbl = val;
|
||||
}
|
||||
|
||||
public void IncInt() {
|
||||
in = in + 1;
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user