Class BeanWriter<T>

java.lang.Object
org.klojang.invoke.BeanWriter<T>
Type Parameters:
T - The type of the bean

public final class BeanWriter<T> extends Object
A dynamic bean writer class. This class uses method handles instead of reflection to write bean properties. However, it still uses reflection to figure out what those properties are in the first place. Therefore, if you use this class from within a Java module you must open the module to the klojang-invoke module. Reflection is used only transiently. No reflection objects are cached. They are disposed of once the required information has been extracted from them.
Author:
Ayco Holleman
  • Constructor Summary

    Constructors
    Constructor
    Description
    BeanWriter(Class<T> beanClass, String... properties)
    Creates a BeanWriter for the specified class.
    BeanWriter(Class<T> beanClass, BeanValueTransformer<T> transformer, String... properties)
    Creates a BeanWriter for the specified class.
    BeanWriter(Class<T> beanClass, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties)
    Creates a BeanWriter for the specified class.
    BeanWriter(Class<T> beanClass, IncludeExclude includeExclude, String... properties)
    Creates a BeanWriter for the specified class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canWrite(String property)
    Returns true if the specified string represents a property that can be written by this BeanWriter.
    void
    copy(Map<String,?> fromMap, T toBean)
    Overwrites all properties in the specified bean with the corresponding values in the specified map.
    void
    copy(T fromBean, T toBean)
    Overwrites all properties in the second bean with the values they have in the first bean.
    void
    copyNonNull(Map<String,?> fromMap, T toBean)
    Copies all non-null values from the specified map to the specified bean.
    void
    copyNonNull(T fromBean, T toBean)
    Copies all non-null properties from the first bean to the second bean.
    void
    enrich(Map<String,?> fromMap, T toBean)
    Overwrites all properties in the specified bean whose value is null with the corresponding values in the specified map.
    void
    enrich(T fromBean, T toBean)
    Overwrites all properties in the second bean whose value is null with the values they have in the first bean.
    Returns the type of the objects this BeanWriter can write to.
    Returns the setters used by the BeanWriter to write bean properties.
    Returns the properties that this BeanWriter will write.
    void
    write(T bean, String property, Object value)
    Sets the specified property to the specified value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BeanWriter

      public BeanWriter(Class<T> beanClass, String... properties)
      Creates a BeanWriter for the specified class. You can optionally specify an array of properties that you intend to write. If you specify a zero-length array, all properties will be writable. If you intend to use this BeanWriter to repetitively write just one or two properties on bulky bean types, explicitly specifying the properties you intend to write might make the BeanWriter slightly more efficient.
      Parameters:
      beanClass - the bean class
      properties - the properties to be written
    • BeanWriter

      public BeanWriter(Class<T> beanClass, BeanValueTransformer<T> transformer, String... properties)
      Creates a BeanWriter for the specified class. You can optionally specify an array of properties that you intend to write. If you specify a zero-length array all properties will be writable. Input values will first be converted by the specified conversion function before being assigned to properties.
      Parameters:
      beanClass - the bean class
      transformer - a conversion function for input values. The function is passed the bean onto which to set the value; the property to set; and input value. Using these three parameters, the function can compute a new value, which will be the value to which to property is actually set.
      properties - the properties you allow to be written
    • BeanWriter

      public BeanWriter(Class<T> beanClass, IncludeExclude includeExclude, String... properties)
      Creates a BeanWriter for the specified class. You can optionally specify an array of properties that you intend or do not intend to write. If you specify a zero-length array all properties will be writable.
      Parameters:
      beanClass - the bean class
      includeExclude - whether to include or exclude the specified properties
      properties - the properties to be included/excluded
    • BeanWriter

      public BeanWriter(Class<T> beanClass, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties)
      Creates a BeanWriter for the specified class. You can optionally specify an array of properties that you intend or do not intend to write. If you specify a zero-length array all properties will be writable. If you intend to use this BeanWriter to repetitively write just one or two properties from bulky bean types, explicitly specifying the properties you intend to write might make the BeanWriter more efficient. Input values will first be converted by the specified conversion function before being assigned to properties.

      Specifying one or more non-existent properties will not cause an exception to be thrown. They will be quietly ignored.

      Parameters:
      beanClass - the bean class
      transformer - A conversion function for input values. The conversion is given the Setter for the property to be set as the first argument, and the input value as the second argument. The return value should be the actual value to assign to the property. The Setter should only be used to get the name and type of the property to be set. You should not use it to actually write the property, as this will happen anyhow once the conversion function returns. Unless the conversion fails for extraordinary reasons, it should throw an IllegalAssignmentException upon failure. You can again use the Setter to generate the exception.
      includeExclude - whether to include or exclude the specified properties
      properties - the properties to be included/excluded
  • Method Details

    • write

      public void write(T bean, String property, Object value) throws Throwable
      Sets the specified property to the specified value. If this BeanWriter was instantiated with a BeanValueTransformer, the property is set to the output from the transformer.
      Parameters:
      bean - The bean instance
      property - The property
      value - The value to set it to
      Throws:
      IllegalAssignmentException - If the value cannot be cast to the type of the property, or if the value is null and the property has a primitive type. This is a RuntimeException, but you might still want to catch it as it can often be handled in a meaningful way.
      Throwable - The Throwable thrown from inside the java.lang.invoke package
    • copy

      public void copy(T fromBean, T toBean) throws Throwable
      Overwrites all properties in the second bean with the values they have in the first bean. This can potentially nullify non-null properties in the target bean.
      Parameters:
      fromBean - The bean from which to copy the values.
      toBean - The bean to which to copy the values.
      Throws:
      Throwable - The Throwable thrown from inside the java.lang.invoke package
    • copyNonNull

      public void copyNonNull(T fromBean, T toBean) throws Throwable
      Copies all non-null properties from the first bean to the second bean. This can potentially overwrite non-null properties in the second bean, but it will never nullify them.
      Parameters:
      fromBean - The bean from which to copy the values.
      toBean - The bean to which to copy the values.
      Throws:
      Throwable - The Throwable thrown from inside the java.lang.invoke package
    • enrich

      public void enrich(T fromBean, T toBean) throws Throwable
      Overwrites all properties in the second bean whose value is null with the values they have in the first bean. Non-null properties in the second bean are left alone.
      Parameters:
      fromBean - The bean from which to copy the values.
      toBean - The bean to which to copy the values.
      Throws:
      Throwable - The Throwable thrown from inside the java.lang.invoke package
    • copy

      public void copy(Map<String,?> fromMap, T toBean) throws IllegalAssignmentException, Throwable
      Overwrites all properties in the specified bean with the corresponding values in the specified map. This can potentially nullify non-null values in the target bean.
      Parameters:
      fromMap - The Map providing the data for the JavaBean
      toBean - The JavaBean to populate
      Throws:
      IllegalAssignmentException - If a value cannot be cast or converted to the type of the destination property, or if the value is null and the destination property has a primitive type. This is a RuntimeException, but you might still want to catch it as it can often be handled in a meaningful way.
      Throwable - The Throwable thrown from inside the java.lang.invoke package
    • copyNonNull

      public void copyNonNull(Map<String,?> fromMap, T toBean) throws Throwable
      Copies all non-null values from the specified map to the specified bean. Map keys that do not correspond to bean properties are quietly ignored.
      Parameters:
      fromMap - The Map providing the data for the JavaBean
      toBean - The JavaBean to populate
      Throws:
      IllegalAssignmentException - If a value cannot be cast or converted to the type of the destination property, or if the value is null and the destination property has a primitive type. This is a RuntimeException, but you might still want to catch it as it can often be handled in a meaningful way.
      Throwable - The Throwable thrown from inside the java.lang.invoke package
    • enrich

      public void enrich(Map<String,?> fromMap, T toBean) throws IllegalAssignmentException, Throwable
      Overwrites all properties in the specified bean whose value is null with the corresponding values in the specified map. Non-null properties in the target bean are left alone.
      Parameters:
      fromMap - The Map providing the data for the JavaBean
      toBean - The JavaBean to populate
      Throws:
      IllegalAssignmentException - If a value cannot be cast or converted to the type of the destination property, or if the value is null and the destination property has a primitive type. This is a RuntimeException, but you might still want to catch it as it can often be handled in a meaningful way.
      Throwable - The Throwable thrown from inside the java.lang.invoke package
    • getBeanClass

      public Class<T> getBeanClass()
      Returns the type of the objects this BeanWriter can write to.
      Returns:
      The type of the objects this BeanWriter can write to
    • canWrite

      public boolean canWrite(String property)
      Returns true if the specified string represents a property that can be written by this BeanWriter. Note that this check is already done by the write(Object, String, Object) method before it will actually attempt to set the property. Only perform this check if there is a considerable chance that the provided string is not a writable property.
      Parameters:
      property - the string to be tested
      Returns:
      true if the specified string represents a property that can be written by this BeanWriter
    • getWritableProperties

      public Set<String> getWritableProperties()
      Returns the properties that this BeanWriter will write. That will be all write-accessible properties minus the properties excluded through the constructor (if any).
      Returns:
      the properties that this BeanWriter will write
    • getIncludedSetters

      public Map<String,Setter> getIncludedSetters()
      Returns the setters used by the BeanWriter to write bean properties. The returned Map maps the name of a property to the Setter used to write it.
      Returns:
      The setters used by the BeanWriter to write bean properties.