Class PythonInterfaceDefaults

java.lang.Object
io.micronaut.context.python.PythonInterfaceDefaults

@Internal public final class PythonInterfaceDefaults extends Object
The default methods of the Java interfaces a Python class implements, made available to Python code.

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
  • 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 class
      interfaceNames - The names of the Java interfaces the class implements
      Returns:
      The Python class