mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: optimize FieldWrapper.java
This commit is contained in:
parent
f8f9583bfc
commit
ab7e705b45
@ -53,14 +53,14 @@ public class FieldWrapper {
|
|||||||
if (fieldWrapper == null) {
|
if (fieldWrapper == null) {
|
||||||
Field findField = ClassUtil.getFirstField(clazz, field -> field.getName().equals(fieldName));
|
Field findField = ClassUtil.getFirstField(clazz, field -> field.getName().equals(fieldName));
|
||||||
if (findField == null) {
|
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);
|
String setterName = "set" + StringUtil.firstCharToUpperCase(fieldName);
|
||||||
Method setter = ClassUtil.getFirstMethod(clazz, method ->
|
Method setter = ClassUtil.getFirstMethod(clazz, method ->
|
||||||
method.getParameterCount() == 1
|
method.getParameterCount() == 1
|
||||||
&& Modifier.isPublic(method.getModifiers())
|
&& Modifier.isPublic(method.getModifiers())
|
||||||
&& method.getName().equals(setterName));
|
&& method.getName().equals(setterName));
|
||||||
|
|
||||||
fieldWrapper = new FieldWrapper();
|
fieldWrapper = new FieldWrapper();
|
||||||
fieldWrapper.field = findField;
|
fieldWrapper.field = findField;
|
||||||
@ -76,8 +76,8 @@ public class FieldWrapper {
|
|||||||
|
|
||||||
String[] getterNames = new String[]{"get" + StringUtil.firstCharToUpperCase(fieldName), "is" + StringUtil.firstCharToUpperCase(fieldName)};
|
String[] getterNames = new String[]{"get" + StringUtil.firstCharToUpperCase(fieldName), "is" + StringUtil.firstCharToUpperCase(fieldName)};
|
||||||
fieldWrapper.getterMethod = ClassUtil.getFirstMethod(clazz, method -> method.getParameterCount() == 0
|
fieldWrapper.getterMethod = ClassUtil.getFirstMethod(clazz, method -> method.getParameterCount() == 0
|
||||||
&& Modifier.isPublic(method.getModifiers())
|
&& Modifier.isPublic(method.getModifiers())
|
||||||
&& ArrayUtil.contains(getterNames, method.getName()));
|
&& ArrayUtil.contains(getterNames, method.getName()));
|
||||||
|
|
||||||
wrapperMap.put(fieldName, fieldWrapper);
|
wrapperMap.put(fieldName, fieldWrapper);
|
||||||
}
|
}
|
||||||
@ -117,7 +117,7 @@ public class FieldWrapper {
|
|||||||
public void set(Object value, Object to) {
|
public void set(Object value, Object to) {
|
||||||
try {
|
try {
|
||||||
if (setterMethod == null) {
|
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);
|
setterMethod.invoke(to, value);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -128,7 +128,8 @@ public class FieldWrapper {
|
|||||||
public Object get(Object target) {
|
public Object get(Object target) {
|
||||||
try {
|
try {
|
||||||
if (getterMethod == null) {
|
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);
|
return getterMethod.invoke(target);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -155,4 +156,4 @@ public class FieldWrapper {
|
|||||||
public boolean isIgnore() {
|
public boolean isIgnore() {
|
||||||
return isIgnore;
|
return isIgnore;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user