Compare commits

..

No commits in common. "4a3b173acb9fbd39bc22dd26074d0c02c29bc40e" and "b33150047fd17a3549afe877fe79330026f5e722" have entirely different histories.

7 changed files with 125 additions and 594 deletions

103
jni.go
View File

@ -93,7 +93,7 @@ func varArgs(args []Value) *C.jvalue {
// Java object. // 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 == TRUE return same == C.JNI_TRUE
} }
// CallStaticIntMethod calls a static method on a Java class, returning an int. // CallStaticIntMethod calls a static method on a Java class, returning an int.
@ -270,8 +270,11 @@ func GetObjectClass(e Env, obj Object) Class {
if obj == 0 { if obj == 0 {
panic("null object") panic("null object")
} }
// GetObjectClass does not throw any exceptions
cls := C._jni_GetObjectClass(e.env, C.jobject(obj)) cls := C._jni_GetObjectClass(e.env, C.jobject(obj))
if err := exception(e); err != nil {
// GetObjectClass should never fail.
panic(err)
}
return Class(cls) return Class(cls)
} }
@ -284,9 +287,11 @@ func IsInstanceOf(e Env, obj Object, cls Class) bool {
if cls == 0 { if cls == 0 {
panic("null class") panic("null class")
} }
// Note: does not throw any exceptions
res := C._jni_IsInstanceOf(e.env, C.jobject(obj), C.jclass(cls)) res := C._jni_IsInstanceOf(e.env, C.jobject(obj), C.jclass(cls))
return res == TRUE if err := exception(e); err != nil {
panic(err)
}
return res == C.JNI_TRUE
} }
// GetStaticMethodID returns the id for a static method. It panics if the method // GetStaticMethodID returns the id for a static method. It panics if the method
@ -391,127 +396,181 @@ func GoString(e Env, str String) string {
} }
// GetStaticObjectField looks up the value of a static field of type Object. // GetStaticObjectField looks up the value of a static field of type Object.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticObjectField(env Env, clazz Class, fieldID FieldID) Object { func GetStaticObjectField(env Env, clazz Class, fieldID FieldID) Object {
value := C._jni_GetStaticObjectField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticObjectField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return Object(value) return Object(value)
} }
// GetStaticBooleanField looks up the value of a static field of type boolean. // GetStaticBooleanField looks up the value of a static field of type boolean.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticBooleanField(env Env, clazz Class, fieldID FieldID) bool { func GetStaticBooleanField(env Env, clazz Class, fieldID FieldID) bool {
value := C._jni_GetStaticBooleanField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticBooleanField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return value != 0 return value != 0
} }
// GetStaticByteField looks up the value of a static field of type byte. // GetStaticByteField looks up the value of a static field of type byte.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticByteField(env Env, clazz Class, fieldID FieldID) byte { func GetStaticByteField(env Env, clazz Class, fieldID FieldID) byte {
value := C._jni_GetStaticByteField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticByteField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return byte(value) return byte(value)
} }
// GetStaticCharField looks up the value of a static field of type char. // GetStaticCharField looks up the value of a static field of type char.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticCharField(env Env, clazz Class, fieldID FieldID) byte { func GetStaticCharField(env Env, clazz Class, fieldID FieldID) byte {
value := C._jni_GetStaticCharField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticCharField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return byte(value) return byte(value)
} }
// GetStaticShortField looks up the value of a static field of type short. // GetStaticShortField looks up the value of a static field of type short.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticShortField(env Env, clazz Class, fieldID FieldID) int16 { func GetStaticShortField(env Env, clazz Class, fieldID FieldID) int16 {
value := C._jni_GetStaticShortField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticShortField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return int16(value) return int16(value)
} }
// GetStaticIntField looks up the value of a static field of type int. // GetStaticIntField looks up the value of a static field of type int.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticIntField(env Env, clazz Class, fieldID FieldID) int32 { func GetStaticIntField(env Env, clazz Class, fieldID FieldID) int32 {
value := C._jni_GetStaticIntField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticIntField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return int32(value) return int32(value)
} }
// GetStaticLongField looks up the value of a static field of type long. // GetStaticLongField looks up the value of a static field of type long.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticLongField(env Env, clazz Class, fieldID FieldID) int64 { func GetStaticLongField(env Env, clazz Class, fieldID FieldID) int64 {
value := C._jni_GetStaticLongField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticLongField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return int64(value) return int64(value)
} }
// GetStaticFloatField looks up the value of a static field of type float. // GetStaticFloatField looks up the value of a static field of type float.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticFloatField(env Env, clazz Class, fieldID FieldID) float32 { func GetStaticFloatField(env Env, clazz Class, fieldID FieldID) float32 {
value := C._jni_GetStaticFloatField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticFloatField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return float32(value) return float32(value)
} }
// GetStaticDoubleField looks up the value of a static field of type double. // GetStaticDoubleField looks up the value of a static field of type double.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetStaticDoubleField(env Env, clazz Class, fieldID FieldID) float64 { func GetStaticDoubleField(env Env, clazz Class, fieldID FieldID) float64 {
value := C._jni_GetStaticDoubleField(env.env, C.jclass(clazz), C.jfieldID(fieldID)) value := C._jni_GetStaticDoubleField(env.env, C.jclass(clazz), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return float64(value) return float64(value)
} }
// GetObjectField looks up the value of a static field of type Object. // GetObjectField looks up the value of a static field of type Object.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetObjectField(env Env, obj Object, fieldID FieldID) Object { func GetObjectField(env Env, obj Object, fieldID FieldID) Object {
value := C._jni_GetObjectField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetObjectField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return Object(value) return Object(value)
} }
// GetBooleanField looks up the value of a static field of type boolean. // GetBooleanField looks up the value of a static field of type boolean.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetBooleanField(env Env, obj Object, fieldID FieldID) bool { func GetBooleanField(env Env, obj Object, fieldID FieldID) bool {
value := C._jni_GetBooleanField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetBooleanField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return value != 0 return value != 0
} }
// GetByteField looks up the value of a static field of type byte. // GetByteField looks up the value of a static field of type byte.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetByteField(env Env, obj Object, fieldID FieldID) byte { func GetByteField(env Env, obj Object, fieldID FieldID) byte {
value := C._jni_GetByteField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetByteField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return byte(value) return byte(value)
} }
// GetCharField looks up the value of a static field of type char. // GetCharField looks up the value of a static field of type char.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetCharField(env Env, obj Object, fieldID FieldID) byte { func GetCharField(env Env, obj Object, fieldID FieldID) byte {
value := C._jni_GetCharField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetCharField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return byte(value) return byte(value)
} }
// GetShortField looks up the value of a static field of type short. // GetShortField looks up the value of a static field of type short.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetShortField(env Env, obj Object, fieldID FieldID) int16 { func GetShortField(env Env, obj Object, fieldID FieldID) int16 {
value := C._jni_GetShortField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetShortField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return int16(value) return int16(value)
} }
// GetIntField looks up the value of a static field of type int. // GetIntField looks up the value of a static field of type int.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetIntField(env Env, obj Object, fieldID FieldID) int32 { func GetIntField(env Env, obj Object, fieldID FieldID) int32 {
value := C._jni_GetIntField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetIntField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return int32(value) return int32(value)
} }
// GetLongField looks up the value of a static field of type long. // GetLongField looks up the value of a static field of type long.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetLongField(env Env, obj Object, fieldID FieldID) int64 { func GetLongField(env Env, obj Object, fieldID FieldID) int64 {
value := C._jni_GetLongField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetLongField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return int64(value) return int64(value)
} }
// GetFloatField looks up the value of a static field of type float. // GetFloatField looks up the value of a static field of type float.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetFloatField(env Env, obj Object, fieldID FieldID) float32 { func GetFloatField(env Env, obj Object, fieldID FieldID) float32 {
value := C._jni_GetFloatField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetFloatField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return float32(value) return float32(value)
} }
// GetDoubleField looks up the value of a static field of type double. // GetDoubleField looks up the value of a static field of type double.
// This should never throw an exception. // It panics if it is unable to find the field.
func GetDoubleField(env Env, obj Object, fieldID FieldID) float64 { func GetDoubleField(env Env, obj Object, fieldID FieldID) float64 {
value := C._jni_GetDoubleField(env.env, C.jobject(obj), C.jfieldID(fieldID)) value := C._jni_GetDoubleField(env.env, C.jobject(obj), C.jfieldID(fieldID))
if err := exception(env); err != nil {
panic(err)
}
return float64(value) return float64(value)
} }

View File

@ -9,13 +9,29 @@ package jni
*/ */
import "C" import "C"
import (
"unsafe"
)
// CreateJavaVM creates a new Java VM with the options specified (if any). // CreateJavaVM creates a new Java VM with the options specified (if any).
// This should not be called more than once as it can result in an error if // This should not be called more than once as it can result in an error if
// a JVM already exists for a given process. // a JVM already exists for a given process.
// //
// This is not implemented on Android and is therefore excluded on that // This is not implemented on Android and is therefore excluded on that
// platform by build flags and C. // platform by build flags and C.
func CreateJavaVM() JVM { func CreateJavaVM(opts ...string) JVM {
jvm := C._jni_CreateJavaVM((**C.char)(nil), 0) nOptions := len(opts)
optstrings := make([]*C.char, nOptions)
for i, _ := range optstrings {
optstrings[i] = C.CString(opts[i])
defer C.free(unsafe.Pointer(optstrings[i]))
}
var jvm *C.JavaVM
if nOptions > 0 {
jvm = C._jni_CreateJavaVM(&optstrings[0], C.int(nOptions))
} else {
jvm = C._jni_CreateJavaVM((**C.char)(nil), 0)
}
return JVM{ jvm: jvm } return JVM{ jvm: jvm }
} }

View File

@ -1,12 +1,11 @@
// +build !android // +build !android
//go:generate javac test/AClass.java test/AClass2.java //go:generate javac test/AClass.java
package jni package jni
import ( import (
"sync" "sync"
"testing" "testing"
"unsafe"
) )
var ( var (
@ -26,17 +25,6 @@ func TestCreateJVM(t *testing.T) {
}) })
} }
func TestDo(t *testing.T) {
// create an invalid JVM
vm2 := JVMFor(uintptr(unsafe.Pointer(vm.jvm)))
err := Do(vm2, func(env Env) error {
return nil
})
if err != nil {
t.Errorf("Do() returned an error")
}
}
func TestFindClass(t *testing.T) { func TestFindClass(t *testing.T) {
err := Do(vm, func(env Env) error { err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass") cls := FindClass(env, "test/AClass")
@ -48,34 +36,6 @@ func TestFindClass(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Error: %s", err) t.Errorf("Error: %s", err)
} }
defer func() {
if r := recover(); r == nil {
t.Errorf("No exception for class not found")
} else {
t.Logf("Panic: %s", r)
}
}()
err = Do(vm, func(env Env) error {
cls := FindClass(env, "test/NoSuchClass")
if cls == 0 {
t.Errorf("Class is nil")
}
return nil
})
}
func TestEnvFor(t *testing.T) {
err := Do(vm, func(env Env) error {
env2 := EnvFor(uintptr(unsafe.Pointer(env.env)))
cls := FindClass(env2, "test/AClass")
if cls == 0 {
t.Errorf("Class is nil")
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
} }
func TestGetMethodID(t *testing.T) { func TestGetMethodID(t *testing.T) {
@ -107,7 +67,6 @@ func TestNewObject(t *testing.T) {
if inst == 0 { if inst == 0 {
t.Errorf("NewGlobalRef returned nil") t.Errorf("NewGlobalRef returned nil")
} }
DeleteGlobalRef(env, inst)
return nil return nil
}) })
if err != nil { if err != nil {
@ -134,7 +93,6 @@ func TestIsSameObject(t *testing.T) {
if !IsSameObject(env, inst, inst2) { if !IsSameObject(env, inst, inst2) {
t.Errorf("New local ref is not the same as original object") t.Errorf("New local ref is not the same as original object")
} }
DeleteLocalRef(env, inst)
return nil return nil
}) })
if err != nil { if err != nil {
@ -142,21 +100,6 @@ func TestIsSameObject(t *testing.T) {
} }
} }
func TestStaticMethod(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("GetStaticMethodID() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
GetStaticMethodID(env, cls, "noSuchMethod","()V")
return nil
})
}
func TestStaticIntMethod(t *testing.T) { func TestStaticIntMethod(t *testing.T) {
err := Do(vm, func(env Env) error { err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass") cls := FindClass(env, "test/AClass")
@ -179,40 +122,6 @@ func TestStaticIntMethod(t *testing.T) {
} }
} }
func TestStaticObjectMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
mid = GetStaticMethodID(env, cls, "GetStaticObject", "()Ljava/lang/Object;")
if mid == nil {
t.Errorf("MethodID is nil")
}
_, err := CallStaticObjectMethod(env, cls, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
return err
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestMethod(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("GetMethodID() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
GetMethodID(env, cls, "noSuchMethod","()V")
return nil
})
}
func TestIntMethod(t *testing.T) { func TestIntMethod(t *testing.T) {
err := Do(vm, func(env Env) error { err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass") cls := FindClass(env, "test/AClass")
@ -226,7 +135,7 @@ func TestIntMethod(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Method invocation failed") t.Errorf("Method invocation failed")
} }
if res != 101 { if res != 0 {
t.Errorf("Method returned %d, not expected value of %d.", res, 0) t.Errorf("Method returned %d, not expected value of %d.", res, 0)
} }
return nil return nil
@ -410,6 +319,14 @@ func TestVoidMethod(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Method invocation failed") t.Errorf("Method invocation failed")
} }
mid = GetMethodID(env, cls, "GetInt", "()I")
res, err := CallIntMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation falied")
}
if res != 1 {
t.Errorf("Method returned %d, not expected value of %d.", res, 1)
}
return nil return nil
}) })
if err != nil { if err != nil {
@ -505,23 +422,6 @@ func TestStaticVoidMethod(t *testing.T) {
} }
func TestGetObjectClass(t *testing.T) { func TestGetObjectClass(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("GetObjectClass() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
err := Do(vm, func(env Env) error {
GetObjectClass(env, Object(unsafe.Pointer(uintptr(0))))
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestGetObjectClass2(t *testing.T) {
err := Do(vm, func(env Env) error { err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass") cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V") mid := GetMethodID(env, cls, "<init>", "()V")
@ -540,135 +440,18 @@ func TestGetObjectClass2(t *testing.T) {
} }
} }
func TestIsInstanceOf(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("IsInstanceOf() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
err := Do(vm, func(env Env) error {
IsInstanceOf(env, Object(0), Class(0))
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestIsInstanceOf2(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("IsInstanceOf() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
IsInstanceOf(env, inst, Class(0))
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestIsInstanceOf3(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
cls = FindClass(env, "java/lang/String")
if IsInstanceOf(env, inst, cls) {
t.Errorf("IsInstanceOf() returned wrong value")
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestField(t *testing.T) { func TestField(t *testing.T) {
err := Do(vm, func(env Env) error { err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass") cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V") mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid) inst, _ := NewObject(env, cls, mid)
fid := GetFieldID(env, cls, "in", "I") fid := GetFieldID(env, cls, "intval", "I")
if fid == nil { if fid == nil {
t.Errorf("FieldID is nil") t.Errorf("FieldID is nil")
} }
r1 := GetIntField(env, inst, fid) res := GetIntField(env, inst, fid)
if r1 != 101 { if res != 0 {
t.Errorf("Method returned %d, not expected value of %d.", r1, 0) t.Errorf("Method returned %d, not expected value of %d.", res, 0)
}
fid = GetFieldID(env, cls, "bl", "Z")
if fid == nil {
t.Errorf("FieldID is nil")
}
r2 := GetBooleanField(env, inst, fid)
if r2 != false {
t.Errorf("Boolean field value is incorrect")
}
fid = GetFieldID(env, cls, "bt", "B")
if fid == nil {
t.Errorf("FieldID is nil")
}
r3 := GetByteField(env, inst, fid)
if r3 != 'c' {
t.Errorf("Byte field value is incorrect")
}
fid = GetFieldID(env, cls, "ch", "C")
if fid == nil {
t.Errorf("FieldID is nil")
}
r4 := GetCharField(env, inst, fid)
if r4 != 'd' {
t.Errorf("Char field value is incorrect")
}
fid = GetFieldID(env, cls, "obj", "Ljava/lang/Object;")
if fid == nil {
t.Errorf("FieldID is nil")
}
r5 := GetObjectField(env, inst, fid)
if r5 == 0 {
t.Errorf("Object field value is incorrect")
}
fid = GetFieldID(env, cls, "sh", "S")
if fid == nil {
t.Errorf("FieldID is nil")
}
r6 := GetShortField(env, inst, fid)
if r6 != 11 {
t.Errorf("Short field value is incorrect")
}
fid = GetFieldID(env, cls, "lo", "J")
if fid == nil {
t.Errorf("FieldID is nil")
}
r7 := GetLongField(env, inst, fid)
if r7 != 1001 {
t.Errorf("Long field value is incorrect")
}
fid = GetFieldID(env, cls, "fl", "F")
if fid == nil {
t.Errorf("FieldID is nil")
}
r8 := GetFloatField(env, inst, fid)
if r8 != 2.2 {
t.Errorf("Float field value is incorrect")
}
fid = GetFieldID(env, cls, "db", "D")
if fid == nil {
t.Errorf("FieldID is nil")
}
r9 := GetDoubleField(env, inst, fid)
if r9 != 1001.1001 {
t.Errorf("Double field value is incorrect")
} }
return nil return nil
}) })
@ -677,95 +460,16 @@ func TestField(t *testing.T) {
} }
} }
func TestField2(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("GetFieldID() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
GetFieldID(env, cls, "noSuchField", "I")
return nil
})
}
func TestStaticField(t *testing.T) { func TestStaticField(t *testing.T) {
err := Do(vm, func(env Env) error { err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass") cls := FindClass(env, "test/AClass")
fid := GetStaticFieldID(env, cls, "staticint", "I") fid := GetStaticFieldID(env, cls, "staticintval", "I")
if fid == nil { if fid == nil {
t.Errorf("FieldID is nil") t.Errorf("FieldID is nil")
} }
r1 := GetStaticIntField(env, cls, fid) res := GetStaticIntField(env, cls, fid)
if r1 != 100 { if res != 100 {
t.Errorf("Method returned %d, not expected value of %d.", r1, 100) t.Errorf("Method returned %d, not expected value of %d.", res, 100)
}
fid = GetStaticFieldID(env, cls, "staticbl", "Z")
if fid == nil {
t.Errorf("FieldID is nil")
}
r2 := GetStaticBooleanField(env, cls, fid)
if r2 != true {
t.Errorf("Boolean field value is incorrect")
}
fid = GetStaticFieldID(env, cls, "staticbt", "B")
if fid == nil {
t.Errorf("FieldID is nil")
}
r3 := GetStaticByteField(env, cls, fid)
if r3 != 'a' {
t.Errorf("Byte field value is incorrect")
}
fid = GetStaticFieldID(env, cls, "staticch", "C")
if fid == nil {
t.Errorf("FieldID is nil")
}
r4 := GetStaticCharField(env, cls, fid)
if r4 != 'b' {
t.Errorf("Char field value is incorrect")
}
fid = GetStaticFieldID(env, cls, "staticobj", "Ljava/lang/Object;")
if fid == nil {
t.Errorf("FieldID is nil")
}
r5 := GetStaticObjectField(env, cls, fid)
if r5 == 0 {
t.Errorf("Object field value is incorrect")
}
fid = GetStaticFieldID(env, cls, "staticsh", "S")
if fid == nil {
t.Errorf("FieldID is nil")
}
r6 := GetStaticShortField(env, cls, fid)
if r6 != 10 {
t.Errorf("Short field value is incorrect")
}
fid = GetStaticFieldID(env, cls, "staticlo", "J")
if fid == nil {
t.Errorf("FieldID is nil")
}
r7 := GetStaticLongField(env, cls, fid)
if r7 != 1000 {
t.Errorf("Long field value is incorrect")
}
fid = GetStaticFieldID(env, cls, "staticfl", "F")
if fid == nil {
t.Errorf("FieldID is nil")
}
r8 := GetStaticFloatField(env, cls, fid)
if r8 != 1.1 {
t.Errorf("Float field value is incorrect")
}
fid = GetStaticFieldID(env, cls, "staticdb", "D")
if fid == nil {
t.Errorf("FieldID is nil")
}
r9 := GetStaticDoubleField(env, cls, fid)
if r9 != 1000.0001 {
t.Errorf("Double field value is incorrect")
} }
return nil return nil
}) })
@ -773,171 +477,3 @@ func TestStaticField(t *testing.T) {
t.Errorf("Error: %s", err) t.Errorf("Error: %s", err)
} }
} }
func TestStaticField2(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("GetStaticField() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
fid := GetStaticFieldID(env, cls, "FieldNotFound", "I")
GetStaticIntField(env, cls, fid)
return nil
})
}
func TestByteArray(t *testing.T) {
arr := []byte{'a','b','c'}
Do(vm, func(env Env) error {
jarr := NewByteArray(env, arr)
arr2 := GetByteArrayElements(env, jarr)
if arr2[0] != 'a' ||
arr2[1] != 'b' ||
arr2[2] != 'c' ||
len(arr2) != 3 {
t.Errorf("GetByteArrayElements() failed")
}
return nil
})
}
func TestClassLoaderFor(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("ClassLoaderFor() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
Do(vm, func(env Env) error {
cls := FindClass(env, "java/lang/Object")
if cls == 0 {
t.Errorf("Class is nil")
}
mid := GetMethodID(env, cls, "<init>", "()V")
if mid == nil {
t.Errorf("Method ID is nil")
}
inst, err := NewObject(env, cls, mid)
if err != nil {
t.Errorf("NewObject raised exception")
}
if inst == 0 {
t.Errorf("NewObject returned nil")
}
ClassLoaderFor(env, inst)
return nil
})
}
func TestClassLoaderFor2(t *testing.T) {
Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
cl := ClassLoaderFor(env, inst)
if cl == 0 {
t.Errorf("ClassLoaderFor() returned nil")
}
return nil
})
}
func TestClassLoaderFor3(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("ClassLoaderFor() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
Do(vm, func(env Env) error {
cls := FindClass(env, "java/lang/Object")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
cl := ClassLoaderFor(env, inst)
if cl == 0 {
t.Errorf("ClassLoaderFor() returned nil")
}
return nil
})
}
func TestClassLoaderFor4(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("ClassLoaderFor() did not panic")
} else {
t.Logf("Panic: %s", r)
}
}()
Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass2")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
cl := ClassLoaderFor(env, inst)
if cl == 0 {
t.Errorf("ClassLoaderFor() returned nil")
}
return nil
})
}
func TestLoadClass(t *testing.T) {
Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
cl := ClassLoaderFor(env, inst)
if cl == 0 {
t.Errorf("ClassLoaderFor() returned nil")
}
cls, err := LoadClass(env, cl, "java.lang.String")
if err != nil {
t.Errorf("LoadClass() returned error: %s", err)
}
if cls == 0 {
t.Errorf("LoadClass() failed")
}
return nil
})
}
func TestLoadClass2(t *testing.T) {
Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
_, err := LoadClass(env, inst, "java.lang.String")
if err == nil {
t.Errorf("LoadClass() did not return an error")
} else {
t.Logf("Error: %s", err)
}
return nil
})
}
func TestJavaString(t *testing.T) {
Do(vm, func(env Env) error {
jstr := JavaString(env, "")
if jstr != 0 {
t.Errorf("String is not nil")
}
gstr := GoString(env, jstr)
if gstr != "" {
t.Errorf("String is not empty")
}
jstr = JavaString(env, "test string")
gstr = GoString(env, jstr)
if gstr != "test string" {
t.Errorf("Strings do not match")
}
return nil
})
}

Binary file not shown.

View File

@ -1,86 +1,24 @@
package test; package test;
import java.lang.Class;
import java.lang.ClassLoader;
import java.lang.Exception;
import java.lang.Object;
import java.lang.String;
public class AClass { public class AClass {
public static Object staticobj = new Object(); public static int staticintval = 100;
public static boolean staticbl = true;
public static byte staticbt = 'a';
public static char staticch = 'b';
public static short staticsh = 10;
public static int staticint = 100;
public static long staticlo = 1000;
public static float staticfl = (float)1.1;
public static double staticdb = 1000.0001;
private static Object pstaticobj = new Object(); public int intval = 0;
private static boolean pstaticbl = true;
private static byte pstaticbt = 'a';
private static char pstaticch = 'b';
private static short pstaticsh = 10;
private static int pstaticint = 100;
private static long pstaticlo = 1000;
private static float pstaticfl = (float)1.1;
private static double pstaticdb = 1000.0001;
public Object obj = new Object();
public boolean bl = false;
public byte bt = 'c';
public char ch = 'd';
public short sh = 11;
public int in = 101;
public long lo = 1001;
public float fl = (float)2.2;
public double db = 1001.1001;
private Object pobj = new Object();
private boolean pbl = false;
private byte pbt = 'c';
private char pch = 'd';
private short psh = 11;
private int pin = 101;
private long plo = 1001;
private float pfl = (float)2.2;
private double pdb = 1001.1001;
public static Boolean GetStaticBoolean() {
return staticbl;
}
public static byte GetStaticByte() {
return staticbt;
}
public static char GetStaticChar() {
return staticch;
}
public static short GetStaticShort() {
return staticsh;
}
public static int GetStaticInt() { public static int GetStaticInt() {
return staticint; return staticintval;
} }
public static void SetStaticInt(int val) { public static void SetStaticInt(int val) {
staticint = val; staticintval = val;
}
public static Object GetStaticObject() {
return staticobj;
} }
public void IncInt() { public void IncInt() {
in = in + 1; intval = intval + 1;
} }
public int GetInt() { public int GetInt() {
return in; return intval;
} }
public boolean GetBoolean() { public boolean GetBoolean() {
@ -112,18 +50,10 @@ public class AClass {
} }
public void SetInt(int val) { public void SetInt(int val) {
in = val; intval = val;
} }
public void AddInts(int val1, int val2) { public void AddInts(int val1, int val2) {
in = val1 + val2; intval = val1 + val2;
}
public Class loadClass(String s) throws Exception {
throw new Exception("AClass: loadClass exception");
}
public ClassLoader getClassLoader() {
return this.getClass().getClassLoader();
} }
} }

Binary file not shown.

View File

@ -1,10 +0,0 @@
package test;
import java.lang.ClassLoader;
import java.lang.Exception;
public class AClass2 {
public ClassLoader getClassLoader() throws Exception {
throw new Exception("Exception");
}
}