public synchronized Method getWriteMethod() {
Method writeMethod = ();
if (writeMethod == null) {
Class<?> cls = getClass0();
if (cls == null || (writeMethodName == null && !this.writeMethodRef.isSet())) {
// The write method was explicitly set to null.
return null;
}
// We need the type to fetch the correct method.
Class<?> type = getPropertyType0();
if (type == null) {
try {
// Can't use getPropertyType since it will lead to recursive loop.
type = findPropertyType(getReadMethod(), null);
setPropertyType(type);
} catch (IntrospectionException ex) {
// Without the correct property type we can't be guaranteed
// to find the correct method.
return null;
}
}
if (writeMethodName == null) {
writeMethodName = Introspector.SET_PREFIX + getBaseName();
}
Class<?>[] args = (type == null) ? null : new Class<?>[] { type };
writeMethod = Introspector.findMethod(cls, writeMethodName, 1, args);
if (writeMethod != null) {
if (!ReturnType().equals(void.class)) {
writeMethod = null;
}
}
try {
setWriteMethod(writeMethod);
} catch (IntrospectionException ex) {
// fall through
}
}
return writeMethod;
}
通过分析getWriteMethod()源码,发现setter⽅法的返回值必须是void,否则返回null
if (writeMethod != null) {
if (!ReturnType().equals(void.class)) {
writeMethod = null;
}
}
使⽤refactor 菜单的 delombok 选项将lombok 注解转换成源码发现,@Accessors(chain = true)⽣成的setter⽅法返回的不是void⽽是对象本⾝,源码如下
@Entity
@Table(name = "image")
@Accessors(chain = true)
public class HouseImage {
@Id
@Column(name = "id")
private String image_id;
//图⽚地址
private String url = null;
public String getImage_id() {
return this.image_id;
}
public String getUrl() {
return this.url;
}
public HouseImage setImage_id(String image_id) {
this.image_id = image_id;
return this;
}
public HouseImage setUrl(String url) {
this.url = url;
return this;
}
}
结:@Accessors(chain = true) 注解导致JavaBean⽣成的 setter ⽅法不被 PropertyDescriptor 识别,导致获取 setter为null 进⼀步验证@Builder,发现需添加@NoArgsConstructor @AllArgsConstructor,否则会报如下异常
net.sf.json.JSONException: java.lang.NoSuchMethodException: com.qj.HouseImage.<init>()
at net.sf.Bean(JSONObject.java:288)
at net.sf.Bean(JSONObject.java:233)
object toat com.web.website.unit.image.MapInfoUnitTest.t(MapInfoUnitTest.java:65)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at org.del.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.del.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at st.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73) at st.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at st.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at st.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at st.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at st.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at st.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at st.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at st.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at st.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.NoSuchMethodException: com.qj.HouseImage.<init>()
at java.Constructor0(Class.java:3082)
at java.DeclaredConstructor(Class.java:2178)
at net.sf.json.util.wInstance(NewBeanInstanceStrategy.java:55)
at net.sf.Bean(JSONObject.java:282)
... 32 more
使⽤@Builder源码
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论