Class PropertyHolder

java.lang.Object
de.cuioss.tools.property.PropertyHolder

public class PropertyHolder extends Object

Overview

An instance of PropertyHolder 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 Details

  • Method Details

    • readFrom

      public Object readFrom(Object bean)
      Reads the property on the given bean identified by the concrete PropertyHolder and the given bean. First it tries to access the readMethod derived by the PropertyDescriptor. 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 using PropertyUtil.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, see PropertyReadWrite#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 null
      propertyValue - 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, see PropertyReadWrite#isWriteable()
      IllegalStateException - in case some Exception occurred while writing
    • from

      public static Optional<PropertyHolder> from(Class<?> beanType, String attributeName)
      Factory Method for creating a concrete PropertyHolder
      Parameters:
      beanType - must not be null
      attributeName - must not be null nor empty
      Returns:
      the concrete PropertyHolder for the given parameter if applicable
      Throws:
      IllegalArgumentException - for cases where Introspector is not capable of resolving a PropertyDescriptor. This is usually the case if it is not a valid bean.