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_ELEMENTSConstant 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 BeanElementBuilderaddAssociatedBean(ClassElement type)This method adds an associated bean using this class element as the originating element.default ClassElementfoldBoundGenericTypes(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.ClassElementfromArray()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.StringgetCanonicalName()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 PackageElementgetPackage()The package name.default java.lang.StringgetPackageName()The package name.default java.util.Optional<MethodElement>getPrimaryConstructor()Find and return a single primary constructor.default ClassElementgetRawClassElement()Get aClassElementinstance corresponding to this type, but without any type arguments bound.default java.lang.StringgetSimpleName()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 ClassElementgetType()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 booleanisAssignable(ClassElement type)Tests whether one type is assignable to another.default booleanisAssignable(java.lang.Class<?> type)Tests whether one type is assignable to another.booleanisAssignable(java.lang.String type)Tests whether one type is assignable to another.default booleanisEnum()Whether this element is an enum.default booleanisGenericPlaceholder()default booleanisInner()Is this type an inner class.default booleanisInterface()default booleanisIterable()default booleanisOptional()Whether this element is anOptional.default booleanisProxy()default booleanisRecord()default booleanisTypeVariable()In this case of callinggetTypeArguments()a returnedClassElementmay represent a type variable in which case this method will returntrue.default booleanisWildcard()static ClassElementof(java.lang.Class<?> type)Create a class element for the given simple type.static ClassElementof(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 ClassElementof(java.lang.reflect.Type type)Create a class element for the given complex type.static ClassElementof(java.lang.String typeName)Create a class element for the given simple type.static ClassElementof(java.lang.String typeName, boolean isInterface, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)Create a class element for the given simple type.static ClassElementof(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.ClassElementtoArray()Convert the class element to an element for the same type, but representing an array.default ClassElementwithBoundGenericTypes(java.util.List<? extends ClassElement> typeArguments)Get aClassElementinstance 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:
trueif and only if this type is assignable to the second
-
isTypeVariable
default boolean isTypeVariable()
In this case of callinggetTypeArguments()a returnedClassElementmay 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:
trueif 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 eitherCreatororInject.- 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:
getTypein interfaceTypedElement- Returns:
- The type of the element
-
getSimpleName
default java.lang.String getSimpleName()
The simple name without the package name.- Specified by:
getSimpleNamein 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 returnTas 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 calledEof typeObject.- Returns:
- The type arguments declared on this class.
- Since:
- 3.1.0
-
getRawClassElement
@NonNull default ClassElement getRawClassElement()
Get aClassElementinstance 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 aClassElementinstance corresponding to this type, but with the given type arguments. This is best effort – implementations may only supportClassElements that come from the same visitor context, and otherClassElements only to a limited degree.- Parameters:
typeArguments- The new type arguments.- Returns:
- A
ClassElementof 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.nullhas special meaning here. Returningnullfrom a fold operation will try to make the surrounding type a raw type. For example, forMap<String, Object>, returningnullfor the fold onObjectwill lead to the parameterizedMap<String, null>type being replaced byMap.This also means that this method may return
nullif 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:
trueif 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
UnsupportedOperationExceptionwill 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
-
-