gpt4 book ai didi

Java8 从类中检索 lambda setter

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:14 24 4
gpt4 key购买 nike

我正在尝试为私有(private)字段的 setter 获取 lambda 方法句柄,但由于某种原因,无法找到 setter。

这是我正在使用的功能接口(interface):

@FunctionalInterface
public interface ISetter<T, R> {
void set(T object, R value);
}

这是用来获取 setter 的方法:

public ISetter getSetter(Class clazz, String fieldName, Class fieldType) throws Throwable {

MethodHandles.Lookup caller = MethodHandles.lookup();
MethodType setter = MethodType.methodType(Void.class, fieldType);
MethodHandle target = caller.findVirtual(clazz, computeSetterName(fieldName), setter);
MethodType func = target.type();

CallSite site = LambdaMetafactory.metafactory(
caller,
"set",
MethodType.methodType(ISetter.class),
func.generic(),
target,
func
);

MethodHandle factory = site.getTarget();
ISetter r = (ISetter) factory.invoke();

return r;
}

按照惯例,所有 setter 的名称都相同:“setField(...)”。这是我的测试类:

public class TestEntity {

private Long id;


public TestEntity(Long id) {
this.id = id;
}


public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}

但是当我为“id”和“Long.class”执行这个方法时,我得到了以下异常:

Exception in thread "main" java.lang.NoSuchMethodException: no such method: de.cyclonit.exercise.TestEntity.setId(Long)Void/invokeVirtual
at java.lang.invoke.MemberName.makeAccessException(MemberName.java:871)
at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1003)
at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1381)
at java.lang.invoke.MethodHandles$Lookup.findVirtual(MethodHandles.java:859)
at de.cyclonit.exercise.AccessorFactory.getSetter(AccessorFactory.java:41)
at de.cyclonit.exercise.Main.main(Main.java:21)
Caused by: java.lang.NoSuchFieldError: method resolution failed
at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:975)
at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1000)
... 4 more

我不明白这个错误是从哪里来的。 “void setId(Long)”方法确实存在。


public String computeSetterName(String fieldName) {
return "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
}

调用:

ISetter setter = accessorFactory.getSetter(TestEntity.class, "id", Long.class);

最佳答案

您的代码中有一些错误。

首先,您正在使用 Void.class,您应该改用 void.class

其次,func.generic返回(Object,Object)Object,真正要的是func.erase,所以返回类型被保留 ((Object,Object)void)。

第三,建议不要使用原始类型,我建议改为使用以下实现:

public static <T, R> ISetter<T, R> getSetter(Class<T> clazz, String fieldName,
Class<R> fieldType) throws Throwable {

MethodHandles.Lookup caller = MethodHandles.lookup();
MethodType setter = MethodType.methodType(void.class, fieldType);
MethodHandle target = caller.findVirtual(clazz, computeSetterName(fieldName), setter);
MethodType func = target.type();

CallSite site = LambdaMetafactory.metafactory(
caller,
"set",
MethodType.methodType(ISetter.class),
func.erase(),
target,
func
);

MethodHandle factory = site.getTarget();
ISetter<T, R> r = (ISetter<T, R>) factory.invoke();

return r;
}

哪个对我有用:http://ideone.com/KKx10r

关于Java8 从类中检索 lambda setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43967504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com