Interface BeanIntrospection<T>

Type Parameters:
T - The bean type
All Superinterfaces:
AnnotationMetadata, AnnotationMetadataDelegate, AnnotationMetadataProvider, AnnotationSource, ArgumentCoercible<T>, BeanInfo<T>, Ordered
All Known Subinterfaces:
EnumBeanIntrospection<E>, ReflectiveIntrospection<T>, UnsafeBeanInstantiationIntrospection<T>
All Known Implementing Classes:
AbstractEnumBeanIntrospectionAndReference, AbstractInitializableBeanIntrospection, AbstractInitializableBeanIntrospectionAndReference, ReflectionBeanIntrospection, SupplementedBeanIntrospection

public interface BeanIntrospection<T> extends AnnotationMetadataDelegate, BeanInfo<T>
A BeanIntrospection is the result of compile time computation of a beans properties and annotation metadata.

This interface allows you to instantiate and read and write to bean properties without using reflection or caching reflective metadata, which is expensive from a memory consumption perspective.

BeanIntrospection instances can be obtained either via getIntrospection(Class) or via the BeanIntrospector.

A BeanIntrospection is only computed at compilation time if the class is annotated with Introspected.

Since:
1.1
Author:
graemerocher
See Also:
  • Method Details

    • getBeanProperties

      Collection<BeanProperty<T,Object>> getBeanProperties()
      Returns:
      An immutable collection of properties.
    • getBeanReadProperties

      default List<BeanReadProperty<T,Object>> getBeanReadProperties()
      Returns:
      An immutable collection of read properties.
      Since:
      4.4.0
    • getBeanWriteProperties

      default List<BeanWriteProperty<T,Object>> getBeanWriteProperties()
      Returns:
      An immutable collection of write properties.
      Since:
      4.4.0
    • getIndexedProperties

      Collection<BeanProperty<T,Object>> getIndexedProperties(Class<? extends Annotation> annotationType)
      Get all the bean properties annotated for the given annotation type. If the annotation is Introspected.indexed() by the given annotation, then it will be included in the resulting list.
      Parameters:
      annotationType - The annotation type
      Returns:
      An immutable collection of properties.
      See Also:
    • builder

      Returns:
      Obtain a builder for this introspection.
      Since:
      4.1.0
    • isBuildable

      default boolean isBuildable()
      Can the introspection be constructed with builder().
      Returns:
      True if it can
      Since:
      4.1.0
    • hasBuilder

      default boolean hasBuilder()
      Does the introspection have a backing builder.

      This method exists so that consumers can choose to optimize towards direct instantiation using instantiate() if no builder is present.

      Returns:
      True if a backing builder is present.
      Since:
      4.1.0
    • instantiate

      T instantiate() throws InstantiationException
      Instantiates an instance of the bean, throwing an exception is instantiation is not possible.
      Returns:
      An instance
      Throws:
      InstantiationException - If the bean cannot be instantiated or the arguments are not satisfied.
    • instantiate

      default T instantiate(@Nullable Object... arguments) throws InstantiationException
      Instantiates an instance of the bean, throwing an exception is instantiation is not possible.
      Parameters:
      arguments - The arguments required to instantiate bean. Should match the types returned by getConstructorArguments()
      Returns:
      An instance
      Throws:
      InstantiationException - If the bean cannot be instantiated.
    • instantiate

      T instantiate(boolean strictNullable, @Nullable Object... arguments) throws InstantiationException
      Instantiates an instance of the bean, throwing an exception is instantiation is not possible.
      Parameters:
      strictNullable - If true, require null parameters to be annotated with a nullable annotation
      arguments - The arguments required to instantiate bean. Should match the types returned by getConstructorArguments()
      Returns:
      An instance
      Throws:
      InstantiationException - If the bean cannot be instantiated.
    • getBeanType

      Class<T> getBeanType()
      The bean type.
      Specified by:
      getBeanType in interface BeanInfo<T>
      Returns:
      The bean type
    • getIndexedProperty

      Optional<BeanProperty<T,Object>> getIndexedProperty(Class<? extends Annotation> annotationType, String annotationValue)
      Get all the bean properties annotated for the given type.
      Parameters:
      annotationType - The annotation type
      annotationValue - The annotation value
      Returns:
      An immutable collection of properties.
      See Also:
    • getBeanMethods

      default Collection<BeanMethod<T,Object>> getBeanMethods()
      Returns the BeanMethod instances for this introspection.

      The BeanMethod instances are only those methods annotated with io.micronaut.context.annotation.Executable and hence represent a subset of the actual methods of the class and do not include any methods that are exposed as BeanProperty instances.

      Returns:
      An immutable collection of methods.
      Since:
      2.3.0
    • getIndexedProperty

      default Optional<BeanProperty<T,Object>> getIndexedProperty(Class<? extends Annotation> annotationType)
      Get first bean properties annotated for the given type.
      Parameters:
      annotationType - The annotation type
      Returns:
      An immutable collection of properties.
      See Also:
    • getConstructorArguments

      default Argument<?>[] getConstructorArguments()
      The constructor arguments needed to instantiate the bean.
      Returns:
      An argument array
    • getTypeArguments

      default List<Argument<?>> getTypeArguments()
      If the bean itself declares any type arguments this method will return the arguments that represent those types.

      Note this returns the type arguments the bean type declares, not the arguments it binds in a super type. Use getTypeArguments(Class) for the latter.

      Returns:
      The type arguments
      Since:
      5.2.0
    • getTypeArguments

      default List<Argument<?>> getTypeArguments(@Nullable Class<?> type)
      Return the type arguments this bean binds in the given interface or super type.

      For example a type declared as class Foo implements Function<String, Integer> answers [String T, Integer R] for Function.class.

      Parameters:
      type - The super class or interface type
      Returns:
      The type arguments, never null
      Since:
      5.2.0
    • getTypeArguments

      default List<Argument<?>> getTypeArguments(@Nullable String type)
      Return the type arguments this bean binds in the given interface or super type.
      Parameters:
      type - The super class or interface type name
      Returns:
      The type arguments, never null
      Since:
      5.2.0
    • getProperty

      default Optional<BeanProperty<T,Object>> getProperty(String name)
      Obtain a property by name.
      Parameters:
      name - The name of the property
      Returns:
      A bean property if found
    • getReadProperty

      default Optional<BeanReadProperty<T,Object>> getReadProperty(String name)
      Obtain a read property by name.
      Parameters:
      name - The name of the property
      Returns:
      A bean read property if found
      Since:
      4.4.0
    • getWriteProperty

      default Optional<BeanWriteProperty<T,Object>> getWriteProperty(String name)
      Obtain a write property by name.
      Parameters:
      name - The name of the property
      Returns:
      A bean write property if found
      Since:
      4.4.0
    • propertyIndexOf

      default int propertyIndexOf(String name)
      Obtain the property index position.
      Parameters:
      name - The name of the property
      Returns:
      A property index or -1 of not found.
      Since:
      3.1
    • getRequiredProperty

      default <P> BeanProperty<T,P> getRequiredProperty(String name, Class<P> type)
      Gets a property of the given name and type or throws IntrospectionException if the property is not present.
      Type Parameters:
      P - The property generic type
      Parameters:
      name - The name
      type - The type
      Returns:
      The property
    • getRequiredReadProperty

      default <P> BeanReadProperty<T,P> getRequiredReadProperty(String name, Class<P> type)
      Gets a read property of the given name and type or throws IntrospectionException if the property is not present.
      Type Parameters:
      P - The property generic type
      Parameters:
      name - The name
      type - The type
      Returns:
      The property
      Since:
      4.4.0
    • getRequiredWriteProperty

      default <P> BeanWriteProperty<T,P> getRequiredWriteProperty(String name, Class<P> type)
      Gets a write property of the given name and type or throws IntrospectionException if the property is not present.
      Type Parameters:
      P - The property generic type
      Parameters:
      name - The name
      type - The type
      Returns:
      The property
      Since:
      4.4.0
    • getProperty

      default <P> Optional<BeanProperty<T,P>> getProperty(String name, Class<P> type)
      Obtain a property by name and type.
      Type Parameters:
      P - The property type
      Parameters:
      name - The name of the property
      type - The property type to search for
      Returns:
      A bean property if found
    • getReadProperty

      default <P> Optional<BeanReadProperty<T,P>> getReadProperty(String name, Class<P> type)
      Obtain a read property by name and type.
      Type Parameters:
      P - The property type
      Parameters:
      name - The name of the property
      type - The property type to search for
      Returns:
      A bean read property if found
    • getWriteProperty

      default <P> Optional<BeanWriteProperty<T,P>> getWriteProperty(String name, Class<P> type)
      Obtain a write property by name and type.
      Type Parameters:
      P - The property type
      Parameters:
      name - The name of the property
      type - The property type to search for
      Returns:
      A bean write property if found
      Since:
      4.4.0
    • getPropertyNames

      default String[] getPropertyNames()
      The property names as an array.
      Returns:
      The property names
    • getReadPropertyNames

      default String[] getReadPropertyNames()
      The read property names as an array.
      Returns:
      The property names
      Since:
      4.4.0
    • getConstructor

      default BeanConstructor<T> getConstructor()
      Returns:
      The bean constructor.
      Since:
      3.0.0
    • getConstructors

      default List<BeanConstructor<T>> getConstructors()
      Returns the constructors this introspection describes.

      By default an introspection only describes the constructor used to instantiate beans, in which case this method returns a singleton list containing getConstructor(). When the introspected type opts in via Introspected.constructors(), or by annotating individual constructors with io.micronaut.context.annotation.Executable, every described constructor is returned, including its own annotation metadata and the annotation metadata of its parameters.

      A described constructor reports the parameters of its JVM signature, which for a non-static inner class means that BeanConstructor.getArguments() starts with the implicit enclosing instance, named this$0, exactly as Constructor.getParameterTypes() reports it. The enclosing instance must therefore be passed first to BeanConstructor.instantiate(Object...). getConstructor() and getConstructorArguments() keep the source level view and never describe it, so for a non-static inner class the first element of this list does not report the same arguments as getConstructor().

      Returns:
      Every described constructor of the type, getConstructor() first
      Since:
      5.2.0
    • separatesDeclarations

      default boolean separatesDeclarations()
      Whether this introspection tells the declarations of the bean type apart from the ones it inherits.

      A property merges what its field, its accessors and every super type declaring one of them carry into one annotation metadata, and a method merges the annotations of the methods it overrides. A specification attributing an annotation to the element declaring it - Jakarta Validation puts a constraint an interface declares in the implicit group of that interface, validates a constraint of a field against the field rather than the getter, and reports the constraints of an element apart from the inherited ones - needs them apart. An introspection separating them lists in BeanProperty.getMembers() the field of a property and the accessor of every type of the hierarchy declaring one, each carrying the annotations of its own declaration only, and answers what a method declares itself through BeanMethod.getDeclaredMethodAnnotationMetadata().

      A generated introspection separates the declarations when it is compiled with Introspected.members(); by default it does not, and the members of its properties are empty.

      Returns:
      True if the members of a property and the annotations of a method are reported by the type declaring them
      Since:
      5.2.0
    • getIntrospection

      static <T2> BeanIntrospection<T2> getIntrospection(Class<T2> type)
      Obtains an introspection from the default BeanIntrospector.
      Type Parameters:
      T2 - The generic type
      Parameters:
      type - The type
      Returns:
      The introspection
      Throws:
      IntrospectionException - If the introspection cannot be found or errors when loading