Interface TypedPropertyPath<T, P extends @Nullable Object>

Type Parameters:
T - the owning type of this path segment; the root type for composed paths.
P - the property value type at this path segment.
All Superinterfaces:
Iterable<PropertyPath>, PropertyPath, Serializable, Streamable<PropertyPath>, Supplier<Stream<PropertyPath>>
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 TypedPropertyPath<T, P extends @Nullable Object> extends PropertyPath, Serializable
Interface providing type-safe property path navigation through method references expressions.

This functional interface extends PropertyPath to provide compile-time type safety and refactoring support. Instead of using string-based property paths for textual property representation that are easy to miss when changing the domain model, TypedPropertyPath leverages Java's declarative method references to ensure type-safe property access.

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

TypedPropertyPath.path(Person::getName);
The resulting object can be used to obtain the dot-path and to interact with the targeting property. Typed paths allow for composition to navigate nested object structures using then(PropertyReference):
// factory method chaining
TypedPropertyPath<Person, String> city = TypedPropertyPath.path(Person::getAddress, Address::getCity);

// fluent API
TypedPropertyPath<Person, String> city = TypedPropertyPath.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 TypedPropertyPath using method references (strongly recommended)s 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

    • path

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

      This method returns a resolved TypedPropertyPath 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 path.
    • of

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

      This method returns a resolved TypedPropertyPath 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 path.
    • ofMany

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

      This method returns a resolved TypedPropertyPath 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 collection property.
      Returns:
      the typed property path.
    • 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()
      Description copied from interface: PropertyPath
      Returns the owning type of the PropertyPath.
      Specified by:
      getOwningType in interface PropertyPath
      Returns:
      the owningType will never be null.
    • getSegment

      default String getSegment()
      Description copied from interface: PropertyPath
      Returns the current property path segment (i.e. first part of PropertyPath.toDotPath()).

      For example:

      PropertyPath.from("address.city.name", Person.class).getSegment();
      
      results in address.
      Specified by:
      getSegment in interface PropertyPath
      Returns:
      the current property path segment.
    • getTypeInformation

      default TypeInformation<P> getTypeInformation()
      Description copied from interface: PropertyPath
      Returns the type information for the property at this segment.
      Specified by:
      getTypeInformation in interface PropertyPath
      Returns:
      the type information for the property at this segment.
    • next

      default @Nullable PropertyPath next()
      Description copied from interface: PropertyPath
      Returns the next PropertyPath segment in the property path chain.
      PropertyPath.from("address.city.name", Person.class).next().toDotPath();
      
      results in the output: city.name.
      Specified by:
      next in interface PropertyPath
      Returns:
      the next PropertyPath or null if the path does not contain further segments.
      See Also:
    • hasNext

      default boolean hasNext()
      Description copied from interface: PropertyPath
      Returns true if the property path contains further segments or false if the path ends at this segment.
      Specified by:
      hasNext in interface PropertyPath
      Returns:
      true if the property path contains further segments or false if the path ends at this segment.
    • iterator

      default Iterator<PropertyPath> iterator()
      Description copied from interface: PropertyPath
      Returns an Iterator of PropertyPath that iterates over all property path segments. For example:
      PropertyPath path = PropertyPath.from("address.city.name", Person.class);
      path.forEach(p -> p.toDotPath());
      
      results in the dot paths:
      address.city.name     (this object)
      city.name             (next() object)
      name             (next().next() object)
      
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface PropertyPath
    • then

      default <N extends @Nullable Object> TypedPropertyPath<T,N> then(PropertyReference<P,N> next)
      Extend the 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 a 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 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 a property.
      Returns:
      a new composed TypedPropertyPath.