Browse Source

add methods

Looly 5 years ago
parent
commit
3a579171c1

+ 3 - 0
CHANGELOG.md

@@ -9,8 +9,11 @@
 * 【setting】     Setting中增加addSetting和autoLoad重载(pr#104@Gitee)
 * 【core   】     增加copyProperties,根据Class创建对象并进行属性拷贝(pr#105@Gitee)
 * 【core   】     添加获取class当前文件夹名称方法(pr#106@Gitee)
+* 【core   】     BooleanUtil中重载歧义修正,修改了包装参数的方法名(issue#I1BSK8@Gitee)
+* 【core   】     XmlUtil增加xmlToBean和beanToXml方法
 
 ### Bug修复
+* 【core   】     修复TypeUtil无法获取泛型接口的泛型参数问题(issue#I1BRFI@Gitee)
 
 -------------------------------------------------------------------------------------------------------------
 ## 5.2.3

+ 16 - 16
hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java

@@ -23,7 +23,7 @@ public class BooleanUtil {
 		if (bool == null) {
 			return null;
 		}
-		return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
+		return bool ? Boolean.FALSE : Boolean.TRUE;
 	}
 
 	/**
@@ -99,7 +99,7 @@ public class BooleanUtil {
 	 * @return Integer值
 	 */
 	public static Integer toInteger(boolean value) {
-		return Integer.valueOf(toInt(value));
+		return toInt(value);
 	}
 
 	/**
@@ -119,7 +119,7 @@ public class BooleanUtil {
 	 * @return Character值
 	 */
 	public static Character toCharacter(boolean value) {
-		return Character.valueOf(toChar(value));
+		return toChar(value);
 	}
 
 	/**
@@ -139,7 +139,7 @@ public class BooleanUtil {
 	 * @return Byte值
 	 */
 	public static Byte toByteObj(boolean value) {
-		return Byte.valueOf(toByte(value));
+		return toByte(value);
 	}
 
 	/**
@@ -149,7 +149,7 @@ public class BooleanUtil {
 	 * @return long值
 	 */
 	public static long toLong(boolean value) {
-		return (long) toInt(value);
+		return toInt(value);
 	}
 
 	/**
@@ -159,7 +159,7 @@ public class BooleanUtil {
 	 * @return Long值
 	 */
 	public static Long toLongObj(boolean value) {
-		return Long.valueOf(toLong(value));
+		return toLong(value);
 	}
 
 	/**
@@ -179,7 +179,7 @@ public class BooleanUtil {
 	 * @return Short值
 	 */
 	public static Short toShortObj(boolean value) {
-		return Short.valueOf(toShort(value));
+		return toShort(value);
 	}
 
 	/**
@@ -199,7 +199,7 @@ public class BooleanUtil {
 	 * @return float值
 	 */
 	public static Float toFloatObj(boolean value) {
-		return Float.valueOf(toFloat(value));
+		return toFloat(value);
 	}
 
 	/**
@@ -209,7 +209,7 @@ public class BooleanUtil {
 	 * @return double值
 	 */
 	public static double toDouble(boolean value) {
-		return (double) toInt(value);
+		return toInt(value);
 	}
 
 	/**
@@ -219,7 +219,7 @@ public class BooleanUtil {
 	 * @return double值
 	 */
 	public static Double toDoubleObj(boolean value) {
-		return Double.valueOf(toDouble(value));
+		return toDouble(value);
 	}
 
 	/**
@@ -325,12 +325,12 @@ public class BooleanUtil {
 	 * @param array {@code Boolean}数组
 	 * @return 取与为真返回{@code true}
 	 */
-	public static Boolean and(final Boolean... array) {
+	public static Boolean andOfWrap(Boolean... array) {
 		if (ArrayUtil.isEmpty(array)) {
 			throw new IllegalArgumentException("The Array must not be empty !");
 		}
 		final boolean[] primitive = Convert.convert(boolean[].class, array);
-		return Boolean.valueOf(and(primitive));
+		return and(primitive);
 	}
 
 	/**
@@ -376,12 +376,12 @@ public class BooleanUtil {
 	 * @param array {@code Boolean}数组
 	 * @return 取或为真返回{@code true}
 	 */
-	public static Boolean or(Boolean... array) {
+	public static Boolean orOfWrap(Boolean... array) {
 		if (ArrayUtil.isEmpty(array)) {
 			throw new IllegalArgumentException("The Array must not be empty !");
 		}
 		final boolean[] primitive = Convert.convert(boolean[].class, array);
-		return Boolean.valueOf(or(primitive));
+		return or(primitive);
 	}
 
 	/**
@@ -424,12 +424,12 @@ public class BooleanUtil {
 	 * @param array {@code Boolean} 数组
 	 * @return 异或为真取{@code true}
 	 */
-	public static Boolean xor(Boolean... array) {
+	public static Boolean xorOfWrap(Boolean... array) {
 		if (ArrayUtil.isEmpty(array)) {
 			throw new IllegalArgumentException("The Array must not be empty !");
 		}
 		final boolean[] primitive = Convert.convert(boolean[].class, array);
-		return Boolean.valueOf(xor(primitive));
+		return xor(primitive);
 	}
 
 	/**

+ 16 - 5
hutool-core/src/main/java/cn/hutool/core/util/TypeUtil.java

@@ -1,5 +1,7 @@
 package cn.hutool.core.util;
 
+import cn.hutool.core.map.TableMap;
+
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
@@ -8,8 +10,6 @@ import java.lang.reflect.TypeVariable;
 import java.lang.reflect.WildcardType;
 import java.util.Map;
 
-import cn.hutool.core.map.TableMap;
-
 /**
  * 针对 {@link Type} 的工具类封装<br>
  * 最主要功能包括:
@@ -253,12 +253,23 @@ public class TypeUtil {
 	 * @since 4.5.2
 	 */
 	public static ParameterizedType toParameterizedType(Type type) {
+		ParameterizedType result = null;
 		if (type instanceof ParameterizedType) {
-			return (ParameterizedType) type;
+			result = (ParameterizedType) type;
 		} else if (type instanceof Class) {
-			return toParameterizedType(((Class<?>) type).getGenericSuperclass());
+			final Class<?> clazz = (Class<?>) type;
+			Type genericSuper = clazz.getGenericSuperclass();
+			if(null == genericSuper || Object.class.equals(genericSuper)){
+				// 如果类没有父类,而是实现一些定义好的泛型接口,则取接口的Type
+				final Type[] genericInterfaces = clazz.getGenericInterfaces();
+				if(ArrayUtil.isNotEmpty(genericInterfaces)){
+					// 默认取第一个实现接口的泛型Type
+					genericSuper = genericInterfaces[0];
+				}
+			}
+			result = toParameterizedType(genericSuper);
 		}
-		return null;
+		return result;
 	}
 	
 	/**

+ 29 - 0
hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java

@@ -1,5 +1,6 @@
 package cn.hutool.core.util;
 
+import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.exceptions.UtilException;
 import cn.hutool.core.io.FileUtil;
@@ -757,6 +758,19 @@ public class XmlUtil {
 	}
 
 	/**
+	 * XML转Java Bean
+	 *
+	 * @param <T> bean类型
+	 * @param node XML节点
+	 * @param bean bean类
+	 * @return bean
+	 * @since 5.2.4
+	 */
+	public static <T> T xmlToBean(Node node, Class<T> bean){
+		return BeanUtil.toBean(xmlToMap(node), bean);
+	}
+
+	/**
 	 * XML格式字符串转换为Map
 	 *
 	 * @param node XML节点
@@ -962,6 +976,21 @@ public class XmlUtil {
 	}
 
 	/**
+	 * 将Bean转换为XML
+	 *
+	 * @param bean      Bean对象
+	 * @param namespace 命名空间,可以为null
+	 * @return XML
+	 * @since 5.2.4
+	 */
+	public static Document beanToXml(Object bean, String namespace) {
+		if(null == bean){
+			return null;
+		}
+		return mapToXml(BeanUtil.beanToMap(bean), bean.getClass().getSimpleName(), namespace);
+	}
+
+	/**
 	 * 给定节点是否为{@link Element} 类型节点
 	 *
 	 * @param node 节点

+ 18 - 0
hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java

@@ -21,4 +21,22 @@ public class BooleanUtilTest {
 		Assert.assertFalse(BooleanUtil.toBoolean("6455434"));
 		Assert.assertFalse(BooleanUtil.toBoolean(""));
 	}
+
+	@Test
+	public void andTest(){
+		Assert.assertFalse(BooleanUtil.and(true,false));
+		Assert.assertFalse(BooleanUtil.andOfWrap(true,false));
+	}
+
+	@Test
+	public void orTest(){
+		Assert.assertTrue(BooleanUtil.or(true,false));
+		Assert.assertTrue(BooleanUtil.orOfWrap(true,false));
+	}
+
+	@Test
+	public void xorTest(){
+		Assert.assertTrue(BooleanUtil.xor(true,false));
+		Assert.assertTrue(BooleanUtil.xorOfWrap(true,false));
+	}
 }

+ 20 - 3
hutool-core/src/test/java/cn/hutool/core/util/TypeUtilTest.java

@@ -1,13 +1,13 @@
 package cn.hutool.core.util;
 
+import org.junit.Assert;
+import org.junit.Test;
+
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.junit.Assert;
-import org.junit.Test;
-
 public class TypeUtilTest {
 	
 	@Test
@@ -39,4 +39,21 @@ public class TypeUtilTest {
 			return 1;
 		}
 	}
+
+	@Test
+	public void getTypeArgumentTest(){
+		// 测试不继承父类,而是实现泛型接口时是否可以获取成功。
+		final Type typeArgument = TypeUtil.getTypeArgument(IPService.class);
+		Assert.assertEquals(String.class, typeArgument);
+	}
+
+	public interface OperateService<T> {
+		void service(T t);
+	}
+
+	public static class IPService implements OperateService<String> {
+		@Override
+		public void service(String string) {
+		}
+	}
 }