Interface ClassElement
- All Superinterfaces:
AnnotatedElement
,AnnotationMetadata
,AnnotationMetadataDelegate
,AnnotationMetadataProvider
,AnnotationSource
,Described
,Element
,MutableAnnotationMetadataDelegate<Element>
,Named
,TypedElement
- All Known Subinterfaces:
AnnotationElement
,ArrayableClassElement
,EnumElement
,GenericElement
,GenericPlaceholderElement
,WildcardElement
- All Known Implementing Classes:
GroovyClassElement
,JavaClassElement
,PrimitiveElement
- Since:
- 1.0
- Author:
- James Kleeh, graemerocher
-
Field Summary
Modifier and TypeFieldDescriptionstatic final ClassElement[]
Constant for an empty class element array.Fields inherited from interface io.micronaut.core.annotation.AnnotationMetadata
CLASS_NAME_SUFFIX, EMPTY_METADATA, VALUE_MEMBER
Fields inherited from interface io.micronaut.core.annotation.AnnotationSource
EMPTY
Fields inherited from interface io.micronaut.inject.ast.Element
EMPTY_ELEMENT_ARRAY
Fields inherited from interface io.micronaut.inject.ast.annotation.MutableAnnotationMetadataDelegate
EMPTY
-
Method Summary
Modifier and TypeMethodDescriptiondefault @NonNull BeanElementBuilder
This method adds an associated bean using this class element as the originating element.default Optional<MethodElement>
Find and return a single default static creator.default @NonNull Optional<FieldElement>
Find an instance/static field with a name in this class, super class or an interface.default @NonNull Optional<MethodElement>
findMethod
(String name) Find a method with a name.default Optional<MethodElement>
Find and return a single primary static creator.default @Nullable ClassElement
Perform a fold operation on the type arguments (type arguments, wildcard bounds, resolved viagetBoundGenericTypes()
), and then on this type.Dereference a class element denoting an array type by converting it to its element type.default @NonNull List<ConstructorElement>
Find accessible constructors.default @NonNull List<MethodElement>
Get accessible static creators.Builds a map of all the type parameters for a class, its super classes and interfaces.default @NonNull List<PropertyElement>
Returns the bean properties (getters and setters) for this class element.default @NonNull List<PropertyElement>
getBeanProperties
(@NonNull PropertyElementQuery propertyElementQuery) Returns the bean properties (getters and setters) for this class element based on custom configuration.default @NonNull List<? extends ClassElement>
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.default String
This method will return the name of the underlying type automatically unwrapping in the case of an optional or wrapped representation of the type.default @NonNull List<? extends GenericPlaceholderElement>
The type arguments declared on the raw class.default Optional<MethodElement>
Find and return a single default constructor.getEnclosedElement
(@NonNull ElementQuery<T> query) Return the first enclosed element matching the given query.getEnclosedElements
(@NonNull ElementQuery<T> query) Return the elements that match the given query.default Optional<ClassElement>
Returns the enclosing type ifisInner()
returntrue
.default @NonNull List<FieldElement>
Return all the fields of this class element.default Optional<ClassElement>
default Collection<ClassElement>
default @NonNull List<MethodElement>
Find an instance/static method with a name in this class, super class or an interface.default Optional<ClassElement>
Gets optional value type.default PackageElement
The package name.default String
The package name.default @NonNull Optional<MethodElement>
Find and return a single primary constructor.default @NonNull ClassElement
Get aClassElement
instance corresponding to this type, but without any type arguments bound.default String
The simple name without the package name.default Optional<ClassElement>
Returns the super type of this element or empty if the element has no super type.default @NonNull List<PropertyElement>
Returns the synthetic bean properties.default @NonNull ClassElement
getType()
Returns the type annotations.default @NonNull Map<String,
ClassElement> default @NonNull Map<String,
ClassElement> getTypeArguments
(@NonNull Class<?> type) Get the type arguments for the given type name.default @NonNull Map<String,
ClassElement> getTypeArguments
(@NonNull String type) Get the type arguments for the given type name.default boolean
isAssignable
(ClassElement type) Tests whether one type is assignable to another.default boolean
isAssignable
(Class<?> type) Tests whether one type is assignable to another.boolean
isAssignable
(String type) Tests whether one type is assignable to another.default boolean
Checks whether the bean type is a container type.default boolean
isEnum()
Whether this element is an enum.default boolean
default boolean
isInner()
Is this type an inner class.default boolean
default boolean
default boolean
Whether this element is anOptional
.default boolean
isProxy()
default boolean
Is raw type.default boolean
isRecord()
default boolean
In this case of callinggetTypeArguments()
a returnedClassElement
may represent a type variable in which case this method will returntrue
.default boolean
static @NonNull ClassElement
Create a class element for the given simple type.static @NonNull ClassElement
of
(@NonNull Class<?> type, @NonNull AnnotationMetadata annotationMetadata, @NonNull Map<String, ClassElement> typeArguments) Create a class element for the given simple type.static @NonNull ClassElement
Create a class element for the given complex type.static @NonNull ClassElement
Create a class element for the given simple type.static @NonNull ClassElement
of
(@NonNull String typeName, boolean isInterface, @Nullable AnnotationMetadata annotationMetadata) Create a class element for the given simple type.static @NonNull ClassElement
of
(@NonNull String typeName, boolean isInterface, @Nullable AnnotationMetadata annotationMetadata, Map<String, ClassElement> typeArguments) Create a class element for the given simple type.toArray()
Convert the class element to an element for the same type, but representing an array.default ClassElement
withAnnotationMetadata
(AnnotationMetadata annotationMetadata) Copies this element and overrides its annotations.default @NonNull ClassElement
withBoundGenericTypes
(@NonNull List<? extends ClassElement> typeArguments) Deprecated, for removal: This API element is subject to removal in a future version.default @NonNull ClassElement
withTypeArguments
(@NonNull Collection<ClassElement> typeArguments) Copies this element and overrides its type arguments.default @NonNull ClassElement
withTypeArguments
(Map<String, ClassElement> typeArguments) Copies this element and overrides its type arguments.Methods inherited from interface io.micronaut.core.annotation.AnnotatedElement
isDeclaredNonNull, isDeclaredNullable, isNonNull, isNullable
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadata
enumValuesSet, enumValuesSet, getValues, hasDeclaredStereotype, hasEvaluatedExpressions, hasStereotypeNonRepeating, isAnnotationPresent, isDeclaredAnnotationPresent
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataDelegate
booleanValue, booleanValue, booleanValue, booleanValue, classValue, classValue, classValue, classValue, classValues, classValues, classValues, classValues, copyAnnotationMetadata, doubleValue, doubleValue, doubleValue, enumValue, enumValue, enumValue, enumValue, enumValues, enumValues, enumValues, enumValues, findAnnotation, findAnnotation, findDeclaredAnnotation, findDeclaredAnnotation, findRepeatableAnnotation, findRepeatableAnnotation, getAnnotation, getAnnotation, getAnnotationNameByStereotype, getAnnotationNameByStereotype, getAnnotationNames, getAnnotationNamesByStereotype, getAnnotationNamesByStereotype, getAnnotationType, getAnnotationType, getAnnotationTypeByStereotype, getAnnotationTypeByStereotype, getAnnotationTypesByStereotype, getAnnotationTypesByStereotype, getAnnotationTypesByStereotype, getAnnotationValuesByName, getAnnotationValuesByStereotype, getAnnotationValuesByType, getDeclaredAnnotation, getDeclaredAnnotation, getDeclaredAnnotationNameByStereotype, getDeclaredAnnotationNames, getDeclaredAnnotationNamesByStereotype, getDeclaredAnnotationTypeByStereotype, getDeclaredAnnotationTypeByStereotype, getDeclaredAnnotationValuesByName, getDeclaredAnnotationValuesByType, getDeclaredMetadata, getDeclaredStereotypeAnnotationNames, getDefaultValue, getDefaultValue, getDefaultValue, getDefaultValue, getDefaultValues, getStereotypeAnnotationNames, getTargetAnnotationMetadata, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValues, getValues, hasAnnotation, hasAnnotation, hasDeclaredAnnotation, hasDeclaredAnnotation, hasDeclaredStereotype, hasDeclaredStereotype, hasDeclaredStereotype, hasPropertyExpressions, hasSimpleAnnotation, hasSimpleDeclaredAnnotation, hasStereotype, hasStereotype, hasStereotype, hasStereotype, intValue, intValue, intValue, isAnnotationPresent, isDeclaredAnnotationPresent, isEmpty, isFalse, isFalse, isPresent, isPresent, isRepeatableAnnotation, isRepeatableAnnotation, isTrue, isTrue, longValue, longValue, stringValue, stringValue, stringValue, stringValue, stringValues, stringValues, stringValues, stringValues, synthesize, synthesize, synthesizeAll, synthesizeAnnotationsByType, synthesizeDeclared, synthesizeDeclared, synthesizeDeclared, synthesizeDeclaredAnnotationsByType
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataProvider
getAnnotationMetadata
Methods inherited from interface io.micronaut.inject.ast.Element
annotate, annotate, annotate, annotate, annotate, getDescription, getDescription, getDocumentation, getModifiers, getName, getNativeType, isAbstract, isFinal, isPackagePrivate, isPrivate, isProtected, isPublic, isStatic, isSynthetic, removeAnnotation, removeAnnotation, removeAnnotationIf, removeStereotype, removeStereotype
Methods inherited from interface io.micronaut.inject.ast.TypedElement
getArrayDimensions, getGenericType, isArray, isPrimitive, isVoid
-
Field Details
-
ZERO_CLASS_ELEMENTS
Constant for an empty class element array.- Since:
- 3.1.0
-
-
Method Details
-
getTypeAnnotationMetadata
@NonNull default @NonNull MutableAnnotationMetadataDelegate<AnnotationMetadata> getTypeAnnotationMetadata()Returns the type annotations. Added by: - The declaration of the type variableElementType.TYPE_PARAMETER
- The use of the typeElementType.TYPE
- Returns:
- the type annotations
- Since:
- 4.0.0
-
isAssignable
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 callinggetTypeArguments()
a returnedClassElement
may represent a type variable in which case this method will returntrue
.- 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
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 anOptional
.- 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
Gets optional value type.- Returns:
- the value type
- Since:
- 4.0.0
-
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
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 eitherCreator
orAnnotationUtil.INJECT
.- Returns:
- The primary constructor if one is present
-
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
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 withCreator
.- Returns:
- The primary creator if one is present
-
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
Find accessible constructors.- Returns:
- accessible constructors
- Since:
- 4.0.0
-
getAccessibleStaticCreators
Get accessible static creators. A static creator is a static method annotated withCreator
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
Returns the super type of this element or empty if the element has no super type.- Returns:
- An optional of the super type
-
getInterfaces
- Returns:
- The interfaces implemented by this class element
-
getType
- Specified by:
getType
in interfaceTypedElement
- Returns:
- The type of the element
-
getSimpleName
The simple name without the package name.- Specified by:
getSimpleName
in interfaceElement
- Returns:
- The simple name
-
getPackageName
The package name.- Returns:
- The package name
-
getPackage
The package name.- Returns:
- The package name
- Since:
- 3.0.0
-
getBeanProperties
Returns the bean properties (getters and setters) for this class element.- Returns:
- The bean properties for this class element
-
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 @NonNull List<PropertyElement> getBeanProperties(@NonNull @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
Return all the fields of this class element.- Returns:
- The fields
-
findField
Find an instance/static field with a name in this class, super class or an interface.- Parameters:
name
- The field name- Returns:
- The field
- Since:
- 4.0.0
-
getMethods
Find an instance/static method with a name in this class, super class or an interface.- Returns:
- The methods
- Since:
- 4.0.0
-
findMethod
Find a method with a name.- Parameters:
name
- The method name- Returns:
- The method
- Since:
- 4.0.0
-
getEnclosedElements
@NonNull default <T extends Element> @NonNull List<T> getEnclosedElements(@NonNull @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
Returns the enclosing type ifisInner()
returntrue
.- Returns:
- The enclosing type if any
- Since:
- 3.0.0
-
getEnclosedElement
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
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 aclass Test<T> { T field; }
, this method will returnT
as the field type, even if the field type was obtained through aTest<String>
.- Returns:
- The list of type arguments, in the same order as
getDeclaredGenericPlaceholders()
. Must be empty or of the same length asgetDeclaredGenericPlaceholders()
. - Since:
- 3.1.0
-
getDeclaredGenericPlaceholders
@NonNull default @NonNull List<? extends GenericPlaceholderElement> getDeclaredGenericPlaceholders()The type arguments declared on the raw class. Independent of the actualbound 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 calledE
of typeObject
.- Returns:
- The type arguments declared on this class.
- Since:
- 3.1.0
-
getRawClassElement
Get aClassElement
instance corresponding to this type, but without any type arguments bound. ForList<String>
, this returnsList
.- Returns:
- The raw class of this potentially parameterized type.
- Since:
- 3.1.0
-
withBoundGenericTypes
@NonNull @Deprecated(since="4", forRemoval=true) default @NonNull ClassElement withBoundGenericTypes(@NonNull @NonNull List<? extends ClassElement> typeArguments) Deprecated, for removal: This API element is subject to removal in a future version.replaced withwithTypeArguments(Collection)
for consistent API.Get aClassElement
instance corresponding to this type, but with the given type arguments. This is best effort – implementations may only supportClassElement
s that come from the same visitor context, and otherClassElement
s 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
@Nullable default @Nullable ClassElement foldBoundGenericTypes(@NonNull @NonNull Function<ClassElement, ClassElement> fold) Perform a fold operation on the type arguments (type arguments, wildcard bounds, resolved viagetBoundGenericTypes()
), and then on this type. ForList<? extends String>
, this returnsf(List<f(? extends f(String))>)
. The bounds of type variables are not folded.null
has special meaning here. Returningnull
from a fold operation will try to make the surrounding type a raw type. For example, forMap<String, Object>
, returningnull
for the fold onObject
will lead to the parameterizedMap<String, null>
type being replaced byMap
.This also means that this method may return
null
if the top-level fold operation returnednull
.- Parameters:
fold
- The fold operation to apply recursively to all component types.- Returns:
- The folded type.
- Since:
- 3.1.0
-
getTypeArguments
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 @NonNull Map<String,ClassElement> getTypeArguments(@NonNull @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
- Returns:
- The type arguments for this class element
-
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
- Returns:
- The first type argument
-
isAssignable
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
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
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
Description copied from interface:Element
Copies this element and overrides its annotations.- Specified by:
withAnnotationMetadata
in interfaceElement
- Parameters:
annotationMetadata
- The annotation metadata- Returns:
- A new element
-
withTypeArguments
Copies this element and overrides its type arguments.- Parameters:
typeArguments
- The type arguments- Returns:
- A new element
- Since:
- 4.0.0
-
withTypeArguments
@NonNull default @NonNull ClassElement withTypeArguments(@NonNull @NonNull Collection<ClassElement> typeArguments) Copies this element and overrides its type arguments. Variation ofwithTypeArguments(Map)
that doesn't require type argument names.- Parameters:
typeArguments
- The type arguments- Returns:
- A new element
- Since:
- 4.0.0
-
of
Create a class element for the given simple type.- Parameters:
type
- The type- Returns:
- The class element
-
of
Create a class element for the given complex type.- Parameters:
type
- The type- Returns:
- The class element
-
of
@NonNull static @NonNull ClassElement of(@NonNull @NonNull Class<?> type, @NonNull @NonNull AnnotationMetadata annotationMetadata, @NonNull @NonNull Map<String, ClassElement> typeArguments) Create a class element for the given simple type.- Parameters:
type
- The typeannotationMetadata
- The annotation metadatatypeArguments
- 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 @NonNull ClassElement of(@NonNull @NonNull String typeName, boolean isInterface, @Nullable @Nullable AnnotationMetadata annotationMetadata) Create a class element for the given simple type.- Parameters:
typeName
- The typeisInterface
- Is the type an interfaceannotationMetadata
- The annotation metadata- Returns:
- The class element
-
of
@Internal @NonNull static @NonNull ClassElement of(@NonNull @NonNull String typeName, boolean isInterface, @Nullable @Nullable AnnotationMetadata annotationMetadata, Map<String, ClassElement> typeArguments) Create a class element for the given simple type.- Parameters:
typeName
- The typeisInterface
- Is the type an interfaceannotationMetadata
- The annotation metadatatypeArguments
- The type arguments- Returns:
- The class element
-
withTypeArguments(Collection)
for consistent API.