@@ -22,7 +22,10 @@ command line flag on Graal: `--python.EmulateJython`.
22
22
23
23
### Importing
24
24
Import statements allow you to import Java classes, but (unlike Jython), only
25
- packages in the ` java ` namespace can be directly imported. This will work:
25
+ packages in the ` java ` namespace can be directly imported. Importing classes
26
+ from packages outside ` java ` namespace also requires the
27
+ ` --python.EmulateJython ` option to be active.
28
+ This will work:
26
29
```
27
30
import java.lang as lang
28
31
```
@@ -33,7 +36,7 @@ from javax.swing import *
33
36
```
34
37
Instead, you will have to import one of the classes you are interested in directly:
35
38
```
36
- import javax.swing.Window as Window
39
+ import javax.swing.JWindow as JWindow
37
40
```
38
41
39
42
### Basic Object Usage
@@ -47,7 +50,7 @@ methods:
47
50
>>> boundNextInt = rg.nextInt
48
51
>>> rg.nextInt()
49
52
1491444859
50
- >>> boundNextInt = rg.nextInt
53
+ >>> boundNextInt()
51
54
1672896916
52
55
53
56
### Java-to-Python Types: Automatic Conversion
@@ -60,18 +63,18 @@ more dynamic approach to matching — Python types emulating `int` or
60
63
example, to use Pandas frames as ` double[][] ` or NumPy array elements as ` int[] `
61
64
when the elements fit into those Java primitive types.
62
65
63
- | Java type | Python type |
64
- | :-----------------------| : ----------------------------------------------------------------------------------|
65
- | null | None |
66
- | boolean | bool |
67
- | byte, short, int, long | int, any object that has an ` __int__ ` method |
68
- | float | float, any object that has a ` __float__ ` method |
69
- | char | str of length 1 |
70
- | java.lang.String | str |
71
- | byte[ ] | bytes, bytearray, wrapped Java array, Python list with only the appropriate types |
72
- | Java arrays | Wrapped Java array or Python list with only the appropriate types |
73
- | Java objects | Wrapped Java object of the appropriate type |
74
- | java.lang.Object | Any object |
66
+ | Java type | Python type |
67
+ | :-------------------------------- | : --------------------------------------------------------------------------|
68
+ | ` null ` | ` None ` |
69
+ | ` boolean ` | ` bool ` |
70
+ | ` byte ` , ` short ` , ` int ` , ` long ` | ` int ` , any object that has an ` __int__ ` method |
71
+ | ` float ` , ` double ` | ` float ` , any object that has a ` __float__ ` method |
72
+ | ` char ` | ` str ` of length 1 |
73
+ | ` java.lang.String ` | ` str ` |
74
+ | ` byte[] ` | ` bytes ` , ` bytearray ` , wrapped Java array, Python list with only the appropriate types |
75
+ | Java arrays | Wrapped Java array or Python list with only the appropriate types |
76
+ | Java objects | Wrapped Java object of the appropriate type |
77
+ | ` java.lang.Object ` | Any object |
75
78
76
79
### Special Jython Modules
77
80
Any of the special Jython modules are not available. For example, the ` jarray `
@@ -85,6 +88,8 @@ The code that only needs to pass a Java array can also use Python types. However
85
88
implicitly, this may entail a copy of the array data, which can be deceiving when
86
89
using Java arrays as output parameters:
87
90
91
+ >>> # This example needs the --python.EmulateJython flag for the java.io import
92
+ >>> import java
88
93
>>> i = java.io.ByteArrayInputStream(b"foobar")
89
94
>>> buf = [0, 0, 0]
90
95
>>> i.read(buf) # buf is automatically converted to a byte[] array
@@ -115,6 +120,8 @@ There is no automatic mapping of the Python syntax for accessing dictionary
115
120
elements to the ` java.util ` mapping and list classes' ` get ` , ` set ` , or ` put `
116
121
methods. To use these mapping and list clases, you must call the Java methods:
117
122
123
+ >>> # This example needs the --python.EmulateJython flag for the java.util import
124
+ >>> import java
118
125
>>> ht = java.util.Hashtable()
119
126
>>> ht.put("foo", "bar")
120
127
>>> ht.get("foo")
@@ -153,6 +160,7 @@ public class PythonHandler extends Handler {
153
160
```
154
161
Then you can use it like this in Python:
155
162
```
163
+ # This example needs the --python.EmulateJython flag for the java.util import
156
164
from java.util.logging import LogManager, Logger
157
165
158
166
class MyHandler():
0 commit comments