我正在尝试为私有字段的setter获取lambda方法句柄,但由于某种原因,找不到setter。
这是我正在使用的功能界面:
@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);
答案 0 :(得分:8)
您的代码中存在一些错误。
首先,您使用的是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