style(core): 规范命名,暴露异常

This commit is contained in:
wy 2022-12-17 08:41:27 +08:00
parent 344a292a30
commit d6cf4d70e9
4 changed files with 55 additions and 53 deletions

View File

@ -1,5 +1,6 @@
package cn.hutool.core.lang.func; package cn.hutool.core.lang.func;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt; import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.mutable.MutableEntry; import cn.hutool.core.lang.mutable.MutableEntry;
@ -51,8 +52,8 @@ public class LambdaFactory {
* @param paramTypes 方法参数数组 * @param paramTypes 方法参数数组
* @return 接受Lambda的函数式接口对象 * @return 接受Lambda的函数式接口对象
*/ */
public static <F> F buildLambda(Class<F> functionInterfaceType, Class methodClass, String methodName, Class... paramTypes) { public static <F> F build(Class<F> functionInterfaceType, Class methodClass, String methodName, Class... paramTypes) {
return buildLambda(functionInterfaceType, MethodUtil.getMethod(methodClass, methodName, paramTypes)); return build(functionInterfaceType, MethodUtil.getMethod(methodClass, methodName, paramTypes));
} }
/** /**
@ -62,7 +63,7 @@ public class LambdaFactory {
* @param method 方法对象 * @param method 方法对象
* @return 接受Lambda的函数式接口对象 * @return 接受Lambda的函数式接口对象
*/ */
public static <F> F buildLambda(Class<F> functionInterfaceType, Method method) { public static <F> F build(Class<F> functionInterfaceType, Method method) {
Assert.notNull(functionInterfaceType); Assert.notNull(functionInterfaceType);
Assert.notNull(method); Assert.notNull(method);
MutableEntry<Class<?>, Method> cacheKey = new MutableEntry<>(functionInterfaceType, method); MutableEntry<Class<?>, Method> cacheKey = new MutableEntry<>(functionInterfaceType, method);
@ -82,31 +83,29 @@ public class LambdaFactory {
MethodType samMethodType = methodType(invokeMethod.getReturnType(), invokeMethod.getParameterTypes()); MethodType samMethodType = methodType(invokeMethod.getReturnType(), invokeMethod.getParameterTypes());
MethodHandle implMethod = Opt.ofTry(() -> caller.unreflect(method)).get(); MethodHandle implMethod = Opt.ofTry(() -> caller.unreflect(method)).get();
MethodType insMethodType = methodType(method.getReturnType(), method.getDeclaringClass(), method.getParameterTypes()); MethodType insMethodType = methodType(method.getReturnType(), method.getDeclaringClass(), method.getParameterTypes());
boolean isSerializable = Arrays.stream(functionInterfaceType.getInterfaces()).anyMatch(i -> i.isAssignableFrom(Serializable.class)); boolean isSerializable = Serializable.class.isAssignableFrom(functionInterfaceType);
CallSite callSite = Opt.ofTry(() -> isSerializable ?
LambdaMetafactory.altMetafactory(
caller,
invokeName,
invokedType,
samMethodType,
implMethod,
insMethodType,
FLAG_SERIALIZABLE
) :
LambdaMetafactory.metafactory(
caller,
invokeName,
invokedType,
samMethodType,
implMethod,
insMethodType
)).get();
try { try {
//noinspection unchecked CallSite callSite = isSerializable ?
LambdaMetafactory.altMetafactory(
caller,
invokeName,
invokedType,
samMethodType,
implMethod,
insMethodType,
FLAG_SERIALIZABLE
) :
LambdaMetafactory.metafactory(
caller,
invokeName,
invokedType,
samMethodType,
implMethod,
insMethodType
);
return (F) callSite.getTarget().invoke(); return (F) callSite.getTarget().invoke();
} catch (Throwable e) { } catch (Throwable e) {
throw new RuntimeException(e); throw new UtilException(e);
} }
}); });

View File

@ -140,8 +140,8 @@ public class LambdaUtil {
* @param <R> getter方法返回值类型 * @param <R> getter方法返回值类型
* @return Obj::getXxx * @return Obj::getXxx
*/ */
public static <T, R> Function<T, R> getter(Method getMethod) { public static <T, R> Function<T, R> buildGetter(Method getMethod) {
return LambdaFactory.buildLambda(Function.class, getMethod); return LambdaFactory.build(Function.class, getMethod);
} }
/** /**
@ -153,8 +153,8 @@ public class LambdaUtil {
* @param <R> getter方法返回值类型 * @param <R> getter方法返回值类型
* @return Obj::getXxx * @return Obj::getXxx
*/ */
public static <T, R> Function<T, R> getter(Class<T> clazz, String fieldName) { public static <T, R> Function<T, R> buildGetter(Class<T> clazz, String fieldName) {
return LambdaFactory.buildLambda(Function.class, BeanUtil.getBeanDesc(clazz).getGetter(fieldName)); return LambdaFactory.build(Function.class, BeanUtil.getBeanDesc(clazz).getGetter(fieldName));
} }
/** /**
@ -165,8 +165,8 @@ public class LambdaUtil {
* @param <P> setter方法返回的值类型 * @param <P> setter方法返回的值类型
* @return Obj::setXxx * @return Obj::setXxx
*/ */
public static <T, P> BiConsumer<T, P> setter(Method setMethod) { public static <T, P> BiConsumer<T, P> buildSetter(Method setMethod) {
return LambdaFactory.buildLambda(BiConsumer.class, setMethod); return LambdaFactory.build(BiConsumer.class, setMethod);
} }
/** /**
@ -178,8 +178,8 @@ public class LambdaUtil {
* @param <P> setter方法返回的值类型 * @param <P> setter方法返回的值类型
* @return Obj::setXxx * @return Obj::setXxx
*/ */
public static <T, P> BiConsumer<T, P> setter(Class<T> clazz, String fieldName) { public static <T, P> BiConsumer<T, P> buildSetter(Class<T> clazz, String fieldName) {
return LambdaFactory.buildLambda(BiConsumer.class, BeanUtil.getBeanDesc(clazz).getSetter(fieldName)); return LambdaFactory.build(BiConsumer.class, BeanUtil.getBeanDesc(clazz).getSetter(fieldName));
} }
/** /**
@ -192,8 +192,8 @@ public class LambdaUtil {
* @param <F> 函数式接口类型 * @param <F> 函数式接口类型
* @return Obj::method * @return Obj::method
*/ */
public static <F> F lambda(Class<F> lambdaType, Class<?> clazz, String methodName, Class... paramsTypes) { public static <F> F build(Class<F> lambdaType, Class<?> clazz, String methodName, Class... paramsTypes) {
return LambdaFactory.buildLambda(lambdaType, clazz, methodName, paramsTypes); return LambdaFactory.build(lambdaType, clazz, methodName, paramsTypes);
} }
//region Private methods //region Private methods

View File

@ -1,6 +1,7 @@
package cn.hutool.core.lang.func; package cn.hutool.core.lang.func;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.reflect.MethodHandleUtil; import cn.hutool.core.reflect.MethodHandleUtil;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
@ -11,10 +12,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import java.lang.invoke.MethodHandle; import java.lang.invoke.*;
import java.lang.invoke.MethodHandleProxies;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -28,9 +26,14 @@ import java.util.function.Function;
*/ */
public class LambdaFactoryTest { public class LambdaFactoryTest {
@Test(expected = RuntimeException.class) // @Test
@Test
public void testMethodNotMatch() { public void testMethodNotMatch() {
LambdaFactory.buildLambda(Function.class, Something.class, "setId", Long.class); try {
LambdaFactory.build(Function.class, Something.class, "setId", Long.class);
} catch (Exception e) {
Assert.assertTrue(e.getCause() instanceof LambdaConversionException);
}
} }
@Test @Test
@ -39,14 +42,14 @@ public class LambdaFactoryTest {
something.setId(1L); something.setId(1L);
something.setName("name"); something.setName("name");
Function<Something, Long> get11 = LambdaFactory.buildLambda(Function.class, Something.class, "getId"); Function<Something, Long> get11 = LambdaFactory.build(Function.class, Something.class, "getId");
Function<Something, Long> get12 = LambdaFactory.buildLambda(Function.class, Something.class, "getId"); Function<Something, Long> get12 = LambdaFactory.build(Function.class, Something.class, "getId");
Assert.assertEquals(get11, get12); Assert.assertEquals(get11, get12);
Assert.assertEquals(something.getId(), get11.apply(something)); Assert.assertEquals(something.getId(), get11.apply(something));
String name = "sname"; String name = "sname";
BiConsumer<Something, String> set = LambdaFactory.buildLambda(BiConsumer.class, Something.class, "setName", String.class); BiConsumer<Something, String> set = LambdaFactory.build(BiConsumer.class, Something.class, "setName", String.class);
set.accept(something, name); set.accept(something, name);
Assert.assertEquals(something.getName(), name); Assert.assertEquals(something.getName(), name);
@ -139,7 +142,7 @@ public class LambdaFactoryTest {
Method getByReflect = Something.class.getMethod("getId"); Method getByReflect = Something.class.getMethod("getId");
MethodHandle getByMh = MethodHandleUtil.findMethod(Something.class, "getId", MethodType.methodType(Long.class)); MethodHandle getByMh = MethodHandleUtil.findMethod(Something.class, "getId", MethodType.methodType(Long.class));
Function getByProxy = MethodHandleProxies.asInterfaceInstance(Function.class, MethodHandles.lookup().unreflect(getByReflect)); Function getByProxy = MethodHandleProxies.asInterfaceInstance(Function.class, MethodHandles.lookup().unreflect(getByReflect));
Function getByLambda = LambdaFactory.buildLambda(Function.class, getByReflect); Function getByLambda = LambdaFactory.build(Function.class, getByReflect);
Task lambdaTask = new Task("lambda", () -> getByLambda.apply(something)); Task lambdaTask = new Task("lambda", () -> getByLambda.apply(something));
Task mhTask = new Task("mh", () -> getByMh.invoke(something)); Task mhTask = new Task("mh", () -> getByMh.invoke(something));
Task proxyTask = new Task("proxy", () -> getByProxy.apply(something)); Task proxyTask = new Task("proxy", () -> getByProxy.apply(something));
@ -214,7 +217,7 @@ public class LambdaFactoryTest {
Method setByReflect = Something.class.getMethod("setName", String.class); Method setByReflect = Something.class.getMethod("setName", String.class);
MethodHandle setByMh = MethodHandleUtil.findMethod(Something.class, "setName", MethodType.methodType(Void.TYPE, String.class)); MethodHandle setByMh = MethodHandleUtil.findMethod(Something.class, "setName", MethodType.methodType(Void.TYPE, String.class));
BiConsumer setByProxy = MethodHandleProxies.asInterfaceInstance(BiConsumer.class, setByMh); BiConsumer setByProxy = MethodHandleProxies.asInterfaceInstance(BiConsumer.class, setByMh);
BiConsumer setByLambda = LambdaFactory.buildLambda(BiConsumer.class, setByReflect); BiConsumer setByLambda = LambdaFactory.build(BiConsumer.class, setByReflect);
String name = "name1"; String name = "name1";
Task lambdaTask = new Task("lambda", () -> { Task lambdaTask = new Task("lambda", () -> {
setByLambda.accept(something, name); setByLambda.accept(something, name);

View File

@ -155,8 +155,8 @@ public class LambdaUtilTest {
Bean bean = new Bean(); Bean bean = new Bean();
bean.setId(2L); bean.setId(2L);
Function<Bean, Long> getId = cn.hutool.core.lang.func.LambdaUtil.getter(MethodUtil.getMethod(Bean.class, "getId")); Function<Bean, Long> getId = LambdaUtil.buildGetter(MethodUtil.getMethod(Bean.class, "getId"));
Function<Bean, Long> getId2 = cn.hutool.core.lang.func.LambdaUtil.getter(Bean.class, Bean.Fields.id); Function<Bean, Long> getId2 = LambdaUtil.buildGetter(Bean.class, Bean.Fields.id);
Assert.assertEquals(getId, getId2); Assert.assertEquals(getId, getId2);
Assert.assertEquals(bean.getId(), getId.apply(bean)); Assert.assertEquals(bean.getId(), getId.apply(bean));
@ -168,9 +168,9 @@ public class LambdaUtilTest {
bean.setId(2L); bean.setId(2L);
bean.setFlag(false); bean.setFlag(false);
BiConsumer<Bean, Long> setId = cn.hutool.core.lang.func.LambdaUtil.setter(MethodUtil.getMethod(Bean.class, "setId", Long.class)); BiConsumer<Bean, Long> setId = LambdaUtil.buildSetter(MethodUtil.getMethod(Bean.class, "setId", Long.class));
BiConsumer<Bean, Long> setId2 = cn.hutool.core.lang.func.LambdaUtil.setter(Bean.class, Bean.Fields.id); BiConsumer<Bean, Long> setId2 = LambdaUtil.buildSetter(Bean.class, Bean.Fields.id);
BiConsumer<Bean, Boolean> setFlag = cn.hutool.core.lang.func.LambdaUtil.setter(Bean.class, Bean.Fields.flag); BiConsumer<Bean, Boolean> setFlag = LambdaUtil.buildSetter(Bean.class, Bean.Fields.flag);
Assert.assertEquals(setId, setId2); Assert.assertEquals(setId, setId2);
setId.accept(bean, 3L); setId.accept(bean, 3L);
@ -185,8 +185,8 @@ public class LambdaUtilTest {
bean.setId(1L); bean.setId(1L);
bean.setPid(0L); bean.setPid(0L);
bean.setFlag(true); bean.setFlag(true);
BiFunction<Bean, String, Tuple> uniqueKeyFunction = LambdaUtil.lambda(BiFunction.class, Bean.class, "uniqueKey", String.class); BiFunction<Bean, String, Tuple> uniqueKeyFunction = LambdaUtil.build(BiFunction.class, Bean.class, "uniqueKey", String.class);
Function4<Tuple, Bean, String, Integer, Double> paramsFunction = LambdaUtil.lambda(Function4.class, Bean.class, "params", String.class, Integer.class, Double.class); Function4<Tuple, Bean, String, Integer, Double> paramsFunction = LambdaUtil.build(Function4.class, Bean.class, "params", String.class, Integer.class, Double.class);
Assert.assertEquals(bean.uniqueKey("test"), uniqueKeyFunction.apply(bean, "test")); Assert.assertEquals(bean.uniqueKey("test"), uniqueKeyFunction.apply(bean, "test"));
Assert.assertEquals(bean.params("test", 1, 0.5), paramsFunction.apply(bean, "test", 1, 0.5)); Assert.assertEquals(bean.params("test", 1, 0.5), paramsFunction.apply(bean, "test", 1, 0.5));
} }