Class GraalPyUtil

java.lang.Object
io.micronaut.python.processing.util.GraalPyUtil

public final class GraalPyUtil extends Object
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 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 convert
      visitorContext - 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 def
      visitorContext - the visitor context for class element lookup
      boundGenerics - 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 lookup
      boundGenerics - 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 convert
      classElement - the ClassElement representing the target Java type
      visitorContext - The visitor context
      Returns:
      the converted Java object, or the original value if conversion is not possible
    • isValidClassName

      public static boolean isValidClassName(String className)
    • parsePythonDocstring

      public static String parsePythonDocstring(String docstring)
      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