浏览代码

change getPropery return to T

Looly 6 年之前
父节点
当前提交
e168de8622
共有 2 个文件被更改,包括 12 次插入18 次删除
  1. 1 0
      CHANGELOG.md
  2. 11 18
      hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java

+ 1 - 0
CHANGELOG.md

@@ -10,6 +10,7 @@
 * 【core】       Convert.toList支持[1,2]字符串(issue#I149XN@Gitee)
 * 【core】       修正DateUtil.thisWeekOfMonth注释错误(issue#614@Github)
 * 【core】       DateUtil增加toLocalDate等方法,DateTime更好的支持时区
+* 【core】       BeanUtil.getProperty返回泛型对象(issue#I14PIW@Gitee)
 
 ### Bug修复
 * 【db】         修复MetaUtil.getTableMeta()方法未释放ResultSet的bug(issue#I148GH@Gitee)

+ 11 - 18
hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java

@@ -175,12 +175,9 @@ public class BeanUtil {
 		} catch (IntrospectionException e) {
 			throw new BeanException(e);
 		}
-		return ArrayUtil.filter(beanInfo.getPropertyDescriptors(), new Filter<PropertyDescriptor>() {
-			@Override
-			public boolean accept(PropertyDescriptor t) {
-				// 过滤掉getClass方法
-				return false == "class".equals(t.getName());
-			}
+		return ArrayUtil.filter(beanInfo.getPropertyDescriptors(), (Filter<PropertyDescriptor>) t -> {
+			// 过滤掉getClass方法
+			return false == "class".equals(t.getName());
 		});
 	}
 
@@ -211,8 +208,8 @@ public class BeanUtil {
 	 */
 	private static Map<String, PropertyDescriptor> internalGetPropertyDescriptorMap(Class<?> clazz, boolean ignoreCase) throws BeanException {
 		final PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(clazz);
-		final Map<String, PropertyDescriptor> map = ignoreCase ? new CaseInsensitiveMap<String, PropertyDescriptor>(propertyDescriptors.length, 1)
-				: new HashMap<String, PropertyDescriptor>((int) (propertyDescriptors.length), 1);
+		final Map<String, PropertyDescriptor> map = ignoreCase ? new CaseInsensitiveMap<>(propertyDescriptors.length, 1)
+				: new HashMap<>((int) (propertyDescriptors.length), 1);
 
 		for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
 			map.put(propertyDescriptor.getName(), propertyDescriptor);
@@ -295,14 +292,16 @@ public class BeanUtil {
 	/**
 	 * 解析Bean中的属性值
 	 *
+	 * @param <T> 属性值类型
 	 * @param bean       Bean对象,支持Map、List、Collection、Array
 	 * @param expression 表达式,例如:person.friend[5].name
 	 * @return Bean属性值
 	 * @see BeanPath#get(Object)
 	 * @since 3.0.7
 	 */
-	public static Object getProperty(Object bean, String expression) {
-		return BeanPath.create(expression).get(bean);
+	@SuppressWarnings("unchecked")
+	public static <T> T getProperty(Object bean, String expression) {
+		return (T) BeanPath.create(expression).get(bean);
 	}
 
 	/**
@@ -504,7 +503,7 @@ public class BeanUtil {
 	 * @return Map
 	 */
 	public static Map<String, Object> beanToMap(Object bean, boolean isToUnderlineCase, boolean ignoreNullValue) {
-		return beanToMap(bean, new LinkedHashMap<String, Object>(), isToUnderlineCase, ignoreNullValue);
+		return beanToMap(bean, new LinkedHashMap<>(), isToUnderlineCase, ignoreNullValue);
 	}
 
 	/**
@@ -522,13 +521,7 @@ public class BeanUtil {
 			return null;
 		}
 
-		return beanToMap(bean, targetMap, ignoreNullValue, new Editor<String>() {
-
-			@Override
-			public String edit(String key) {
-				return isToUnderlineCase ? StrUtil.toUnderlineCase(key) : key;
-			}
-		});
+		return beanToMap(bean, targetMap, ignoreNullValue, key -> isToUnderlineCase ? StrUtil.toUnderlineCase(key) : key);
 	}
 
 	/**