Class GraalPyUtil
java.lang.Object
io.micronaut.python.processing.util.GraalPyUtil
Utility class for GraalPy integration, providing type conversion and resolution utilities
for Python AST processing within Micronaut.
- Since:
- 5.2.0
- Author:
- Micronaut Team
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectconvertValueToJava(org.graalvm.polyglot.Value value, ClassElement classElement, PythonVisitorContext visitorContext) Converts GraalPy Value objects to Java types based on the provided ClassElement type information.static ObjectconvertValueToJava(org.graalvm.polyglot.Value value, VisitorContext visitorContext) Utility method to convert GraalPy Value objects to Java types.static booleanisValidClassName(String className) static StringparsePythonDocstring(String docstring) Parses a Python docstring to extract the main description.static ClassElementresolvePythonTypeToJava(TypeRef typeRef, PythonVisitorContext visitorContext, Map<String, ClassElement> boundGenerics) Resolves a Python type annotation to a Java ClassElement.static ClassElementresolvePythonTypeToJava(String typeAnnotation, PythonVisitorContext visitorContext, Map<String, ClassElement> boundGenerics) Resolves a Python type annotation to a Java ClassElement.
-
Constructor Details
-
GraalPyUtil
public GraalPyUtil()
-
-
Method Details
-
convertValueToJava
public static Object convertValueToJava(org.graalvm.polyglot.Value value, VisitorContext visitorContext) Utility method to convert GraalPy Value objects to Java types. This extracts the common type conversion logic used for both annotations and attribute values.- Parameters:
value- the GraalPy Value to convertvisitorContext- The visitor context- Returns:
- the converted Java object, or the original value if conversion is not possible
-
resolvePythonTypeToJava
public static ClassElement resolvePythonTypeToJava(TypeRef typeRef, PythonVisitorContext visitorContext, Map<String, ClassElement> boundGenerics) Resolves a Python type annotation to a Java ClassElement. Handles primitive types, collections with generics (list[int], dict[str, int]), and supports recursive resolution for nested generics.- Parameters:
typeRef- The python type defvisitorContext- the visitor context for class element lookupboundGenerics- The bound generics- Returns:
- the resolved ClassElement, or Object ClassElement if resolution fails
-
resolvePythonTypeToJava
public static ClassElement resolvePythonTypeToJava(String typeAnnotation, PythonVisitorContext visitorContext, Map<String, ClassElement> boundGenerics) Resolves a Python type annotation to a Java ClassElement. Handles primitive types, collections with generics (list[int], dict[str, int]), and supports recursive resolution for nested generics.- Parameters:
typeAnnotation- the Python type annotation string (e.g., "int", "str", "bool", "float", "list[int]", "dict[str, int]")visitorContext- the visitor context for class element lookupboundGenerics- The bound generic types- Returns:
- the resolved ClassElement, or Object ClassElement if resolution fails
-
convertValueToJava
public static Object convertValueToJava(org.graalvm.polyglot.Value value, ClassElement classElement, PythonVisitorContext visitorContext) Converts GraalPy Value objects to Java types based on the provided ClassElement type information. This method handles type-specific conversion for annotation members and other typed values.- Parameters:
value- the GraalPy Value to convertclassElement- the ClassElement representing the target Java typevisitorContext- The visitor context- Returns:
- the converted Java object, or the original value if conversion is not possible
-
isValidClassName
-
parsePythonDocstring
Parses a Python docstring to extract the main description. Removes opening/closing quotes and stops at structured sections like Args:, Returns:, etc.- Parameters:
docstring- the raw Python docstring- Returns:
- the parsed main description, or empty string if docstring is null/empty
-