mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
e491349b81
commit
40ff9f051e
@ -1,6 +1,6 @@
|
||||
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.reflect.ClassUtil;
|
||||
import cn.hutool.core.reflect.ConstructorUtil;
|
||||
@ -16,7 +16,7 @@ import java.util.Map;
|
||||
* @author Looly
|
||||
* @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 final Class<?> beanClass;
|
||||
@ -224,4 +224,13 @@ public class DynaBean extends CloneSupport<DynaBean> implements Serializable {
|
||||
public String toString() {
|
||||
return this.bean.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DynaBean clone() {
|
||||
try {
|
||||
return (DynaBean) super.clone();
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
throw new CloneRuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package cn.hutool.core.clone;
|
||||
|
||||
/**
|
||||
* 克隆支持接口
|
||||
* @author Looly
|
||||
*
|
||||
* @param <T> 实现克隆接口的类型
|
||||
*/
|
||||
public interface Cloneable<T> extends java.lang.Cloneable{
|
||||
|
||||
/**
|
||||
* 克隆当前对象,浅复制
|
||||
* @return 克隆后的对象
|
||||
*/
|
||||
T clone();
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +0,0 @@
|
||||
/**
|
||||
* 克隆封装
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.core.clone;
|
@ -1,4 +1,4 @@
|
||||
package cn.hutool.core.clone;
|
||||
package cn.hutool.core.exceptions;
|
||||
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
@ -1,8 +1,8 @@
|
||||
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.iter.ArrayIter;
|
||||
import cn.hutool.core.exceptions.CloneRuntimeException;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -20,7 +20,7 @@ import java.util.stream.StreamSupport;
|
||||
*
|
||||
* @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 final Object[] members;
|
||||
@ -176,4 +176,13 @@ public class Tuple extends CloneSupport<Tuple> implements Iterable<Object>, Seri
|
||||
public final Spliterator<Object> spliterator() {
|
||||
return Spliterators.spliterator(this.members, Spliterator.ORDERED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple clone() {
|
||||
try {
|
||||
return (Tuple) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new CloneRuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ public class LambdaUtil {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
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);
|
||||
}
|
||||
|
||||
@ -78,18 +78,18 @@ public class LambdaUtil {
|
||||
ClassLoaderUtil.loadClass(serializedLambda.getImplClass().replace("/", "."), true);
|
||||
try {
|
||||
implClass = Class.forName(serializedLambda.getImplClass().replace("/", "."), true, Thread.currentThread().getContextClassLoader());
|
||||
} catch (ClassNotFoundException e) {
|
||||
} catch (final ClassNotFoundException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
if ("<init>".equals(methodName)) {
|
||||
for (Constructor<?> constructor : implClass.getDeclaredConstructors()) {
|
||||
for (final Constructor<?> constructor : implClass.getDeclaredConstructors()) {
|
||||
if (ReflectUtil.getDescriptor(constructor).equals(serializedLambda.getImplMethodSignature())) {
|
||||
return new LambdaInfo(constructor, serializedLambda);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Method[] methods = MethodUtil.getMethods(implClass);
|
||||
for (Method method : methods) {
|
||||
final Method[] methods = MethodUtil.getMethods(implClass);
|
||||
for (final Method method : methods) {
|
||||
if (method.getName().equals(methodName)
|
||||
&& ReflectUtil.getDescriptor(method).equals(serializedLambda.getImplMethodSignature())) {
|
||||
return new LambdaInfo(method, serializedLambda);
|
||||
|
@ -6,6 +6,7 @@ import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.test.bean.ExamInfoDict;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.SystemUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@ -13,10 +14,25 @@ import org.junit.Test;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
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版本,如:7、8、11、15、17
|
||||
* @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
|
||||
public void getMethodsTest() {
|
||||
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()));
|
||||
@ -28,7 +44,7 @@ public class MethodUtilTest {
|
||||
//null过滤器测试
|
||||
methods = MethodUtil.getMethods(ExamInfoDict.class, null);
|
||||
|
||||
Assert.assertEquals(20, methods.length);
|
||||
Assert.assertEquals(isGteJdk15 ? 19 : 20, methods.length);
|
||||
final Method method2 = methods[0];
|
||||
Assert.assertNotNull(method2);
|
||||
}
|
||||
@ -110,7 +126,7 @@ public class MethodUtilTest {
|
||||
public void getMethodsFromClassExtends() {
|
||||
// 继承情况下,需解决方法去重问题
|
||||
Method[] methods = MethodUtil.getMethods(ReflectUtilTest.C2.class);
|
||||
Assert.assertEquals(15, methods.length);
|
||||
Assert.assertEquals(isGteJdk15 ? 14 : 15, methods.length);
|
||||
|
||||
// 排除Object中的方法
|
||||
// 3个方法包括类
|
||||
|
@ -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.EqualsAndHashCode;
|
||||
import org.junit.Assert;
|
||||
@ -36,7 +37,7 @@ public class CloneTest {
|
||||
*
|
||||
*/
|
||||
@Data
|
||||
static class Cat implements Cloneable<Cat>{
|
||||
static class Cat implements Cloneable{
|
||||
private String name = "miaomiao";
|
||||
private int age = 2;
|
||||
|
||||
@ -57,8 +58,17 @@ public class CloneTest {
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Data
|
||||
static class Dog extends CloneSupport<Dog>{
|
||||
static class Dog implements Cloneable{
|
||||
private String name = "wangwang";
|
||||
private int age = 3;
|
||||
|
||||
@Override
|
||||
public Dog clone() {
|
||||
try {
|
||||
return (Dog) super.clone();
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
throw new CloneRuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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.Data;
|
||||
import org.junit.Assert;
|
||||
@ -18,7 +19,7 @@ public class DefaultCloneTest {
|
||||
oldCar.setId(1);
|
||||
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.getWheelList(), newCar.getWheelList());
|
||||
|
||||
@ -31,9 +32,18 @@ public class DefaultCloneTest {
|
||||
}
|
||||
|
||||
@Data
|
||||
static class Car implements DefaultCloneable<Car> {
|
||||
static class Car implements Cloneable {
|
||||
private Integer id;
|
||||
private List<Wheel> wheelList;
|
||||
|
||||
@Override
|
||||
public Car clone() {
|
||||
try {
|
||||
return (Car) super.clone();
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
throw new CloneRuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
@ -1,9 +1,9 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.clone.CloneSupport;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.exceptions.CloneRuntimeException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -75,10 +75,19 @@ public class ObjUtilTest {
|
||||
Assert.assertEquals("OK", obj2.doSomeThing());
|
||||
}
|
||||
|
||||
static class Obj extends CloneSupport<Obj> {
|
||||
static class Obj implements Cloneable {
|
||||
public String doSomeThing() {
|
||||
return "OK";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Obj clone() {
|
||||
try {
|
||||
return (Obj) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new CloneRuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.hutool.db.ds;
|
||||
|
||||
import cn.hutool.core.clone.CloneRuntimeException;
|
||||
import cn.hutool.core.exceptions.CloneRuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.hutool.db.ds.simple;
|
||||
|
||||
import cn.hutool.core.clone.CloneRuntimeException;
|
||||
import cn.hutool.core.exceptions.CloneRuntimeException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.Closeable;
|
||||
|
@ -1,13 +1,14 @@
|
||||
package cn.hutool.db.sql;
|
||||
|
||||
import cn.hutool.core.clone.CloneSupport;
|
||||
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.util.ArrayUtil;
|
||||
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.Collection;
|
||||
import java.util.List;
|
||||
@ -17,7 +18,8 @@ import java.util.List;
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class Condition extends CloneSupport<Condition> {
|
||||
public class Condition implements Cloneable, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* SQL中 LIKE 语句查询方式<br>
|
||||
@ -362,6 +364,15 @@ public class Condition extends CloneSupport<Condition> {
|
||||
return conditionStrBuilder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition clone() {
|
||||
try {
|
||||
return (Condition) super.clone();
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
throw new CloneRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
|
@ -372,7 +372,7 @@
|
||||
<dependency>
|
||||
<groupId>jakarta.validation</groupId>
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<version>3.0.2</version>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
@ -464,7 +464,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-expression</artifactId>
|
||||
<version>5.3.19</version>
|
||||
<version>5.3.20</version>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
@ -490,5 +490,23 @@
|
||||
<version>6.1.6</version>
|
||||
<scope>provided</scope>
|
||||
</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>
|
||||
</project>
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.hutool.core.util;
|
||||
package cn.hutool.extra.script;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.map.WeakConcurrentMap;
|
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* 使用Java对脚本引擎的工具封装
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
package cn.hutool.extra.script;
|
@ -1,9 +1,11 @@
|
||||
package cn.hutool.core.util;
|
||||
package cn.hutool.extra.xml;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
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.Marshaller;
|
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* XML和JAXB(Java Architecture for XML Binding)相关封装<br>
|
||||
* 由于JDK11+移除了"javax.xml.bind"相关类型,因此封装于extra模块
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.extra.xml;
|
@ -1,4 +1,4 @@
|
||||
package cn.hutool.core.util;
|
||||
package cn.hutool.extra.script;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
@ -1,4 +1,4 @@
|
||||
package cn.hutool.core.util;
|
||||
package cn.hutool.extra.xml;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
0
hutool-core/src/test/resources/filter1.js → hutool-extra/src/test/resources/filter1.js
Executable file → Normal file
0
hutool-core/src/test/resources/filter1.js → hutool-extra/src/test/resources/filter1.js
Executable file → Normal file
@ -1,7 +1,6 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.BeanPath;
|
||||
import cn.hutool.core.reflect.TypeReference;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.io.StringWriter;
|
||||
@ -171,16 +170,4 @@ public interface JSON extends Cloneable, Serializable {
|
||||
default <T> T toBean(final Type type) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -333,6 +333,10 @@ public class JSONUtil {
|
||||
if (obj instanceof CharSequence) {
|
||||
return StrUtil.str((CharSequence) obj);
|
||||
}
|
||||
|
||||
if(obj instanceof Number){
|
||||
return obj.toString();
|
||||
}
|
||||
return toJsonStr(parse(obj, jsonConfig));
|
||||
}
|
||||
|
||||
|
@ -233,4 +233,18 @@ public class JSONUtilTest {
|
||||
final String xmlStr = JSONUtil.toXmlStr(obj);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user