add methods

This commit is contained in:
Looly 2020-03-16 22:04:59 +08:00
parent 2801373c9e
commit 3a579171c1
6 changed files with 102 additions and 24 deletions

View File

@ -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

View File

@ -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);
}
/**

View File

@ -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;
}
/**

View File

@ -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;
@ -756,6 +757,19 @@ public class XmlUtil {
return xmlToMap(xmlStr, new HashMap<>());
}
/**
* 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
*
@ -961,6 +975,21 @@ public class XmlUtil {
return doc;
}
/**
* 将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} 类型节点
*

View File

@ -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));
}
}

View File

@ -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) {
}
}
}