Class BeanWriter<T>
java.lang.Object
org.klojang.invoke.BeanWriter<T>
- Type Parameters:
T
- The type of the bean
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
ConstructorsConstructorDescriptionBeanWriter
(Class<T> beanClass, String... properties) Creates aBeanWriter
for the specified class.BeanWriter
(Class<T> beanClass, BeanValueTransformer<T> transformer, String... properties) Creates aBeanWriter
for the specified class.BeanWriter
(Class<T> beanClass, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties) Creates aBeanWriter
for the specified class.BeanWriter
(Class<T> beanClass, IncludeExclude includeExclude, String... properties) Creates aBeanWriter
for the specified class. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if the specified string represents a property that can be written by thisBeanWriter
.void
Overwrites all properties in the specified bean with the corresponding values in the specified map.void
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
Overwrites all properties in the specified bean whose value isnull
with the corresponding values in the specified map.void
Overwrites all properties in the second bean whose value isnull
with the values they have in the first bean.Returns the type of the objects thisBeanWriter
can write to.Returns thesetters
used by theBeanWriter
to write bean properties.Returns the properties that thisBeanWriter
will write.void
Sets the specified property to the specified value.
-
Constructor Details
-
BeanWriter
Creates aBeanWriter
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 thisBeanWriter
to repetitively write just one or two properties on bulky bean types, explicitly specifying the properties you intend to write might make theBeanWriter
slightly more efficient.- Parameters:
beanClass
- the bean classproperties
- the properties to be written
-
BeanWriter
Creates aBeanWriter
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 classtransformer
- 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
Creates aBeanWriter
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 classincludeExclude
- whether to include or exclude the specified propertiesproperties
- the properties to be included/excluded
-
BeanWriter
public BeanWriter(Class<T> beanClass, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties) Creates aBeanWriter
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 thisBeanWriter
to repetitively write just one or two properties from bulky bean types, explicitly specifying the properties you intend to write might make theBeanWriter
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 classtransformer
- A conversion function for input values. The conversion is given theSetter
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. TheSetter
should only be used to get thename
andtype
of the property to be set. You should not use it to actuallywrite
the property, as this will happen anyhow once the conversion function returns. Unless the conversion fails for extraordinary reasons, it should throw anIllegalAssignmentException
upon failure. You can again use theSetter
togenerate
the exception.includeExclude
- whether to include or exclude the specified propertiesproperties
- the properties to be included/excluded
-
-
Method Details
-
write
Sets the specified property to the specified value. If thisBeanWriter
was instantiated with aBeanValueTransformer
, the property is set to the output from the transformer.- Parameters:
bean
- The bean instanceproperty
- The propertyvalue
- The value to set it to- Throws:
IllegalAssignmentException
- If the value cannot be cast to the type of the property, or if the value isnull
and the property has a primitive type. This is aRuntimeException
, but you might still want to catch it as it can often be handled in a meaningful way.Throwable
- TheThrowable
thrown from inside thejava.lang.invoke
package
-
copy
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
- TheThrowable
thrown from inside thejava.lang.invoke
package
-
copyNonNull
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
- TheThrowable
thrown from inside thejava.lang.invoke
package
-
enrich
Overwrites all properties in the second bean whose value isnull
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
- TheThrowable
thrown from inside thejava.lang.invoke
package
-
copy
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
- TheMap
providing the data for the JavaBeantoBean
- 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 isnull
and the destination property has a primitive type. This is aRuntimeException
, but you might still want to catch it as it can often be handled in a meaningful way.Throwable
- TheThrowable
thrown from inside thejava.lang.invoke
package
-
copyNonNull
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
- TheMap
providing the data for the JavaBeantoBean
- 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 isnull
and the destination property has a primitive type. This is aRuntimeException
, but you might still want to catch it as it can often be handled in a meaningful way.Throwable
- TheThrowable
thrown from inside thejava.lang.invoke
package
-
enrich
Overwrites all properties in the specified bean whose value isnull
with the corresponding values in the specified map. Non-null properties in the target bean are left alone.- Parameters:
fromMap
- TheMap
providing the data for the JavaBeantoBean
- 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 isnull
and the destination property has a primitive type. This is aRuntimeException
, but you might still want to catch it as it can often be handled in a meaningful way.Throwable
- TheThrowable
thrown from inside thejava.lang.invoke
package
-
getBeanClass
-
canWrite
Returnstrue
if the specified string represents a property that can be written by thisBeanWriter
. Note that this check is already done by thewrite(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 thisBeanWriter
-
getWritableProperties
-
getIncludedSetters
-