feat: optimize FieldWrapper.java

This commit is contained in:
michael 2024-01-21 12:58:51 +08:00
parent f8f9583bfc
commit ab7e705b45

View File

@ -53,14 +53,14 @@ public class FieldWrapper {
if (fieldWrapper == null) {
Field findField = ClassUtil.getFirstField(clazz, field -> field.getName().equals(fieldName));
if (findField == null) {
throw new IllegalStateException("Can not find field \"" + fieldName + "\" in class: " + clazz);
throw new IllegalStateException("Can not find field \"" + fieldName + "\" in class: " + clazz.getName());
}
String setterName = "set" + StringUtil.firstCharToUpperCase(fieldName);
Method setter = ClassUtil.getFirstMethod(clazz, method ->
method.getParameterCount() == 1
&& Modifier.isPublic(method.getModifiers())
&& method.getName().equals(setterName));
method.getParameterCount() == 1
&& Modifier.isPublic(method.getModifiers())
&& method.getName().equals(setterName));
fieldWrapper = new FieldWrapper();
fieldWrapper.field = findField;
@ -76,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);
}
@ -117,7 +117,7 @@ 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());
throw new IllegalStateException("Can not find method \"set" + StringUtil.firstCharToUpperCase(field.getName()) + "\" in class: " + to.getClass().getName());
}
setterMethod.invoke(to, value);
} catch (Exception e) {
@ -128,7 +128,8 @@ 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());
throw new IllegalStateException("Can not find method \"get" + StringUtil.firstCharToUpperCase(field.getName()) + ", is"
+ StringUtil.firstCharToUpperCase(field.getName()) + "\" in class: " + target.getClass().getName());
}
return getterMethod.invoke(target);
} catch (Exception e) {
@ -155,4 +156,4 @@ public class FieldWrapper {
public boolean isIgnore() {
return isIgnore;
}
}
}