Class PythonInvocation

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

@Internal public final class PythonInvocation extends Object
Invocation of Python methods and descriptors from generated Java bridges.
Since:
5.2.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.graalvm.polyglot.Value
    bindPythonDescriptor(org.graalvm.polyglot.Value descriptor, Object receiver, org.graalvm.polyglot.Value owner)
    Bind a raw Python descriptor to a receiver when the descriptor protocol is available.
    static @Nullable org.graalvm.polyglot.Value
    getRawClassMember(org.graalvm.polyglot.Value pythonClass, String name)
    Read a Python class member directly from the MRO dictionaries, bypassing descriptor binding.
    static org.graalvm.polyglot.Value
    invokeIntroducedMethod(org.graalvm.polyglot.Value receiver, String name, Class<?> returnType, Object @Nullable [] arguments)
    Invoke a method that an introduction advice adds to a Python class through the interfaces of its @Introduction.
    static org.graalvm.polyglot.Value
    invokePythonMethod(org.graalvm.polyglot.Value receiver, String name, Object @Nullable [] arguments)
    Invoke a generated bridge method on a Python receiver.
    static Object[]
    withVarargs(Object[] arguments, @Nullable Object varargs)
    Spread a Java varargs array into the positional arguments of a Python call.

    Methods inherited from class Object

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

    • invokePythonMethod

      public static org.graalvm.polyglot.Value invokePythonMethod(org.graalvm.polyglot.Value receiver, String name, Object @Nullable [] arguments)
      Invoke a generated bridge method on a Python receiver.

      Generated Java stubs use this method instead of calling Value.invokeMember(String, Object...) directly so every Python call enters the PythonContextRuntime execution tracker for the receiver's actual context. The execution frame keeps graceful shutdown and pooled-context cleanup from observing the context as idle while a bridge invocation, or nested bridge invocation, is still unwinding.

      Invocation is delegated to a context-local Python helper because Python method lookup is not just a map lookup. The helper first uses getattr for normal bound-method behavior, then falls back to walking the class __mro__ and applying __get__ so descriptors and inherited methods are invoked with Python semantics before the result crosses back to Java.

      Parameters:
      receiver - The Python receiver
      name - The method name
      arguments - The method arguments, or null for none
      Returns:
      The invocation result
    • withVarargs

      public static Object[] withVarargs(Object[] arguments, @Nullable Object varargs)
      Spread a Java varargs array into the positional arguments of a Python call.

      A Python *args parameter is bridged as a trailing Java array. Python expects the elements as separate positional arguments, so the generated bridge appends them to the fixed arguments instead of passing the array as a single argument.

      Parameters:
      arguments - The fixed arguments
      varargs - The varargs array, or null for no additional arguments
      Returns:
      The positional arguments of the Python call
    • invokeIntroducedMethod

      public static org.graalvm.polyglot.Value invokeIntroducedMethod(org.graalvm.polyglot.Value receiver, String name, Class<?> returnType, Object @Nullable [] arguments)
      Invoke a method that an introduction advice adds to a Python class through the interfaces of its @Introduction. The proxy of the class carries such a method as a Python member; an instance that is not the proxy (an entity instantiated by JPA, a value created from Java) has no member of that name and, as in Java where only the proxy class implements the introduced interface, no implementation for it: the result is then None when the method returns a reference or nothing. A method returning a primitive has no value to answer without an implementation, so the missing member is reported instead of surfacing later as a failed conversion of None.
      Parameters:
      receiver - The Python receiver
      name - The method name
      returnType - The declared return type of the introduced method
      arguments - The method arguments, or null for none
      Returns:
      The invocation result, or None if the receiver has no such member
      Throws:
      IllegalStateException - If the receiver has no such member and the method returns a primitive
    • getRawClassMember

      public static @Nullable org.graalvm.polyglot.Value getRawClassMember(org.graalvm.polyglot.Value pythonClass, String name)
      Read a Python class member directly from the MRO dictionaries, bypassing descriptor binding.
      Parameters:
      pythonClass - The Python class
      name - The member name
      Returns:
      The raw member, or null if none exists
    • bindPythonDescriptor

      public static org.graalvm.polyglot.Value bindPythonDescriptor(org.graalvm.polyglot.Value descriptor, Object receiver, org.graalvm.polyglot.Value owner)
      Bind a raw Python descriptor to a receiver when the descriptor protocol is available.
      Parameters:
      descriptor - The raw descriptor
      receiver - The receiver object
      owner - The owner class
      Returns:
      The bound descriptor, or the original descriptor if it cannot be bound