@@ -3,32 +3,85 @@ Java wrapper
3
3
============
4
4
5
5
With 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.
8
7
8
+ Example with static method
9
+ --------------------------
10
+
11
+ Java::
12
+
13
+ package org.test;
9
14
public class Hardware {
10
15
static int getDPI() {
11
16
return metrics.densityDpi;
12
17
}
13
18
}
14
19
15
- You can create this Python class to use it ::
20
+ Python::
16
21
17
22
class Hardware(JavaClass):
23
+ __metaclass__ = MetaJavaClass
18
24
__javaclass__ = 'org/test/Hardware'
19
25
getDPI = JavaStaticMethod('()I')
20
26
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;')
22
77
23
- hardware = Hardware()
24
- hardware.getDPI()
78
+ # access directly to the static field
79
+ print Test.field1
25
80
26
- Limitations
27
- -----------
81
+ # create the instance, and access to the instance field
82
+ test = Test()
83
+ print test.field2
28
84
29
- - Even if the method is static in Java, you need to instanciate the object in
30
- Python.
31
- - Array currently not supported
32
85
'''
33
86
34
87
__all__ = (' JavaObject' , ' JavaClass' , ' JavaMethod' , ' JavaStaticMethod' ,
0 commit comments