Class PythonClassElement

All Implemented Interfaces:
ElementProvider, AnnotatedElement, AnnotationMetadata, AnnotationMetadataDelegate, AnnotationMetadataProvider, AnnotationSource, Described, Named, MutableAnnotationMetadataDelegate<Element>, ArrayableClassElement, ClassElement, Element, TypedElement
Direct Known Subclasses:
PythonAnnotationElement

public sealed class PythonClassElement extends AbstractPythonClassElement permits PythonAnnotationElement
Class element implementation for Python classes.
  • Constructor Details

  • Method Details

    • registered

      @Internal public static PythonClassElement registered(ClassDef classDef, PythonProcessingEnvironment environment)
      Creates the element of a Python class for the class registry of the processing environment without deriving its class level metadata yet: initializeClassMetadata() runs annotation mappers, which may look other Python classes up through the visitor context, so it is applied once every class of the environment is registered.
      Parameters:
      classDef - The class definition
      environment - The processing environment
      Returns:
      The element
    • initializeClassMetadata

      @Internal public void initializeClassMetadata()
      Derives the class level metadata that depends on the annotations of the class definition: the introspection excludes, the bean stereotype of a class with property injection points and the interfaces introduced by an introduction advice. Building that metadata runs the annotation mappers of the class annotations.
    • getType

      public @NonNull ClassElement getType()
      Returns:
      The type of the element
    • copyThis

      protected PythonClassElement copyThis()
      Specified by:
      copyThis in class AbstractPythonClassElement
      Returns:
      copy of this element
    • copyValues

      protected void copyValues(io.micronaut.python.processing.element.AbstractPythonElement element)
      Overrides:
      copyValues in class AbstractPythonClassElement
      Parameters:
      element - the values to be copied to
    • isPythonSource

      public final boolean isPythonSource()
    • addAssociatedBean

      public BeanElementBuilder addAssociatedBean(ClassElement type)
      Description copied from interface: ClassElement
      This method adds an associated bean using this class element as the originating element.

      Note that this method can only be called on classes being directly compiled by Micronaut. If the ClassElement is loaded from pre-compiled code an UnsupportedOperationException will be thrown.

      Parameters:
      type - The type of the bean
      Returns:
      A bean builder
    • isInner

      public boolean isInner()
      Description copied from interface: ClassElement
      Is this type an inner class.
      Returns:
      True if it is an inner class
    • isStatic

      public boolean isStatic()
      Returns:
      True if the element is static.
    • getEnclosingType

      public Optional<ClassElement> getEnclosingType()
      Description copied from interface: ClassElement
      Returns the enclosing type if ClassElement.isInner() return true.
      Returns:
      The enclosing type if any
    • isMemberOfEnclosingType

      public boolean isMemberOfEnclosingType()
      Whether the Java class generated for this class is a member type of the Java class generated for the enclosing Python class.

      A class nested in a Python class compiles to a member type of the generated class of its enclosing class (binary name Outer$Inner, as before), so that Java sees the nesting: JUnit runs a @Nested test class with the context of the enclosing @MicronautTest. Enums, interfaces and pooled classes are generated as top-level types named Outer$Inner, whether they are nested or enclose other classes.

      Returns:
      true when the generated class is a member type of the enclosing generated class
    • getCanonicalName

      public String getCanonicalName()
      Description copied from interface: ClassElement
      This method will return the name of the underlying type automatically unwrapping in the case of an optional or wrapped representation of the type.
      Returns:
      Returns the canonical name of the type.
    • createWithArrayDimensions

      protected ClassElement createWithArrayDimensions(int arrayDimensions)
      Specified by:
      createWithArrayDimensions in class AbstractPythonClassElement
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getDefaultConstructor

      public Optional<MethodElement> getDefaultConstructor()
      Description copied from interface: ClassElement
      Find and return a single default constructor. A default constructor is one without arguments that is accessible.
      Returns:
      The default constructor if one is present
    • isCallableWithoutArguments

      public static boolean isCallableWithoutArguments(MethodElement constructor)
      Whether the given __init__ takes parameters that all have a default value, so the Python class can be instantiated without arguments.
      Parameters:
      constructor - The constructor
      Returns:
      True if it is a constructor whose parameters all have defaults
    • getPrimaryConstructor

      public Optional<MethodElement> getPrimaryConstructor()
      Description copied from interface: ClassElement
      Find and return a single primary constructor. If more than constructor candidate exists, then return empty unless a constructor is found that is annotated with either Creator or AnnotationUtil.INJECT.
      Returns:
      The primary constructor if one is present
    • isDataclass

      public boolean isDataclass()
      Whether this class is decorated with @dataclass.
      Returns:
      true for a Python dataclass
    • isAssignable

      public boolean isAssignable(String type)
      Description copied from interface: ClassElement
      Tests whether one type is assignable to another.
      Parameters:
      type - The type to check
      Returns:
      true if and only if this type is assignable to the second
    • isIntroductionInterface

      public boolean isIntroductionInterface(ClassElement anInterface)
      Whether the interface is implemented through an @Introduction of the class rather than declared as a base of the Python class. The proxy of the class implements its methods.
      Parameters:
      anInterface - The interface
      Returns:
      Whether it is an introduction interface
    • getInterfaces

      public Collection<ClassElement> getInterfaces()
      Specified by:
      getInterfaces in interface ClassElement
      Overrides:
      getInterfaces in class AbstractPythonClassElement
      Returns:
      The interfaces implemented by this class element
    • isInterface

      public boolean isInterface()
      Whether the class compiles to a Java interface. A class is an interface when it has no state (no constructor, attributes or properties) and only declares abstract methods, in one of two shapes:
      • a plain abstract class or Protocol without a bean or interceptor stereotype, the Python spelling of a Java interface;
      • an introduction interface: a class decorated with an Introduction stereotype (@Client, an AI service, ...) that has no class base and whose instance methods are all abstract, none of which declares a *args parameter. Micronaut implements such a type with an introduction proxy, and frameworks that build the implementation reflectively need the Java interface, not a class wrapping a Python object.
      Returns:
      True if the class compiles to an interface
    • isIntroductionInterface

      public boolean isIntroductionInterface()
      Whether this class is an interface implemented by an introduction proxy: it is decorated with an Introduction stereotype and compiles to a Java interface.
      Returns:
      True if the class is an introduction interface
    • getSuperType

      public Optional<ClassElement> getSuperType()
      Description copied from interface: ClassElement
      Returns the super type of this element or empty if the element has no super type.
      Returns:
      An optional of the super type
    • getTypeArguments

      public Map<String, ClassElement> getTypeArguments()
      Returns:
      The type arguments for this class element
    • getAllTypeArguments

      public Map<String, Map<String, ClassElement>> getAllTypeArguments()
      Description copied from interface: ClassElement
      Builds a map of all the type parameters for a class, its super classes and interfaces. The resulting map contains the name of the class to the map of the resolved generic types.

      The arguments of every super type are expressed in the type arguments of this class: a Reversed that extends HashMap with its own two variables swapped answers with those two variables, in that swapped order, for HashMap and for Map alike.

      Returns:
      The type arguments for this class element
    • withTypeArguments

      public ClassElement withTypeArguments(Map<String, ClassElement> typeArguments)
      Description copied from interface: ClassElement
      Copies this element and overrides its type arguments.
      Parameters:
      typeArguments - The type arguments
      Returns:
      A new element
    • getDeclaredGenericPlaceholders

      @NonNull public @NonNull List<? extends GenericPlaceholderElement> getDeclaredGenericPlaceholders()
      Description copied from interface: ClassElement
      The type arguments declared on the raw class. Independent of the actual bound type arguments.

      This method will resolve the generic placeholders defined of the declaring class, if any.

      For example List<String> will result a single placeholder called E of type Object.

      Returns:
      The type arguments declared on this class.
    • 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.
    • isPublic

      public boolean isPublic()
      Specified by:
      isPublic in interface Element
      Returns:
      True if the element is public.
    • 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
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object