func) {
+ public static String getMethodName(Func1
func) {
return resolve(func).getImplMethodName();
}
+ /**
+ * 获取lambda表达式函数(方法)名称
+ *
+ * @param Lambda返回类型
+ * @param func 函数(无参方法)
+ * @return 函数名称
+ * @since 5.7.23
+ */
public static String getMethodName(Func0 func) {
return resolve(func).getImplMethodName();
}
@@ -59,30 +76,31 @@ public class LambdaUtil {
*
* @param Lambda类型
* @param func 函数(无参方法)
- * @return 函数名称
+ * @return 方法名称
* @throws IllegalArgumentException 非Getter或Setter方法
* @since 5.7.10
*/
public static String getFieldName(Func1 func) throws IllegalArgumentException {
- final String methodName = getMethodName(func);
- if (methodName.startsWith("get") || methodName.startsWith("set")) {
- return StrUtil.removePreAndLowerFirst(methodName, 3);
- } else if (methodName.startsWith("is")) {
- return StrUtil.removePreAndLowerFirst(methodName, 2);
- } else {
- throw new IllegalArgumentException("Invalid Getter or Setter name: " + methodName);
- }
+ return BeanUtil.getFieldName(getMethodName(func));
}
- public static String getFieldName(Func0 func) throws IllegalArgumentException {
- final String methodName = getMethodName(func);
- if (methodName.startsWith("get") || methodName.startsWith("set")) {
- return StrUtil.removePreAndLowerFirst(methodName, 3);
- } else if (methodName.startsWith("is")) {
- return StrUtil.removePreAndLowerFirst(methodName, 2);
- } else {
- throw new IllegalArgumentException("Invalid Getter or Setter name: " + methodName);
- }
+ /**
+ * 获取lambda表达式Getter或Setter函数(方法)对应的字段名称,规则如下:
+ *
+ * - getXxxx获取为xxxx,如getName得到name。
+ * - setXxxx获取为xxxx,如setName得到name。
+ * - isXxxx获取为xxxx,如isName得到name。
+ * - 其它不满足规则的方法名抛出{@link IllegalArgumentException}
+ *
+ *
+ * @param Lambda类型
+ * @param func 函数(无参方法)
+ * @return 方法名称
+ * @throws IllegalArgumentException 非Getter或Setter方法
+ * @since 5.7.23
+ */
+ public static String getFieldName(Func0 func) throws IllegalArgumentException {
+ return BeanUtil.getFieldName(getMethodName(func));
}
/**
diff --git a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java
index 194777224..63b8c02b7 100644
--- a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java
@@ -281,8 +281,8 @@ public class MapUtil {
/**
* 根据给定的Pair数组创建Map对象
*
- * @param 键类型
- * @param 值类型
+ * @param 键类型
+ * @param 值类型
* @param pairs 键值对
* @return Map
* @since 5.4.1
@@ -629,7 +629,7 @@ public class MapUtil {
}
Map map2 = ReflectUtil.newInstanceIfPossible(map.getClass());
- if(null == map2){
+ if (null == map2) {
map2 = new HashMap<>(map.size(), 1f);
}
if (isEmpty(map)) {
@@ -662,7 +662,7 @@ public class MapUtil {
* @since 3.1.0
*/
public static Map filter(Map map, Filter> filter) {
- if(null == map || null == filter){
+ if (null == map || null == filter) {
return map;
}
return edit(map, t -> filter.accept(t) ? t : null);
@@ -680,12 +680,12 @@ public class MapUtil {
*/
@SuppressWarnings("unchecked")
public static Map filter(Map map, K... keys) {
- if(null == map || null == keys){
+ if (null == map || null == keys) {
return map;
}
Map map2 = ReflectUtil.newInstanceIfPossible(map.getClass());
- if(null == map2){
+ if (null == map2) {
map2 = new HashMap<>(map.size(), 1f);
}
if (isEmpty(map)) {
@@ -792,9 +792,9 @@ public class MapUtil {
/**
* 按照值排序,可选是否倒序
*
- * @param map 需要对值排序的map
- * @param 键类型
- * @param 值类型
+ * @param map 需要对值排序的map
+ * @param 键类型
+ * @param 值类型
* @param isDesc 是否倒序
* @return 排序后新的Map
* @since 5.5.8
@@ -802,7 +802,7 @@ public class MapUtil {
public static > Map sortByValue(Map map, boolean isDesc) {
Map result = new LinkedHashMap<>();
Comparator> entryComparator = Entry.comparingByValue();
- if(isDesc){
+ if (isDesc) {
entryComparator = entryComparator.reversed();
}
map.entrySet().stream().sorted(entryComparator).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java
index ec9bd481d..81d595708 100644
--- a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java
@@ -1,13 +1,15 @@
package cn.hutool.core.lang;
+import cn.hutool.core.builder.GenericBuilder;
import cn.hutool.core.date.DateTime;
import org.junit.Assert;
import org.junit.Test;
-import static cn.hutool.core.lang.OptTest.User;
import java.util.HashMap;
import java.util.Map;
+import static cn.hutool.core.lang.OptTest.User;
+
public class DictTest {
@Test
public void dictTest(){
@@ -62,7 +64,7 @@ public class DictTest {
@Test
public void setFieldsTest() {
- User user = User.builder().username("hutool").nickname(null).build();
+ User user = GenericBuilder.of(User::new).with(User::setUsername, "hutool").build();
Dict dict = Dict.create();
dict.setFields(user::getNickname, user::getUsername);
Assert.assertEquals("hutool", dict.get("username"));