Interface BeanProperty<B,T>

Type Parameters:
B - The bean type
T - The bean property type
All Superinterfaces:
AnnotatedElement, AnnotationMetadata, AnnotationMetadataDelegate, AnnotationMetadataProvider, AnnotationSource, ArgumentCoercible<T>, BeanReadProperty<B,T>, BeanWriteProperty<B,T>, Named
All Known Subinterfaces:
UnsafeBeanProperty<B,T>
All Known Implementing Classes:
AbstractBeanProperty

@NextMajorVersion("Remove all the methods, only keeping those in read/write interfaces") public interface BeanProperty<B,T> extends BeanReadProperty<B,T>, BeanWriteProperty<B,T>, ArgumentCoercible<T>
Represents a bean property and associated annotation metadata.

A BeanProperty allows you to read the value of a property via get(Object) or write to it via set(Object, Object), without using reflection.

The annotations of a property can be inspected via the AnnotationMetadataProvider.getAnnotationMetadata() method.

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

    • getDeclaringBean

      BeanIntrospection<B> getDeclaringBean()
      Specified by:
      getDeclaringBean in interface BeanReadProperty<B,T>
      Specified by:
      getDeclaringBean in interface BeanWriteProperty<B,T>
      Returns:
      The declaring bean introspection.
    • get

      @Nullable T get(B bean)
      Read the bean value.
      Specified by:
      get in interface BeanReadProperty<B,T>
      Parameters:
      bean - The bean to read from
      Returns:
      The value
      Throws:
      IllegalArgumentException - If the bean instance if not of the correct type
    • get

      default <T2> Optional<T2> get(B bean, Class<T2> type)
      Read the value and try to convert it to the given type.
      Specified by:
      get in interface BeanReadProperty<B,T>
      Type Parameters:
      T2 - The generic type
      Parameters:
      bean - The bean
      type - The type
      Returns:
      The value if conversion was possible.
    • get

      default <T2> Optional<T2> get(B bean, Argument<T2> argument)
      Read the value and try to convert it to the given type.
      Specified by:
      get in interface BeanReadProperty<B,T>
      Type Parameters:
      T2 - The generic type
      Parameters:
      bean - The bean
      argument - The type
      Returns:
      The value if conversion was possible.
    • get

      default <T2> Optional<T2> get(B bean, ArgumentConversionContext<T2> conversionContext)
      Read the value and try to convert it to the given type.
      Specified by:
      get in interface BeanReadProperty<B,T>
      Type Parameters:
      T2 - The generic type
      Parameters:
      bean - The bean
      conversionContext - The conversion context to use
      Returns:
      The value if conversion was possible.
    • get

      default <T2> @Nullable T2 get(B bean, Class<T2> type, @Nullable T2 defaultValue)
      Read the value and try to convert it to the given type.
      Specified by:
      get in interface BeanReadProperty<B,T>
      Type Parameters:
      T2 - The generic type
      Parameters:
      bean - The bean
      type - The type
      defaultValue - The default value if conversion is not possible
      Returns:
      The value if conversion was possible.
    • hasSetterOrConstructorArgument

      default boolean hasSetterOrConstructorArgument()
      This method returns true if the property can be mutated either via copy constructor or bean setter.
      Returns:
      True if it is mutable
      Since:
      2.3.0
      See Also:
    • withValue

      default B withValue(B bean, @Nullable T value)
      This method will attempt to modify the property or if this is an immutable type using a copy constructor to return a new instance with the new value.

      This differs from set(Object, Object) which will throw an exception if the property does not have a setter.

      • If the property is read-only but can be provided via constructor argument a new instance representing a copy of the bean is returned.
      • If the property is mutable then the passed instance is returned and set(Object, Object) invoked to mutate the property
      • If there is no way for the property to be mutated then an UnsupportedOperationException is thrown
      Specified by:
      withValue in interface BeanWriteProperty<B,T>
      Parameters:
      bean - The bean
      value - The new value
      Returns:
      Either the existing instance or the property is mutable or a newly created instance via the copy constructor pattern.
      Throws:
      UnsupportedOperationException - if the property cannot be mutated
      Since:
      2.3.0
    • set

      default void set(B bean, @Nullable T value)
      Write the bean value.
      Specified by:
      set in interface BeanWriteProperty<B,T>
      Parameters:
      bean - The bean
      value - The value to write
      Throws:
      IllegalArgumentException - If either the bean type or value type are not correct
    • convertAndSet

      default void convertAndSet(B bean, @Nullable Object value)
      Convert the value and set if on the bean using the default conversion service.
      Specified by:
      convertAndSet in interface BeanWriteProperty<B,T>
      Parameters:
      bean - The bean
      value - The value
      Throws:
      ConversionErrorException - If the value couldn't be converted
    • getType

      Class<T> getType()
      Specified by:
      getType in interface BeanReadProperty<B,T>
      Specified by:
      getType in interface BeanWriteProperty<B,T>
      Returns:
      The property type.
    • asArgument

      default Argument<T> asArgument()
      Represent the type as an argument, including any generic type information.
      Specified by:
      asArgument in interface ArgumentCoercible<B>
      Specified by:
      asArgument in interface BeanReadProperty<B,T>
      Specified by:
      asArgument in interface BeanWriteProperty<B,T>
      Returns:
      The argument
    • isReadOnly

      default boolean isReadOnly()
      Returns:
      Whether the property is read-only
    • isWriteOnly

      default boolean isWriteOnly()
      Returns:
      Whether the property is write-only
    • isReadWrite

      default boolean isReadWrite()
      Whether the property can be written to and read from.
      Returns:
      True if it can.
    • getDeclaringType

      default Class<B> getDeclaringType()
      The declaring type of the property.
      Specified by:
      getDeclaringType in interface BeanReadProperty<B,T>
      Specified by:
      getDeclaringType in interface BeanWriteProperty<B,T>
      Returns:
      The type
    • getMembers

      default List<BeanPropertyMember<B,?>> getMembers()
      The individual members (the field, the read methods and the write methods) that this property is composed of.

      A property merges its members into a single element with a single merged AnnotationMetadata. This method exposes each member separately, with its own annotation metadata, its own Argument and its own accessor, which is required by specifications such as Jakarta Bean Validation that treat a field and a getter as two distinct constrained elements, and a getter an interface declares apart from the one implementing it.

      The members are the field of the property and the read and write methods of every type of the hierarchy declaring one: the accessor of the bean type, then the ones it overrides in its super classes, then the ones of its interfaces, each reported by the type declaring it and carrying the annotations of its own declaration only, the fields first, then the read methods, then the write methods.

      The members are only computed at compilation time if the introspection was generated with Introspected.members() set to true, which BeanIntrospection.separatesDeclarations() reports. Otherwise, and for any BeanProperty implementation that doesn't support members, an empty list is returned.

      Returns:
      The members of this property, or an empty list if unavailable
      Since:
      5.2.0