def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 2): jcls = jenv.FindClass(b"java/lang/Void") TYPE = jenv.GetStaticObjectField( jcls, jenv.GetStaticFieldID(jcls, b"TYPE", b"Ljava/lang/Class;")) self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.TYPE = jni.cast(jenv.NewGlobalRef(TYPE), jni.jclass)
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 2): jcls = jenv.FindClass(b"java/lang/Character") TYPE = jenv.GetStaticObjectField(jcls, jenv.GetStaticFieldID(jcls, b"TYPE", b"Ljava/lang/Class;")) self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.TYPE = jni.cast(jenv.NewGlobalRef(TYPE), jni.jclass) self.Constructor = jenv.GetMethodID (jcls, b"<init>", b"(C)V") self.valueOf = jenv.GetStaticMethodID(jcls, b"valueOf", b"(C)Ljava/lang/Character;") self.charValue = jenv.GetMethodID (jcls, b"charValue", b"()C")
def releaseDoubleBuffer(self, buf: object, mode: Optional[bool] = None): with self.jvm as (jvm, jenv): if mode is None: jenv.ReleaseDoubleArrayElements( self._jobj, jni.cast(buf, jni.POINTER(jni.jdouble))) else: jenv.ReleaseDoubleArrayElements( self._jobj, jni.cast(buf, jni.POINTER(jni.jdouble)), jni.JNI_COMMIT if mode else jni.JNI_ABORT)
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 4): jcls = jenv.FindClass(b"java/io/IOException") self.IOException = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) jcls = jenv.FindClass(b"java/io/EOFException") self.EOFException = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) jcls = jenv.FindClass(b"java/io/IOError") self.IOError = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) jcls = jenv.FindClass(b"java/io/FileNotFoundException") self.FileNotFoundException = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 2): jcls = jenv.FindClass(b"java/lang/Double") TYPE = jenv.GetStaticObjectField(jcls, jenv.GetStaticFieldID(jcls, b"TYPE", b"Ljava/lang/Class;")) self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.TYPE = jni.cast(jenv.NewGlobalRef(TYPE), jni.jclass) self.Constructor = jenv.GetMethodID (jcls, b"<init>", b"(D)V") self.valueOf = jenv.GetStaticMethodID(jcls, b"valueOf", b"(D)Ljava/lang/Double;") self.MIN_VALUE = jenv.GetStaticDoubleField(jcls, jenv.GetStaticFieldID(jcls, b"MIN_VALUE", b"D")) self.MAX_VALUE = jenv.GetStaticDoubleField(jcls, jenv.GetStaticFieldID(jcls, b"MAX_VALUE", b"D"))
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 initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 2): jcls = jenv.FindClass(b"java/lang/Long") TYPE = jenv.GetStaticObjectField(jcls, jenv.GetStaticFieldID(jcls, b"TYPE", b"Ljava/lang/Class;")) self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.TYPE = jni.cast(jenv.NewGlobalRef(TYPE), jni.jclass) self.Constructor = jenv.GetMethodID (jcls, b"<init>", b"(J)V") self.valueOf = jenv.GetStaticMethodID(jcls, b"valueOf", b"(J)Ljava/lang/Long;") self.MIN_VALUE = jenv.GetStaticLongField(jcls, jenv.GetStaticFieldID(jcls, b"MIN_VALUE", b"J")) self.MAX_VALUE = jenv.GetStaticLongField(jcls, jenv.GetStaticFieldID(jcls, b"MAX_VALUE", b"J")) # Some JVM's incorrectly return positive values if self.MIN_VALUE > 0: self.MIN_VALUE = -self.MIN_VALUE
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/lang/StackTraceElement") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.Constructor = jenv.GetMethodID( jcls, b"<init>", b"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V")
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/lang/reflect/Modifier") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.PUBLIC = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"PUBLIC", b"I")) self.PROTECTED = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"PROTECTED", b"I")) self.PRIVATE = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"PRIVATE", b"I")) self.FINAL = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"FINAL", b"I")) self.STATIC = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"STATIC", b"I")) self.ABSTRACT = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"ABSTRACT", b"I")) self.INTERFACE = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"INTERFACE", b"I")) self.NATIVE = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"NATIVE", b"I")) self.STRICT = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"STRICT", b"I")) self.SYNCHRONIZED = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"SYNCHRONIZED", b"I")) self.TRANSIENT = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"TRANSIENT", b"I")) self.VOLATILE = jenv.GetStaticIntField(jcls, jenv.GetStaticFieldID(jcls, b"VOLATILE", b"I")) self.isAbstract = jenv.GetStaticMethodID(jcls, b"isAbstract", b"(I)Z") self.isFinal = jenv.GetStaticMethodID(jcls, b"isFinal", b"(I)Z") self.isInterface = jenv.GetStaticMethodID(jcls, b"isInterface", b"(I)Z") self.isNative = jenv.GetStaticMethodID(jcls, b"isNative", b"(I)Z") self.isPrivate = jenv.GetStaticMethodID(jcls, b"isPrivate", b"(I)Z") self.isProtected = jenv.GetStaticMethodID(jcls, b"isProtected", b"(I)Z") self.isPublic = jenv.GetStaticMethodID(jcls, b"isPublic", b"(I)Z") self.isStatic = jenv.GetStaticMethodID(jcls, b"isStatic", b"(I)Z") self.isStrict = jenv.GetStaticMethodID(jcls, b"isStrict", b"(I)Z") self.isSynchronized = jenv.GetStaticMethodID(jcls, b"isSynchronized", b"(I)Z") self.isTransient = jenv.GetStaticMethodID(jcls, b"isTransient", b"(I)Z") self.isVolatile = jenv.GetStaticMethodID(jcls, b"isVolatile", b"(I)Z") self.toString = jenv.GetStaticMethodID(jcls, b"toString", b"(I)Ljava/lang/String;")
def registerClass(jenv: jni.JNIEnv, class_name: str, class_code, native_methods: Optional[Sequence[Callable]] = None, class_loader=None): if inspect.ismodule(class_code) or inspect.isclass(class_code): if native_methods is None: native_methods = getattr(class_code, "__jnimethods__", ()) class_code = class_code.__javacode__ else: if native_methods is None: native_methods = () jenv.PushLocalFrame(3) try: if class_loader is None: jcls = jenv.FindClass(b"java/lang/ClassLoader") jmid = jenv.GetStaticMethodID(jcls, b"getSystemClassLoader", b"()Ljava/lang/ClassLoader;") class_loader = jenv.CallStaticObjectMethod(jcls, jmid) try: jcls = jenv.FindClass(class_name.replace(".", "/").encode("utf-8")) except Exception: size = len(class_code) jcls = jenv.DefineClass(class_name.replace(".", "/").encode("utf-8"), class_loader, jni.cast(jni.from_buffer(class_code), jni.POINTER(jni.jbyte)), size) methods = jni.new_array(jni.JNINativeMethod, len(native_methods)) for idx, method in enumerate(native_methods): methods[idx] = method jenv.RegisterNatives(jcls, methods, len(methods)) finally: jenv.PopLocalFrame(jni.NULL)
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 setByteSlice(self, start: int, stop: int, step: int, val: Union[Sequence[Union[int, bytes]], bytes, bytearray]): """???.""" with self.jvm as (jvm, jenv): size = JArray.size(start, stop, step) jarr = self._jobj if step == 1 and is_memview(val) and val.itemsize == jni.sizeof(jni.jbyte): val = val.as_ctypes if isinstance(val, memview) else memview(val).as_ctypes jenv.SetByteArrayRegion(jarr, start, size, jni.cast(val, jni.POINTER(jni.jbyte))) else: if is_memview(val): val = val.obj jels = jenv.GetByteArrayElements(jarr) try: if isinstance(val, (bytes, bytearray)): if step == 1 and JArray._jbyte_equiv_byte: if isinstance(val, bytearray): val = jni.from_buffer(val) # print(("RRRRRRRRRRRRRRRRRR", # type(jels), type(jels.contents), type(val))) jni.memmove(jni.byref(jels.contents, start), val, JArray.size(start, stop)) else: for ix, idx in enumerate(range(start, stop, step)): jels[idx] = val[ix] else: for ix, idx in enumerate(range(start, stop, step)): v = val[ix] jels[idx] = v[0] if isinstance(v, bytes) else v jenv.ReleaseByteArrayElements(jarr, jels) except Exception as exc: jenv.ReleaseByteArrayElements(jarr, jels, jni.JNI_ABORT) raise exc
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/io/PrintWriter") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.Constructor = jenv.GetMethodID(jcls, b"<init>", b"(Ljava/io/Writer;)V") self.flush = jenv.GetMethodID(jcls, b"flush", b"()V")
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/beans/BeanInfo") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.getPropertyDescriptors = jenv.GetMethodID( jcls, b"getPropertyDescriptors", b"()[Ljava/beans/PropertyDescriptor;")
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/beans/Introspector") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.getBeanInfo = jenv.GetStaticMethodID( jcls, b"getBeanInfo", b"(Ljava/lang/Class;)Ljava/beans/BeanInfo;")
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/lang/reflect/Constructor") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.getParameterTypes = jenv.GetMethodID(jcls, b"getParameterTypes", b"()[Ljava/lang/Class;") self.getExceptionTypes = jenv.GetMethodID(jcls, b"getExceptionTypes", b"()[Ljava/lang/Class;") self.isVarArgs = jenv.GetMethodID(jcls, b"isVarArgs", b"()Z")
def setCharSlice(self, start: int, stop: int, step: int, val: Union[Sequence[str], str]): with self.jvm as (jvm, jenv): size = JArray.size(start, stop, step) jarr = self._jobj if step == 1 and is_memview(val) and val.itemsize == jni.sizeof( jni.jchar): val = val.as_ctypes if isinstance( val, memview) else memview(val).as_ctypes jenv.SetCharArrayRegion(jarr, start, size, jni.cast(val, jni.POINTER(jni.jchar))) else: if is_memview(val): val = val.obj jels = jenv.GetCharArrayElements(jarr) try: if isinstance( val, str) and step == 1 and JArray._jchar_equiv_unicode: print("UUUUUUUUUUUUUUUUU") jni.memmove( jni.byref(jels.contents, start * jni.sizeof(jni.jchar)), val, JArray.size(start, stop) * jni.sizeof(jni.jchar)) else: for ix, idx in enumerate(range(start, stop, step)): jels[idx] = val[ix] jenv.ReleaseCharArrayElements(jarr, jels) except Exception as exc: jenv.ReleaseCharArrayElements(jarr, jels, jni.JNI_ABORT) raise exc
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/lang/Object") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.getClass = jenv.GetMethodID(jcls, b"getClass", b"()Ljava/lang/Class;") self.hashCode = jenv.GetMethodID(jcls, b"hashCode", b"()I") self.toString = jenv.GetMethodID(jcls, b"toString", b"()Ljava/lang/String;") self.equals = jenv.GetMethodID(jcls, b"equals", b"(Ljava/lang/Object;)Z")
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/lang/String") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.ConstructorFromBytes = jenv.GetMethodID( jcls, b"<init>", b"([BLjava/lang/String;)V") self.getBytes = jenv.GetMethodID(jcls, b"getBytes", b"(Ljava/lang/String;)[B")
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/lang/reflect/Field") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.getType = jenv.GetMethodID(jcls, b"getType", b"()Ljava/lang/Class;") self.isEnumConstant = jenv.GetMethodID(jcls, b"isEnumConstant", b"()Z")
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/lang/reflect/AnnotatedElement") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.getDeclaredAnnotations = jenv.GetMethodID(jcls, b"getDeclaredAnnotations", b"()[Ljava/lang/annotation/Annotation;") self.getAnnotations = jenv.GetMethodID(jcls, b"getAnnotations", b"()[Ljava/lang/annotation/Annotation;") self.getAnnotation = jenv.GetMethodID(jcls, b"getAnnotation", b"(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;") self.isAnnotationPresent = jenv.GetMethodID(jcls, b"isAnnotationPresent", b"(Ljava/lang/Class;)Z")
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 1): jcls = jenv.FindClass(b"java/lang/reflect/Member") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.getName = jenv.GetMethodID(jcls, b"getName", b"()Ljava/lang/String;") self.getModifiers = jenv.GetMethodID(jcls, b"getModifiers", b"()I") self.getDeclaringClass = jenv.GetMethodID(jcls, b"getDeclaringClass", b"()Ljava/lang/Class;") self.isSynthetic = jenv.GetMethodID(jcls, b"isSynthetic", b"()Z")
def __init__(self, jenv: jni.JNIEnv, jobj: jni.jobject, own: bool = True): self._jobj = jni.NULL self._own = own if not jobj: from .jconstants import EStatusCode from .jvm import JVMException raise JVMException(EStatusCode.UNKNOWN, "Allocating null Object") self._jobj = jni.cast(jenv.NewGlobalRef(jobj) if own else jobj, jni.jobject)
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 8): self.BooleanArray1Class = jni.cast(jenv.NewGlobalRef(jenv.FindClass(b"[Z")), jni.jclass) self.CharArray1Class = jni.cast(jenv.NewGlobalRef(jenv.FindClass(b"[C")), jni.jclass) self.ByteArray1Class = jni.cast(jenv.NewGlobalRef(jenv.FindClass(b"[B")), jni.jclass) self.ShortArray1Class = jni.cast(jenv.NewGlobalRef(jenv.FindClass(b"[S")), jni.jclass) self.IntArray1Class = jni.cast(jenv.NewGlobalRef(jenv.FindClass(b"[I")), jni.jclass) self.LongArray1Class = jni.cast(jenv.NewGlobalRef(jenv.FindClass(b"[J")), jni.jclass) self.FloatArray1Class = jni.cast(jenv.NewGlobalRef(jenv.FindClass(b"[F")), jni.jclass) self.DoubleArray1Class = jni.cast(jenv.NewGlobalRef(jenv.FindClass(b"[D")), jni.jclass)
def initialize(self, jenv: jni.JNIEnv): with JFrame(jenv, 7): jcls = jenv.FindClass(b"java/nio/ByteBuffer") self.ByteBufferClass = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.ByteBuffer_order = jenv.GetMethodID( jcls, b"order", b"()Ljava/nio/ByteOrder;") jcls = jenv.FindClass(b"java/nio/ShortBuffer") self.ShortBufferClass = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.ShortBuffer_order = jenv.GetMethodID( jcls, b"order", b"()Ljava/nio/ByteOrder;") jcls = jenv.FindClass(b"java/nio/IntBuffer") self.IntBufferClass = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.IntBuffer_order = jenv.GetMethodID(jcls, b"order", b"()Ljava/nio/ByteOrder;") jcls = jenv.FindClass(b"java/nio/LongBuffer") self.LongBufferClass = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.LongBuffer_order = jenv.GetMethodID( jcls, b"order", b"()Ljava/nio/ByteOrder;") jcls = jenv.FindClass(b"java/nio/FloatBuffer") self.FloatBufferClass = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.FloatBuffer_order = jenv.GetMethodID( jcls, b"order", b"()Ljava/nio/ByteOrder;") jcls = jenv.FindClass(b"java/nio/DoubleBuffer") self.DoubleBufferClass = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.DoubleBuffer_order = jenv.GetMethodID( jcls, b"order", b"()Ljava/nio/ByteOrder;") jcls = jenv.FindClass(b"java/nio/ByteOrder") self.ByteOrderClass = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.ByteOrder_nativeOrder = jenv.GetStaticMethodID( jcls, b"nativeOrder", b"()Ljava/nio/ByteOrder;")
def initialize(self, jenv: jni.JNIEnv): from .org.jt.ref import Reference registerClass(jenv, "org.jt.ref.Reference", Reference) with JFrame(jenv, 1): jcls = jenv.FindClass(b"org/jt/ref/Reference") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.Constructor = jenv.GetMethodID( jcls, b"<init>", b"(Ljava/lang/Object;JLjava/lang/ref/ReferenceQueue;)V")
def initialize(self, jenv: jni.JNIEnv): from .org.jt.reflect import ProxyHandler registerClass(jenv, "org.jt.reflect.ProxyHandler", ProxyHandler) with JFrame(jenv, 1): jcls = jenv.FindClass(b"org/jt/reflect/ProxyHandler") self.Class = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass) self.Constructor = jenv.GetMethodID(jcls, b"<init>", b"(J)V") self.getClass = jenv.GetMethodID(jcls, b"getClass", b"()Ljava/lang/Class;")
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 getExceptionTypes(self) -> Tuple[JClass, ...]: with self.jvm as (jvm, jenv), JFrame(jenv, 1): arg_types = jni.cast(jenv.CallObjectMethod(self._jobj, jvm.Constructor.getExceptionTypes), jni.jobjectArray) jlen = jenv.GetArrayLength(arg_types) with JFrame(jenv, jlen): return tuple(self.jvm.JClass(jenv, jenv.GetObjectArrayElement(arg_types, idx)) for idx in range(jlen))
def getPackages(self) -> Tuple[JPackage, ...]: """Returns all of the Packages defined by this class loader and its ancestors.""" with self.jvm as (jvm, jenv), JFrame(jenv, 1): jarr = jni.cast(jenv.CallObjectMethod(self._jobj, jvm.ClassLoader.getPackages), jni.jobjectArray) jlen = jenv.GetArrayLength(jarr) with JFrame(jenv, jlen): return tuple(self.jvm.JPackage(jenv, jenv.GetObjectArrayElement(jarr, idx)) for idx in range(jlen))