def start(self, *jvmoptions, **jvmargs): ignoreUnrecognized = jvmargs.get("ignoreUnrecognized", True) try: pjvm = jni.obj(jni.POINTER(jni.JavaVM)) penv = jni.obj(jni.POINTER(jni.JNIEnv)) jvm_args = jni.obj(jni.JavaVMInitArgs) jvm_args.version = JVM.JNI_VERSION jvm_args.nOptions = len(jvmoptions) jvm_args.options = joptions = jni.new_array( jni.JavaVMOption, jvm_args.nOptions) _keep = [] for i, option in enumerate(jvmoptions): optionString = jni.new_cstr(option if isinstance( option, bytes) else str(option).encode("utf-8")) _keep.append(optionString) jvm_args.options[i].optionString = optionString jvm_args.options[i].extraInfo = jni.NULL jvm_args.ignoreUnrecognized = jni.JNI_TRUE if ignoreUnrecognized else jni.JNI_FALSE err = self._JNI.CreateJavaVM(pjvm, penv, jvm_args) del _keep, joptions, jvm_args if err != jni.JNI_OK or jni.isNULL(pjvm): raise jni.JNIException( err if err != jni.JNI_OK else jni.JNI_ERR, info="JNI_CreateJavaVM") self._jnijvm = jni.JVM(pjvm) return self._jnijvm, jni.JEnv(penv) except Exception as exc: try: self.handleException(exc) finally: self._jnijvm = None
def __init__(self, thr: jni.Throwable): self._jobj = jni.obj(jni.jthrowable) self.__jinfo = jni.obj(jni.jstring) _, jenv = self.jvm # try: jthr = thr.getCause() jinfo = thr.getInfo() self._jobj = jni.cast( jenv.NewGlobalRef(jthr) if jthr else 0, jni.jthrowable) self.__jinfo = jni.cast( jenv.NewGlobalRef(jinfo) if jinfo else 0, jni.jstring)
def start(self, *jvmoptions, **jvmargs): # -> Tuple['_JVM', jni.JNIEnv]: jvmoptions = tuple([ "-Djava.class.path=" + os.pathsep.join([ item.partition("=")[2] for item in jvmoptions if item.lstrip().startswith("-Djava.class.path=") ] + [str(path) for path in INTERNAL_CLASSPATHS]) ] + [ item for item in jvmoptions if not item.lstrip().startswith("-Djava.class.path=") ]) ignoreUnrecognized = jvmargs.get("ignoreUnrecognized", True) try: pjvm = jni.obj(jni.POINTER(jni.JavaVM)) penv = jni.obj(jni.POINTER(jni.JNIEnv)) jvm_args = jni.obj(jni.JavaVMInitArgs) jvm_args.version = JVM.JNI_VERSION jvm_args.nOptions = len(jvmoptions) jvm_args.options = joptions = jni.new_array( jni.JavaVMOption, jvm_args.nOptions) _keep = [] for i, option in enumerate(jvmoptions): optionString = jni.new_cstr(option if isinstance( option, bytes) else str(option).encode("utf-8")) _keep.append(optionString) jvm_args.options[i].optionString = optionString jvm_args.options[i].extraInfo = jni.NULL jvm_args.ignoreUnrecognized = jni.JNI_TRUE if ignoreUnrecognized else jni.JNI_FALSE err = self._jvm.JNI.CreateJavaVM(pjvm, penv, jvm_args) del _keep, joptions, jvm_args if err != jni.JNI_OK or jni.isNULL(pjvm): raise jni.JNIException( err if err != jni.JNI_OK else jni.JNI_ERR, info="JNI_CreateJavaVM") self._jvm.jnijvm = jni.JVM(pjvm) jenv = jni.JEnv(penv) try: self._jvm._initialize(jenv) except Exception as exc: try: self._jvm.jnijvm.DestroyJavaVM() except Exception: pass raise exc return self._jvm, jenv except Exception as exc: try: self.handleException(exc) finally: self._jvm.jnijvm = None
def __init__(self, jenv: jni.JNIEnv=None, jstr: jni.jobject=jni.obj(jni.POINTER(jni.jchar)), own: bool = True): self.__jstr = jni.cast(jstr, jni.jstring) self.__size = 0 self.__jchars = jni.obj(jni.POINTER(jni.jchar)) if jenv is not None and jstr: length = jenv.GetStringLength(self.__jstr) jchars = jenv.GetStringChars(self.__jstr) try: self.__jchars = jni.new_array(jni.jchar, length + 1) jni.memmove(self.__jchars, jchars, length * jni.sizeof(jni.jchar)) self.__jchars[length] = "\0" finally: jenv.ReleaseStringChars(self.__jstr, jchars) self.__size = length
def getDoubleBuffer(self) -> Tuple: with self.jvm as (jvm, jenv): is_copy = jni.obj(jni.jboolean) return JArrayBuffer( jenv.GetDoubleArrayElements(self._jobj, is_copy), jni.sizeof(jni.jdouble), b"d", bool(is_copy))
def getFloatBuffer(self) -> Tuple: with self.jvm as (jvm, jenv): is_copy = jni.obj(jni.jboolean) return JArrayBuffer( jenv.GetFloatArrayElements(self._jobj, is_copy), jni.sizeof(jni.jfloat), b"f", bool(is_copy))
def attach(self, pjvm: Optional[object] = None): # -> Tuple['_JVM', jni.JNIEnv]: if_bind = pjvm is not None try: if if_bind and not pjvm: raise JVMException(EStatusCode.EINVAL, "First paramter must be a JNI jvm handle") penv = jni.obj(jni.POINTER(jni.JNIEnv)) if if_bind: self._jvm.jnijvm = jni.cast(pjvm, jni.POINTER(jni.JavaVM))[0] self._jvm.jnijvm.AttachCurrentThread(penv) else: self._jvm.jnijvm.GetEnv(penv, JVM.JNI_VERSION) jenv = jni.JEnv(penv) self._jvm._initialize(jenv) return self._jvm, jenv except Exception as exc: try: self.handleException(exc) finally: if if_bind: self._jvm.jnijvm = None
def __init__(self, interfaces: Tuple['JClass', ...]): self.__interfaces = interfaces self._jitf_array = jni.obj(jni.jobjectArray) with self.jvm as (jvm, jenv), JFrame(jenv, 1): itf_arr = jenv.NewObjectArray(len(self.__interfaces), jvm.Class.Class) for i, jitf in enumerate(self.__interfaces): jenv.SetObjectArrayElement(itf_arr, i, jitf.handle) self._jitf_array = jni.cast(jenv.NewGlobalRef(itf_arr), jni.jobjectArray)
def attachThread(self, daemon: bool = False): try: penv = jni.obj(jni.POINTER(jni.JNIEnv)) if not daemon: self._jvm.jnijvm.AttachCurrentThread(penv) else: self._jvm.jnijvm.AttachCurrentThreadAsDaemon(penv) return self._jvm, jni.JEnv(penv) except Exception as exc: self.handleException(exc)
def __enter__(self): if self._jvm is None: raise JVMException( EStatusCode.EDETACHED, "Unable to use JVM: thread detached from the VM") if self._jvm.jnijvm: penv = jni.obj(jni.POINTER(jni.JNIEnv)) self._jvm.jnijvm.AttachCurrentThread(penv) return self._jvm, jni.JEnv(penv) else: return self._jvm, None
def shutdown(self): if self._jnijvm is None: return try: penv = jni.obj(jni.POINTER(jni.JNIEnv)) self._jnijvm.AttachCurrentThread(penv) self._jnijvm.DestroyJavaVM() except Exception as exc: try: self.handleException(exc) finally: self._jnijvm = None
def isThreadAttached(self) -> bool: try: penv = jni.obj(jni.POINTER(jni.JNIEnv)) self._jvm.jnijvm.GetEnv(penv, JVM.JNI_VERSION) except jni.JNIException as exc: if exc.getError() == jni.JNI_EDETACHED: return False self.handleException(exc) except Exception as exc: self.handleException(exc) else: return not jni.isNULL(penv)
def __init__(self, jenv: Optional[jni.JNIEnv] = None): if jenv is None: from .jconstants import EStatusCode from .jvm import JVMException raise JVMException(EStatusCode.EDETACHED, "Unable to use JVM: thread detached from the VM") pjvm = jni.obj(jni.POINTER(jni.JavaVM)) jenv.GetJavaVM(pjvm) from .jvm import _JVM jvm = _JVM() jvm.jnijvm = jni.JVM(pjvm) self.jvm = jvm # jvm._JVM self.jenv = jenv # jni.JNIEnv
def __init__(self, size: int, own: bool = True): self._own = own self.__jvalues = (jni.new_array(jni.jvalue, size) if size > 0 else jni.obj(jni.POINTER(jni.jvalue))) self.__jtypes = [EJavaType.VOID] * max(size, 0)
def getLongBuffer(self) -> Tuple: with self.jvm as (jvm, jenv): is_copy = jni.obj(jni.jboolean) return JArrayBuffer(jenv.GetLongArrayElements(self._jobj, is_copy), jni.sizeof(jni.jlong), b"q", bool(is_copy))
def getIntBuffer(self) -> Tuple: """???.""" with self.jvm as (jvm, jenv): is_copy = jni.obj(jni.jboolean) return JArrayBuffer(jenv.GetIntArrayElements(self._jobj, is_copy), jni.sizeof(jni.jint), b"i", bool(is_copy))