Class PythonConversion
Maintainer notes. The public methods are called by generated bridge code (see
PythonStubGenerator) on every value that crosses from Python to Java, so they sit on the
hot path: keep them free of meta-object lookups and string formatting. The package-private methods
are the per-type converters PythonCoercion and convertValue(Value, Class) dispatch to. Every
converter follows the same rules: None converts to null (or an empty
Optional), a value that is already a host object of the target type is returned as is,
a generated wrapper (ValueCoercible) is unwrapped rather than copied, and a conversion
that cannot be done is an exception, never an empty result, so a bad element does not silently
shrink a collection.
- Since:
- 5.2.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @Nullable TReturn a value asObjectso generated code can perform unchecked generic casts.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 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 StringenumStringValue(@Nullable org.graalvm.polyglot.Value value) Convert a Python enum value to the string representation exposed by Python.static booleanisNone(@Nullable org.graalvm.polyglot.Value value) Returns whether the value represents Java null or Python None.static <T> @Nullable TsubclassWrapper(@Nullable org.graalvm.polyglot.Value value, Class<T> declaredType) The wrapper of a Python object for the generated subclass it is an instance of, when the object's own class is a generated subclass of the declared wrapper type: a Python subclass instance passed or returned as its Python base type then keeps its runtime type, and with it its introspection.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.
-
Method Details
-
isNone
public static boolean isNone(@Nullable org.graalvm.polyglot.Value value) Returns whether the value represents Java null or Python None.Python
Noneis an interop null, soValue.isNull()is the complete check. This method is on the hot path of every bridge call and conversion, so it must not touch the meta object or the string representation of the value.- Parameters:
value- The polyglot value- Returns:
- Whether the value represents Java null or Python None
-
subclassWrapper
public static <T> @Nullable T subclassWrapper(@Nullable org.graalvm.polyglot.Value value, Class<T> declaredType) The wrapper of a Python object for the generated subclass it is an instance of, when the object's own class is a generated subclass of the declared wrapper type: a Python subclass instance passed or returned as its Python base type then keeps its runtime type, and with it its introspection. Returnsnullwhen the object is of the declared type itself, of a class with no generated wrapper, or not a Python object at all, in which case the caller wraps it as the declared type.The lookup reads the module and qualified name of the object's class; the resolved factory is cached per declared type and Python class.
- Type Parameters:
T- The declared wrapper type- Parameters:
value- The Python objectdeclaredType- The declared wrapper type- Returns:
- The wrapper of the object's own generated class, or
null
-
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.The attempts run in this order, and the order matters: a host object that is a
ValueCoercibleproxy is unwrapped first (a generated wrapper that went through Python must come back as the same Java object, not a copy); a host object of the target type is returned as is; a Java enum target is resolved from the Python enum's name or value; a mapped wrapper type (TargetTypeMapping) is resolved through the wrapper registry; and only then doesValue.as(Class)run, which handles primitives, strings and the collection types by GraalPy's own rules.- Type Parameters:
T- The target type- Parameters:
value- The source polyglot valuetargetType- The target Java type- Returns:
- The converted value or
null
-
enumStringValue
Convert a Python enum value to the string representation exposed by Python.- Parameters:
value- The Python enum value- Returns:
- The enum string value
-