Interface ClassElement
-
- All Superinterfaces:
io.micronaut.core.annotation.AnnotatedElement
,io.micronaut.core.annotation.AnnotationMetadata
,io.micronaut.core.annotation.AnnotationMetadataDelegate
,io.micronaut.core.annotation.AnnotationMetadataProvider
,io.micronaut.core.annotation.AnnotationSource
,io.micronaut.core.naming.Described
,Element
,io.micronaut.core.naming.Named
,TypedElement
- All Known Subinterfaces:
AnnotationElement
,ArrayableClassElement
,EnumElement
,GenericPlaceholderElement
,WildcardElement
- All Known Implementing Classes:
PrimitiveElement
public interface ClassElement extends TypedElement
Stores data about an element that references a class.- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description static ClassElement[]
ZERO_CLASS_ELEMENTS
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.inject.ast.Element
EMPTY_ELEMENT_ARRAY
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default BeanElementBuilder
addAssociatedBean(ClassElement type)
This method adds an associated bean using this class element as the originating element.default ClassElement
foldBoundGenericTypes(java.util.function.Function<ClassElement,ClassElement> fold)
Perform a fold operation on the type arguments (type arguments, wildcard bounds, resolved viagetBoundGenericTypes()
), and then on this type.ClassElement
fromArray()
Dereference a class element denoting an array type by converting it to its element type.default java.util.Map<java.lang.String,java.util.Map<java.lang.String,ClassElement>>
getAllTypeArguments()
Builds a map of all the type parameters for a class, its super classes and interfaces.default java.util.List<PropertyElement>
getBeanProperties()
Returns the bean properties (getters and setters) for this class element.default java.util.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.default java.lang.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.default java.util.List<? extends GenericPlaceholderElement>
getDeclaredGenericPlaceholders()
The type arguments declared on the raw class.default java.util.Optional<MethodElement>
getDefaultConstructor()
Find and return a single default constructor.default <T extends Element>
java.util.Optional<T>getEnclosedElement(ElementQuery<T> query)
Return the first enclosed element matching the given query.default <T extends Element>
java.util.List<T>getEnclosedElements(ElementQuery<T> query)
Return the elements that match the given query.default java.util.Optional<ClassElement>
getEnclosingType()
Returns the enclosing type ifisInner()
returntrue
.default java.util.List<FieldElement>
getFields()
Return all the fields of this class element.default java.util.List<FieldElement>
getFields(java.util.function.Predicate<java.util.Set<ElementModifier>> modifierFilter)
Deprecated.UsegetEnclosedElements(ElementQuery)
insteaddefault java.util.Optional<ClassElement>
getFirstTypeArgument()
default java.util.Collection<ClassElement>
getInterfaces()
default PackageElement
getPackage()
The package name.default java.lang.String
getPackageName()
The package name.default java.util.Optional<MethodElement>
getPrimaryConstructor()
Find and return a single primary constructor.default ClassElement
getRawClassElement()
Get aClassElement
instance corresponding to this type, but without any type arguments bound.default java.lang.String
getSimpleName()
The simple name without the package name.default java.util.Optional<ClassElement>
getSuperType()
Returns the super type of this element or empty if the element has no super type.default ClassElement
getType()
default java.util.Map<java.lang.String,ClassElement>
getTypeArguments()
default java.util.Map<java.lang.String,ClassElement>
getTypeArguments(java.lang.Class<?> type)
Get the type arguments for the given type name.default java.util.Map<java.lang.String,ClassElement>
getTypeArguments(java.lang.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(java.lang.Class<?> type)
Tests whether one type is assignable to another.boolean
isAssignable(java.lang.String type)
Tests whether one type is assignable to another.default boolean
isEnum()
Whether this element is an enum.default boolean
isGenericPlaceholder()
default boolean
isInner()
Is this type an inner class.default boolean
isInterface()
default boolean
isIterable()
default boolean
isOptional()
Whether this element is anOptional
.default boolean
isProxy()
default boolean
isRecord()
default boolean
isTypeVariable()
In this case of callinggetTypeArguments()
a returnedClassElement
may represent a type variable in which case this method will returntrue
.default boolean
isWildcard()
static ClassElement
of(java.lang.Class<?> type)
Create a class element for the given simple type.static ClassElement
of(java.lang.Class<?> type, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.util.Map<java.lang.String,ClassElement> typeArguments)
Create a class element for the given simple type.static ClassElement
of(java.lang.reflect.Type type)
Create a class element for the given complex type.static ClassElement
of(java.lang.String typeName)
Create a class element for the given simple type.static ClassElement
of(java.lang.String typeName, boolean isInterface, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
Create a class element for the given simple type.static ClassElement
of(java.lang.String typeName, boolean isInterface, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.util.Map<java.lang.String,ClassElement> typeArguments)
Create a class element for the given simple type.ClassElement
toArray()
Convert the class element to an element for the same type, but representing an array.default ClassElement
withBoundGenericTypes(java.util.List<? extends ClassElement> typeArguments)
Get aClassElement
instance corresponding to this type, but with the given type arguments.-
Methods inherited from interface io.micronaut.core.annotation.AnnotatedElement
isDeclaredNonNull, isDeclaredNullable, isNonNull, isNullable
-
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadata
getAnnotationValuesByName, getAnnotationValuesByStereotype, getDeclaredAnnotationValuesByName, getValues, hasDeclaredStereotype, isAnnotationPresent, isDeclaredAnnotationPresent
-
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataDelegate
booleanValue, booleanValue, booleanValue, booleanValue, classValue, classValue, classValue, classValue, classValues, classValues, classValues, classValues, 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, getAnnotationValuesByType, getDeclaredAnnotation, getDeclaredAnnotation, getDeclaredAnnotationNameByStereotype, getDeclaredAnnotationNames, getDeclaredAnnotationNamesByStereotype, getDeclaredAnnotationTypeByStereotype, getDeclaredAnnotationTypeByStereotype, getDeclaredAnnotationValuesByType, getDeclaredMetadata, getDeclaredStereotypeAnnotationNames, getDefaultValue, getDefaultValue, getDefaultValue, getDefaultValue, getDefaultValues, getStereotypeAnnotationNames, 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, removeAnnotation, removeAnnotation, removeAnnotationIf, removeStereotype, removeStereotype
-
Methods inherited from interface io.micronaut.inject.ast.TypedElement
getArrayDimensions, getGenericType, isArray, isPrimitive
-
-
-
-
Field Detail
-
ZERO_CLASS_ELEMENTS
static final ClassElement[] ZERO_CLASS_ELEMENTS
Constant for an empty class element array.- Since:
- 3.1.0
-
-
Method Detail
-
isAssignable
boolean isAssignable(java.lang.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 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:
GenericPlaceholderElement
-
isWildcard
default boolean isWildcard()
- Returns:
- Whether this is a wildcard.
- See Also:
WildcardElement
-
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 anOptional
.- Returns:
- Is this element an optional
- Since:
- 2.3.0
-
getCanonicalName
default java.lang.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 java.util.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 eitherCreator
orInject
.- Returns:
- The primary constructor if one is present
-
getDefaultConstructor
@NonNull default java.util.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
-
getSuperType
default java.util.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 java.util.Collection<ClassElement> getInterfaces()
- Returns:
- The interfaces implemented by this class element
-
getType
@NonNull default ClassElement getType()
- Specified by:
getType
in interfaceTypedElement
- Returns:
- The type of the element
-
getSimpleName
default java.lang.String getSimpleName()
The simple name without the package name.- Specified by:
getSimpleName
in interfaceElement
- Returns:
- The simple name
-
getPackageName
default java.lang.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
default java.util.List<PropertyElement> getBeanProperties()
Returns the bean properties (getters and setters) for this class element.- Returns:
- The bean properties for this class element
-
getFields
default java.util.List<FieldElement> getFields()
Return all the fields of this class element.- Returns:
- The fields
-
getFields
@Deprecated default java.util.List<FieldElement> getFields(@NonNull java.util.function.Predicate<java.util.Set<ElementModifier>> modifierFilter)
Deprecated.UsegetEnclosedElements(ElementQuery)
insteadReturn fields contained with the given modifiers include / exclude rules.- Parameters:
modifierFilter
- Can be used to filter fields by modifier- Returns:
- The fields
-
getEnclosedElements
default <T extends Element> java.util.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 java.util.Optional<ClassElement> getEnclosingType()
Returns the enclosing type ifisInner()
returntrue
.- Returns:
- The enclosing type if any
- Since:
- 3.0.0
-
getEnclosedElement
default <T extends Element> java.util.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 java.util.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 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 java.util.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
@NonNull default ClassElement 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 default ClassElement withBoundGenericTypes(@NonNull java.util.List<? extends ClassElement> typeArguments)
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:
java.lang.UnsupportedOperationException
- If any of the given type arguments are unsupported.
-
foldBoundGenericTypes
default ClassElement foldBoundGenericTypes(@NonNull java.util.function.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
@NonNull default java.util.Map<java.lang.String,ClassElement> getTypeArguments(@NonNull java.lang.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 java.util.Map<java.lang.String,ClassElement> getTypeArguments(@NonNull java.lang.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 java.util.Map<java.lang.String,ClassElement> getTypeArguments()
- Returns:
- The type arguments for this class element
-
getAllTypeArguments
@NonNull default java.util.Map<java.lang.String,java.util.Map<java.lang.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 java.util.Optional<ClassElement> getFirstTypeArgument()
- Returns:
- The first type argument
-
isAssignable
default boolean isAssignable(java.lang.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
@NonNull ClassElement 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:
java.lang.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
-
of
@NonNull static ClassElement of(@NonNull java.lang.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 java.lang.reflect.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 java.lang.Class<?> type, @NonNull io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, @NonNull java.util.Map<java.lang.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
@Internal @NonNull static ClassElement of(@NonNull java.lang.String typeName)
Create a class element for the given simple type.- Parameters:
typeName
- The type- Returns:
- The class element
-
of
@Internal @NonNull static ClassElement of(@NonNull java.lang.String typeName, boolean isInterface, @Nullable io.micronaut.core.annotation.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 ClassElement of(@NonNull java.lang.String typeName, boolean isInterface, @Nullable io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.util.Map<java.lang.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
-
-