Class GraalPyRuntimeUtil
java.lang.Object
io.micronaut.context.python.GraalPyRuntimeUtil
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAdapter invoked from Python async member facades. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @Nullable TReturn a value asObjectso generated code can perform unchecked generic casts.static @Nullable ObjectasyncMemberValue(org.graalvm.polyglot.Value target, @Nullable Object value) Convert an injected Java member into a Python-context-local value suitable for async code.static org.graalvm.polyglot.ValuebindPythonDescriptor(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 Object[]coerceArgumentsToContext(org.graalvm.polyglot.Context context, Object[] args) Coerce arguments passed into a target Python context.coerceList(@Nullable List<E> list) Coerce a list of types that may extend fromValueCoercibleback to a native value list.Coerce a map of types that may extend fromValueCoercibleback to a native value map.static @Nullable ObjectcoerceToContext(@Nullable Object value, org.graalvm.polyglot.Context context) Coerce values passed into a target Python context.static @Nullable ObjectcoerceToContext(@Nullable Object value, org.graalvm.polyglot.Context context, Class<?> declaredType) Coerce a value using the generated Java bridge's declared parameter type.static @Nullable ObjectcoerceValue(@Nullable Object value) Coerce a generated Python-backed Java wrapper back to its native Python value.static <T> HttpResponse<T> convertHttpResponse(HttpResponse<?> response, Class<T> bodyType) Convert a response body to the declared Java body type.static <T> HttpResponse<T> convertHttpResponse(org.graalvm.polyglot.Value value, Class<T> bodyType) Convert a GraalPy-createdHttpResponseand its response body to the declared Java body type.static <T> @Nullable List<T> convertList(@Nullable List<?> list, PolyglotValueConverter<T> converter) Convert a Java list that may contain GraalPy values using a generated element converter.static <T> @Nullable List<T> convertList(org.graalvm.polyglot.Value graalValue, PolyglotValueConverter<T> converter) Convert a GraalPy Value representing a list using a generated element converter.static <T> @Nullable List<T> convertList(org.graalvm.polyglot.Value graalValue, Class<T> elementType) Convert a GraalPy Value representing a list to a Java List.static <K,V> @Nullable Map <K, V> convertMap(org.graalvm.polyglot.Value graalValue, Class<K> keyType, Class<V> valueType) Convert a GraalPy Value representing a dict to a Java Map.static @Nullable ObjectconvertObject(@Nullable org.graalvm.polyglot.Value value) Convert a GraalPy value to a general Java object while preserving host objects.static <T> Optional<T> convertOptional(org.graalvm.polyglot.Value graalValue, Class<T> elementType) Convert a GraalPy Value representing an Optional to a Java Optional.static <T> @Nullable Publisher<T> convertPublisher(org.graalvm.polyglot.Value value, Class<T> itemType) Convert a GraalPy Value representing a publisher to a typed Java publisher.static <T> Publisher<T> convertPublisher(Publisher<?> publisher, Class<T> itemType) Convert each item emitted by a Python-returned publisher to the declared Java item type.static <T> @Nullable Set<T> convertSet(org.graalvm.polyglot.Value graalValue, Class<T> elementType) Convert a GraalPy Value representing a set to a Java Set.static <T> @Nullable TconvertValue(org.graalvm.polyglot.Value value, Class<T> targetType) Generic value conversion method that handles primitives and recursively converts collections.static voidcopyTransferableMembers(@Nullable org.graalvm.polyglot.Value source, @Nullable org.graalvm.polyglot.Value target) Copy simple and host-backed Python instance members into another context.static StringenumStringValue(@Nullable org.graalvm.polyglot.Value value) Convert a Python enum value to the string representation exposed by Python.static @Nullable org.graalvm.polyglot.ValuegetRawClassMember(org.graalvm.polyglot.Value pythonClass, String name) Read a Python class member directly from the MRO dictionaries, bypassing descriptor binding.static org.graalvm.polyglot.ValueinvokePythonMethod(org.graalvm.polyglot.Value receiver, String name, Object[] arguments) Invoke a generated bridge method on a Python receiver.static booleanisNone(@Nullable org.graalvm.polyglot.Value value) Returns whether the value represents Java null or Python None.static voidAssign a member on a Python value after coercing the value into the same context.static @Nullable ObjectunwrapHostObject(@Nullable org.graalvm.polyglot.Value value, Class<?> targetType) Unwraps a generated Python wrapper that crossed a polyglot boundary as a host or proxy object.
-
Field Details
-
PYTHON
- See Also:
-
-
Constructor Details
-
GraalPyRuntimeUtil
public GraalPyRuntimeUtil()
-
-
Method Details
-
isNone
public static boolean isNone(@Nullable org.graalvm.polyglot.Value value) Returns whether the value represents Java null or Python None.- Parameters:
value- The polyglot value- Returns:
- Whether the value represents Java null or Python None
-
coerceMap
-
coerceList
Coerce a list of types that may extend fromValueCoercibleback to a native value list.- Type Parameters:
E- The element type of the list- Parameters:
list- The list- Returns:
- The resulting list
-
coerceValue
-
coerceToContext
-
coerceToContext
public static @Nullable Object coerceToContext(@Nullable Object value, org.graalvm.polyglot.Context context, Class<?> declaredType) Coerce a value using the generated Java bridge's declared parameter type. Some host objects implement collection interfaces as an implementation detail and should stay host objects unless the Python method declares the plain collection contract.- Parameters:
value- The value to coercecontext- The target contextdeclaredType- The declared Java bridge parameter type- Returns:
- The coerced value
-
coerceArgumentsToContext
-
putMember
-
asyncMemberValue
public static @Nullable Object asyncMemberValue(org.graalvm.polyglot.Value target, @Nullable Object value) Convert an injected Java member into a Python-context-local value suitable for async code.- Parameters:
target- The target Python object receiving the member.value- The Java value to expose.- Returns:
- A value that adapts Java async method results to Python awaitables.
-
copyTransferableMembers
public static void copyTransferableMembers(@Nullable org.graalvm.polyglot.Value source, @Nullable org.graalvm.polyglot.Value target) Copy simple and host-backed Python instance members into another context.- Parameters:
source- The source Python object.target- The target Python object.
-
invokePythonMethod
public static org.graalvm.polyglot.Value invokePythonMethod(org.graalvm.polyglot.Value receiver, String name, Object[] 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 thePythonContextRuntimeexecution 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
getattrfor 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 receivername- The method namearguments- The method arguments- Returns:
- The invocation result
-
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 classname- 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 descriptorreceiver- The receiver objectowner- The owner class- Returns:
- The bound descriptor, or the original descriptor if it cannot be bound
-
asObject
-
convertObject
Convert a GraalPy value to a general Java object while preserving host objects.- Parameters:
value- The source polyglot value- Returns:
- The converted object
-
unwrapHostObject
public static @Nullable Object unwrapHostObject(@Nullable org.graalvm.polyglot.Value value, Class<?> targetType) Unwraps a generated Python wrapper that crossed a polyglot boundary as a host or proxy object.- Parameters:
value- The source polyglot valuetargetType- The expected Java wrapper type- Returns:
- The existing host wrapper, or
nullwhen the value is not one
-
convertList
public static <T> @Nullable List<T> convertList(org.graalvm.polyglot.Value graalValue, Class<T> elementType) Convert a GraalPy Value representing a list to a Java List. Recursively converts nested collections.- Type Parameters:
T- the expected list element type- Parameters:
graalValue- the GraalPy Value (should be a list-like object)elementType- the expected element type for conversion- Returns:
- a Java List with converted elements
-
convertList
public static <T> @Nullable List<T> convertList(org.graalvm.polyglot.Value graalValue, PolyglotValueConverter<T> converter) Convert a GraalPy Value representing a list using a generated element converter.- Type Parameters:
T- the expected list element type- Parameters:
graalValue- the GraalPy Value (should be a list-like object)converter- the converter to apply to each element- Returns:
- a Java List with converted elements
-
convertList
public static <T> @Nullable List<T> convertList(@Nullable List<?> list, PolyglotValueConverter<T> converter) Convert a Java list that may contain GraalPy values using a generated element converter.- Type Parameters:
T- the expected list element type- Parameters:
list- the source listconverter- the converter to apply to GraalPy elements- Returns:
- a Java List with converted elements
-
convertMap
public static <K,V> @Nullable Map<K,V> convertMap(org.graalvm.polyglot.Value graalValue, Class<K> keyType, Class<V> valueType) Convert a GraalPy Value representing a dict to a Java Map. Recursively converts nested collections.- Type Parameters:
K- the expected key typeV- the expected value type- Parameters:
graalValue- the GraalPy Value (should be a dict-like object)keyType- the expected key type for conversionvalueType- the expected value type for conversion- Returns:
- a Java Map with converted keys and values
-
convertOptional
public static <T> Optional<T> convertOptional(org.graalvm.polyglot.Value graalValue, Class<T> elementType) Convert a GraalPy Value representing an Optional to a Java Optional. Handles None -> Optional.empty() and value -> Optional.of(value)- Type Parameters:
T- the expected optional element type- Parameters:
graalValue- the GraalPy Value to convertelementType- the expected element type for conversion- Returns:
- a Java Optional with the converted value or empty
-
convertSet
public static <T> @Nullable Set<T> convertSet(org.graalvm.polyglot.Value graalValue, Class<T> elementType) Convert a GraalPy Value representing a set to a Java Set. Recursively converts nested collections.- Type Parameters:
T- the expected set element type- Parameters:
graalValue- the GraalPy Value (should be a set-like object)elementType- the expected element type for conversion- Returns:
- a Java Set with converted elements
-
convertValue
Generic value conversion method that handles primitives and recursively converts collections.- Type Parameters:
T- The target type- Parameters:
value- The source polyglot valuetargetType- The target Java type- Returns:
- The converted value or
null
-
convertHttpResponse
public static <T> HttpResponse<T> convertHttpResponse(org.graalvm.polyglot.Value value, Class<T> bodyType) Convert a GraalPy-createdHttpResponseand its response body to the declared Java body type.- Type Parameters:
T- The response body type- Parameters:
value- The source polyglot responsebodyType- The declared response body type- Returns:
- The converted response
-
convertPublisher
Convert each item emitted by a Python-returned publisher to the declared Java item type.- Type Parameters:
T- The item type- Parameters:
publisher- The source publisheritemType- The declared publisher item type- Returns:
- The converted publisher
-
convertPublisher
public static <T> @Nullable Publisher<T> convertPublisher(org.graalvm.polyglot.Value value, Class<T> itemType) Convert a GraalPy Value representing a publisher to a typed Java publisher.- Type Parameters:
T- The item type- Parameters:
value- The source polyglot publisheritemType- The declared publisher item type- Returns:
- The converted publisher
-
convertHttpResponse
Convert a response body to the declared Java body type.- Type Parameters:
T- The response body type- Parameters:
response- The source responsebodyType- The declared response body type- Returns:
- The converted response
-
enumStringValue
Convert a Python enum value to the string representation exposed by Python.- Parameters:
value- The Python enum value- Returns:
- The enum string value
-