The JNI hack thing could be factored out, but as it's just going to be removed I didn't define a function.