Class PropertyHolder
- java.lang.Object
-
- io.cui.tools.property.PropertyHolder
-
public final class PropertyHolder extends Object
Overview
An instance ofPropertyHolderprovides runtime information for a specific BeanProperty. Under the hood it usesBeanstooling provided by the JDK and the utilitiesPropertyUtilan andMoreReflection. 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 usingbuilder()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)andwriteTo(Object, Object). Directly usinggetReadMethod()andgetWriteMethod()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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPropertyHolder.PropertyHolderBuilder
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static PropertyHolder.PropertyHolderBuilderbuilder()booleanequals(Object o)static Optional<PropertyHolder>from(Class<?> beanType, String attributeName)Factory Method for creating a concretePropertyHolder@NonNull PropertyMemberInfogetMemberInfo()Provides additional runtime information for the property, seePropertyMemberInfo@NonNull StringgetName()The name of the property.MethodgetReadMethod()Derived byPropertyDescriptor, may be null@NonNull PropertyReadWritegetReadWrite()Provides additional Runtime-information, seePropertyReadWrite@NonNull Class<?>getType()The actual type of the property.MethodgetWriteMethod()Derived byPropertyDescriptor, may be nullinthashCode()ObjectreadFrom(Object bean)Reads the property on the given bean identified by the concretePropertyHolderand the given bean.StringtoString()ObjectwriteTo(Object bean, Object propertyValue)
-
-
-
Method Detail
-
readFrom
public Object readFrom(Object bean)
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
public Object writeTo(Object bean, Object propertyValue)
- 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
public static Optional<PropertyHolder> from(Class<?> beanType, String attributeName)
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.
-
builder
public static PropertyHolder.PropertyHolderBuilder builder()
-
getMemberInfo
@NonNull public @NonNull PropertyMemberInfo getMemberInfo()
Provides additional runtime information for the property, seePropertyMemberInfo
-
getReadWrite
@NonNull public @NonNull PropertyReadWrite getReadWrite()
Provides additional Runtime-information, seePropertyReadWrite
-
getReadMethod
public Method getReadMethod()
Derived byPropertyDescriptor, may be null
-
getWriteMethod
public Method getWriteMethod()
Derived byPropertyDescriptor, may be null
-
-