Class BeanReader<T>
java.lang.Object
org.klojang.invoke.BeanReader<T>
- Type Parameters:
T
- The type of the bean
A dynamic bean reader class. This class uses method handles instead of reflection to
read bean properties. However, it still uses reflection to figure out what those
properties are in the first place. Therefore, if you use a
BeanReader
to read
beans residing in a Java 9+ module, that module must be "open" to reflective access.
Reflection is used only transiently. No reflection objects are cached. They are
disposed of once the required information has been extracted from them.
If you prefer, you can use the BeanReaderBuilder
class to configure
BeanReader
instances in a completely reflection-free manner. You obtain a
BeanReaderBuilder
via BeanReader.forClass()
.
- Author:
- Ayco Holleman
-
Constructor Summary
ConstructorsConstructorDescriptionBeanReader
(Class<T> beanClass, boolean strictNaming, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties) Creates aBeanReader
for the specified class.BeanReader
(Class<T> beanClass, boolean strictNaming, IncludeExclude includeExclude, String... properties) Creates aBeanReader
for the specified class.BeanReader
(Class<T> beanClass, String... properties) Creates aBeanReader
for the specified class.BeanReader
(Class<T> beanClass, BeanValueTransformer<T> transformer, String... properties) Creates aBeanReader
for the specified class.BeanReader
(Class<T> beanClass, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties) Creates aBeanReader
for the specified class.BeanReader
(Class<T> beanClass, IncludeExclude includeExclude, String... properties) Creates aBeanReader
for the specified class. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if the specified string represents a property that can be read by thisBeanReader
.static <T> BeanReaderBuilder
<T> Returns aBuilder
forBeanReader
instances.Returns the class of the objects thisBeanReader
can read.Returns thegetters
used by theBeanReader
to read bean properties.Returns the properties that thisBeanReader
can read.<U> U
Returns the value of the specified property on the specified bean.readAllProperties
(T bean) Returns the values of all readable properties in the specified JavaBean.
-
Constructor Details
-
BeanReader
Creates aBeanReader
for the specified class. You can optionally specify an array of properties that you intend to read. If you specify a zero-length array, all properties will be readable. If you intend to use thisBeanReader
to repetitively read just one or two properties from bulky bean types, explicitly specifying the properties you intend to read might make theBeanReader
slightly more efficient. It is not an error to specify non-existent properties. They will be tacitly ignored. A side effect of specifying one or more properties is that it forces the values returned fromreadAllProperties()
to be in the order in which you specify the properties.- Parameters:
beanClass
- the bean class (may be arecord
type)properties
- the properties to be included/excluded- See Also:
-
BeanReader
Creates aBeanReader
for the specified class. You can optionally specify an array of properties that you intend to read. If you specify a zero-length array, all properties will be readable. If you intend to use thisBeanReader
to repetitively read just one or two properties from bulky bean types, explicitly specifying the properties you intend to read might make theBeanReader
slightly more efficient. It is not an error to specify non-existent properties. They will be tacitly ignored. A side effect of specifying one or more properties to be included is that it forces the values returned fromreadAllProperties()
to be in the order in which you specify the properties.- Parameters:
beanClass
- the bean class (may be arecord
type)includeExclude
- whether to include or exclude the specified propertiesproperties
- the properties to be included/excluded- See Also:
-
BeanReader
public BeanReader(Class<T> beanClass, boolean strictNaming, IncludeExclude includeExclude, String... properties) Creates aBeanReader
for the specified class. You can optionally specify an array of properties that you intend to read. If you specify a zero-length array, all properties will be readable. If you intend to use thisBeanReader
to repetitively read just one or two properties from bulky bean types, explicitly specifying the properties you intend to read might make theBeanReader
slightly more efficient. It is not an error to specify non-existent properties. They will be tacitly ignored. A side effect of specifying one or more properties to be included is that it forces the values returned fromreadAllProperties()
to be in the order in which you specify the properties.- Parameters:
beanClass
- the bean class (may be arecord
type)strictNaming
- iffalse
, all methods with a zero-length parameter list and a non-void
return type, exceptgetClass()
,hashCode()
andtoString()
, will be regarded as getters. Otherwise JavaBeans naming conventions will be applied regarding which methods qualify as getters. By way of exception, methods returning aBoolean
are allowed to have a name starting with "is" (just like methods returning aboolean
). ThestrictNaming
parameter is tacitly ignored forrecord
classes. Records are always processed as thoughstrictNaming
werefalse
.includeExclude
- whether to include or exclude the subsequently specified propertiesproperties
- the properties to be included/excluded
-
BeanReader
Creates aBeanReader
for the specified class. You can optionally specify an array of properties that you intend to read. If you specify a zero-length array, all properties will be readable. If you intend to use thisBeanReader
to repetitively read just one or two properties from bulky bean types, explicitly specifying the properties you intend to read might make theBeanReader
slightly more efficient. It is not an error to specify non-existent properties. They will be tacitly ignored. A side effect of specifying one or more properties is that it forces the values returned fromreadAllProperties()
to be in the order in which you specify the properties.- Parameters:
beanClass
- the bean class (may be arecord
type)transformer
- a conversion function for bean values. The function is passed the bean from which the value was retrieved, the property that was read, and the value of the property. Using these three parameters, the function can compute a new value, which will be the value that is actually returned fromBeanReader.read()
.properties
- the properties to be included
-
BeanReader
public BeanReader(Class<T> beanClass, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties) Creates aBeanReader
for the specified class. You can optionally specify an array of properties that you intend to read. If you specify a zero-length array, all properties will be readable. If you intend to use thisBeanReader
to repetitively read just one or two properties from bulky bean types, explicitly specifying the properties you intend to read might make theBeanReader
slightly more efficient. It is not an error to specify non-existent properties. They will be tacitly ignored. A side effect of specifying one or more properties to be included is that it forces the values returned fromreadAllProperties()
to be in the order in which you specify the properties.- Parameters:
beanClass
- the bean class (may be arecord
type)transformer
- a conversion function for bean values. The function is passed the bean from which the value was retrieved, the property that was read, and the value of the property. Using these three parameters, the function can compute a new value, which will be the value that is actually returned fromBeanReader.read()
.includeExclude
- whether to include or exclude the subsequently specified propertiesproperties
- the properties to be included
-
BeanReader
public BeanReader(Class<T> beanClass, boolean strictNaming, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties) Creates aBeanReader
for the specified class. You can optionally specify an array of properties that you intend to read. If you specify a zero-length array, all properties will be readable. If you intend to use thisBeanReader
to repetitively read just one or two properties from bulky bean types, explicitly specifying the properties you intend to read might make theBeanReader
slightly more efficient. It is not an error to specify non-existent properties. They will be tacitly ignored. A side effect of specifying one or more properties to be included is that it forces the values returned fromreadAllProperties()
to be in the order in which you specify the properties.- Parameters:
beanClass
- the bean classstrictNaming
- iffalse
, all methods with a zero-length parameter list and a non-void
return type, exceptgetClass()
,hashCode()
andtoString()
, will be regarded as getters. Otherwise JavaBeans naming conventions will be applied regarding which methods qualify as getters. By way of exception, methods returning aBoolean
are allowed to have a name starting with "is" (just like methods returning aboolean
). ThestrictNaming
parameter is tacitly ignored forrecord
classes. Records are always processed as thoughstrictNaming
werefalse
.transformer
- a conversion function for bean values. The function is passed the bean from which the value was retrieved, the property that was read, and the value of the property. Using these three parameters, the function can compute a new value, which will be the value that is actually returned fromBeanReader.read()
.includeExclude
- whether to include or exclude the subsequently specified propertiesproperties
- the properties to be included/excluded
-
-
Method Details
-
forClass
Returns aBuilder
forBeanReader
instances. The builder will produce a 100% reflection-freeBeanReader
, which you may find desirable when writing Java 9+ modules.- Type Parameters:
T
- the type of the objects to be read- Parameters:
beanClass
- the class for which to create aBeanReader
(may be arecord
type)- Returns:
- a
Builder
forBeanReader
instances
-
read
Returns the value of the specified property on the specified bean. If the property does not exist aNoSuchPropertyException
is thrown. If thisBeanReader
was instantiated with aBeanValueTransformer
, the output from the transformer is returned.- Type Parameters:
U
- the type of the return value- Parameters:
bean
- the bean instanceproperty
- The property- Returns:
- its value
- Throws:
NoSuchPropertyException
- if the specified property does not exist
-
readAllProperties
Returns the values of all readable properties in the specified JavaBean. The values will be returned in the same order as the property names returned bygetReadableProperties()
.- Parameters:
bean
- the bean from which to read the values- Returns:
- the values of all readable properties in the specified JavaBean
-
getBeanClass
-
canRead
Returnstrue
if the specified string represents a property that can be read by thisBeanReader
. Note that this check is already done by theread
method before it will actually attempt to read from the provided bean. Only perform this check if there is a considerable chance that the provided string is not a readable property.- Parameters:
property
- The string to be tested- Returns:
true
if the specified string represents a property that can be read by thisBeanReader
- See Also:
-
getReadableProperties
Returns the properties that thisBeanReader
can read. If one or more properties were excluded through the constructor, they will not be contained in the returnedSet
. If no properties were excluded, or they were excluded viaIncludeExclude.EXCLUDE
, the properties will be returned in arbitrary order. If you specified one or more properties to be included (viaIncludeExclude.INCLUDE
), the properties will be returned in the order in which you specified those properties.- Returns:
- the properties that this
BeanReader
will read
-
getIncludedGetters
-