@@ -3,32 +3,85 @@ Java wrapper
33============
44
55With this module, you can create Python class that reflect a Java class, and use
6- it directly in Python. For example, if you have a Java class named
7- Hardware.java, in org/test directory::
6+ it directly in Python.
87
8+ Example with static method
9+ --------------------------
10+
11+ Java::
12+
13+ package org.test;
914 public class Hardware {
1015 static int getDPI() {
1116 return metrics.densityDpi;
1217 }
1318 }
1419
15- You can create this Python class to use it ::
20+ Python::
1621
1722 class Hardware(JavaClass):
23+ __metaclass__ = MetaJavaClass
1824 __javaclass__ = 'org/test/Hardware'
1925 getDPI = JavaStaticMethod('()I')
2026
21- And then, do::
27+ Hardware.getDPI()
28+
29+
30+ Example with instance method
31+ ----------------------------
32+
33+ Java::
34+
35+ package org.test;
36+ public class Action {
37+ public String getName() {
38+ return new String("Hello world")
39+ }
40+ }
41+
42+ Python::
43+
44+ class Action(JavaClass):
45+ __metaclass__ = MetaJavaClass
46+ __javaclass__ = 'org/test/Action'
47+ getName = JavaMethod('()Ljava/lang/String;')
48+
49+ action = Action()
50+ print action.getName()
51+ # will output Hello World
52+
53+
54+ Example with static/instance field
55+ ----------------------------------
56+
57+ Java::
58+
59+ package org.test;
60+ public class Test {
61+ public static String field1 = new String("hello");
62+ public String field2;
63+
64+ public Test() {
65+ this.field2 = new String("world");
66+ }
67+ }
68+
69+ Python::
70+
71+ class Test(JavaClass):
72+ __metaclass__ = MetaJavaClass
73+ __javaclass__ = 'org/test/Test'
74+
75+ field1 = JavaStaticField('Ljava/lang/String;')
76+ field2 = JavaField('Ljava/lang/String;')
2277
23- hardware = Hardware()
24- hardware.getDPI()
78+ # access directly to the static field
79+ print Test.field1
2580
26- Limitations
27- -----------
81+ # create the instance, and access to the instance field
82+ test = Test()
83+ print test.field2
2884
29- - Even if the method is static in Java, you need to instanciate the object in
30- Python.
31- - Array currently not supported
3285'''
3386
3487__all__ = (' JavaObject' , ' JavaClass' , ' JavaMethod' , ' JavaStaticMethod' ,
0 commit comments