Skip to content

Commit 4d81cb4

Browse files
committed
Update docs about subclassing from Java and Jython emulation in response to GH #470
1 parent 437be67 commit 4d81cb4

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

docs/user/Python-on-JVM.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ This enables you, for example, to use a Pandas frame as `double[][]` or NumPy ar
109109
### Special Jython Module: `jarray`
110110

111111
GraalPy implements the `jarray` module (to create primitive Java arrays) for compatibility.
112+
This module is always available, since we have not found its presence to have a negative impact.
112113
For example:
113114

114115
```python
@@ -142,10 +143,7 @@ However, implicitly, this may produce a copy of the array data, which can be dec
142143

143144
### Exceptions from Java
144145

145-
To catch Java exceptions, use the `--python.EmulateJython` option.
146-
147-
> Note: Catching a Java exception incurs a performance penalty.
148-
146+
You can catch Java exceptions as you would expect.
149147
For example:
150148

151149
```python
@@ -161,8 +159,8 @@ For example:
161159

162160
### Java Collections
163161

164-
* Java arrays and collections that implement the `java.util.Collection` interface can be accessed using the `[]` syntax.
165-
An empty collection is considered `false` in boolean conversions.
162+
* Java arrays and collections that implement the `java.util.Collection` interface can be accessed using the `[]` syntax.
163+
An empty collection is considered `false` in boolean conversions.
166164
The length of a collection is exposed by the `len` built-in function.
167165
For example:
168166

@@ -186,7 +184,7 @@ For example:
186184
False
187185
```
188186

189-
* Java iterables that implement the `java.lang.Iterable` interface can be iterated over using a `for` loop or the `iter` built-in function and are accepted by all built-ins that expect an iterable.
187+
* Java iterables that implement the `java.lang.Iterable` interface can be iterated over using a `for` loop or the `iter` built-in function and are accepted by all built-ins that expect an iterable.
190188
For example:
191189

192190
```python
@@ -242,12 +240,24 @@ For example:
242240

243241
### Inheritance from Java
244242

245-
Inheriting from a Java class (or implementing a Java interface) is supported with some syntactical differences from Jython.
246-
To create a class that inherits from a Java class (or implements a Java interface), use the conventional Python `class` statement: declared methods
247-
override (implement) superclass (interface) methods when their names match.
248-
To call the a superclass method, use the special attribute `self.__super__`.
243+
Inheriting from a Java class (or implementing a Java interface) is supported with some syntactical and significant behavioral differences from Jython.
244+
To create a class that inherits from a Java class (or implements a Java interface), use the conventional Python `class` statement.
245+
Declared methods override (implement) superclass (interface) methods when their names match.
246+
247+
It is important to understand that there is actually delegation happening here - when inheriting from Java, two classes are created, one in Java and one in Python.
248+
These reference each other and any methods that are declared in Python that override or implement a Java method on the superclass are declared on the Java side as delegating to Python.
249249
The created object does not behave like a Python object but instead in the same way as a foreign Java object.
250-
Its Python-level members can be accessed using its `this` attribute. For example:
250+
The reason for this is that when you create an instance of your new class, you get a reference to the *Java* object.
251+
252+
To call Python methods that do *not* override or implement methods that already existed on the superclass, you need to use the special `this` attribute.
253+
Once you are in a Python method, your `self` refers to the Python object, and to get back from a Python method to Java, use the special attribute `__super__`.
254+
And since we do not expose static members on the instance side, if you need to call a static method from an instance on the Java side, use `getClass().static` to get to the meta-object holding the static members.
255+
256+
One important consequence of the two-object-schema here is that the `__init__` method on the Python object is actually called *before* the connection to the Java side is established.
257+
So you cannot currently override construction of the Java object or run code during initialization that would affect the Java half of the combined structure.
258+
You will have to create a factory method if you want to achieve this.
259+
260+
For example:
251261

252262
```python
253263
import atexit
@@ -277,6 +287,8 @@ for record in handler.this.logged:
277287
print(f'Python captured message "{record.getMessage()}" at level {record.getLevel().getName()}')
278288
```
279289

290+
For more information about how the generated Java subclass behaves, see the [Truffle documentation](https://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/TruffleLanguage.Env.html#createHostAdapter(java.lang.Object%5B%5D)).
291+
280292
## Embedding Python into Java
281293

282294
The other way to use Jython was to embed it into a Java application. There were two options for such an embedding.

0 commit comments

Comments
 (0)