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
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2013-01-03 11:57:46 +00:00
|
|
|
__all__ = ('JavaObject', 'JavaClass', 'JavaMethod', 'JavaField',
|
2018-04-30 12:32:57 +00:00
|
|
|
'JavaStaticMethod', 'JavaStaticField', '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
|
2013-06-24 22:08:24 +00:00
|
|
|
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"
|
2015-05-01 16:16:58 +00:00
|
|
|
ELIF JNIUS_PLATFORM == "win32":
|
2012-08-14 01:42:43 +00:00
|
|
|
include "jnius_jvm_desktop.pxi"
|
2015-05-01 16:16:58 +00:00
|
|
|
ELSE:
|
|
|
|
include "jnius_jvm_dlopen.pxi"
|
2012-08-14 01:42:43 +00:00
|
|
|
|
2013-06-24 15:58:20 +00:00
|
|
|
include "jnius_env.pxi"
|
2012-08-20 07:35:14 +00:00
|
|
|
include "jnius_utils.pxi"
|
|
|
|
include "jnius_conversion.pxi"
|
|
|
|
include "jnius_localref.pxi"
|
2015-05-01 21:57:22 +00:00
|
|
|
IF JNIUS_PYTHON3:
|
|
|
|
include "jnius_nativetypes3.pxi"
|
|
|
|
ELSE:
|
|
|
|
include "jnius_nativetypes.pxi"
|
2012-08-18 10:59:15 +00:00
|
|
|
|
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"
|