BeanUtilworld, bean property is a class field with its optional setter and getter (aka accessors) methods. When accessing properties,
BeanUtilfirst tries to use accessors methods. If they don't exist,
BeanUtilfail-backs to using the field of the same visibility. Therefore, the existence of accessors methods is not required and depends on usage, which often may be handy.
BeanUtilis used internally inside the Jodd library, so this behavior applies everywhere.
BeanUtilusage: setting value of read-write property through its accessors methods. Setting
readonlyproperty in above example is only possible with default implementation, so we use
BeanUtil.declared. This variant first tries to use
setReadonly()method, but since such method doesn't exist, field value is accessed directly.
BeanUtilsupports nested properties. Nested properties can be java beans, a List, a Map or an array element:
BeanUtilaccess one property at time and, by default, expects that all inner properties exist i.e. are not-
null. Above example is executed like the following pseudo-code:
null. Using forced feature of
BeanUtil, such properties still may be set!
xin the above example has an uninitialized property
BeanUtilwill first create a new instance of
ytype, and set it to property
fooproperty of newly created object
ywill be set. In the second example,
yyis an array. If it is uninitialized,
BeanUtilwill create a new array of length 3. Then, it will create a new instance of
yytype that will be stored as the third element of the array. Finally, the
fooproperty is set.
BeanUtiltries to instantiate all uninitialized properties needed for setting the final property. Instantiation depends on the inner property type: if it is a simple bean, the no-args constructor will be invoked. If it is a list, new
ArrayListwill be created. Similar applies to arrays and map types. Additionally,
BeanUtilwill check the length of existing initialized arrays and lists and if the current size is not enough, list or array will be expanded by adding
nullelements up to the new size.
BeanUtilwill consider existing generics information in order to create an element of correct type.
BeanUtilExceptionto be thrown. Sometimes this is not desired behavior. For these cases,
BeanUtiloffers silent implementation that does not throw any exception at all.
BeanUtilalso offers a convenient way to test if some property exists: