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.
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 Summary
Modifier and TypeMethodDescription@Nullable PGet the property value for the given object.default StringgetName()Returns the name of the property.default TypeInformation<T> Returns the owning type of the referenced property.default Class<?> getType()Returns the actual type of the property at this segment.default TypeInformation<?> Returns the type information for the property at this segment.default booleanReturns whether the property is a collection.static <T, P extends @Nullable Object>
PropertyReference<T, P> of(PropertyReference<T, P> property) Syntax sugar to create aPropertyReferencefrom a method reference to a Java beans property.static <T,P> PropertyReference <T, P> ofMany(PropertyReference<T, ? extends Iterable<P>> property) Syntax sugar to create aPropertyReferencefrom a method reference to a Java beans property.static <T, P extends @Nullable Object>
PropertyReference<T, P> property(PropertyReference<T, P> property) Syntax sugar to create aPropertyReferencefrom a method reference to a Java beans property.default <N extends @Nullable Object>
TypedPropertyPath<T, N> then(PropertyReference<P, N> next) Extend the property to a 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 to a property path by appending thenextpath segment and return a new property path instance.
-
Method Details
-
property
static <T, P extends @Nullable Object> PropertyReference<T,P> property(PropertyReference<T, P> property) Syntax sugar to create aPropertyReferencefrom a method reference to a Java beans property. Suitable for static imports.This method returns a resolved
PropertyReferenceby 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
Syntax sugar to create aPropertyReferencefrom a method reference to a Java beans property.This method returns a resolved
PropertyReferenceby 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
Syntax sugar to create aPropertyReferencefrom a method reference to a Java beans property.This method returns a resolved
PropertyReferenceby introspecting the given method reference. Note thatget(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 property.- Returns:
- the typed property reference.
-
get
-
getOwningType
Returns the owning type of the referenced property.- Returns:
- the owningType will never be null.
-
getName
-
getType
-
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
Extend the property to a 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 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 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 the property.- Returns:
- a new composed
TypedPropertyPath.
-