Usage

Working with bean properties

In BeanUtil world, bean property is a class field with its optional setter and getter (aka accessors) methods. When accessing properties, BeanUtil first tries to use accessors methods. If they don't exist, BeanUtil fail-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. BeanUtil is used internally inside the Jodd library, so this behavior applies everywhere.
Simple bean:
1
public class Foo {
2
private String readwrite; // with getter and setter
3
private String readonly; // with getter
4
...
5
}
Copied!
Usage:
1
Foo foo = new Foo();
2
BeanUtil.pojo.setProperty(foo, "readwrite", "data");
3
BeanUtil.pojo.getProperty(foo, "readwrite");
4
BeanUtil.declared.setProperty(foo, "readonly", "data");
Copied!
Lines #2 and #3 show common and expected BeanUtil usage: setting value of read-write property through its accessors methods. Setting readonly property 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.

Nested properties

BeanUtil supports nested properties. Nested properties can be java beans, a List, a Map or an array element:
1
BeanUtil.pojo.getProperty(cbean, "list[0].map[foo].foo");
2
BeanUtil.pojo.setProperty(cbean, "arr[4].map[elem.boo].foo", "test");
Copied!
When accessing nested properties, BeanUtil access 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:
1
cbean.getList().get(0).get("foo").getFoo();
2
cbean.getArr()[4].get("elem.boo").setFoo("test");
Copied!

Forced setting of nested properties

The setting of nested properties fails if one of the inner elements is null. Using forced feature of BeanUtil, such properties still may be set!
1
BeanUtil.forced.setProperty(x, "y.foo", value);
2
BeanUtil.forced.setProperty(x, "yy[2].foo", "xxx");
Copied!
If the object x in the above example has an uninitialized property y, BeanUtil will first create a new instance of y type, and set it to property y. Then, foo property of newly created object y will be set. In the second example, yy is an array. If it is uninitialized, BeanUtil will create a new array of length 3. Then, it will create a new instance of yy type that will be stored as the third element of the array. Finally, the foo property is set.
In forced mode, BeanUtil tries 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 ArrayList will be created. Similar applies to arrays and map types. Additionally, BeanUtil will 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 null elements up to the new size.

Generics support

When creating a new element of a list, BeanUtil will consider existing generics information in order to create an element of correct type.

Silent mode (no exceptions)

Property setting may fail for various reasons, causing an unchecked exception BeanUtilException to be thrown. Sometimes this is not desired behavior. For these cases, BeanUtil offers silent implementation that does not throw any exception at all.

Maps and lists instead of beans

You can pass maps and list instead of beans as a root object. Just omit the bean name (since we do not work on a bean anymore):
1
Properties properties = new Properties();
2
BeanUtil.pojo.setProperty(property, "[ldap.auth.enabled]", "true");
Copied!

Testing of property existence

BeanUtil also offers a convenient way to test if some property exists:
1
BeanUtil.pojo.hasProperty(fb, "fooInteger")
Copied!
Last modified 10mo ago