勤劳的家庭,饥饿过其门而不入。——富兰克林
分享一个通过getter
获取setter
函数
放在:https://gitee.com/dromara/stream-query
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public static <T, R> SerBiCons<T, R> getSetter(SerFunc<T, R> getter) { return getSetter(getter, SerBiCons.class); }
public static <F extends Serializable, C> C getSetter(F getter, Class<? super C> lambdaType) { LambdaExecutable executable = LambdaHelper.resolve(getter); Object setter = getSetter(executable.getClazz(), BeanHelper.getPropertyName(executable.getName()), lambdaType); return SerFunc.<Object, C>cast().apply(setter); }
|
此处使用方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Test void testGetSetter() { SerBiCons<LambdaExecutable, String> nameSetter = LambdaHelper.getSetter(LambdaExecutable.class, "name"); LambdaExecutable executable = new LambdaExecutable(); nameSetter.accept(executable, "kubernetes"); Assertions.assertEquals("kubernetes", executable.getName()); val lambda = LambdaHelper.getSetter(LambdaExecutable.class, "clazz", BiConsumer.class); BiConsumer<LambdaExecutable, Class<?>> clazzSetter = SerFunc.<BiConsumer<?, ?>, BiConsumer<LambdaExecutable, Class<?>>>cast().apply(lambda); clazzSetter.accept(executable, String.class); Assertions.assertEquals(String.class, executable.getClazz()); clazzSetter = LambdaHelper.getSetter(LambdaExecutable::getClazz); clazzSetter.accept(executable, Object.class); Assertions.assertEquals(Object.class, executable.getClazz()); SerBiCons<LambdaExecutable, String> nameSerSetter = LambdaHelper .<SerFunc<LambdaExecutable, String>, SerBiCons<LambdaExecutable, String>> getSetter(LambdaExecutable::getName, SerBiCons.class); nameSerSetter.accept(executable, "serializable"); Assertions.assertEquals("serializable", executable.getName()); }
|