view raw
wener wener - 7 months ago 68
Java Question

BeanUtils not works for chain setter


class tester
public void testBeanUtils() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException
Stranger stranger = new 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 ?


Back to this problem again.This time I can not remove the
Accessors chain
Now, I use the
to achieve.

// force access = true is required
Field field = FieldUtils.getField(bean.getClass(), attrName, true);

For those who got the same problem.


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).