Overview
An instance ofPropertyHolder provides runtime
information for a specific BeanProperty. Under the hood it uses Beans
tooling provided by the JDK and the utilities PropertyUtil an and
MoreReflection. Compared to the standard tooling it is more flexible
regarding fluent api style of bean / DTOs.
Usage
The usual entry-point is from(Class, String). In case you want to
create your own instance you can use the contained builder directly using
#builder()
Now you can access the metadata for that property, see
#getMemberInfo(), #getName(), #getType(),
#getReadMethod()
Reading and writing of properties should be done by readFrom(Object)
and writeTo(Object, Object). Directly using #getReadMethod()
and #getWriteMethod() is more error-prone and less versatile.
Caution:
Use reflection only if there is no other way. Even if some of the problems are minimized by using this type. It should be used either in test-code, what we actually do, and not production code. An other reason could be framework code. as for that you should exactly know what you do.
- Author:
- Oliver Wolff
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<PropertyHolder>Factory Method for creating a concretePropertyHolderReads the property on the given bean identified by the concretePropertyHolderand the given bean.
-
Constructor Details
-
PropertyHolder
public PropertyHolder()
-
-
Method Details
-
readFrom
Reads the property on the given bean identified by the concretePropertyHolderand the given bean. First it tries to access the readMethod derived by thePropertyDescriptor. If this can not be achieved, e.g. for types that do not match exactly Java-Bean-Specification it tries to read the property by usingPropertyUtil.readProperty(Object, String)- Parameters:
bean- instance to be read from, must not be null- Returns:
- the object read from the property
- Throws:
IllegalStateException- in case the property can not be read, seePropertyReadWrite#isReadable()IllegalStateException- in case some Exception occurred while reading
-
writeTo
- Parameters:
bean- instance to be read from, must not be nullpropertyValue- to be set- Returns:
- In case the property set method is void the given bean will be returned. Otherwise, the return value of the method invocation, assuming the setMethods is a builder / fluent-api type.
- Throws:
IllegalStateException- in case the property can not be read, seePropertyReadWrite#isWriteable()IllegalStateException- in case some Exception occurred while writing
-
from
Factory Method for creating a concretePropertyHolder- Parameters:
beanType- must not be nullattributeName- must not be null nor empty- Returns:
- the concrete
PropertyHolderfor the given parameter if applicable - Throws:
IllegalArgumentException- for cases whereIntrospectoris not capable of resolving aPropertyDescriptor. This is usually the case if it is not a valid bean.
-