Compare commits
1 Commits
afef4e65f6
...
0218b56949
Author | SHA1 | Date | |
---|---|---|---|
0218b56949 |
28
jni.go
28
jni.go
|
@ -43,12 +43,21 @@ type (
|
||||||
Value uint64 // All JNI types fit into 64-bits.
|
Value uint64 // All JNI types fit into 64-bits.
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TRUE C.JNI_TRUE
|
||||||
|
FALSE C.JNI_FALSE
|
||||||
|
)
|
||||||
|
|
||||||
|
// JVMFor creates a JVM object, interpreting the given uintptr as a pointer
|
||||||
|
// to a C.JavaVM object.
|
||||||
func JVMFor(jvmPtr uintptr) JVM {
|
func JVMFor(jvmPtr uintptr) JVM {
|
||||||
return JVM{
|
return JVM{
|
||||||
jvm: (*C.JavaVM)(unsafe.Pointer(jvmPtr)),
|
jvm: (*C.JavaVM)(unsafe.Pointer(jvmPtr)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnvFor creates an Env object, interpreting the given uintptr as a pointer
|
||||||
|
// to a C.JNIEnv object.
|
||||||
func EnvFor(envPtr uintptr) Env {
|
func EnvFor(envPtr uintptr) Env {
|
||||||
return Env{
|
return Env{
|
||||||
env: (*C.JNIEnv)(unsafe.Pointer(envPtr)),
|
env: (*C.JNIEnv)(unsafe.Pointer(envPtr)),
|
||||||
|
@ -81,18 +90,22 @@ func varArgs(args []Value) *C.jvalue {
|
||||||
return (*C.jvalue)(unsafe.Pointer(&args[0]))
|
return (*C.jvalue)(unsafe.Pointer(&args[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsSameObject returns true if the two given objects refer to the same
|
||||||
|
// Java object.
|
||||||
func IsSameObject(e Env, ref1, ref2 Object) bool {
|
func IsSameObject(e Env, ref1, ref2 Object) bool {
|
||||||
same := C._jni_IsSameObject(e.env, C.jobject(ref1), C.jobject(ref2))
|
same := C._jni_IsSameObject(e.env, C.jobject(ref1), C.jobject(ref2))
|
||||||
return same == C.JNI_TRUE
|
return same == C.JNI_TRUE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CallStaticIntMethod calls a static method on a Java class, returning an int.
|
||||||
func CallStaticIntMethod(e Env, cls Class, method MethodID, args ...Value) (int, error) {
|
func CallStaticIntMethod(e Env, cls Class, method MethodID, args ...Value) (int, error) {
|
||||||
res := C._jni_CallStaticIntMethodA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
res := C._jni_CallStaticIntMethodA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
||||||
return int(res), exception(e)
|
return int(res), exception(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindClass finds a class with a given name, using the JVM's default
|
// FindClass returns a reference to a Java class with a given name, using the
|
||||||
// class loader.
|
// 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.
|
||||||
func FindClass(e Env, name string) Class {
|
func FindClass(e Env, name string) Class {
|
||||||
mname := C.CString(name)
|
mname := C.CString(name)
|
||||||
defer C.free(unsafe.Pointer(mname))
|
defer C.free(unsafe.Pointer(mname))
|
||||||
|
@ -103,33 +116,40 @@ func FindClass(e Env, name string) Class {
|
||||||
return Class(res)
|
return Class(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewObject creates a new object given a class, initializer methodID, and
|
// NewObject creates a new object given a class, initializer method, and
|
||||||
// arguments (if any).
|
// initializer arguments (if any).
|
||||||
func NewObject(e Env, cls Class, method MethodID, args ...Value) (Object, error) {
|
func NewObject(e Env, cls Class, method MethodID, args ...Value) (Object, error) {
|
||||||
res := C._jni_NewObjectA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
res := C._jni_NewObjectA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
||||||
return Object(res), exception(e)
|
return Object(res), exception(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CallStaticVoidMethod calls a static method on a Java class, returning
|
||||||
|
// nothing.
|
||||||
func CallStaticVoidMethod(e Env, cls Class, method MethodID, args ...Value) error {
|
func CallStaticVoidMethod(e Env, cls Class, method MethodID, args ...Value) error {
|
||||||
C._jni_CallStaticVoidMethodA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
C._jni_CallStaticVoidMethodA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
||||||
return exception(e)
|
return exception(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CallVoidMethod calls a method on an object, returning nothing.
|
||||||
func CallVoidMethod(e Env, obj Object, method MethodID, args ...Value) error {
|
func CallVoidMethod(e Env, obj Object, method MethodID, args ...Value) error {
|
||||||
C._jni_CallVoidMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
|
C._jni_CallVoidMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
|
||||||
return exception(e)
|
return exception(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CallStaticObjectMethod calls a static method on a class, returning a
|
||||||
|
// Java object
|
||||||
func CallStaticObjectMethod(e Env, cls Class, method MethodID, args ...Value) (Object, error) {
|
func CallStaticObjectMethod(e Env, cls Class, method MethodID, args ...Value) (Object, error) {
|
||||||
res := C._jni_CallStaticObjectMethodA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
res := C._jni_CallStaticObjectMethodA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
|
||||||
return Object(res), exception(e)
|
return Object(res), exception(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CallObjectMethod calls a method on an object, returning a Java object.
|
||||||
func CallObjectMethod(e Env, obj Object, method MethodID, args ...Value) (Object, error) {
|
func CallObjectMethod(e Env, obj Object, method MethodID, args ...Value) (Object, error) {
|
||||||
res := C._jni_CallObjectMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
|
res := C._jni_CallObjectMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
|
||||||
return Object(res), exception(e)
|
return Object(res), exception(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CallIntMethod calls a method on an object, returning an int32.
|
||||||
func CallIntMethod(e Env, obj Object, method MethodID, args ...Value) (int32, error) {
|
func CallIntMethod(e Env, obj Object, method MethodID, args ...Value) (int32, error) {
|
||||||
res := C._jni_CallIntMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
|
res := C._jni_CallIntMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
|
||||||
return int32(res), exception(e)
|
return int32(res), exception(e)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user