e.g.
class tester
{
@Test
public void testBeanUtils() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException
{
Stranger stranger = new Stranger();
BeanUtils.setProperty(stranger,"name","wener");
BeanUtils.setProperty(stranger,"xname","xwener");
BeanUtils.setProperty(stranger,"yname","ywener");
System.out.println(stranger);
}
@Data// lombok annotation generate all setter and getter
public static class Stranger
{
@Accessors(chain = true)// generate chained setter
String name;
String xname;
String yname;
public Stranger setYname(String yname)// no lombok, still not work
{
this.yname = yname;
return this;
}
}
}
My output:
TestValues.Stranger(name=null, xname=xwener, yname=null)
What's wrong with this? chain setter is a good thing. Any suggests ?
EDIT
Back to this problem again.This time I can not remove the Accessors chain
.
Now, I use the commons-lang3
to achieve.
// force access = true is required
Field field = FieldUtils.getField(bean.getClass(), attrName, true);
field.set(bean,value);
For those who got the same problem.
That's simple:
BeanUtils
are rather strange and so isIntrospector
it uses:Although
BeanUtils.setProperty
declares some exceptions, it seems to silently ignore the non-existence of the property to be set. The ultimate culprit is theIntrospector
which simply requires the voidness of setter.I'd call it broken by design, but YMMV. It's an old class and fluent interfaces weren't invented yet in those dark times. Use
Accessors(chain=false)
to disable chaining.More important: Use the source. Get it and get a debugger (it's already in your IDE) to find it out yourself (still feel free to ask if it doesn't work, just try a bit harder).
In my project we use chained accessors across the board, so setting
chain=false
was not an option. I ended up writing my own introspector, which is similar to the one recommended by @mthielcke, and may be registered in the same way.Introspector
Registration
You can use the FluentPropertyBeanIntrospector implementation:
"An implementation of the BeanIntrospector interface which can detect write methods for properties used in fluent API scenario."
https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/FluentPropertyBeanIntrospector.html