Class PythonJavaBases

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

@Internal public final class PythonJavaBases extends Object
The runtime side of a Python class that extends a Java class.

The Java class generated for such a Python class extends the Java base and is the only Java instance of it. At run time the Java base is replaced in the Python class by a Python base class (created by baseClass(Class)) that records the arguments of super().__init__(...) and defines a Python method for every accessible instance method of the Java base. Those methods reach the base implementation through the Java instance bound to the Python object (bind(Value, ValueCoercible)), which the generated constructors create from the recorded arguments; when the Java super constructor takes no argument, they create it before __init__ runs, so the inherited methods __init__ calls reach it. A Python object created in Python code gets its Java instance the first time an inherited method is called.

Since:
5.2.0
Author:
Micronaut Team
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    The construction of the Java instance of a Python object, see
    invalid reference
    #constructing(Value)
    .
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The attribute of a Python object holding the Java instance of its class.
    static final String
    The attribute of a Python object holding the arguments of its super().__init__(...) call.
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.graalvm.polyglot.Value
    argument(org.graalvm.polyglot.Value pythonObject, int index, String constructor)
    An argument of the super().__init__(...) call recorded on a Python object, for the Java super constructor.
    static org.graalvm.polyglot.Value
    baseClass(Class<?> javaClass)
    The Python base class standing in for a Java class, created once per context.
    static void
    bind(org.graalvm.polyglot.Value pythonObject, ValueCoercible javaInstance)
    Binds the Java instance of a Python object.
    static <T> @Nullable T
    bound(org.graalvm.polyglot.Value pythonObject, Class<T> type)
    The Java instance already bound to a Python object.
    constructing(org.graalvm.polyglot.Value pythonObject, Class<?> javaClass)
    Marks the Python object whose Java instance is being constructed on the current thread.
    static @Nullable Object
    invoke(org.graalvm.polyglot.Value self, String name, org.graalvm.polyglot.Value arguments)
    Invokes a method of the Java base on the Java instance of a Python object; called by the methods of the Python base class.
    static boolean
    matchesVarargs(List<org.graalvm.polyglot.Value> arguments, int from, Class<?> componentType)
    Whether the trailing Python arguments, from the given index, are accepted by the varargs parameter of a Java base method: each one is an element of the component type, or the single trailing argument is already an array of it.
    noSuchMethod(String baseClass, String name, List<org.graalvm.polyglot.Value> arguments)
    The error for a call of a Java base method that no accessible overload of the base accepts; thrown by the generated micronautInvokeJavaBaseMethod.
    static @Nullable org.graalvm.polyglot.Value
    underConstruction(Class<?> javaClass)
    The Python object whose Java instance of the given class is being constructed on the current thread, for a bridge method called by the Java super constructor; null outside such a construction.
    static Object
    varargs(List<org.graalvm.polyglot.Value> arguments, int from, Class<?> componentType)
    The array a varargs parameter of a Java base method receives for the trailing Python arguments, from the given index: each one converted to the component type, or the single trailing argument when it is already an array of it, as in a Java call.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • JAVA_INSTANCE_MEMBER

      public static final String JAVA_INSTANCE_MEMBER
      The attribute of a Python object holding the Java instance of its class.
      See Also:
    • SUPER_ARGUMENTS_MEMBER

      public static final String SUPER_ARGUMENTS_MEMBER
      The attribute of a Python object holding the arguments of its super().__init__(...) call.
      See Also:
  • Method Details

    • baseClass

      public static org.graalvm.polyglot.Value baseClass(Class<?> javaClass)
      The Python base class standing in for a Java class, created once per context.
      Parameters:
      javaClass - The Java class a Python class extends
      Returns:
      The Python base class
    • invoke

      public static @Nullable Object invoke(org.graalvm.polyglot.Value self, String name, org.graalvm.polyglot.Value arguments) throws Exception
      Invokes a method of the Java base on the Java instance of a Python object; called by the methods of the Python base class.
      Parameters:
      self - The Python object
      name - The method name
      arguments - The Python arguments
      Returns:
      The result, converted for Python
      Throws:
      Exception - The checked exception the base method declares, raised in Python as a host exception
    • constructing

      public static PythonJavaBases.Construction constructing(org.graalvm.polyglot.Value pythonObject, Class<?> javaClass)
      Marks the Python object whose Java instance is being constructed on the current thread. The generated (Value) constructor evaluates this before it runs the Java super constructor, which may call a method the Python class overrides: until the instance holds its Python object, the bridge of that method reaches it through underConstruction(Class).
      Parameters:
      pythonObject - The Python object
      javaClass - The generated Java class being constructed
      Returns:
      The construction, finished by the constructor once the super constructor returned
    • underConstruction

      public static @Nullable org.graalvm.polyglot.Value underConstruction(Class<?> javaClass)
      The Python object whose Java instance of the given class is being constructed on the current thread, for a bridge method called by the Java super constructor; null outside such a construction. Constructions nest when a super constructor creates the Java instance of another Python object, so the asking class selects the innermost construction of its own class.
      Parameters:
      javaClass - The generated Java class of the asking instance
      Returns:
      The Python object, or null
    • bind

      public static void bind(org.graalvm.polyglot.Value pythonObject, ValueCoercible javaInstance)
      Binds the Java instance of a Python object.
      Parameters:
      pythonObject - The Python object
      javaInstance - The generated Java instance wrapping it
    • bound

      public static <T> @Nullable T bound(org.graalvm.polyglot.Value pythonObject, Class<T> type)
      The Java instance already bound to a Python object.
      Type Parameters:
      T - The generated Java class
      Parameters:
      pythonObject - The Python object
      type - The generated Java class
      Returns:
      The bound instance, or null when the object has none of that type
    • argument

      public static org.graalvm.polyglot.Value argument(org.graalvm.polyglot.Value pythonObject, int index, String constructor)
      An argument of the super().__init__(...) call recorded on a Python object, for the Java super constructor.
      Parameters:
      pythonObject - The Python object
      index - The argument index
      constructor - A description of the Java constructor, for the error message
      Returns:
      The argument
    • matchesVarargs

      public static boolean matchesVarargs(List<org.graalvm.polyglot.Value> arguments, int from, Class<?> componentType)
      Whether the trailing Python arguments, from the given index, are accepted by the varargs parameter of a Java base method: each one is an element of the component type, or the single trailing argument is already an array of it.
      Parameters:
      arguments - The Python arguments
      from - The index of the first argument of the varargs parameter
      componentType - The component type of the varargs parameter
      Returns:
      Whether the arguments are accepted
    • varargs

      public static Object varargs(List<org.graalvm.polyglot.Value> arguments, int from, Class<?> componentType)
      The array a varargs parameter of a Java base method receives for the trailing Python arguments, from the given index: each one converted to the component type, or the single trailing argument when it is already an array of it, as in a Java call.
      Parameters:
      arguments - The Python arguments
      from - The index of the first argument of the varargs parameter
      componentType - The component type of the varargs parameter
      Returns:
      The array, typed with the component type
    • noSuchMethod

      public static IllegalArgumentException noSuchMethod(String baseClass, String name, List<org.graalvm.polyglot.Value> arguments)
      The error for a call of a Java base method that no accessible overload of the base accepts; thrown by the generated micronautInvokeJavaBaseMethod.
      Parameters:
      baseClass - The Java base class name
      name - The method name
      arguments - The Python arguments
      Returns:
      The error