Example #1
0
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)
Example #2
0
 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)
Example #3
0
 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)
Example #4
0
 def initialize(self, jenv: jni.JNIEnv):
     with JFrame(jenv, 2):
         jfcls = jenv.FindClass(b"java/beans/FeatureDescriptor")
         jcls  = jenv.FindClass(b"java/beans/PropertyDescriptor")
         self.Class           = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.hashCode        = jenv.GetMethodID(jcls,  b"hashCode",        b"()I")
         self.toString        = jenv.GetMethodID(jcls,  b"toString",        b"()Ljava/lang/String;")
         self.getName         = jenv.GetMethodID(jfcls, b"getName",         b"()Ljava/lang/String;")
         self.getPropertyType = jenv.GetMethodID(jcls,  b"getPropertyType", b"()Ljava/lang/Class;")
         self.getReadMethod   = jenv.GetMethodID(jcls,  b"getReadMethod",   b"()Ljava/lang/reflect/Method;")
         self.getWriteMethod  = jenv.GetMethodID(jcls,  b"getWriteMethod",  b"()Ljava/lang/reflect/Method;")
         self.setReadMethod   = jenv.GetMethodID(jcls,  b"setReadMethod",   b"(Ljava/lang/reflect/Method;)V")
         self.setWriteMethod  = jenv.GetMethodID(jcls,  b"setWriteMethod",  b"(Ljava/lang/reflect/Method;)V")
Example #5
0
 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;")
Example #6
0
File: jnij.py Project: almanar/jvm
 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")
Example #7
0
File: jnij.py Project: almanar/jvm
 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;")
Example #8
0
File: jnij.py Project: almanar/jvm
 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;")
Example #9
0
File: jnij.py Project: almanar/jvm
 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")
Example #10
0
 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")
Example #11
0
 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")
Example #12
0
 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")
Example #13
0
File: jnij.py Project: almanar/jvm
 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")
Example #14
0
File: jnij.py Project: almanar/jvm
 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)
Example #15
0
File: jnij.py Project: almanar/jvm
 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")
Example #16
0
def unregisterNatives(jenv: jni.JNIEnv, class_name: str):

    jenv.PushLocalFrame(1)
    try:
        jcls = jenv.FindClass(class_name.replace(".", "/").encode("utf-8"))
        jenv.UnregisterNatives(jcls)
    finally:
        jenv.PopLocalFrame(jni.NULL)
Example #17
0
 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")
Example #18
0
File: jnij.py Project: almanar/jvm
 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;")
Example #19
0
 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;")
Example #20
0
 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")
Example #21
0
 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")
Example #22
0
 def initialize(self, jenv: jni.JNIEnv):
     with JFrame(jenv, 1):
         jcls = jenv.FindClass(b"java/lang/System")
         self.Class            = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.identityHashCode = jenv.GetStaticMethodID(jcls, b"identityHashCode", b"(Ljava/lang/Object;)I")
         self.getProperty      = jenv.GetStaticMethodID(jcls, b"getProperty",      b"(Ljava/lang/String;)Ljava/lang/String;")
         self.setProperty      = jenv.GetStaticMethodID(jcls, b"setProperty",      b"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")
         self.clearProperty    = jenv.GetStaticMethodID(jcls, b"clearProperty",    b"(Ljava/lang/String;)Ljava/lang/String;")
         self.loadLibrary      = jenv.GetStaticMethodID(jcls, b"loadLibrary",      b"(Ljava/lang/String;)V")
         self.runFinalization  = jenv.GetStaticMethodID(jcls, b"runFinalization",  b"()V")
Example #23
0
 def initialize(self, jenv: jni.JNIEnv):
     with JFrame(jenv, 1):
         jcls = jenv.FindClass(b"java/lang/Throwable")
         self.Class               = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.getMessage          = jenv.GetMethodID(jcls, b"getMessage",          b"()Ljava/lang/String;")
         self.getLocalizedMessage = jenv.GetMethodID(jcls, b"getLocalizedMessage", b"()Ljava/lang/String;")
         self.getCause            = jenv.GetMethodID(jcls, b"getCause",            b"()Ljava/lang/Throwable;")
         self.getStackTrace       = jenv.GetMethodID(jcls, b"getStackTrace",       b"()[Ljava/lang/StackTraceElement;")
         self.setStackTrace       = jenv.GetMethodID(jcls, b"setStackTrace",       b"([Ljava/lang/StackTraceElement;)V")
         self.printStackTrace     = jenv.GetMethodID(jcls, b"printStackTrace",     b"(Ljava/io/PrintWriter;)V")
Example #24
0
 def initialize(self, jenv: jni.JNIEnv):
     with JFrame(jenv, 1):
         jcls = jenv.FindClass(b"java/lang/reflect/Method")
         self.Class             = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.getReturnType     = jenv.GetMethodID(jcls, b"getReturnType",     b"()Ljava/lang/Class;")
         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")
         self.isBridge          = jenv.GetMethodID(jcls, b"isBridge",          b"()Z")
         self.toGenericString   = jenv.GetMethodID(jcls, b"toGenericString",   b"()Ljava/lang/String;")
Example #25
0
 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"))
Example #26
0
 def initialize(self, jenv: jni.JNIEnv):
     with JFrame(jenv, 1):
         jcls = jenv.FindClass(b"java/lang/Number")
         self.Class       = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.byteValue   = jenv.GetMethodID(jcls, b"byteValue",   b"()B")
         self.shortValue  = jenv.GetMethodID(jcls, b"shortValue",  b"()S")
         self.intValue    = jenv.GetMethodID(jcls, b"intValue",    b"()I")
         self.longValue   = jenv.GetMethodID(jcls, b"longValue",   b"()J")
         self.floatValue  = jenv.GetMethodID(jcls, b"floatValue",  b"()F")
         self.doubleValue = jenv.GetMethodID(jcls, b"doubleValue", b"()D")
Example #27
0
 def initialize(self, jenv: jni.JNIEnv):
     with JFrame(jenv, 9):
         jcls = jenv.FindClass(b"java/util/List")
         self.ListClass    = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.List_addAll  = jenv.GetMethodID(jcls, b"addAll",  b"(Ljava/util/Collection;)Z")
         self.List_add     = jenv.GetMethodID(jcls, b"add",     b"(Ljava/lang/Object;)Z")
         self.List_get     = jenv.GetMethodID(jcls, b"get",     b"(I)Ljava/lang/Object;")
         self.List_set     = jenv.GetMethodID(jcls, b"set",     b"(ILjava/lang/Object;)Ljava/lang/Object;")
         self.List_remove  = jenv.GetMethodID(jcls, b"remove",  b"(I)Ljava/lang/Object;")
         self.List_clear   = jenv.GetMethodID(jcls, b"clear",   b"()V")
         self.List_subList = jenv.GetMethodID(jcls, b"subList", b"(II)Ljava/util/List;")
         jcls = jenv.FindClass(b"java/util/Set")
         self.SetClass = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         jcls = jenv.FindClass(b"java/util/Map")
         self.MapClass        = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.Map_size        = jenv.GetMethodID(jcls, b"size",        b"()I")
         self.Map_containsKey = jenv.GetMethodID(jcls, b"containsKey", b"(Ljava/lang/Object;)Z")
         self.Map_get         = jenv.GetMethodID(jcls, b"get",         b"(Ljava/lang/Object;)Ljava/lang/Object;")
         self.Map_put         = jenv.GetMethodID(jcls, b"put",         b"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
         self.Map_remove      = jenv.GetMethodID(jcls, b"remove",      b"(Ljava/lang/Object;)Ljava/lang/Object;")
         self.Map_clear       = jenv.GetMethodID(jcls, b"clear",       b"()V")
         self.Map_keySet      = jenv.GetMethodID(jcls, b"keySet",      b"()Ljava/util/Set;")
         self.Map_entrySet    = jenv.GetMethodID(jcls, b"entrySet",    b"()Ljava/util/Set;")
         jcls = jenv.FindClass(b"java/util/Map$Entry")
         self.MapEntryClass     = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.MapEntry_getKey   = jenv.GetMethodID(jcls, b"getKey",   b"()Ljava/lang/Object;")
         self.MapEntry_getValue = jenv.GetMethodID(jcls, b"getValue", b"()Ljava/lang/Object;")
         jcls = jenv.FindClass(b"java/util/Iterator")
         self.IteratorClass    = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.Iterator_hasNext = jenv.GetMethodID(jcls, b"hasNext", b"()Z")
         self.Iterator_next    = jenv.GetMethodID(jcls, b"next",    b"()Ljava/lang/Object;")
         jcls = jenv.FindClass(b"java/util/Collection")
         self.CollectionClass     = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.Collection_size     = jenv.GetMethodID(jcls, b"size",     b"()I")
         self.Collection_contains = jenv.GetMethodID(jcls, b"contains", b"(Ljava/lang/Object;)Z")
         self.Collection_iterator = jenv.GetMethodID(jcls, b"iterator", b"()Ljava/util/Iterator;")
         jcls = jenv.FindClass(b"java/util/Collections")
         self.CollectionsClass             = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.Collections_unmodifiableList = jenv.GetStaticMethodID(jcls, b"unmodifiableList", b"(Ljava/util/List;)Ljava/util/List;")
         jcls = jenv.FindClass(b"java/util/ArrayList")
         self.ArrayListClass          = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.ArrayListConstructorInt = jenv.GetMethodID(jcls, b"<init>", b"(I)V")
         self.ArrayList_add           = jenv.GetMethodID(jcls, b"add",    b"(Ljava/lang/Object;)Z")
         jcls = jenv.FindClass(b"java/util/HashMap")
         self.HashMapClass          = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.HashMapConstructorInt = jenv.GetMethodID(jcls, b"<init>", b"(I)V")
         self.HashMap_put           = jenv.GetMethodID(jcls, b"put",    b"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
Example #28
0
def throwJavaException(jenv: jni.JNIEnv, jecls, message):

    if isinstance(jecls, str):
        class_name = jecls
        jenv.PushLocalFrame(1)
        try:
            jecls = jenv.FindClass(class_name.replace(".", "/").encode("utf-8"))
            jenv.ThrowNew(jecls, str(message).encode("utf-8"))
        finally:
            jenv.PopLocalFrame(jni.NULL)
    else:
        jenv.ThrowNew(jecls, str(message).encode("utf-8"))
Example #29
0
 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
Example #30
0
 def initialize(self, jenv: jni.JNIEnv):
     with JFrame(jenv, 1):
         jcls = jenv.FindClass(b"java/lang/reflect/Proxy")
         self.Class                = jni.cast(jenv.NewGlobalRef(jcls), jni.jclass)
         self.getProxyClass        = jenv.GetStaticMethodID(jcls, b"getProxyClass",
                                                            b"(Ljava/lang/ClassLoader;[Ljava/lang/Class;)Ljava/lang/Class;")
         self.isProxyClass         = jenv.GetStaticMethodID(jcls, b"isProxyClass", b"(Ljava/lang/Class;)Z")
         self.newProxyInstance     = jenv.GetStaticMethodID(jcls, b"newProxyInstance",
                                                            b"(Ljava/lang/ClassLoader;[Ljava/lang/Class;"
                                                            b"Ljava/lang/reflect/InvocationHandler;)Ljava/lang/Object;")
         self.getInvocationHandler = jenv.GetStaticMethodID(jcls, b"getInvocationHandler",
                                                            b"(Ljava/lang/Object;)Ljava/lang/reflect/InvocationHandler;")