mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-28 22:04:04 +08:00
improved fluent setter method support
This commit is contained in:
@@ -377,7 +377,7 @@ class JavaBeanInfo {
|
||||
// support builder set
|
||||
|
||||
Class<?> returnType = method.getReturnType();
|
||||
if ((!(returnType == Void.TYPE || returnType == clazz)) //
|
||||
if ((!(returnType == Void.TYPE || returnType == method.getDeclaringClass())) //
|
||||
|| method.getParameterTypes().length != 1 //
|
||||
|| method.getDeclaringClass() == Object.class //
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.alibaba.json.bvt;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class FluentSetterTest extends TestCase {
|
||||
|
||||
public void test_fluent() throws Exception {
|
||||
B b = new B();
|
||||
b.setId(1001);
|
||||
b.setValue(1002);
|
||||
|
||||
String text = JSON.toJSONString(b);
|
||||
Assert.assertEquals("{\"id\":1001,\"value\":1002}", text);
|
||||
|
||||
B b1 = JSON.parseObject(text, B.class);
|
||||
Assert.assertEquals(b.getId(), b1.getId());
|
||||
Assert.assertEquals(b.getValue(), b1.getValue());
|
||||
}
|
||||
|
||||
public static class A {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public A setId(int id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class B extends A {
|
||||
|
||||
private int value;
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public B setValue(int value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user