mirror of https://github.com/kivy/pyjnius.git
Merge branch 'master' of github.com:kivy/pyjnius
This commit is contained in:
commit
6aa4760721
1
Makefile
1
Makefile
|
@ -5,4 +5,5 @@ build_ext:
|
||||||
|
|
||||||
tests: build_ext
|
tests: build_ext
|
||||||
cd tests && javac org/jnius/HelloWorld.java
|
cd tests && javac org/jnius/HelloWorld.java
|
||||||
|
cd tests && javac org/jnius/BasicsTest.java
|
||||||
cd tests && env PYTHONPATH=..:$(PYTHONPATH) nosetests -v
|
cd tests && env PYTHONPATH=..:$(PYTHONPATH) nosetests -v
|
||||||
|
|
|
@ -1,4 +1,2 @@
|
||||||
from .jnius import (JavaObject, JavaMethod, JavaStaticMethod,
|
from .jnius import *
|
||||||
JavaField, JavaStaticField, JavaClass, MetaJavaClass,
|
from .reflect import *
|
||||||
JavaException, cast)
|
|
||||||
|
|
||||||
|
|
|
@ -86,7 +86,8 @@ Python::
|
||||||
'''
|
'''
|
||||||
|
|
||||||
__all__ = ('JavaObject', 'JavaClass', 'JavaMethod', 'JavaStaticMethod',
|
__all__ = ('JavaObject', 'JavaClass', 'JavaMethod', 'JavaStaticMethod',
|
||||||
'JavaField', 'JavaStaticField', 'MetaJavaClass', 'JavaException', 'cast')
|
'JavaField', 'JavaStaticField', 'MetaJavaClass', 'JavaException', 'cast',
|
||||||
|
'find_javaclass')
|
||||||
|
|
||||||
from libc.stdlib cimport malloc, free
|
from libc.stdlib cimport malloc, free
|
||||||
|
|
||||||
|
@ -929,3 +930,19 @@ def cast(destclass, obj):
|
||||||
jc.instanciate_from(jobj.j_self)
|
jc.instanciate_from(jobj.j_self)
|
||||||
return jc
|
return jc
|
||||||
|
|
||||||
|
def find_javaclass(bytes name):
|
||||||
|
from reflect import Class
|
||||||
|
cdef JavaClass cls
|
||||||
|
cdef jclass jc
|
||||||
|
cdef JNIEnv *j_env = get_jnienv()
|
||||||
|
|
||||||
|
name = name.replace('.', '/')
|
||||||
|
|
||||||
|
jc = j_env[0].FindClass(j_env, name)
|
||||||
|
if jc == NULL:
|
||||||
|
raise JavaException('Class not found {0!r}'.format(name))
|
||||||
|
|
||||||
|
cls = Class(noinstance=True)
|
||||||
|
cls.instanciate_from(create_local_ref(j_env, jc))
|
||||||
|
return cls
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ cdef convert_jarray_to_python(JNIEnv *j_env, definition, jobject j_object):
|
||||||
elif r == 'C':
|
elif r == 'C':
|
||||||
j_chars = j_env[0].GetCharArrayElements(
|
j_chars = j_env[0].GetCharArrayElements(
|
||||||
j_env, j_object, &iscopy)
|
j_env, j_object, &iscopy)
|
||||||
ret = [(<char>j_chars[i]) for i in range(array_size)]
|
ret = [chr(<char>j_chars[i]) for i in range(array_size)]
|
||||||
if iscopy:
|
if iscopy:
|
||||||
j_env[0].ReleaseCharArrayElements(
|
j_env[0].ReleaseCharArrayElements(
|
||||||
j_env, j_object, j_chars, 0)
|
j_env, j_object, j_chars, 0)
|
||||||
|
|
|
@ -9,15 +9,27 @@ cdef extern from "jni.h":
|
||||||
jint version
|
jint version
|
||||||
jint nOptions
|
jint nOptions
|
||||||
jboolean ignoreUnrecognized
|
jboolean ignoreUnrecognized
|
||||||
|
JavaVMOption *options
|
||||||
|
ctypedef struct JavaVMOption:
|
||||||
|
char *optionString
|
||||||
|
void *extraInfo
|
||||||
|
|
||||||
cdef JNIEnv *default_env = NULL
|
cdef JNIEnv *default_env = NULL
|
||||||
|
|
||||||
cdef void create_jnienv():
|
cdef void create_jnienv():
|
||||||
cdef JavaVM* jvm
|
cdef JavaVM* jvm
|
||||||
cdef JavaVMInitArgs args
|
cdef JavaVMInitArgs args
|
||||||
|
cdef JavaVMOption options[1]
|
||||||
|
cdef bytes py_bytes
|
||||||
|
|
||||||
|
from os.path import realpath
|
||||||
|
py_bytes = <bytes>('-Djava.class.path={0}'.format(realpath('.')))
|
||||||
|
options[0].optionString = py_bytes
|
||||||
|
options[0].extraInfo = NULL
|
||||||
|
|
||||||
args.version = JNI_VERSION_1_4
|
args.version = JNI_VERSION_1_4
|
||||||
args.nOptions = 0
|
args.options = options
|
||||||
|
args.nOptions = 1
|
||||||
args.ignoreUnrecognized = JNI_FALSE
|
args.ignoreUnrecognized = JNI_FALSE
|
||||||
|
|
||||||
JNI_CreateJavaVM(&jvm, <void **>&default_env, &args)
|
JNI_CreateJavaVM(&jvm, <void **>&default_env, &args)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
__all__ = ('autoclass', 'ensureclass')
|
__all__ = ('autoclass', 'ensureclass')
|
||||||
|
|
||||||
from jnius import JavaClass, MetaJavaClass, JavaMethod, JavaStaticMethod, \
|
from jnius import JavaClass, MetaJavaClass, JavaMethod, JavaStaticMethod, \
|
||||||
JavaField, JavaStaticField, JavaMethodMultiple
|
JavaField, JavaStaticField, JavaMethodMultiple, find_javaclass
|
||||||
|
|
||||||
class Class(JavaClass):
|
class Class(JavaClass):
|
||||||
__metaclass__ = MetaJavaClass
|
__metaclass__ = MetaJavaClass
|
||||||
|
@ -102,7 +102,8 @@ def autoclass(clsname):
|
||||||
|
|
||||||
classDict = {}
|
classDict = {}
|
||||||
|
|
||||||
c = Class.forName(clsname)
|
#c = Class.forName(clsname)
|
||||||
|
c = find_javaclass(clsname)
|
||||||
if c is None:
|
if c is None:
|
||||||
raise Exception('Java class {0} not found'.format(c))
|
raise Exception('Java class {0} not found'.format(c))
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -0,0 +1,115 @@
|
||||||
|
package org.jnius;
|
||||||
|
|
||||||
|
import java.lang.String;
|
||||||
|
|
||||||
|
public class BasicsTest {
|
||||||
|
static public boolean methodStaticZ() { return true; };
|
||||||
|
static public byte methodStaticB() { return 127; };
|
||||||
|
static public char methodStaticC() { return 'k'; };
|
||||||
|
static public short methodStaticS() { return 32767; };
|
||||||
|
static public int methodStaticI() { return 2147483467; };
|
||||||
|
static public long methodStaticJ() { return 2147483467; };
|
||||||
|
static public float methodStaticF() { return 1.23456789f; };
|
||||||
|
static public double methodStaticD() { return 1.23456789; };
|
||||||
|
static public String methodStaticString() { return new String("helloworld"); }
|
||||||
|
|
||||||
|
public boolean methodZ() { return true; };
|
||||||
|
public byte methodB() { return 127; };
|
||||||
|
public char methodC() { return 'k'; };
|
||||||
|
public short methodS() { return 32767; };
|
||||||
|
public int methodI() { return 2147483467; };
|
||||||
|
public long methodJ() { return 2147483467; };
|
||||||
|
public float methodF() { return 1.23456789f; };
|
||||||
|
public double methodD() { return 1.23456789; };
|
||||||
|
public String methodString() { return new String("helloworld"); }
|
||||||
|
|
||||||
|
static public boolean fieldStaticZ = true;
|
||||||
|
static public byte fieldStaticB = 127;
|
||||||
|
static public char fieldStaticC = 'k';
|
||||||
|
static public short fieldStaticS = 32767;
|
||||||
|
static public int fieldStaticI = 2147483467;
|
||||||
|
static public long fieldStaticJ = 2147483467;
|
||||||
|
static public float fieldStaticF = 1.23456789f;
|
||||||
|
static public double fieldStaticD = 1.23456789;
|
||||||
|
static public String fieldStaticString = new String("helloworld");
|
||||||
|
|
||||||
|
public boolean fieldZ = true;
|
||||||
|
public byte fieldB = 127;
|
||||||
|
public char fieldC = 'k';
|
||||||
|
public short fieldS = 32767;
|
||||||
|
public int fieldI = 2147483467;
|
||||||
|
public long fieldJ = 2147483467;
|
||||||
|
public float fieldF = 1.23456789f;
|
||||||
|
public double fieldD = 1.23456789;
|
||||||
|
public String fieldString = new String("helloworld");
|
||||||
|
|
||||||
|
public boolean[] methodArrayZ() {
|
||||||
|
boolean[] x = new boolean[3];
|
||||||
|
x[0] = x[1] = x[2] = true;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
public byte[] methodArrayB() {
|
||||||
|
byte[] x = new byte[3];
|
||||||
|
x[0] = x[1] = x[2] = 127;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
public char[] methodArrayC() {
|
||||||
|
char[] x = new char[3];
|
||||||
|
x[0] = x[1] = x[2] = 'k';
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
public short[] methodArrayS() {
|
||||||
|
short[] x = new short[3];
|
||||||
|
x[0] = x[1] = x[2] = 32767;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
public int[] methodArrayI() {
|
||||||
|
int[] x = new int[3];
|
||||||
|
x[0] = x[1] = x[2] = 2147483467;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
public long[] methodArrayJ() {
|
||||||
|
long[] x = new long[3];
|
||||||
|
x[0] = x[1] = x[2] = 2147483467;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
public float[] methodArrayF() {
|
||||||
|
float[] x = new float[3];
|
||||||
|
x[0] = x[1] = x[2] = 1.23456789f;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
public double[] methodArrayD() {
|
||||||
|
double[] x = new double[3];
|
||||||
|
x[0] = x[1] = x[2] = 1.23456789;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
public String[] methodArrayString() {
|
||||||
|
String[] x = new String[3];
|
||||||
|
x[0] = x[1] = x[2] = new String("helloworld");
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
public boolean methodParamsZBCSIJFD(boolean x1, byte x2, char x3, short x4,
|
||||||
|
int x5, long x6, float x7, double x8) {
|
||||||
|
// ADD float / double, but dunno how to do with approx
|
||||||
|
return (x1 == true && x2 == 127 && x3 == 'k' && x4 == 32767 &&
|
||||||
|
x5 == 2147483467 && x6 == 2147483467);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean methodParamsString(String s) {
|
||||||
|
return (s.equals("helloworld"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean methodParamsArrayI(int[] x) {
|
||||||
|
if (x.length != 3)
|
||||||
|
return false;
|
||||||
|
return (x[0] == 1 && x[1] == 2 && x[2] == 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean methodParamsArrayString(String[] x) {
|
||||||
|
if (x.length != 2)
|
||||||
|
return false;
|
||||||
|
return (x[0].equals("hello") && x[1].equals("world"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,79 @@
|
||||||
|
import unittest
|
||||||
|
from jnius.reflect import autoclass
|
||||||
|
|
||||||
|
class BasicsTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_static_methods(self):
|
||||||
|
Test = autoclass('org.jnius.BasicsTest')
|
||||||
|
self.assertEquals(Test.methodStaticZ(), True)
|
||||||
|
self.assertEquals(Test.methodStaticB(), 127)
|
||||||
|
self.assertEquals(Test.methodStaticC(), 'k')
|
||||||
|
self.assertEquals(Test.methodStaticS(), 32767)
|
||||||
|
self.assertEquals(Test.methodStaticI(), 2147483467)
|
||||||
|
self.assertEquals(Test.methodStaticJ(), 2147483467)
|
||||||
|
self.assertAlmostEquals(Test.methodStaticF(), 1.23456789)
|
||||||
|
self.assertEquals(Test.methodStaticD(), 1.23456789)
|
||||||
|
self.assertEquals(Test.methodStaticString(), 'helloworld')
|
||||||
|
|
||||||
|
def test_static_fields(self):
|
||||||
|
Test = autoclass('org.jnius.BasicsTest')
|
||||||
|
self.assertEquals(Test.fieldStaticZ, True)
|
||||||
|
self.assertEquals(Test.fieldStaticB, 127)
|
||||||
|
self.assertEquals(Test.fieldStaticC, 'k')
|
||||||
|
self.assertEquals(Test.fieldStaticS, 32767)
|
||||||
|
self.assertEquals(Test.fieldStaticI, 2147483467)
|
||||||
|
self.assertEquals(Test.fieldStaticJ, 2147483467)
|
||||||
|
self.assertAlmostEquals(Test.fieldStaticF, 1.23456789)
|
||||||
|
self.assertEquals(Test.fieldStaticD, 1.23456789)
|
||||||
|
self.assertEquals(Test.fieldStaticString, 'helloworld')
|
||||||
|
|
||||||
|
def test_instance_methods(self):
|
||||||
|
test = autoclass('org.jnius.BasicsTest')()
|
||||||
|
self.assertEquals(test.methodZ(), True)
|
||||||
|
self.assertEquals(test.methodB(), 127)
|
||||||
|
self.assertEquals(test.methodC(), 'k')
|
||||||
|
self.assertEquals(test.methodS(), 32767)
|
||||||
|
self.assertEquals(test.methodI(), 2147483467)
|
||||||
|
self.assertEquals(test.methodJ(), 2147483467)
|
||||||
|
self.assertAlmostEquals(test.methodF(), 1.23456789)
|
||||||
|
self.assertEquals(test.methodD(), 1.23456789)
|
||||||
|
self.assertEquals(test.methodString(), 'helloworld')
|
||||||
|
|
||||||
|
def test_instance_fields(self):
|
||||||
|
test = autoclass('org.jnius.BasicsTest')()
|
||||||
|
self.assertEquals(test.fieldZ, True)
|
||||||
|
self.assertEquals(test.fieldB, 127)
|
||||||
|
self.assertEquals(test.fieldC, 'k')
|
||||||
|
self.assertEquals(test.fieldS, 32767)
|
||||||
|
self.assertEquals(test.fieldI, 2147483467)
|
||||||
|
self.assertEquals(test.fieldJ, 2147483467)
|
||||||
|
self.assertAlmostEquals(test.fieldF, 1.23456789)
|
||||||
|
self.assertEquals(test.fieldD, 1.23456789)
|
||||||
|
self.assertEquals(test.fieldString, 'helloworld')
|
||||||
|
|
||||||
|
def test_instances_methods_array(self):
|
||||||
|
test = autoclass('org.jnius.BasicsTest')()
|
||||||
|
self.assertEquals(test.methodArrayZ(), [True] * 3)
|
||||||
|
self.assertEquals(test.methodArrayB(), [127] * 3)
|
||||||
|
self.assertEquals(test.methodArrayC(), ['k'] * 3)
|
||||||
|
self.assertEquals(test.methodArrayS(), [32767] * 3)
|
||||||
|
self.assertEquals(test.methodArrayI(), [2147483467] * 3)
|
||||||
|
self.assertEquals(test.methodArrayJ(), [2147483467] * 3)
|
||||||
|
|
||||||
|
ret = test.methodArrayF()
|
||||||
|
ref = [1.23456789] * 3
|
||||||
|
self.assertAlmostEquals(ret[0], ref[0])
|
||||||
|
self.assertAlmostEquals(ret[1], ref[1])
|
||||||
|
self.assertAlmostEquals(ret[2], ref[2])
|
||||||
|
|
||||||
|
self.assertEquals(test.methodArrayD(), [1.23456789] * 3)
|
||||||
|
self.assertEquals(test.methodArrayString(), ['helloworld'] * 3)
|
||||||
|
|
||||||
|
def test_instances_methods_params(self):
|
||||||
|
test = autoclass('org.jnius.BasicsTest')()
|
||||||
|
self.assertEquals(test.methodParamsZBCSIJFD(
|
||||||
|
True, 127, 'k', 32767, 2147483467, 2147483467, 1.23456789, 1.23456789), True)
|
||||||
|
self.assertEquals(test.methodParamsString('helloworld'), True)
|
||||||
|
self.assertEquals(test.methodParamsArrayI([1, 2, 3]), True)
|
||||||
|
self.assertEquals(test.methodParamsArrayString([
|
||||||
|
'hello', 'world']), True)
|
|
@ -2,7 +2,7 @@ import unittest
|
||||||
from jnius.reflect import autoclass
|
from jnius.reflect import autoclass
|
||||||
from jnius import cast
|
from jnius import cast
|
||||||
|
|
||||||
class MultipleSignature(unittest.TestCase):
|
class MultipleSignatureTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_multiple_constructors(self):
|
def test_multiple_constructors(self):
|
||||||
String = autoclass('java.lang.String')
|
String = autoclass('java.lang.String')
|
||||||
|
|
|
@ -3,7 +3,7 @@ from jnius import JavaClass, MetaJavaClass, JavaMethod
|
||||||
|
|
||||||
class HelloWorldTest(unittest.TestCase):
|
class HelloWorldTest(unittest.TestCase):
|
||||||
|
|
||||||
def test(self):
|
def test_helloworld(self):
|
||||||
|
|
||||||
class HelloWorld(JavaClass):
|
class HelloWorld(JavaClass):
|
||||||
__metaclass__ = MetaJavaClass
|
__metaclass__ = MetaJavaClass
|
||||||
|
|
Loading…
Reference in New Issue