Interface PropertyReference<T, P extends @Nullable Object>

Type Parameters:
T - the owning type of this property.
P - the property value type.
All Superinterfaces:
Serializable
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface PropertyReference<T, P extends @Nullable Object> extends Serializable
Interface providing type-safe property references.

This functional interface is typically implemented through method references that allow for compile-time type safety and refactoring support. Instead of string-based property names that are easy to miss when changing the domain model, PropertyReference leverages Java's declarative method references to ensure type-safe property access.

Create a typed property reference using the static factory method property(PropertyReference) with a method reference, for example:

PropertyReference.property(Person::getName);
The resulting object can be used to obtain the property name and to interact with the target property. Typed references can be used to compose property paths to navigate nested object structures using then(PropertyReference):
TypedPropertyPath<Person, String> city = PropertyReference.of(Person::getAddress).then(Address::getCity);

The generic type parameters preserve type information across the property path chain: T represents the owning type of the current segment (or the root type for composed paths), while P represents the property value type at this segment. Composition automatically flows type information forward, ensuring that then() preserves the full chain's type safety.

Implement PropertyReference using method references (strongly recommended) or lambdas that directly access a property getter. Constructor references, method calls with parameters, and complex expressions are not supported and result in PropertyResolutionException. Unlike method references, introspection of lambda expressions requires bytecode analysis of the declaration site classes and thus depends on their availability at runtime.

Since:
4.1
Author:
Mark Paluch
See Also:
  • Method Details

    • property

      static <T, P extends @Nullable Object> PropertyReference<T,P> property(PropertyReference<T,P> property)
      Syntax sugar to create a PropertyReference from a method reference to a Java beans property. Suitable for static imports.

      This method returns a resolved PropertyReference by introspecting the given method reference.

      Type Parameters:
      T - owning type.
      P - property type.
      Parameters:
      property - the method reference to a Java beans property.
      Returns:
      the typed property reference.
    • of

      static <T, P extends @Nullable Object> PropertyReference<T,P> of(PropertyReference<T,P> property)
      Syntax sugar to create a PropertyReference from a method reference to a Java beans property.

      This method returns a resolved PropertyReference by introspecting the given method reference.

      Type Parameters:
      T - owning type.
      P - property type.
      Parameters:
      property - the method reference to a Java beans property.
      Returns:
      the typed property reference.
    • ofMany

      static <T,P> PropertyReference<T,P> ofMany(PropertyReference<T, ? extends Iterable<P>> property)
      Syntax sugar to create a PropertyReference from a method reference to a Java beans property.

      This method returns a resolved PropertyReference by introspecting the given method reference. Note that get(Object) becomes unusable for collection properties as the property type adapted from Iterable &lt;P&gt; and a single P cannot represent a collection of items.

      Type Parameters:
      T - owning type.
      P - property type.
      Parameters:
      property - the method reference to a Java beans property.
      Returns:
      the typed property reference.
    • get

      @Nullable P get(T obj)
      Get the property value for the given object.
      Parameters:
      obj - the object to get the property value from.
      Returns:
      the property value.
    • getOwningType

      default TypeInformation<T> getOwningType()
      Returns the owning type of the referenced property.
      Returns:
      the owningType will never be null.
    • getName

      default String getName()
      Returns the name of the property.
      Returns:
      the current property name.
    • getType

      default Class<?> getType()
      Returns the actual type of the property at this segment. Will return the plain resolved type for simple properties, the component type for any Iterable or the value type of Map properties.
      Returns:
      the actual type of the property.
      See Also:
    • getTypeInformation

      default TypeInformation<?> getTypeInformation()
      Returns the type information for the property at this segment.
      Returns:
      the type information for the property at this segment.
    • isCollection

      default boolean isCollection()
      Returns whether the property is a collection.
      Returns:
      true if the property is a collection.
      See Also:
    • then

      default <N extends @Nullable Object> TypedPropertyPath<T,N> then(PropertyReference<P,N> next)
      Extend the property to a property path by appending the next path segment and return a new property path instance.
      Type Parameters:
      N - the new property value type.
      Parameters:
      next - the next property path segment as method reference accepting the owner object P type and returning N as result of accessing the property.
      Returns:
      a new composed TypedPropertyPath.
    • thenMany

      default <N extends @Nullable Object> TypedPropertyPath<T,N> thenMany(PropertyReference<P, ? extends Iterable<N>> next)
      Extend the property to a property path by appending the next path segment and return a new property path instance.

      Note that get(Object) becomes unusable for collection properties as the property type adapted from Iterable &lt;P&gt; and a single P cannot represent a collection of items.

      Type Parameters:
      N - the new property value type.
      Parameters:
      next - the next property path segment as method reference accepting the owner object P type and returning N as result of accessing the property.
      Returns:
      a new composed TypedPropertyPath.