滞后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

@ -58,13 +58,9 @@ public class FieldWrapper {
String setterName = "set" + StringUtil.firstCharToUpperCase(fieldName);
Method setter = ClassUtil.getFirstMethod(clazz, method ->
method.getParameterCount() == 1
&& Modifier.isPublic(method.getModifiers())
&& method.getName().equals(setterName));
if (setter == null) {
throw new IllegalStateException("Can not find method \"set" + StringUtil.firstCharToUpperCase(fieldName) + "\" in class: " + clazz);
}
method.getParameterCount() == 1
&& Modifier.isPublic(method.getModifiers())
&& method.getName().equals(setterName));
fieldWrapper = new FieldWrapper();
fieldWrapper.field = findField;
@ -80,8 +76,8 @@ public class FieldWrapper {
String[] getterNames = new String[]{"get" + StringUtil.firstCharToUpperCase(fieldName), "is" + StringUtil.firstCharToUpperCase(fieldName)};
fieldWrapper.getterMethod = ClassUtil.getFirstMethod(clazz, method -> method.getParameterCount() == 0
&& Modifier.isPublic(method.getModifiers())
&& ArrayUtil.contains(getterNames, method.getName()));
&& Modifier.isPublic(method.getModifiers())
&& ArrayUtil.contains(getterNames, method.getName()));
wrapperMap.put(fieldName, fieldWrapper);
}
@ -120,6 +116,9 @@ public class FieldWrapper {
public void set(Object value, Object to) {
try {
if (setterMethod == null) {
throw new IllegalStateException("Can not find method \"set" + StringUtil.firstCharToUpperCase(field.getName()) + "\" in class: " + to.getClass());
}
setterMethod.invoke(to, value);
} catch (Exception e) {
throw new RuntimeException(e);
@ -128,6 +127,9 @@ public class FieldWrapper {
public Object get(Object target) {
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);
} catch (Exception e) {
throw new RuntimeException(e);
@ -153,4 +155,4 @@ public class FieldWrapper {
public boolean isIgnore() {
return isIgnore;
}
}
}