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 aBeanWriterfor the specified class.BeanWriter(Class<T> beanClass, FallibleBiFunction<Setter, Object, Object, Throwable> converter, String... properties) Creates aBeanWriterfor the specified class.BeanWriter(Class<T> beanClass, FallibleBiFunction<Setter, Object, Object, Throwable> converter, IncludeExclude includeExclude, String... properties) Creates aBeanWriterfor the specified class.BeanWriter(Class<T> beanClass, IncludeExclude includeExclude, String... properties) Creates aBeanWriterfor the specified class. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif the specified string represents a property that can be written by thisBeanWriter.voidOverwrites all properties in the specified bean with the corresponding values in the specified map.voidOverwrites all properties in the second bean with the values they have in the first bean.voidcopyNonNull(Map<String, ?> fromMap, T toBean) Copies all non-null values from the specified map to the specified bean.voidcopyNonNull(T fromBean, T toBean) Copies all non-null properties from the first bean to the second bean.voidOverwrites all properties in the specified bean whose value isnullwith the corresponding values in the specified map.voidOverwrites all properties in the second bean whose value isnullwith the values they have in the first bean.Returns the type of the objects thisBeanWritercan write to.Returns thesettersused by theBeanWriterto write bean properties.Returns the properties that thisBeanWriterwill write.voidSets the value of the specified property on the specified bean.
-
Constructor Details
-
BeanWriter
Creates aBeanWriterfor 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 thisBeanWriterto repetitively write just one or two properties on bulky bean types, explicitly specifying the properties you intend to write might make theBeanWriterslightly more efficient.- Parameters:
beanClass- the bean classproperties- the properties to be written
-
BeanWriter
public BeanWriter(Class<T> beanClass, FallibleBiFunction<Setter, Object, Object, Throwable> converter, String... properties) Creates aBeanWriterfor 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 classconverter- a conversion function for input values. The conversion is given theSetterfor 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. TheSettershould only be used to get thenameandtypeof the property to be set. You should not use it to actuallywritethe property, as this will happen anyhow once the conversion function returns. Unless the conversion fails for extraordinary reasons, it should throw anIllegalAssignmentExceptionupon failure. You can again use theSettertogeneratethe exception.properties- the properties you allow to be written
-
BeanWriter
Creates aBeanWriterfor 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, FallibleBiFunction<Setter, Object, Object, Throwable> converter, IncludeExclude includeExclude, String... properties) Creates aBeanWriterfor 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 thisBeanWriterto repetitively write just one or two properties from bulky bean types, explicitly specifying the properties you intend to write might make theBeanWritermore 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 classconverter- A conversion function for input values. The conversion is given theSetterfor 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. TheSettershould only be used to get thenameandtypeof the property to be set. You should not use it to actuallywritethe property, as this will happen anyhow once the conversion function returns. Unless the conversion fails for extraordinary reasons, it should throw anIllegalAssignmentExceptionupon failure. You can again use theSettertogeneratethe exception.includeExclude- whether to include or exclude the specified propertiesproperties- the properties to be included/excluded
-
-
Method Details
-
write
Sets the value of the specified property on the specified bean.- 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 isnulland 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- TheThrowablethrown from inside thejava.lang.invokepackage
-
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- TheThrowablethrown from inside thejava.lang.invokepackage
-
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- TheThrowablethrown from inside thejava.lang.invokepackage
-
enrich
Overwrites all properties in the second bean whose value isnullwith 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- TheThrowablethrown from inside thejava.lang.invokepackage
-
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- TheMapproviding 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 isnulland 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- TheThrowablethrown from inside thejava.lang.invokepackage
-
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- TheMapproviding 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 isnulland 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- TheThrowablethrown from inside thejava.lang.invokepackage
-
enrich
Overwrites all properties in the specified bean whose value isnullwith the corresponding values in the specified map. Non-null properties in the target bean are left alone.- Parameters:
fromMap- TheMapproviding 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 isnulland 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- TheThrowablethrown from inside thejava.lang.invokepackage
-
getBeanClass
Returns the type of the objects thisBeanWritercan write to.- Returns:
- The type of the objects this
BeanWritercan write to
-
canWrite
Returnstrueif 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:
trueif the specified string represents a property that can be written by thisBeanWriter
-
getWritableProperties
Returns the properties that thisBeanWriterwill write. That will be all write-accessible properties minus the properties excluded through the constructor (if any).- Returns:
- the properties that this
BeanWriterwill write
-
getIncludedSetters
Returns thesettersused by theBeanWriterto write bean properties. The returnedMapmaps the name of a property to theSetterused to write it.- Returns:
- The
settersused by theBeanWriterto write bean properties.
-