Class PythonMethodElement

java.lang.Object
io.micronaut.inject.ast.annotation.AbstractAnnotationElement
io.micronaut.python.processing.element.PythonMethodElement
All Implemented Interfaces:
ElementProvider, AnnotatedElement, AnnotationMetadata, AnnotationMetadataDelegate, AnnotationMetadataProvider, AnnotationSource, Described, Named, MutableAnnotationMetadataDelegate<Element>, Element, MemberElement, MethodElement
Direct Known Subclasses:
PythonConstructorElement

public non-sealed class PythonMethodElement extends AbstractAnnotationElement implements MethodElement, ElementProvider
Represents a Python method/function as a Micronaut MethodElement.

This class wraps a FunctionDef node of the Python AST, providing full MethodElement interface implementation including parameters, return types, and visibility.

Since:
5.2.0
Author:
Micronaut Team
See Also:
  • Constructor Details

    • PythonMethodElement

      public PythonMethodElement(FunctionDef functionDef, PythonProcessingEnvironment environment, ClassElement declaringType, ClassElement owningType, ElementAnnotationMetadataFactory metadataFactory)
      Constructs a new PythonMethodElement from the given FunctionDef.
      Parameters:
      functionDef - the function definition node; must not be null
      environment - the Python processing environment; must not be null
      declaringType - the class that declares this method; must not be null
      owningType - the class that owns this method (may be a subclass); must not be null
      metadataFactory - the annotation metadata factory; must not be null
      Throws:
      NullPointerException - if any parameter is null
  • Method Details

    • getDescription

      public String getDescription(boolean simple)
      Description copied from interface: Described
      Allows returning a more simple description if requested.
      Specified by:
      getDescription in interface Described
      Specified by:
      getDescription in interface Element
      Specified by:
      getDescription in interface MethodElement
      Parameters:
      simple - True if a simple description is required
      Returns:
      The simple description
    • withNewParameters

      public @NonNull MethodElement withNewParameters(@NotNull @NotNull @NonNull ParameterElement... newParameters)
      Description copied from interface: MethodElement
      Takes this method element and transforms into a new method element with the given parameters appended to the existing parameters.
      Specified by:
      withNewParameters in interface MethodElement
      Parameters:
      newParameters - The new parameters
      Returns:
      A new method element
    • withNewOwningType

      public @NonNull MethodElement withNewOwningType(@NonNull ClassElement owningType)
      Description copied from interface: MethodElement
      Returns a new method with a new owning type.
      Specified by:
      withNewOwningType in interface MethodElement
      Parameters:
      owningType - The owning type.
      Returns:
      A new method element
    • copyValues

      protected void copyValues(io.micronaut.python.processing.element.AbstractPythonElement element)
      Parameters:
      element - the values to be copied to
    • isAbstract

      public boolean isAbstract()
      Whether the method is abstract. A function decorated with @abstractmethod, declared by a Protocol, or declared by an ABC with the ... placeholder body is abstract. A placeholder body is also abstract in an introduction type (@Client, a repository, an AI service), whose methods are implemented by the introduction advice; in a concrete class it is a method returning None.
      Specified by:
      isAbstract in interface Element
      Returns:
      True if the method is abstract
    • isAsync

      public boolean isAsync()
      Returns whether this method was declared with async def.
      Returns:
      Whether this method was declared with async def.
    • isAsyncGenerator

      public boolean isAsyncGenerator()
      Returns whether this method is an async generator (async def with a yield), bridged as a Publisher of its elements.
      Returns:
      Whether this method is an async generator
    • getAnnotationMetadataToWrite

      protected MutableAnnotationMetadataDelegate<?> getAnnotationMetadataToWrite()
      Description copied from class: AbstractAnnotationElement
      Get annotation metadata to add or remove annotations.
      Overrides:
      getAnnotationMetadataToWrite in class AbstractAnnotationElement
      Returns:
      The annotation metadata to write
    • getMethodAnnotationMetadata

      public ElementAnnotationMetadata getMethodAnnotationMetadata()
      Description copied from interface: MethodElement
      Returns the method annotations. The method will only return annotations defined on a method or inherited from the super methods, while AnnotationMetadataProvider.getAnnotationMetadata() for a method combines the class and the method annotations. NOTE: For a constructor AnnotationMetadataProvider.getAnnotationMetadata() will not combine the class annotations. See MethodElement.getDeclaredMethodAnnotationMetadata() for the annotations of this declaration only.

      Implementations that support adding or removing annotations at compilation time are expected to override this method and return a delegate backed by the method's own mutable annotation metadata, typically built with MethodElementAnnotationsHelper. Overriding it is a separate obligation from implementing Element.annotate(String, java.util.function.Consumer): an element that supports the latter and not this one still cannot be annotated through this surface. The default returned here cannot separate the method annotations from the class annotations, so it approximates the read side with AnnotationMetadataProvider.getAnnotationMetadata() and rejects the write side with an exception that names this element.

      Specified by:
      getMethodAnnotationMetadata in interface MethodElement
      Returns:
      The method annotation metadata
    • getAnnotationMetadata

      public AnnotationMetadata getAnnotationMetadata()
      Description copied from interface: AnnotationMetadataProvider
      Supplies the metadata. Defaults to AnnotationMetadata.EMPTY_METADATA.
      Specified by:
      getAnnotationMetadata in interface AnnotationMetadataProvider
      Overrides:
      getAnnotationMetadata in class AbstractAnnotationElement
      Returns:
      The AnnotationMetadata
    • hasStereotype

      public boolean hasStereotype(@Nullable @Nullable Class<? extends Annotation> annotation)
      Description copied from interface: AnnotationMetadata

      Checks whether this object has the given annotation stereotype on the object itself or inherited from a parent

      .

      An annotation stereotype is a meta annotation potentially applied to another annotation

      Specified by:
      hasStereotype in interface AnnotationMetadata
      Specified by:
      hasStereotype in interface AnnotationMetadataDelegate
      Parameters:
      annotation - The annotation
      Returns:
      True if the annotation is present
    • hasStereotype

      public boolean hasStereotype(@Nullable @Nullable String annotation)
      Description copied from interface: AnnotationMetadata

      Checks whether this object has the given annotation stereotype on the object itself or inherited from a parent

      .

      An annotation stereotype is a meta annotation potentially applied to another annotation

      Specified by:
      hasStereotype in interface AnnotationMetadata
      Specified by:
      hasStereotype in interface AnnotationMetadataDelegate
      Parameters:
      annotation - The annotation
      Returns:
      True if the annotation is present
    • getTargetAnnotationMetadata

      public AnnotationMetadata getTargetAnnotationMetadata()
      Description copied from interface: AnnotationSource
      Unwraps possible delegate or provider.
      Specified by:
      getTargetAnnotationMetadata in interface AnnotationMetadata
      Specified by:
      getTargetAnnotationMetadata in interface AnnotationMetadataDelegate
      Specified by:
      getTargetAnnotationMetadata in interface AnnotationMetadataProvider
      Specified by:
      getTargetAnnotationMetadata in interface AnnotationSource
      Returns:
      unwrapped
      See Also:
    • isReflectionRequired

      public boolean isReflectionRequired()
      Description copied from interface: MemberElement
      Returns whether this member element will require reflection to invoke or retrieve at runtime.

      This method uses MemberElement.getOwningType() as the calling type for this method.

      Specified by:
      isReflectionRequired in interface MemberElement
      Returns:
      Will return true if reflection is required.
    • isReflectionRequired

      public boolean isReflectionRequired(ClassElement callingType)
      Description copied from interface: MemberElement
      Returns whether this member element will require reflection to invoke or retrieve at runtime.
      Specified by:
      isReflectionRequired in interface MemberElement
      Parameters:
      callingType - The calling type
      Returns:
      Will return true if reflection is required.
    • isStatic

      public boolean isStatic()
      Specified by:
      isStatic in interface Element
      Returns:
      True if the element is static.
    • getNativeType

      public FunctionDef getNativeType()
      Returns the native FunctionDef object that backs this element.
      Specified by:
      getNativeType in interface Element
      Returns:
      the underlying FunctionDef node
    • getReturnType

      public ClassElement getReturnType()
      Specified by:
      getReturnType in interface MethodElement
      Returns:
      The return type of the method
    • isDeclaredNullable

      public boolean isDeclaredNullable()
      Specified by:
      isDeclaredNullable in interface AnnotatedElement
      Returns:
      Whether the element is nullable.
    • isNullable

      public boolean isNullable()
      Specified by:
      isNullable in interface AnnotatedElement
      Returns:
      Whether the element is nullable.
    • isNonNull

      public boolean isNonNull()
      Specified by:
      isNonNull in interface AnnotatedElement
      Returns:
      Whether the element is declared as not being null
    • isDeclaredNonNull

      public boolean isDeclaredNonNull()
      Specified by:
      isDeclaredNonNull in interface AnnotatedElement
      Returns:
      Whether the element is declared as not being null
    • getParameters

      public ParameterElement[] getParameters()
      Specified by:
      getParameters in interface MethodElement
      Returns:
      The method parameters
    • withParameters

      public MethodElement withParameters(ParameterElement... newParameters)
      Description copied from interface: MethodElement
      Takes this method element and transforms into a new method element with the given parameters.
      Specified by:
      withParameters in interface MethodElement
      Parameters:
      newParameters - The new parameters
      Returns:
      A new method element
    • withInheritedSignature

      public MethodElement withInheritedSignature(ParameterElement[] newParameters, @Nullable @Nullable ClassElement newReturnType)
      Returns a copy of this method that reports the given signature instead of the one derived from the Python type hints. A Python method that overrides a Java method adopts the Java signature this way: the hints are lossy (int for a boxed Integer id, list[T] for Iterable<T>) while the generated stub implements the Java signature, and the bean definition has to dispatch to that one.

      A class may reach the same inherited method through more than one interface -- a repository implementing both CrudRepository and PageableRepository adopts findById twice. The second pass compares the inherited return type against the one already adopted, finds them equal and asks for no change, so null must leave the adopted type alone rather than reset the method to its Python return type.

      Parameters:
      newParameters - The parameters
      newReturnType - The return type, or null to keep the one already in effect
      Returns:
      The copy
    • isPublic

      public boolean isPublic()
      Specified by:
      isPublic in interface Element
      Returns:
      True if the element is public.
    • isPrivate

      public boolean isPrivate()
      Specified by:
      isPrivate in interface Element
      Returns:
      True if the element is private.
    • getDeclaringType

      public ClassElement getDeclaringType()
      Specified by:
      getDeclaringType in interface MemberElement
      Returns:
      The declaring type of the element.
    • getOwningType

      public ClassElement getOwningType()
      Description copied from interface: MemberElement
      The owning type is the type that owns this element. This can differ from MemberElement.getDeclaringType() in the case of inheritance since this method will return the subclass that owners the inherited member, whilst MemberElement.getDeclaringType() will return the super class that declares the type.
      Specified by:
      getOwningType in interface MemberElement
      Returns:
      The owning type.
    • getOverriddenMethods

      public Collection<MethodElement> getOverriddenMethods()
      Description copied from interface: MethodElement
      Get overridden methods by this method.
      Specified by:
      getOverriddenMethods in interface MethodElement
      Returns:
      The overridden method by this method.
    • getGenericReturnType

      public ClassElement getGenericReturnType()
      Description copied from interface: MethodElement
      The generic return type of the method.
      Specified by:
      getGenericReturnType in interface MethodElement
      Returns:
      The return type of the method
    • isVarArgs

      public boolean isVarArgs()
      Description copied from interface: MethodElement
      If method has varargs parameter.
      Specified by:
      isVarArgs in interface MethodElement
      Returns:
      True if it does
    • getDocumentation

      public Optional<String> getDocumentation(boolean parseContent)
      Specified by:
      getDocumentation in interface Element
      Parameters:
      parseContent - True if the content should be parsed to extract documentation
      Returns:
      The documentation, if any.
    • equals

      public boolean equals(Object o)
    • hashCode

      public int hashCode()
    • copyThis

      protected io.micronaut.python.processing.element.AbstractPythonElement copyThis()
      Returns:
      copy of this element
    • withAnnotationMetadata

      public MethodElement withAnnotationMetadata(AnnotationMetadata annotationMetadata)
      Description copied from interface: Element
      Copies this element and overrides its annotations.
      Specified by:
      withAnnotationMetadata in interface Element
      Specified by:
      withAnnotationMetadata in interface MemberElement
      Specified by:
      withAnnotationMetadata in interface MethodElement
      Parameters:
      annotationMetadata - The annotation metadata
      Returns:
      A new element
    • getDeclaredTypeVariables

      public List<? extends GenericPlaceholderElement> getDeclaredTypeVariables()
      Specified by:
      getDeclaredTypeVariables in interface MethodElement
      Returns:
      The type arguments declared on this method.
    • element

      @Nullable public @Nullable Element element()
      Specified by:
      element in interface ElementProvider
      Returns:
      The native element.
    • makeCopy

      protected final io.micronaut.python.processing.element.AbstractPythonElement makeCopy()
    • getName

      public String getName()
      Specified by:
      getName in interface Element
      Specified by:
      getName in interface Named
      Returns:
      The name of the element. For a type this represents the binary name.
    • isProtected

      public boolean isProtected()
      Specified by:
      isProtected in interface Element
      Returns:
      True if the element is protected.
    • getModifiers

      public Set<ElementModifier> getModifiers()
      Python has no access modifiers, so the modifiers are derived from the visibility, static, abstract and final predicates of the element: an element is public unless its name marks it private, which keeps the modifier set consistent with Element.isPublic(), Element.isStatic(), Element.isAbstract() and Element.isFinal() for the callers that filter on modifiers, such as ElementQuery.modifiers(...) and BeanElementBuilder.produceBeans(...).
      Specified by:
      getModifiers in interface Element
      Returns:
      The modifiers