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.
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 Summary
Modifier and TypeMethodDescription@Nullable PGet the property value for the given object.default TypeInformation<T> Returns the owning type of thePropertyPath.default StringReturns the current property path segment (i.e. first part ofPropertyPath.toDotPath()).default TypeInformation<P> Returns the type information for the property at this segment.default booleanhasNext()Returns true if the property path contains further segments or false if the path ends at this segment.default Iterator<PropertyPath> iterator()Returns anIterator of PropertyPaththat iterates over all property path segments.default @Nullable PropertyPathnext()Returns the nextPropertyPathsegment in the property path chain.static <T, P extends @Nullable Object>
TypedPropertyPath<T, P> of(TypedPropertyPath<T, P> property) Syntax sugar to create aTypedPropertyPathfrom a method reference to a Java beans property.static <T,P> TypedPropertyPath <T, P> ofMany(TypedPropertyPath<T, ? extends Iterable<P>> property) Syntax sugar to create aTypedPropertyPathfrom a method reference to a Java beans collection property.static <T, P extends @Nullable Object>
TypedPropertyPath<T, P> path(PropertyReference<T, P> property) Syntax sugar to create aTypedPropertyPathfrom a property described as method reference to a Java beans property.default <N extends @Nullable Object>
TypedPropertyPath<T, N> then(PropertyReference<P, N> next) Extend the property path by appending thenextpath segment and return a new property path instance.default <N extends @Nullable Object>
TypedPropertyPath<T, N> thenMany(PropertyReference<P, ? extends Iterable<N>> next) Extend the property path by appending thenextpath segment and return a new property path instance.Methods inherited from interface Iterable
forEach, spliteratorMethods inherited from interface PropertyPath
getLeafProperty, getLeafType, getType, isCollection, nested, toDotPath
-
Method Details
-
path
Syntax sugar to create aTypedPropertyPathfrom a property described as method reference to a Java beans property. Suitable for static imports.This method returns a resolved
TypedPropertyPathby 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
Syntax sugar to create aTypedPropertyPathfrom a method reference to a Java beans property.This method returns a resolved
TypedPropertyPathby 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
Syntax sugar to create aTypedPropertyPathfrom a method reference to a Java beans collection property.This method returns a resolved
TypedPropertyPathby introspecting the given method reference.Note that
get(Object)becomes unusable for collection properties as the property type adapted fromIterable <P>and a singlePcannot 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
-
getOwningType
Description copied from interface:PropertyPathReturns the owning type of thePropertyPath.- Specified by:
getOwningTypein interfacePropertyPath- Returns:
- the owningType will never be null.
-
getSegment
Description copied from interface:PropertyPathReturns the current property path segment (i.e. first part ofPropertyPath.toDotPath()).For example:
PropertyPath.from("address.city.name", Person.class).getSegment();results inaddress.- Specified by:
getSegmentin interfacePropertyPath- Returns:
- the current property path segment.
-
getTypeInformation
Description copied from interface:PropertyPathReturns the type information for the property at this segment.- Specified by:
getTypeInformationin interfacePropertyPath- Returns:
- the type information for the property at this segment.
-
next
Description copied from interface:PropertyPathReturns the nextPropertyPathsegment in the property path chain.PropertyPath.from("address.city.name", Person.class).next().toDotPath();results in the output:city.name.- Specified by:
nextin interfacePropertyPath- Returns:
- the next
PropertyPathor null if the path does not contain further segments. - See Also:
-
hasNext
default boolean hasNext()Description copied from interface:PropertyPathReturns true if the property path contains further segments or false if the path ends at this segment.- Specified by:
hasNextin interfacePropertyPath- Returns:
- true if the property path contains further segments or false if the path ends at this segment.
-
iterator
Description copied from interface:PropertyPathReturns anIterator of PropertyPaththat 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:
iteratorin interfaceIterable<T>- Specified by:
iteratorin interfacePropertyPath
-
then
Extend the property path by appending thenextpath 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 objectPtype and returningNas 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 thenextpath segment and return a new property path instance.Note that
get(Object)becomes unusable for collection properties as the property type adapted fromIterable <P>and a singlePcannot 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 objectPtype and returningNas result of accessing a property.- Returns:
- a new composed
TypedPropertyPath.
-