This commit is contained in:
Looly 2022-06-16 19:19:56 +08:00
parent e491349b81
commit 40ff9f051e
26 changed files with 160 additions and 119 deletions

View File

@ -1,6 +1,6 @@
package cn.hutool.core.bean; package cn.hutool.core.bean;
import cn.hutool.core.clone.CloneSupport; import cn.hutool.core.exceptions.CloneRuntimeException;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.reflect.ClassUtil; import cn.hutool.core.reflect.ClassUtil;
import cn.hutool.core.reflect.ConstructorUtil; import cn.hutool.core.reflect.ConstructorUtil;
@ -16,7 +16,7 @@ import java.util.Map;
* @author Looly * @author Looly
* @since 3.0.7 * @since 3.0.7
*/ */
public class DynaBean extends CloneSupport<DynaBean> implements Serializable { public class DynaBean implements Cloneable, Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final Class<?> beanClass; private final Class<?> beanClass;
@ -224,4 +224,13 @@ public class DynaBean extends CloneSupport<DynaBean> implements Serializable {
public String toString() { public String toString() {
return this.bean.toString(); return this.bean.toString();
} }
@Override
public DynaBean clone() {
try {
return (DynaBean) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
} }

View File

@ -1,21 +0,0 @@
package cn.hutool.core.clone;
/**
* 克隆支持类提供默认的克隆方法
* @author Looly
*
* @param <T> 继承类的类型
*/
public class CloneSupport<T> implements Cloneable<T>{
@SuppressWarnings("unchecked")
@Override
public T clone() {
try {
return (T) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
}

View File

@ -1,16 +0,0 @@
package cn.hutool.core.clone;
/**
* 克隆支持接口
* @author Looly
*
* @param <T> 实现克隆接口的类型
*/
public interface Cloneable<T> extends java.lang.Cloneable{
/**
* 克隆当前对象浅复制
* @return 克隆后的对象
*/
T clone();
}

View File

@ -1,28 +0,0 @@
package cn.hutool.core.clone;
import cn.hutool.core.reflect.MethodUtil;
/**
* 克隆默认实现接口用于实现返回指定泛型类型的克隆方法
*
* @param <T> 泛型类型
* @since 5.7.17
*/
public interface DefaultCloneable<T> extends java.lang.Cloneable {
/**
* 浅拷贝提供默认的泛型返回值的clone方法
*
* @return obj
*/
default T clone0() {
try {
return MethodUtil.invoke(this, "clone");
} catch (final Exception e) {
throw new CloneRuntimeException(e);
}
}
}

View File

@ -1,7 +0,0 @@
/**
* 克隆封装
*
* @author looly
*
*/
package cn.hutool.core.clone;

View File

@ -1,4 +1,4 @@
package cn.hutool.core.clone; package cn.hutool.core.exceptions;
import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;

View File

@ -1,8 +1,8 @@
package cn.hutool.core.lang; package cn.hutool.core.lang;
import cn.hutool.core.clone.CloneSupport;
import cn.hutool.core.collection.iter.ArrayIter;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.collection.iter.ArrayIter;
import cn.hutool.core.exceptions.CloneRuntimeException;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import java.io.Serializable; import java.io.Serializable;
@ -20,7 +20,7 @@ import java.util.stream.StreamSupport;
* *
* @author Looly * @author Looly
*/ */
public class Tuple extends CloneSupport<Tuple> implements Iterable<Object>, Serializable { public class Tuple implements Iterable<Object>, Serializable, Cloneable {
private static final long serialVersionUID = -7689304393482182157L; private static final long serialVersionUID = -7689304393482182157L;
private final Object[] members; private final Object[] members;
@ -176,4 +176,13 @@ public class Tuple extends CloneSupport<Tuple> implements Iterable<Object>, Seri
public final Spliterator<Object> spliterator() { public final Spliterator<Object> spliterator() {
return Spliterators.spliterator(this.members, Spliterator.ORDERED); return Spliterators.spliterator(this.members, Spliterator.ORDERED);
} }
@Override
public Tuple clone() {
try {
return (Tuple) super.clone();
} catch (CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
} }

View File

@ -59,7 +59,7 @@ public class LambdaUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <R> Class<R> getRealClass(final Serializable func) { public static <R> Class<R> getRealClass(final Serializable func) {
LambdaInfo lambdaInfo = resolve(func); final LambdaInfo lambdaInfo = resolve(func);
return (Class<R>) Opt.of(lambdaInfo).map(LambdaInfo::getInstantiatedTypes).filter(types -> types.length != 0).map(types -> types[types.length - 1]).orElseGet(lambdaInfo::getClazz); return (Class<R>) Opt.of(lambdaInfo).map(LambdaInfo::getInstantiatedTypes).filter(types -> types.length != 0).map(types -> types[types.length - 1]).orElseGet(lambdaInfo::getClazz);
} }
@ -78,18 +78,18 @@ public class LambdaUtil {
ClassLoaderUtil.loadClass(serializedLambda.getImplClass().replace("/", "."), true); ClassLoaderUtil.loadClass(serializedLambda.getImplClass().replace("/", "."), true);
try { try {
implClass = Class.forName(serializedLambda.getImplClass().replace("/", "."), true, Thread.currentThread().getContextClassLoader()); implClass = Class.forName(serializedLambda.getImplClass().replace("/", "."), true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) { } catch (final ClassNotFoundException e) {
throw new UtilException(e); throw new UtilException(e);
} }
if ("<init>".equals(methodName)) { if ("<init>".equals(methodName)) {
for (Constructor<?> constructor : implClass.getDeclaredConstructors()) { for (final Constructor<?> constructor : implClass.getDeclaredConstructors()) {
if (ReflectUtil.getDescriptor(constructor).equals(serializedLambda.getImplMethodSignature())) { if (ReflectUtil.getDescriptor(constructor).equals(serializedLambda.getImplMethodSignature())) {
return new LambdaInfo(constructor, serializedLambda); return new LambdaInfo(constructor, serializedLambda);
} }
} }
} else { } else {
Method[] methods = MethodUtil.getMethods(implClass); final Method[] methods = MethodUtil.getMethods(implClass);
for (Method method : methods) { for (final Method method : methods) {
if (method.getName().equals(methodName) if (method.getName().equals(methodName)
&& ReflectUtil.getDescriptor(method).equals(serializedLambda.getImplMethodSignature())) { && ReflectUtil.getDescriptor(method).equals(serializedLambda.getImplMethodSignature())) {
return new LambdaInfo(method, serializedLambda); return new LambdaInfo(method, serializedLambda);

View File

@ -6,6 +6,7 @@ import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.test.bean.ExamInfoDict; import cn.hutool.core.lang.test.bean.ExamInfoDict;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.SystemUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -13,10 +14,25 @@ import org.junit.Test;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class MethodUtilTest { public class MethodUtilTest {
private static final String JAVA_VERSION = SystemUtil.get("java.version", false);
private static final boolean isGteJdk15 = getJavaVersion() >= 15;
/**
* jdk版本是否>= jdk15
* jdk15: 删除了 object registerNatives
* @return 反馈jdk版本78111517
* @author dazer
*/
private static int getJavaVersion() {
if (JAVA_VERSION.startsWith("1.")) {
return Integer.parseInt(JAVA_VERSION.split("\\.")[1]);
}
return Integer.parseInt(JAVA_VERSION.split("\\.")[0]);
}
@Test @Test
public void getMethodsTest() { public void getMethodsTest() {
Method[] methods = MethodUtil.getMethods(ExamInfoDict.class); Method[] methods = MethodUtil.getMethods(ExamInfoDict.class);
Assert.assertEquals(20, methods.length); Assert.assertEquals(isGteJdk15 ? 19 : 20, methods.length);
//过滤器测试 //过滤器测试
methods = MethodUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType())); methods = MethodUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType()));
@ -28,7 +44,7 @@ public class MethodUtilTest {
//null过滤器测试 //null过滤器测试
methods = MethodUtil.getMethods(ExamInfoDict.class, null); methods = MethodUtil.getMethods(ExamInfoDict.class, null);
Assert.assertEquals(20, methods.length); Assert.assertEquals(isGteJdk15 ? 19 : 20, methods.length);
final Method method2 = methods[0]; final Method method2 = methods[0];
Assert.assertNotNull(method2); Assert.assertNotNull(method2);
} }
@ -110,7 +126,7 @@ public class MethodUtilTest {
public void getMethodsFromClassExtends() { public void getMethodsFromClassExtends() {
// 继承情况下需解决方法去重问题 // 继承情况下需解决方法去重问题
Method[] methods = MethodUtil.getMethods(ReflectUtilTest.C2.class); Method[] methods = MethodUtil.getMethods(ReflectUtilTest.C2.class);
Assert.assertEquals(15, methods.length); Assert.assertEquals(isGteJdk15 ? 14 : 15, methods.length);
// 排除Object中的方法 // 排除Object中的方法
// 3个方法包括类 // 3个方法包括类

View File

@ -1,5 +1,6 @@
package cn.hutool.core.clone; package cn.hutool.core.util;
import cn.hutool.core.exceptions.CloneRuntimeException;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.junit.Assert; import org.junit.Assert;
@ -36,7 +37,7 @@ public class CloneTest {
* *
*/ */
@Data @Data
static class Cat implements Cloneable<Cat>{ static class Cat implements Cloneable{
private String name = "miaomiao"; private String name = "miaomiao";
private int age = 2; private int age = 2;
@ -57,8 +58,17 @@ public class CloneTest {
*/ */
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
@Data @Data
static class Dog extends CloneSupport<Dog>{ static class Dog implements Cloneable{
private String name = "wangwang"; private String name = "wangwang";
private int age = 3; private int age = 3;
@Override
public Dog clone() {
try {
return (Dog) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
} }
} }

View File

@ -1,6 +1,7 @@
package cn.hutool.core.clone; package cn.hutool.core.util;
import cn.hutool.core.exceptions.CloneRuntimeException;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import org.junit.Assert; import org.junit.Assert;
@ -18,7 +19,7 @@ public class DefaultCloneTest {
oldCar.setId(1); oldCar.setId(1);
oldCar.setWheelList(Stream.of(new Wheel("h")).collect(Collectors.toList())); oldCar.setWheelList(Stream.of(new Wheel("h")).collect(Collectors.toList()));
final Car newCar = oldCar.clone0(); final Car newCar = oldCar.clone();
Assert.assertEquals(oldCar.getId(), newCar.getId()); Assert.assertEquals(oldCar.getId(), newCar.getId());
Assert.assertEquals(oldCar.getWheelList(), newCar.getWheelList()); Assert.assertEquals(oldCar.getWheelList(), newCar.getWheelList());
@ -31,9 +32,18 @@ public class DefaultCloneTest {
} }
@Data @Data
static class Car implements DefaultCloneable<Car> { static class Car implements Cloneable {
private Integer id; private Integer id;
private List<Wheel> wheelList; private List<Wheel> wheelList;
@Override
public Car clone() {
try {
return (Car) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
} }
@Data @Data

View File

@ -1,9 +1,9 @@
package cn.hutool.core.util; package cn.hutool.core.util;
import cn.hutool.core.clone.CloneSupport;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.CloneRuntimeException;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -75,10 +75,19 @@ public class ObjUtilTest {
Assert.assertEquals("OK", obj2.doSomeThing()); Assert.assertEquals("OK", obj2.doSomeThing());
} }
static class Obj extends CloneSupport<Obj> { static class Obj implements Cloneable {
public String doSomeThing() { public String doSomeThing() {
return "OK"; return "OK";
} }
@Override
public Obj clone() {
try {
return (Obj) super.clone();
} catch (CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
} }
@Test @Test

View File

@ -1,6 +1,6 @@
package cn.hutool.db.ds; package cn.hutool.db.ds;
import cn.hutool.core.clone.CloneRuntimeException; import cn.hutool.core.exceptions.CloneRuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import javax.sql.DataSource; import javax.sql.DataSource;

View File

@ -1,6 +1,6 @@
package cn.hutool.db.ds.simple; package cn.hutool.db.ds.simple;
import cn.hutool.core.clone.CloneRuntimeException; import cn.hutool.core.exceptions.CloneRuntimeException;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.Closeable; import java.io.Closeable;

View File

@ -1,13 +1,14 @@
package cn.hutool.db.sql; package cn.hutool.db.sql;
import cn.hutool.core.clone.CloneSupport;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.CloneRuntimeException;
import cn.hutool.core.math.NumberUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.text.split.SplitUtil; import cn.hutool.core.text.split.SplitUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.CharUtil;
import cn.hutool.core.math.NumberUtil;
import cn.hutool.core.text.StrUtil;
import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -17,7 +18,8 @@ import java.util.List;
* *
* @author Looly * @author Looly
*/ */
public class Condition extends CloneSupport<Condition> { public class Condition implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
/** /**
* SQL中 LIKE 语句查询方式<br> * SQL中 LIKE 语句查询方式<br>
@ -362,6 +364,15 @@ public class Condition extends CloneSupport<Condition> {
return conditionStrBuilder.toString(); return conditionStrBuilder.toString();
} }
@Override
public Condition clone() {
try {
return (Condition) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
// ----------------------------------------------------------------------------------------------- Private method start // ----------------------------------------------------------------------------------------------- Private method start
/** /**

View File

@ -372,7 +372,7 @@
<dependency> <dependency>
<groupId>jakarta.validation</groupId> <groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId> <artifactId>jakarta.validation-api</artifactId>
<version>3.0.1</version> <version>3.0.2</version>
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
@ -464,7 +464,7 @@
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId> <artifactId>spring-expression</artifactId>
<version>5.3.19</version> <version>5.3.20</version>
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
@ -490,5 +490,23 @@
<version>6.1.6</version> <version>6.1.6</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openjdk.nashorn</groupId>
<artifactId>nashorn-core</artifactId>
<version>15.4</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,4 +1,4 @@
package cn.hutool.core.util; package cn.hutool.extra.script;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.map.WeakConcurrentMap; import cn.hutool.core.map.WeakConcurrentMap;

View File

@ -0,0 +1,6 @@
/**
* 使用Java对脚本引擎的工具封装
*
* @author looly
*/
package cn.hutool.extra.script;

View File

@ -1,9 +1,11 @@
package cn.hutool.core.util; package cn.hutool.extra.xml;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.XmlUtil;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;

View File

@ -0,0 +1,8 @@
/**
* XML和JAXBJava Architecture for XML Binding相关封装<br>
* 由于JDK11+移除了"javax.xml.bind"相关类型因此封装于extra模块
*
* @author looly
*
*/
package cn.hutool.extra.xml;

View File

@ -1,4 +1,4 @@
package cn.hutool.core.util; package cn.hutool.extra.script;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.io.resource.ResourceUtil;

View File

@ -1,4 +1,4 @@
package cn.hutool.core.util; package cn.hutool.extra.xml;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;

View File

@ -1,7 +1,6 @@
package cn.hutool.json; package cn.hutool.json;
import cn.hutool.core.bean.BeanPath; import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.reflect.TypeReference;
import java.io.Serializable; import java.io.Serializable;
import java.io.StringWriter; import java.io.StringWriter;
@ -171,16 +170,4 @@ public interface JSON extends Cloneable, Serializable {
default <T> T toBean(final Type type) { default <T> T toBean(final Type type) {
return JSONConverter.jsonConvert(type, this, getConfig()); return JSONConverter.jsonConvert(type, this, getConfig());
} }
/**
* 转为实体类对象转换异常将被抛出
*
* @param <T> Bean类型
* @param reference {@link TypeReference}类型参考子类可以获取其泛型参数中的Type类型
* @return 实体类对象
* @since 4.2.2
*/
default <T> T toBean(final TypeReference<T> reference) {
return toBean(reference.getType());
}
} }

View File

@ -333,6 +333,10 @@ public class JSONUtil {
if (obj instanceof CharSequence) { if (obj instanceof CharSequence) {
return StrUtil.str((CharSequence) obj); return StrUtil.str((CharSequence) obj);
} }
if(obj instanceof Number){
return obj.toString();
}
return toJsonStr(parse(obj, jsonConfig)); return toJsonStr(parse(obj, jsonConfig));
} }

View File

@ -233,4 +233,18 @@ public class JSONUtilTest {
final String xmlStr = JSONUtil.toXmlStr(obj); final String xmlStr = JSONUtil.toXmlStr(obj);
Assert.assertEquals("<key1>v1</key1><key2>a</key2><key2>b</key2><key2>c</key2>", xmlStr); Assert.assertEquals("<key1>v1</key1><key2>a</key2><key2>b</key2><key2>c</key2>", xmlStr);
} }
@Test
public void toJsonStrOfStringTest(){
String a = "a";
final String s = JSONUtil.toJsonStr(a);
Assert.assertEquals(a, s);
}
@Test
public void toJsonStrOfNumberTest(){
int a = 1;
final String s = JSONUtil.toJsonStr(a);
Assert.assertEquals("1", s);
}
} }