mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-23 16:26:05 +00:00
perf[reflection]: 增加get和set方法的规则,如果属性名的第一个字母是小写且第二个字母大写
This commit is contained in:
@@ -317,6 +317,15 @@ public abstract class ReflectionUtils {
|
||||
// java的get方法对boolean值有可能对应get或者is,所以尝试获取两种不同的get方法,当两种都获取不到才抛异常
|
||||
}
|
||||
|
||||
// 如果属性名的第一个字母是小写且第二个字母大写,那么该属性名直接用作 getter/setter。例如属性名为uName,对应的方法是getuName/setuName。
|
||||
// 如果属性名以大写字母开头,属性名直接用作 getter/setter 方法中 get/set 的后部分。例如属性名为Name,对应的方法是getName/setName。
|
||||
methodName = "get" + fieldName;
|
||||
try {
|
||||
clazz.getDeclaredMethod(methodName, null);
|
||||
return methodName;
|
||||
} catch (NoSuchMethodException e) {
|
||||
}
|
||||
|
||||
methodName = "is" + StringUtils.capitalize(fieldName);
|
||||
try {
|
||||
clazz.getDeclaredMethod(methodName, null);
|
||||
@@ -332,6 +341,13 @@ public abstract class ReflectionUtils {
|
||||
assertIsStandardFieldName(field);
|
||||
|
||||
var methodName = "set" + StringUtils.capitalize(fieldName);
|
||||
try {
|
||||
clazz.getDeclaredMethod(methodName, field.getType());
|
||||
return methodName;
|
||||
} catch (NoSuchMethodException e) {
|
||||
}
|
||||
|
||||
methodName = "set" + fieldName;
|
||||
try {
|
||||
clazz.getDeclaredMethod(methodName, field.getType());
|
||||
return methodName;
|
||||
|
||||
Reference in New Issue
Block a user