pyjnius/jnius/jnius.pyx

115 lines
2.3 KiB
Cython
Raw Normal View History

2012-08-14 01:42:43 +00:00
'''
Java wrapper
============
2012-08-16 12:12:10 +00:00
With this module, you can create Python class that reflects a Java class, and use
2012-08-14 01:42:43 +00:00
it directly in Python.
Example with static method
--------------------------
Java::
package org.test;
public class Hardware {
static int getDPI() {
return metrics.densityDpi;
}
}
Python::
class Hardware(JavaClass):
__metaclass__ = MetaJavaClass
__javaclass__ = 'org/test/Hardware'
getDPI = JavaStaticMethod('()I')
Hardware.getDPI()
Example with instance method
----------------------------
Java::
package org.test;
public class Action {
public String getName() {
return new String("Hello world")
}
}
Python::
class Action(JavaClass):
__metaclass__ = MetaJavaClass
__javaclass__ = 'org/test/Action'
getName = JavaMethod('()Ljava/lang/String;')
action = Action()
print action.getName()
# will output Hello World
Example with static/instance field
----------------------------------
Java::
package org.test;
public class Test {
public static String field1 = new String("hello");
public String field2;
public Test() {
this.field2 = new String("world");
}
}
Python::
class Test(JavaClass):
__metaclass__ = MetaJavaClass
__javaclass__ = 'org/test/Test'
field1 = JavaStaticField('Ljava/lang/String;')
field2 = JavaField('Ljava/lang/String;')
# access directly to the static field
print Test.field1
# create the instance, and access to the instance field
test = Test()
print test.field2
'''
__all__ = ('JavaObject', 'JavaClass', 'JavaMethod', 'JavaField',
'MetaJavaClass', 'JavaException', 'cast', 'find_javaclass',
'PythonJavaClass', 'java_method', 'detach')
2012-08-14 01:42:43 +00:00
from libc.stdlib cimport malloc, free
2013-03-13 20:38:10 +00:00
from functools import partial
import sys
2013-03-13 20:38:10 +00:00
import traceback
2012-08-14 01:42:43 +00:00
include "jni.pxi"
include "config.pxi"
2012-08-20 07:35:14 +00:00
2012-08-14 01:42:43 +00:00
IF JNIUS_PLATFORM == "android":
include "jnius_jvm_android.pxi"
ELSE:
include "jnius_jvm_desktop.pxi"
include "jnius_env.pxi"
2012-08-20 07:35:14 +00:00
include "jnius_utils.pxi"
include "jnius_conversion.pxi"
include "jnius_localref.pxi"
include "jnius_nativetypes.pxi"
2012-08-20 07:35:14 +00:00
include "jnius_export_func.pxi"
include "jnius_export_class.pxi"
2013-03-13 20:38:10 +00:00
include "jnius_proxy.pxi"