滞后FieldWrapper的setter的空判断,添加getter的空判断

因为部分情况下,构建FieldWrapper后,并不需要用到setter方法,所以通常字段上就不声明对应的setter了,但是此时会报错。把错误滞后到调用的时候,可以兼顾的解决问题
This commit is contained in:
唐振超 2023-12-25 06:26:44 +00:00 committed by Gitee
parent f2b9688eea
commit b47bcbfbc7
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -62,10 +62,6 @@ public class FieldWrapper {
&& Modifier.isPublic(method.getModifiers()) && Modifier.isPublic(method.getModifiers())
&& method.getName().equals(setterName)); && method.getName().equals(setterName));
if (setter == null) {
throw new IllegalStateException("Can not find method \"set" + StringUtil.firstCharToUpperCase(fieldName) + "\" in class: " + clazz);
}
fieldWrapper = new FieldWrapper(); fieldWrapper = new FieldWrapper();
fieldWrapper.field = findField; fieldWrapper.field = findField;
fieldWrapper.fieldType = findField.getType(); fieldWrapper.fieldType = findField.getType();
@ -120,6 +116,9 @@ public class FieldWrapper {
public void set(Object value, Object to) { public void set(Object value, Object to) {
try { try {
if (setterMethod == null) {
throw new IllegalStateException("Can not find method \"set" + StringUtil.firstCharToUpperCase(field.getName()) + "\" in class: " + to.getClass());
}
setterMethod.invoke(to, value); setterMethod.invoke(to, value);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -128,6 +127,9 @@ public class FieldWrapper {
public Object get(Object target) { public Object get(Object target) {
try { try {
if (getterMethod == null) {
throw new IllegalStateException("Can not find method \"get" + StringUtil.firstCharToUpperCase(field.getName()) + ", is" + StringUtil.firstCharToUpperCase(field.getName()) + "\" in class: " + target.getClass());
}
return getterMethod.invoke(target); return getterMethod.invoke(target);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);