// GoString returns a Go string given the Java string. func GoString(env Env, str Object) string { if str.IsNull() { return "" } cString := C.GetStringUTFChars(env.value(), C.jstring(str.value()), nil) defer C.ReleaseStringUTFChars(env.value(), C.jstring(str.value()), cString) return C.GoString(cString) }
// Refcounting is probably needed here, TODO: figure that out... func (self *Environment) utf8() C.jstring { if self._UTF8 == nil { cs := C.CString("UTF8") defer C.free(unsafe.Pointer(cs)) tf8 := C.envNewStringUTF(self.env, cs) self._UTF8 = C.jstring(C.envNewGlobalRef(self.env, tf8)) } return self._UTF8 }
//export Java_io_v_v23_i18n_Catalog_nativeFormatParams func Java_io_v_v23_i18n_Catalog_nativeFormatParams(jenv *C.JNIEnv, jCatalog C.jclass, jFormat C.jstring, jParams C.jobjectArray) C.jstring { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) format := jutil.GoString(env, jutil.Object(uintptr(unsafe.Pointer(jFormat)))) strParams, err := jutil.GoStringArray(env, jutil.Object(uintptr(unsafe.Pointer(jParams)))) if err != nil { jutil.JThrowV(env, err) return nil } params := make([]interface{}, len(strParams)) for i, strParam := range strParams { params[i] = strParam } result := i18n.FormatParams(format, params...) jRet := jutil.JString(env, result) return C.jstring(unsafe.Pointer(jRet)) }
//export Java_io_v_impl_google_rpc_ServerCallImpl_nativeSuffix func Java_io_v_impl_google_rpc_ServerCallImpl_nativeSuffix(jenv *C.JNIEnv, jServerCall C.jobject, goRef C.jlong) C.jstring { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) jSuffix := jutil.JString(env, (*(*rpc.ServerCall)(jutil.GoRefValue(jutil.Ref(goRef)))).Suffix()) return C.jstring(unsafe.Pointer(jSuffix)) }
// C callbacks actually start in the 'generifiedX' calls (see the .c files) // Next, goCallbackNArgs is used to determine the number of variadic paramers to expect ( // java doesn't inform us of this, and we can't force any of the callback parameters. // // Finally, goCallback looks up the 'fId' - our internal function reference ID (the X in generified), // un(re) marshalls all the parameters appropriately, calls our function, and returns // any underlying value back to generified who will return it to the JVM. // The initial jbool indicates success, and any failure should check for exceptions. // //export goCallback func goCallback(envp, obj uintptr, fId int, nargs int, argp uintptr) (ok C.jboolean, val interface{}) { args := C.ArgListPtr(unsafe.Pointer(argp)) env := AllEnvs.Find(envp) if env == nil { panic("Got a nil environment") } if env.jvm == nil { panic("Environment pointer has no JVM") } var cd callbackDescriptor var _ok bool if cd, _ok = env.jvm.registered[fId]; !_ok { print("Unknown callbackId: \t", fId, "\n") return } // TODO: pack argp somehow... if nargs != len(cd.Signature.Params) { panic("callback/signature length mismatch") } inCall := []reflect.Value{reflect.ValueOf(env), reflect.ValueOf(newObject(C.jobject(unsafe.Pointer(obj))))} var err error for i := 0; i < nargs; i++ { switch cd.Signature.Params[i].Kind() { case types.BoolKind: inCall = append(inCall, reflect.ValueOf(bool(0 != C.valBool(C.getArg(args, C.int(i)))))) case types.LongKind: inCall = append(inCall, reflect.ValueOf(int64(C.valLong(C.getArg(args, C.int(i)))))) case types.IntKind: inCall = append(inCall, reflect.ValueOf(int(C.valInt(C.getArg(args, C.int(i)))))) case types.ShortKind: inCall = append(inCall, reflect.ValueOf(int16(C.valShort(C.getArg(args, C.int(i)))))) case types.FloatKind: inCall = append(inCall, reflect.ValueOf(float32(C.valFloat(C.getArg(args, C.int(i)))))) case types.DoubleKind: inCall = append(inCall, reflect.ValueOf(float64(C.valDouble(C.getArg(args, C.int(i)))))) case types.ClassKind: inCall = append(inCall, reflect.ValueOf(newObject(C.valObject(C.getArg(args, C.int(i)))))) default: err = errors.New("Couldn't reflect kind " + cd.Signature.Params[i].Kind().TypeString()) } if err != nil { break } } if err != nil { return } outCall := reflect.ValueOf(cd.F).Call(inCall) switch cd.Signature.Return.Kind() { case types.VoidKind: return 1, nil } switch cd.Signature.Return.Kind() { case types.BoolKind: if outCall[0].Interface().(bool) { return 1, C.jboolean(C.JNI_TRUE) } else { return 1, C.jboolean(C.JNI_FALSE) } case types.ByteKind: return 1, C.jbyte(outCall[0].Interface().(byte)) case types.CharKind: return 1, C.jchar(outCall[0].Interface().(int)) case types.IntKind: return 1, C.jint(outCall[0].Interface().(int)) case types.ShortKind: return 1, C.jshort(outCall[0].Interface().(int16)) case types.LongKind: return 1, C.jint(outCall[0].Interface().(int64)) case types.FloatKind: return 1, C.jfloat(outCall[0].Interface().(float32)) case types.DoubleKind: return 1, C.jdouble(outCall[0].Interface().(float64)) case types.ClassKind: klass := cd.Signature.Return.(types.Class).Klass if klass.Cmp(types.JavaLangString) == 0 { var obj *Object str := outCall[0].Interface().(string) obj, err = env.NewStringObject(str) if err == nil { return 1, C.jstring(obj.object) } // else, exception occurred // not needed as callbacks will reap their own refs. // env.DeleteLocalRef(obj) print("String Error\t", err.Error()) return 0, nil } return 1, C.jobject(outCall[0].Interface().(*Object).object) default: panic("array return type not yet supported") } panic("not reached") }
//export Java_io_v_v23_security_BlessingStoreImpl_nativeToString func Java_io_v_v23_security_BlessingStoreImpl_nativeToString(jenv *C.JNIEnv, jBlessingStoreImpl C.jobject, goRef C.jlong) C.jstring { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) str := fmt.Sprintf("%s", (*(*security.BlessingStore)(jutil.GoRefValue(jutil.Ref(goRef))))) jStr := jutil.JString(env, str) return C.jstring(unsafe.Pointer(jStr)) }
//export Java_io_v_v23_security_BlessingStoreImpl_nativeDebugString func Java_io_v_v23_security_BlessingStoreImpl_nativeDebugString(jenv *C.JNIEnv, jBlessingStoreImpl C.jobject, goRef C.jlong) C.jstring { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) debug := (*(*security.BlessingStore)(jutil.GoRefValue(jutil.Ref(goRef)))).DebugString() jDebug := jutil.JString(env, debug) return C.jstring(unsafe.Pointer(jDebug)) }
//export Java_io_v_v23_security_CallImpl_nativeRemoteEndpoint func Java_io_v_v23_security_CallImpl_nativeRemoteEndpoint(jenv *C.JNIEnv, jCall C.jobject, goRef C.jlong) C.jstring { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) jEndpoint := jutil.JString(env, (*(*security.Call)(jutil.GoRefValue(jutil.Ref(goRef)))).RemoteEndpoint().String()) return C.jstring(unsafe.Pointer(jEndpoint)) }
//export Java_io_v_v23_security_CallImpl_nativeMethod func Java_io_v_v23_security_CallImpl_nativeMethod(jenv *C.JNIEnv, jCall C.jobject, goRef C.jlong) C.jstring { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) method := (*(*security.Call)(jutil.GoRefValue(jutil.Ref(goRef)))).Method() jMethod := jutil.JString(env, jutil.CamelCase(method)) return C.jstring(unsafe.Pointer(jMethod)) }