Class PythonInterfaceDefaults
The runtime form of a Python class does not extend the Java interfaces it implements (GraalPy cannot
subclass a host interface), so a Python instance is a plain Python object: injected into another Python
bean, looked up from the context in Python or created in Python, it has the methods of its class but not
the default methods of the interface, which only the generated Java stub inherits. The runtime code of
such a class is decorated with install(Value, Value), which adds a Python method for every default method the
class, or a Python base class, does not define. The added method converts the Python instance to the Java
view the generated stub provides and invokes the default method on it, so the Java implementation runs
with a Java view of the Python object as this, its abstract calls reaching the Python methods
through the stub, while the Python object keeps its identity (is, isinstance, ==).
A class without a generated stub (one defined inside a function that is not a GraalPy host adapter of the
interface, because it takes constructor parameters or has another base) has no such view: the polyglot proxy
GraalPy creates for the interface dispatches every member to the Python object first, which would reach
the installed method again, so the default method is run through a proxy of this class that invokes the
default implementation of that one method itself and delegates every other call to the polyglot proxy:
an abstract method or a default method the Python class overrides reaches the Python code, a default
method the class does not override comes back through its installed method.
A default method overridden in Python is not installed: the Python definition is what both the Python object and, through the stub bridge, the Java view run.
- Since:
- 5.2.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA default method (with its overloads) installed on a Python class;PythonInterfaceDefaults.DefaultMethod.invoke(Value, Value)is what the installed Python method calls. -
Method Summary
Modifier and TypeMethodDescriptionstatic org.graalvm.polyglot.Valueinstall(org.graalvm.polyglot.Value pythonClass, org.graalvm.polyglot.Value interfaceNames) Adds the default methods of the given Java interfaces (and of the interfaces they extend) to a Python class, except those the class already defines or inherits from a Python base.
-
Method Details
-
install
public static org.graalvm.polyglot.Value install(org.graalvm.polyglot.Value pythonClass, org.graalvm.polyglot.Value interfaceNames) Adds the default methods of the given Java interfaces (and of the interfaces they extend) to a Python class, except those the class already defines or inherits from a Python base.- Parameters:
pythonClass- The Python classinterfaceNames- The names of the Java interfaces the class implements- Returns:
- The Python class
-