timob / jnigi

Golang Java JNI library
BSD 2-Clause "Simplified" License
163 stars 44 forks source link

Adding GetJavaVM method #29

Closed jairobjunior closed 4 years ago

jairobjunior commented 4 years ago

When Golang project is used as a library and there is multi-threading involved, I need to get the JavaVM from the current JNIEnv and attach it to the Java thread.

Example:

var env *jnigi.Env
var jvm *jnigi.JVM

func Java_mypackage_mymethod(jniEnv unsafe.Pointer, clazz uintptr) {
    env = jnigi.WrapEnv(jniEnv)
    javaVM, err := env.GetJavaVM()
    if err == nil {        
        jvm = javaVM
    }
}

//Considering this callback is getting called from a goroutine
func callback() {
    var nenv = jvm.AttachCurrentThread()
    nenv.CallStaticMethod(...)
    jvm.DetachCurrentThread()
}
timob commented 4 years ago

Looks good, i've added a test for this. Can you allow changes to the branch in this PR?

Github docs how to do this. https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork

To allow me to push my changes to the PR?

jairobjunior commented 4 years ago

@timob I just checked and the checkmark is already checked to "allow edits by maintainers".

Is there anything else I'd need to do to give you permission to the branch?

timob commented 4 years ago

Your right i already had access. I don't need any other permissions.

Looks good, thanks for the change.