Interface ClassElement

All Superinterfaces:
AnnotatedElement, AnnotationMetadata, AnnotationMetadataDelegate, AnnotationMetadataProvider, AnnotationSource, Described, Element, MutableAnnotationMetadataDelegate<Element>, Named, TypedElement
All Known Subinterfaces:
AnnotationElement, ArrayableClassElement, EnumElement, GenericPlaceholderElement, WildcardElement
All Known Implementing Classes:
GroovyClassElement, JavaClassElement, PrimitiveElement

public interface ClassElement extends TypedElement
Stores data about an element that references a class.
Since:
1.0
Author:
James Kleeh, graemerocher
  • Field Details

    • ZERO_CLASS_ELEMENTS

      static final ClassElement[] ZERO_CLASS_ELEMENTS
      Constant for an empty class element array.
      Since:
      3.1.0
  • Method Details

    • isAssignable

      boolean isAssignable(String type)
      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
    • isTypeVariable

      default boolean isTypeVariable()
      In this case of calling getTypeArguments() a returned ClassElement may represent a type variable in which case this method will return true.
      Returns:
      Is this type a type variable.
      Since:
      3.0.0
    • isGenericPlaceholder

      default boolean isGenericPlaceholder()
      Returns:
      Whether this is a generic placeholder.
      Since:
      3.1.0
      See Also:
    • isWildcard

      default boolean isWildcard()
      Returns:
      Whether this is a wildcard.
      See Also:
    • isRawType

      default boolean isRawType()
      Is raw type.
      Returns:
      true if the type is raw
      Since:
      4.0.0
    • isAssignable

      default boolean isAssignable(ClassElement type)
      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
      Since:
      2.3.0
    • isOptional

      default boolean isOptional()
      Whether this element is an Optional.
      Returns:
      Is this element an optional
      Since:
      2.3.0
    • isContainerType

      default boolean isContainerType()
      Checks whether the bean type is a container type.
      Returns:
      Whether the type is a container type like Iterable.
      Since:
      4.0.0
    • getOptionalValueType

      default Optional<ClassElement> getOptionalValueType()
      Gets optional value type.
      Returns:
      the value type
      Since:
      4.0.0
    • getCanonicalName

      default String getCanonicalName()
      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.
      Since:
      2.3.0
    • isRecord

      default boolean isRecord()
      Returns:
      Whether this element is a record
      Since:
      2.1.0
    • isInner

      default boolean isInner()
      Is this type an inner class.
      Returns:
      True if it is an inner class
      Since:
      2.1.2
    • isEnum

      default boolean isEnum()
      Whether this element is an enum.
      Returns:
      True if it is an enum
    • isProxy

      default boolean isProxy()
      Returns:
      True if the class represents a proxy
    • getPrimaryConstructor

      @NonNull default Optional<MethodElement> getPrimaryConstructor()
      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
    • getDefaultConstructor

      default Optional<MethodElement> getDefaultConstructor()
      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
    • findStaticCreator

      default Optional<MethodElement> findStaticCreator()
      Find and return a single primary static creator. If more than creator candidate exists, then return empty unless a static creator is found that is annotated with Creator.
      Returns:
      The primary creator if one is present
    • findDefaultStaticCreator

      default Optional<MethodElement> findDefaultStaticCreator()
      Find and return a single default static creator. A default static creator is one without arguments that is accessible. *
      Returns:
      a static creator
      Since:
      4.0.0
    • getAccessibleConstructors

      @NonNull default List<ConstructorElement> getAccessibleConstructors()
      Find accessible constructors.
      Returns:
      accessible constructors
      Since:
      4.0.0
    • getAccessibleStaticCreators

      @NonNull default List<MethodElement> getAccessibleStaticCreators()
      Get accessible static creators. A static creator is a static method annotated with Creator that can be used to create the class. For enums "valueOf" is picked as a static creator.
      Returns:
      static creators
      Since:
      4.0.0
    • getSuperType

      default Optional<ClassElement> getSuperType()
      Returns the super type of this element or empty if the element has no super type.
      Returns:
      An optional of the super type
    • getInterfaces

      default Collection<ClassElement> getInterfaces()
      Returns:
      The interfaces implemented by this class element
    • getType

      @NonNull default ClassElement getType()
      Specified by:
      getType in interface TypedElement
      Returns:
      The type of the element
    • getSimpleName

      default String getSimpleName()
      The simple name without the package name.
      Specified by:
      getSimpleName in interface Element
      Returns:
      The simple name
    • getPackageName

      default String getPackageName()
      The package name.
      Returns:
      The package name
    • getPackage

      default PackageElement getPackage()
      The package name.
      Returns:
      The package name
      Since:
      3.0.0
    • getBeanProperties

      @NonNull default List<PropertyElement> getBeanProperties()
      Returns the bean properties (getters and setters) for this class element.
      Returns:
      The bean properties for this class element
    • getSyntheticBeanProperties

      @NonNull default List<PropertyElement> getSyntheticBeanProperties()
      Returns the synthetic bean properties. The properties where one of the methods (getter or setter) is synthetic - not user defined but created by the compiler.
      Returns:
      The bean properties for this class element
      Since:
      4.0.0
    • getBeanProperties

      @NonNull default List<PropertyElement> getBeanProperties(@NonNull PropertyElementQuery propertyElementQuery)
      Returns the bean properties (getters and setters) for this class element based on custom configuration.
      Parameters:
      propertyElementQuery - The configuration
      Returns:
      The bean properties for this class element
      Since:
      4.0.0
    • getFields

      @NonNull default List<FieldElement> getFields()
      Return all the fields of this class element.
      Returns:
      The fields
    • getEnclosedElements

      @NonNull default <T extends Element> List<T> getEnclosedElements(@NonNull ElementQuery<T> query)
      Return the elements that match the given query.
      Type Parameters:
      T - The element type
      Parameters:
      query - The query to use.
      Returns:
      The fields
      Since:
      2.3.0
    • getEnclosingType

      default Optional<ClassElement> getEnclosingType()
      Returns the enclosing type if isInner() return true.
      Returns:
      The enclosing type if any
      Since:
      3.0.0
    • getEnclosedElement

      default <T extends Element> Optional<T> getEnclosedElement(@NonNull ElementQuery<T> query)
      Return the first enclosed element matching the given query.
      Type Parameters:
      T - The element type
      Parameters:
      query - The query to use.
      Returns:
      The fields
      Since:
      2.3.0
    • isInterface

      default boolean isInterface()
      Returns:
      Whether the class element is an interface
    • isIterable

      default boolean isIterable()
      Returns:
      Whether the type is iterable (either an array or an Iterable)
    • getBoundGenericTypes

      @NonNull default List<? extends ClassElement> getBoundGenericTypes()
      The list of type arguments bound to this type, or an empty list if there are no type arguments or this is a raw type.

      Note that for compatibility reasons, this method is inconsistent with getTypeArguments(). In particular, this method reflects the declaration type: If there is a class Test<T> { T field; }, this method will return T as the field type, even if the field type was obtained through a Test<String>.

      Returns:
      The list of type arguments, in the same order as getDeclaredGenericPlaceholders(). Must be empty or of the same length as getDeclaredGenericPlaceholders().
      Since:
      3.1.0
    • getDeclaredGenericPlaceholders

      @NonNull default List<? extends GenericPlaceholderElement> getDeclaredGenericPlaceholders()
      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.
      Since:
      3.1.0
    • getRawClassElement

      @NonNull default ClassElement getRawClassElement()
      Get a ClassElement instance corresponding to this type, but without any type arguments bound. For List<String>, this returns List.
      Returns:
      The raw class of this potentially parameterized type.
      Since:
      3.1.0
    • withBoundGenericTypes

      @NonNull @Deprecated(since="4", forRemoval=true) default ClassElement withBoundGenericTypes(@NonNull List<? extends ClassElement> typeArguments)
      Deprecated, for removal: This API element is subject to removal in a future version.
      replaced with withTypeArguments(Collection) for consistent API.
      Get a ClassElement instance corresponding to this type, but with the given type arguments. This is best effort – implementations may only support ClassElements that come from the same visitor context, and other ClassElements only to a limited degree.
      Parameters:
      typeArguments - The new type arguments.
      Returns:
      A ClassElement of the same raw class with the new type arguments.
      Throws:
      UnsupportedOperationException - If any of the given type arguments are unsupported.
    • foldBoundGenericTypes

      default ClassElement foldBoundGenericTypes(@NonNull Function<ClassElement,ClassElement> fold)
      Perform a fold operation on the type arguments (type arguments, wildcard bounds, resolved via getBoundGenericTypes()), and then on this type. For List<? extends String>, this returns f(List<f(? extends f(String))>). The bounds of type variables are not folded.

      null has special meaning here. Returning null from a fold operation will try to make the surrounding type a raw type. For example, for Map<String, Object>, returning null for the fold on Object will lead to the parameterized Map<String, null> type being replaced by Map.

      This also means that this method may return null if the top-level fold operation returned null.

      Parameters:
      fold - The fold operation to apply recursively to all component types.
      Returns:
      The folded type.
      Since:
      3.1.0
    • getTypeArguments

      @NonNull default Map<String,ClassElement> getTypeArguments(@NonNull String type)
      Get the type arguments for the given type name.
      Parameters:
      type - The type to retrieve type arguments for
      Returns:
      The type arguments for this class element
      Since:
      1.1.1
    • getTypeArguments

      @NonNull default Map<String,ClassElement> getTypeArguments(@NonNull Class<?> type)
      Get the type arguments for the given type name.
      Parameters:
      type - The type to retrieve type arguments for
      Returns:
      The type arguments for this class element
    • getTypeArguments

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

      @NonNull default Map<String,Map<String,ClassElement>> getAllTypeArguments()
      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 a map of the resolved generic types.
      Returns:
      The type arguments for this class element
    • getFirstTypeArgument

      default Optional<ClassElement> getFirstTypeArgument()
      Returns:
      The first type argument
    • isAssignable

      default boolean isAssignable(Class<?> type)
      Tests whether one type is assignable to another.
      Parameters:
      type - The type to check
      Returns:
      true if and only if the this type is assignable to the second
    • toArray

      Convert the class element to an element for the same type, but representing an array. Do not mutate the existing instance. Create a new instance instead.
      Returns:
      A new class element
    • fromArray

      @NonNull ClassElement fromArray()
      Dereference a class element denoting an array type by converting it to its element type. Do not mutate the existing instance. Create a new instance instead.
      Returns:
      A new class element
      Throws:
      IllegalStateException - if this class element doesn't denote an array type
    • addAssociatedBean

      @NonNull default BeanElementBuilder addAssociatedBean(@NonNull ClassElement type)
      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
    • withAnnotationMetadata

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

      @NonNull default ClassElement withTypeArguments(Map<String,ClassElement> typeArguments)
      Copies this element and overrides its type arguments.
      Parameters:
      typeArguments - The type arguments
      Returns:
      A new element
      Since:
      4.0.0
    • withTypeArguments

      @NonNull default ClassElement withTypeArguments(@NonNull Collection<ClassElement> typeArguments)
      Copies this element and overrides its type arguments. Variation of withTypeArguments(Map) that doesn't require type argument names.
      Parameters:
      typeArguments - The type arguments
      Returns:
      A new element
      Since:
      4.0.0
    • of

      @NonNull static ClassElement of(@NonNull Class<?> type)
      Create a class element for the given simple type.
      Parameters:
      type - The type
      Returns:
      The class element
    • of

      @NonNull static ClassElement of(@NonNull Type type)
      Create a class element for the given complex type.
      Parameters:
      type - The type
      Returns:
      The class element
    • of

      @NonNull static ClassElement of(@NonNull Class<?> type, @NonNull AnnotationMetadata annotationMetadata, @NonNull Map<String,ClassElement> typeArguments)
      Create a class element for the given simple type.
      Parameters:
      type - The type
      annotationMetadata - The annotation metadata
      typeArguments - The type arguments
      Returns:
      The class element
      Since:
      2.4.0
    • of

      Create a class element for the given simple type.
      Parameters:
      typeName - The type
      Returns:
      The class element
    • of

      @Internal @NonNull static ClassElement of(@NonNull String typeName, boolean isInterface, @Nullable AnnotationMetadata annotationMetadata)
      Create a class element for the given simple type.
      Parameters:
      typeName - The type
      isInterface - Is the type an interface
      annotationMetadata - The annotation metadata
      Returns:
      The class element
    • of

      @Internal @NonNull static ClassElement of(@NonNull String typeName, boolean isInterface, @Nullable AnnotationMetadata annotationMetadata, Map<String,ClassElement> typeArguments)
      Create a class element for the given simple type.
      Parameters:
      typeName - The type
      isInterface - Is the type an interface
      annotationMetadata - The annotation metadata
      typeArguments - The type arguments
      Returns:
      The class element