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 aBeanReaderfor the specified properties of the specified class.BeanReader(Class<T> beanClass, boolean strictNaming, IncludeExclude includeExclude, String... properties) Creates aBeanReaderfor the specified properties of the specified class.BeanReader(Class<T> beanClass, String... properties) Creates aBeanReaderfor the specified properties of the specified class.BeanReader(Class<T> beanClass, IncludeExclude includeExclude, String... properties) Creates aBeanReaderfor the specified properties of the specified class. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif the specified string represents a property that can be read by thisBeanReader.static <T> BeanReaderBuilder<T> Returns aBuilderforBeanReaderinstances.Returns the class of the objects thisBeanReadercan read.Returns thegettersused by theBeanReaderto read bean properties.Returns the properties that thisBeanReaderwill read.<U> UReturns 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 aBeanReaderfor the specified properties of 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. JavaBeans naming conventions will be applied regarding which methods qualify as getters.- Parameters:
beanClass- the bean classproperties- the properties to be included/excluded
-
BeanReader
Creates aBeanReaderfor the specified properties of 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. JavaBeans naming conventions will be applied regarding which methods qualify as getters.- Parameters:
beanClass- the bean classincludeExclude- whether to include or exclude the specified propertiesproperties- the properties to be included/excluded
-
BeanReader
public BeanReader(Class<T> beanClass, boolean strictNaming, IncludeExclude includeExclude, String... properties) Creates aBeanReaderfor the specified properties of 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 thisBeanReaderto repetitively read just one or two properties from bulky bean types, explicitly specifying the properties you intend to read might make theBeanReaderslightly more efficient.Specifying non-existent properties will not cause an exception to be thrown. They will be tacitly ignored.
- Parameters:
beanClass- the bean classstrictNaming- iffalse, all methods with a zero-length parameter list and a non-voidreturn 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 aBooleanare allowed to have a name starting with "is" (just like methods returning aboolean). ThestrictNamingparameter is quietly ignored forrecordclasses. Records are always processed as thoughstrictNamingwerefalse.includeExclude- whether to include or exclude the subsequently specified propertiesproperties- the properties to be included/excluded
-
BeanReader
public BeanReader(Class<T> beanClass, boolean strictNaming, BeanValueTransformer<T> transformer, IncludeExclude includeExclude, String... properties) Creates aBeanReaderfor the specified properties of 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 thisBeanReaderto repetitively read just one or two properties from bulky bean types, explicitly specifying the properties you intend to read might make theBeanReaderslightly more efficient.Specifying non-existent properties will not cause an exception to be thrown. They will be tacitly ignored.
- Parameters:
beanClass- the bean classstrictNaming- iffalse, all methods with a zero-length parameter list and a non-voidreturn 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 aBooleanare allowed to have a name starting with "is" (just like methods returning aboolean). ThestrictNamingparameter is quietly ignored forrecordclasses. Records are always processed as thoughstrictNamingwerefalse.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 aBuilderforBeanReaderinstances. Note that the specified type may just as well be arecordorenumtype.- Type Parameters:
T- the type of the objects to be read- Parameters:
beanClass- the class for which to create aBeanReader- Returns:
- a
BuilderforBeanReaderinstances
-
read
Returns the value of the specified property on the specified bean. If the property does not exist aNoSuchPropertyExceptionis thrown. If thisBeanReaderwas 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 asgetReadableProperties().- Parameters:
bean- the bean from which to read the values- Returns:
- the values of all reable properties in the specified JavaBean
-
getBeanClass
Returns the class of the objects thisBeanReadercan read.- Returns:
- the class of the objects
BeanReadercan read
-
canRead
Returnstrueif the specified string represents a property that can be read by thisBeanReader. Note that this check is already done by thereadmethod 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:
trueif the specified string represents a property that can be read by thisBeanReader- See Also:
-
getReadableProperties
Returns the properties that thisBeanReaderwill read. If one or more properties were excluded through the constructor, they will not be contained in the returnedSet. If no properties were excluded, the properties will be returned in the order determined byClass.getMethods(). If you specified one or more properties to be included (excluding any other properties), the properties will be returned in the order in which you specified those properties.- Returns:
- the properties that this
BeanReaderwill read
-
getIncludedGetters
Returns thegettersused by theBeanReaderto read bean properties. The returnedMapmaps the name of a property to theGetterused to read it.- Returns:
- all getters used to read bean properties.
-