Class PythonCoercion
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAdapter invoked from Python async member facades. -
Method Summary
Modifier and TypeMethodDescriptionstatic @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 Object[]coerceArgumentsToContext(org.graalvm.polyglot.Context context, Object[] args) Coerce arguments passed into a target Python context.static Object[]coerceDependenciesToContext(org.graalvm.polyglot.Context context, Object[] args) Coerce the dependencies a pooled bean is constructed with into a context.coerceList(@Nullable List<E> list) Coerce a list of types that may extend fromValueCoercibleback to a native value list.coerceList(@Nullable List<E> list, @Nullable org.graalvm.polyglot.Context context) Coerce a list of types that may extend fromValueCoercibleback to a native value list for the given context: generated enum constants become the Python enum members of that context.Coerce a map whose keys or values may extend fromValueCoercibleback to a native value map.Coerce a map whose keys or values may extend fromValueCoercibleback to a native value map for the given context: generated enum constants among the keys and values become the Python enum members of that context, as a bare enum argument does.static org.graalvm.polyglot.ValuecoercePooledValue(PooledValueCoercible value, org.graalvm.polyglot.Context context) Converts a generated wrapper while preserving wrapper identity for the current conversion.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 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 org.graalvm.polyglot.ValueinterceptedTargetValue(InterceptedProxy<?> proxy) The Python object standing in for an AOP proxy of a Python class.static booleanisValueInContext(@Nullable org.graalvm.polyglot.Value value, org.graalvm.polyglot.Context context) Determines whether a polyglot value belongs to the supplied context.static <E> @Nullable List<E> The Java view of alistattribute of a Python object, for the generated field of the property: the Python list stays the attribute and the source of truth, reads and writes through the returned list reach it.static <K,V> @Nullable Map <K, V> The Java view of adictattribute of a Python object, for the generated field of the property, aslistView(Value, Class)for a list.static @Nullable List<?> putListMember(org.graalvm.polyglot.Value target, String name, @Nullable List<?> value, Class<?> elementType) Writes the list held by the generated field of a property to the attribute of the Python object and returns the field value to hold from then on.static @Nullable Map<?, ?> putMapMember(org.graalvm.polyglot.Value target, String name, @Nullable Map<?, ?> value, Class<?> keyType, Class<?> valueType) Writes the map held by the generated field of a property to the attribute of the Python object and returns the field value to hold from then on, asputListMember(Value, String, List, Class)for a list.static voidAssign a member on a Python value after coercing the value into the same context.static voidputMemberByReference(org.graalvm.polyglot.Value target, String name, @Nullable Object value) Assign a member on a Python value, handing a Java collection or a generated wrapper to Python as the host object it is instead of a converted copy.static voidputMembers(org.graalvm.polyglot.Value target, String[] names, @Nullable Object[] values) Assign several members on a Python value in a single guest call.static @Nullable ObjectpythonDateTime(@Nullable Object value) Tags the Java value of an element whose Python type isdatetime, declared withAnnotated[datetime, Instant](seePythonDateTime), so that it reaches Python as an awaredatetime.static voidrememberPooledValue(PooledValueCoercible source, org.graalvm.polyglot.Context context, org.graalvm.polyglot.Value value) Registers a newly allocated Python object before generated code populates its properties.
-
Method Details
-
coerceMap
Coerce a map whose keys or values may extend fromValueCoercibleback to a native value map. Keys are coerced like values, so a map keyed by generated Python wrappers reaches Python with its keys as Python objects.- Type Parameters:
K- The key type of the mapV- The value type of the map- Parameters:
map- The map- Returns:
- The resulting map
-
coerceMap
public static <K,V> @Nullable Map<Object,Object> coerceMap(@Nullable Map<K, V> map, @Nullable org.graalvm.polyglot.Context context) Coerce a map whose keys or values may extend fromValueCoercibleback to a native value map for the given context: generated enum constants among the keys and values become the Python enum members of that context, as a bare enum argument does.- Type Parameters:
K- The key type of the mapV- The value type of the map- Parameters:
map- The mapcontext- The target context, ornullwhen the map is converted later with its context- Returns:
- The resulting map
-
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
-
coerceList
public static <E> @Nullable List<Object> coerceList(@Nullable List<E> list, @Nullable org.graalvm.polyglot.Context context) Coerce a list of types that may extend fromValueCoercibleback to a native value list for the given context: generated enum constants become the Python enum members of that context.- Type Parameters:
E- The element type of the list- Parameters:
list- The listcontext- The target context, ornullwhen the list is converted later with its context- Returns:
- The resulting list
-
coerceValue
-
coerceToContext
-
listView
public static <E> @Nullable List<E> listView(org.graalvm.polyglot.Value member, Class<E> elementType) The Java view of alistattribute of a Python object, for the generated field of the property: the Python list stays the attribute and the source of truth, reads and writes through the returned list reach it. A Java list assigned to the attribute is returned as it is, and an iterable that is not a list is converted the wayPythonConversion.convertList(Value, Class)does.- Type Parameters:
E- The element type- Parameters:
member- The attribute valueelementType- The declared element type- Returns:
- The list, or
nullforNone
-
mapView
public static <K,V> @Nullable Map<K,V> mapView(org.graalvm.polyglot.Value member, Class<K> keyType, Class<V> valueType) The Java view of adictattribute of a Python object, for the generated field of the property, aslistView(Value, Class)for a list.- Type Parameters:
K- The key typeV- The value type- Parameters:
member- The attribute valuekeyType- The declared key typevalueType- The declared value type- Returns:
- The map, or
nullforNone
-
putListMember
public static @Nullable List<?> putListMember(org.graalvm.polyglot.Value target, String name, @Nullable List<?> value, Class<?> elementType) Writes the list held by the generated field of a property to the attribute of the Python object and returns the field value to hold from then on. A view of the attribute of this context is already current. Any other Java list is copied into a new Python list, so the attribute keeps its native type, and the view of that list becomes the field value: the collection assigned from Java is detached from that point. A collection class of its own (not a JDK one) is passed by reference, as an argument would be.- Parameters:
target- The Python objectname- The attribute namevalue- The field valueelementType- The declared element type- Returns:
- The list to keep in the field
-
putMapMember
public static @Nullable Map<?,?> putMapMember(org.graalvm.polyglot.Value target, String name, @Nullable Map<?, ?> value, Class<?> keyType, Class<?> valueType) Writes the map held by the generated field of a property to the attribute of the Python object and returns the field value to hold from then on, asputListMember(Value, String, List, Class)for a list.- Parameters:
target- The Python objectname- The attribute namevalue- The field valuekeyType- The declared key typevalueType- The declared value type- Returns:
- The map to keep in the field
-
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. A collection argument is only rebuilt with coerced elements when the Python method declares the plain collection contract; a parameter declared with a more specific type (a cache, a view) always stays the host object it is.- Parameters:
value- The value to coercecontext- The target contextdeclaredType- The declared Java bridge parameter type- Returns:
- The coerced value
-
isValueInContext
public static boolean isValueInContext(@Nullable org.graalvm.polyglot.Value value, org.graalvm.polyglot.Context context) Determines whether a polyglot value belongs to the supplied context.- Parameters:
value- The value to inspectcontext- The expected context- Returns:
- Whether the value belongs to the context
-
coercePooledValue
public static org.graalvm.polyglot.Value coercePooledValue(PooledValueCoercible value, org.graalvm.polyglot.Context context) Converts a generated wrapper while preserving wrapper identity for the current conversion.- Parameters:
value- The generated wrappercontext- The target context- Returns:
- The context-local Python value
-
rememberPooledValue
public static void rememberPooledValue(PooledValueCoercible source, org.graalvm.polyglot.Context context, org.graalvm.polyglot.Value value) Registers a newly allocated Python object before generated code populates its properties.- Parameters:
source- The source wrappercontext- The target contextvalue- The newly allocated value
-
pythonDateTime
Tags the Java value of an element whose Python type isdatetime, declared withAnnotated[datetime, Instant](seePythonDateTime), so that it reaches Python as an awaredatetime. AnInstant,OffsetDateTimeorZonedDateTimewithout the tag stays the Java object: Python code that declares the Java type calls Java APIs with it.- Parameters:
value- The Java value- Returns:
- The tagged value, or the value itself when it is not one of those types
-
coerceArgumentsToContext
-
coerceDependenciesToContext
public static Object[] coerceDependenciesToContext(org.graalvm.polyglot.Context context, Object[] args) Coerce the dependencies a pooled bean is constructed with into a context.As
coerceArgumentsToContext(Context, Object[])except for a dependency that is a Python bean of a single context: a singleton, or anything else that is not pooled. Such a bean cannot be reconstructed elsewhere -- there is one Python object and it belongs to its own context -- so the Java wrapper is passed instead, and calls through it return to the context that owns it. Rebuilding it is not merely unsupported, it is wrong: two contexts would hold two objects where the application asked for one.This is the same choice module injection makes for the same reason, and it is what the compile-time warning about a pooled type depending on a singleton Python bean is naming: the dependency works, and the work behind it runs in one context however many the pool has.
- Parameters:
context- The target contextargs- The dependencies, already resolved by injection- Returns:
- The arguments to construct with
-
interceptedTargetValue
The Python object standing in for an AOP proxy of a Python class.A generated proxy of a Python class (the scoped proxy of a
@Refreshablefactory bean, for example) is a subclass of the generated stub, or an implementation of the generated interface, that stands in for the bean its scope currently holds; it has no Python object of its own. Python code receiving the proxy gets a Python scoped proxy of the same class instead: every attribute read, write and method call is forwarded to the Python object of the bean the proxy resolves through its scope at that moment, so a refreshed or replaced bean is seen by Python callers the way Java callers see it. The Python proxy is created once per proxy instance and context. The target is not resolved here: a lazy proxy resolves it on the first use from Python, and a target that turns out to be a Java object (a Java implementation of a Python abstract base class) is forwarded to as the host object it is. The Python proxy carries the AOP proxy as its host object reference, so when Python hands it back to Java (a method returning the injected bean) Java receives the AOP proxy again, with its scope and interceptors, rather than the bean the scope holds at that moment.- Parameters:
proxy- The proxy, a generated stub instance or an implementation of a generated interface- Returns:
- The Python scoped proxy of the intercepted target
- Since:
- 5.2.0
-
putMember
-
putMemberByReference
public static void putMemberByReference(org.graalvm.polyglot.Value target, String name, @Nullable Object value) Assign a member on a Python value, handing a Java collection or a generated wrapper to Python as the host object it is instead of a converted copy.Used by the generated wrapper of an introspected class whose state is owned by its Java fields (the object was created from Java or loaded from storage): the Python attribute then is the Java collection, so an item added or removed in Python is added or removed from the Java field, and a nested object is the Java wrapper, so a write to its attribute reaches the Java field of that wrapper. A frozen dataclass, a polyglot value or a value of a standard type is coerced as by
putMember(Value, String, Object).- Parameters:
target- The Python object to updatename- The member namevalue- The member value
-
putMembers
public static void putMembers(org.graalvm.polyglot.Value target, String[] names, @Nullable Object[] values) Assign several members on a Python value in a single guest call.Rebuilding a dataclass in a pooled context writes every field; doing that with one helper invocation instead of one per field removes most of the per-field interop cost.
- Parameters:
target- The Python object to updatenames- The member namesvalues- The member values, positionally matchingnames
-
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.
-