wener wener - 2 months ago 24
Java Question

BeanUtils not works for chain setter

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.

Answer

That's simple: BeanUtils are rather strange and so is Introspector 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 the Introspector 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).