mirror of
https://gitee.com/dromara/hutool.git
synced 2025-11-26 10:19:50 +08:00
fix code
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
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 javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
import java.io.File;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* JAXB(Java Architecture for XML Binding),根据XML Schema产生Java对象,即实现xml和Bean互转。
|
||||
* <p>
|
||||
* 相关介绍:
|
||||
* <ul>
|
||||
* <li><a href="https://www.cnblogs.com/yanghaolie/p/11110991.html">https://www.cnblogs.com/yanghaolie/p/11110991.html</a></li>
|
||||
* <li><a href="https://my.oschina.net/u/4266515/blog/3330113">https://my.oschina.net/u/4266515/blog/3330113</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @author dazer
|
||||
* @see XmlUtil
|
||||
* @since 5.7.3
|
||||
*/
|
||||
public class JAXBUtil {
|
||||
|
||||
/**
|
||||
* JavaBean转换成xml
|
||||
* <p>
|
||||
* bean上面用的常用注解
|
||||
*
|
||||
* @param bean Bean对象
|
||||
* @return 输出的XML字符串
|
||||
* @see XmlRootElement {@code @XmlRootElement(name = "school")}
|
||||
* @see XmlElement {@code @XmlElement(name = "school_name", required = true)}
|
||||
* @see XmlElementWrapper {@code @XmlElementWrapper(name="schools")}
|
||||
* @see XmlTransient JAXB "有两个名为 "**" 的属性,类的两个属性具有相同名称 "**""解决方案
|
||||
*/
|
||||
public static String beanToXml(final Object bean) {
|
||||
return beanToXml(bean, CharsetUtil.UTF_8, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* JavaBean转换成xml
|
||||
*
|
||||
* @param bean Bean对象
|
||||
* @param charset 编码 eg: utf-8
|
||||
* @param format 是否格式化输出eg: true
|
||||
* @return 输出的XML字符串
|
||||
*/
|
||||
public static String beanToXml(final Object bean, final Charset charset, final boolean format) {
|
||||
final StringWriter writer;
|
||||
try {
|
||||
final JAXBContext context = JAXBContext.newInstance(bean.getClass());
|
||||
final Marshaller marshaller = context.createMarshaller();
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
|
||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset.name());
|
||||
writer = new StringWriter();
|
||||
marshaller.marshal(bean, writer);
|
||||
} catch (final Exception e) {
|
||||
throw new UtilException("convertToXml 错误:" + e.getMessage(), e);
|
||||
}
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* xml转换成JavaBean
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param xml XML字符串
|
||||
* @param c Bean类型
|
||||
* @return bean
|
||||
*/
|
||||
public static <T> T xmlToBean(final String xml, final Class<T> c) {
|
||||
return xmlToBean(StrUtil.getReader(xml), c);
|
||||
}
|
||||
|
||||
/**
|
||||
* XML文件转Bean
|
||||
*
|
||||
* @param file 文件
|
||||
* @param charset 编码
|
||||
* @param c Bean类
|
||||
* @param <T> Bean类型
|
||||
* @return Bean
|
||||
*/
|
||||
public static <T> T xmlToBean(final File file, final Charset charset, final Class<T> c) {
|
||||
return xmlToBean(FileUtil.getReader(file, charset), c);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从{@link Reader}中读取XML字符串,并转换为Bean
|
||||
*
|
||||
* @param reader {@link Reader}
|
||||
* @param c Bean类
|
||||
* @param <T> Bean类型
|
||||
* @return Bean
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T xmlToBean(final Reader reader, final Class<T> c) {
|
||||
try {
|
||||
final JAXBContext context = JAXBContext.newInstance(c);
|
||||
final Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
return (T) unmarshaller.unmarshal(reader);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException("convertToJava2 错误:" + e.getMessage(), e);
|
||||
} finally {
|
||||
IoUtil.close(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,273 +0,0 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.map.WeakConcurrentMap;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本工具类
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class ScriptUtil {
|
||||
|
||||
private static final ScriptEngineManager MANAGER = new ScriptEngineManager();
|
||||
private static final WeakConcurrentMap<String, ScriptEngine> CACHE = new WeakConcurrentMap<>();
|
||||
|
||||
/**
|
||||
* 获得单例的{@link ScriptEngine} 实例
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
*/
|
||||
public static ScriptEngine getScript(final String nameOrExtOrMime) {
|
||||
return CACHE.computeIfAbsent(nameOrExtOrMime, (key) -> createScript(nameOrExtOrMime));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 {@link ScriptEngine} 实例
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createScript(final String nameOrExtOrMime) {
|
||||
ScriptEngine engine = MANAGER.getEngineByName(nameOrExtOrMime);
|
||||
if (null == engine) {
|
||||
engine = MANAGER.getEngineByExtension(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
engine = MANAGER.getEngineByMimeType(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
throw new NullPointerException(StrUtil.format("Script for [{}] not support !", nameOrExtOrMime));
|
||||
}
|
||||
return engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的JavaScript引擎
|
||||
*
|
||||
* @return Javascript引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getJsEngine() {
|
||||
return getScript("js");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的JavaScript引擎
|
||||
*
|
||||
* @return Javascript引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createJsEngine() {
|
||||
return createScript("js");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Python引擎<br>
|
||||
* 需要引入org.python:jython
|
||||
*
|
||||
* @return Python引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getPythonEngine() {
|
||||
System.setProperty("python.import.site", "false");
|
||||
return getScript("python");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Python引擎<br>
|
||||
* 需要引入org.python:jython
|
||||
*
|
||||
* @return Python引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createPythonEngine() {
|
||||
System.setProperty("python.import.site", "false");
|
||||
return createScript("python");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Lua引擎<br>
|
||||
* 需要引入org.luaj:luaj-jse
|
||||
*
|
||||
* @return Lua引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getLuaEngine() {
|
||||
return getScript("lua");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Lua引擎<br>
|
||||
* 需要引入org.luaj:luaj-jse
|
||||
*
|
||||
* @return Lua引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createLuaEngine() {
|
||||
return createScript("lua");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Groovy引擎<br>
|
||||
* 需要引入org.codehaus.groovy:groovy-all
|
||||
*
|
||||
* @return Groovy引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getGroovyEngine() {
|
||||
return getScript("groovy");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Groovy引擎<br>
|
||||
* 需要引入org.codehaus.groovy:groovy-all
|
||||
*
|
||||
* @return Groovy引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createGroovyEngine() {
|
||||
return createScript("groovy");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Javascript脚本,返回Invocable,此方法分为两种情况:
|
||||
*
|
||||
* <ol>
|
||||
* <li>执行的脚本返回值是可执行的脚本方法</li>
|
||||
* <li>脚本为函数库,则ScriptEngine本身为可执行方法</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return 执行结果
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static Invocable evalInvocable(final String script) throws UtilException {
|
||||
final ScriptEngine jsEngine = getJsEngine();
|
||||
final Object eval;
|
||||
try {
|
||||
eval = jsEngine.eval(script);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
if(eval instanceof Invocable){
|
||||
return (Invocable)eval;
|
||||
} else if(jsEngine instanceof Invocable){
|
||||
return (Invocable)jsEngine;
|
||||
}
|
||||
throw new UtilException("Script is not invocable !");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的Javascript脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return 执行结果
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(final String script) throws UtilException {
|
||||
try {
|
||||
return getJsEngine().eval(script);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param context 脚本上下文
|
||||
* @return 执行结果
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(final String script, final ScriptContext context) throws UtilException {
|
||||
try {
|
||||
return getJsEngine().eval(script, context);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param bindings 绑定的参数
|
||||
* @return 执行结果
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(final String script, final Bindings bindings) throws UtilException {
|
||||
try {
|
||||
return getJsEngine().eval(script, bindings);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行JS脚本中的指定方法
|
||||
*
|
||||
* @param script js脚本
|
||||
* @param func 方法名
|
||||
* @param args 方法参数
|
||||
* @return 结果
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static Object invoke(final String script, final String func, final Object... args) {
|
||||
final Invocable eval = evalInvocable(script);
|
||||
try {
|
||||
return eval.invokeFunction(func, args);
|
||||
} catch (final ScriptException | NoSuchMethodException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译Javascript脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static CompiledScript compile(final String script) throws UtilException {
|
||||
try {
|
||||
return compile(getJsEngine(), script);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译Javascript脚本
|
||||
*
|
||||
* @param engine 引擎
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptException 脚本异常
|
||||
*/
|
||||
public static CompiledScript compile(final ScriptEngine engine, final String script) throws ScriptException {
|
||||
if (engine instanceof Compilable) {
|
||||
final Compilable compEngine = (Compilable) engine;
|
||||
return compEngine.compile(script);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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,116 +0,0 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.xml.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* {@link JAXBUtil} 工具类
|
||||
* 测试 xml 和 bean 互转工具类
|
||||
*
|
||||
* @author dazer
|
||||
*/
|
||||
public class JAXBUtilTest {
|
||||
|
||||
private final String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
|
||||
"<school>\n" +
|
||||
" <school_name>西安市第一中学</school_name>\n" +
|
||||
" <school_address>西安市雁塔区长安堡一号</school_address>\n" +
|
||||
" <room>\n" +
|
||||
" <room_no>101</room_no>\n" +
|
||||
" <room_name>101教室</room_name>\n" +
|
||||
" </room>\n" +
|
||||
"</school>\n";
|
||||
|
||||
@Test
|
||||
public void beanToXmlTest() {
|
||||
final SchoolVo schoolVo = new SchoolVo();
|
||||
schoolVo.setSchoolName("西安市第一中学");
|
||||
schoolVo.setSchoolAddress("西安市雁塔区长安堡一号");
|
||||
|
||||
final SchoolVo.RoomVo roomVo = new SchoolVo.RoomVo();
|
||||
roomVo.setRoomName("101教室");
|
||||
roomVo.setRoomNo("101");
|
||||
schoolVo.setRoom(roomVo);
|
||||
|
||||
Assert.assertEquals(xmlStr, JAXBUtil.beanToXml(schoolVo));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlToBeanTest() {
|
||||
final SchoolVo schoolVo = JAXBUtil.xmlToBean(xmlStr, SchoolVo.class);
|
||||
Assert.assertNotNull(schoolVo);
|
||||
Assert.assertEquals("西安市第一中学", schoolVo.getSchoolName());
|
||||
Assert.assertEquals("西安市雁塔区长安堡一号", schoolVo.getSchoolAddress());
|
||||
|
||||
Assert.assertEquals("101教室", schoolVo.getRoom().getRoomName());
|
||||
Assert.assertEquals("101", schoolVo.getRoom().getRoomNo());
|
||||
}
|
||||
|
||||
@XmlRootElement(name = "school")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@XmlType(propOrder={"schoolName", "schoolAddress", "room"})
|
||||
public static class SchoolVo {
|
||||
@XmlElement(name = "school_name", required = true)
|
||||
private String schoolName;
|
||||
@XmlElement(name = "school_address", required = true)
|
||||
private String schoolAddress;
|
||||
@XmlElement(name = "room", required = true)
|
||||
private RoomVo room;
|
||||
|
||||
@XmlTransient
|
||||
public String getSchoolName() {
|
||||
return schoolName;
|
||||
}
|
||||
|
||||
public void setSchoolName(final String schoolName) {
|
||||
this.schoolName = schoolName;
|
||||
}
|
||||
|
||||
@XmlTransient
|
||||
public String getSchoolAddress() {
|
||||
return schoolAddress;
|
||||
}
|
||||
|
||||
public void setSchoolAddress(final String schoolAddress) {
|
||||
this.schoolAddress = schoolAddress;
|
||||
}
|
||||
|
||||
@XmlTransient
|
||||
public RoomVo getRoom() {
|
||||
return room;
|
||||
}
|
||||
|
||||
public void setRoom(final RoomVo room) {
|
||||
this.room = room;
|
||||
}
|
||||
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@XmlType(propOrder={"roomNo", "roomName"})
|
||||
public static final class RoomVo {
|
||||
@XmlElement(name = "room_no", required = true)
|
||||
private String roomNo;
|
||||
@XmlElement(name = "room_name", required = true)
|
||||
private String roomName;
|
||||
|
||||
@XmlTransient
|
||||
public String getRoomNo() {
|
||||
return roomNo;
|
||||
}
|
||||
|
||||
public void setRoomNo(final String roomNo) {
|
||||
this.roomNo = roomNo;
|
||||
}
|
||||
|
||||
@XmlTransient
|
||||
public String getRoomName() {
|
||||
return roomName;
|
||||
}
|
||||
|
||||
public void setRoomName(final String roomName) {
|
||||
this.roomName = roomName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,39 +0,0 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本单元测试类
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class ScriptUtilTest {
|
||||
|
||||
@Test
|
||||
public void compileTest() {
|
||||
final CompiledScript script = ScriptUtil.compile("print('Script test!');");
|
||||
try {
|
||||
script.eval();
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void evalTest() {
|
||||
ScriptUtil.eval("print('Script test!');");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeTest() {
|
||||
final Object result = ScriptUtil.invoke(ResourceUtil.readUtf8Str("filter1.js"), "filter1", 2, 1);
|
||||
Assert.assertTrue((Boolean) result);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
function filter1(a, b) {
|
||||
if (a > b) {
|
||||
return a > b;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user