这是Java8从类中检索lambdasetter的后续。
我正在尝试为给定字段获取一个 getter 方法
public <T, R> IGetter<T, R> getGetter(Class<T> clazz, Field field) {
Class<R> fieldType = null;
try {
fieldType = (Class<R>) field.getType();
} catch(ClassCastException e) {
error("Attempted to create a mistyped getter for the field " + field + "!");
}
return getGetter(clazz, field.getName(), fieldType);
}
这是基础方法:
public <T, R> IGetter<T, R> getGetter(Class<T> clazz, String fieldName, Class<R> fieldType) {
MethodHandles.Lookup caller = null;
MethodHandle target = null;
MethodType func = null;
try {
caller = MethodHandles.lookup();
MethodType getter = MethodType.methodType(fieldType);
target = caller.findVirtual(clazz, computeGetterName(fieldName), getter);
func = target.type();
} catch (NoSuchMethodException e) {
error("Could not locate a properly named getter "" + computeGetterName(fieldName) + ""!");
} catch (IllegalAccessException e) {
error("Could not access "" + computeGetterName(fieldName) + ""!");
}
CallSite site = null;
try {
site = LambdaMetafactory.metafactory(
caller,
"get",
MethodType.methodType(IGetter.class),
func.generic(),
target,
func
);
} catch (LambdaConversionException e) {
error("Could not convert the getter "" + computeGetterName(fieldName) + "" into a lambda expression!");
}
MethodHandle factory = site.getTarget();
IGetter<T, R> r = null;
try {
r = (IGetter<T, R>) factory.invoke();
} catch (Throwable throwable) {
error("Casting the factory of "" + computeGetterName(fieldName) + "" failed!");
}
return r;
}
由于类型不匹配,这不会编译:
IGetter<TestEntity, Long> getter = accessorFactory.getGetter(TestEntity.class, "name", String.class);
但是,这确实可以编译:
Field field = TestEntity.class.getDeclaredField("name");
IGetter<TestEntity, Long> getter = accessorFactory.getGetter(TestEntity.class, field);
而且,令我惊讶的是,这确实可以使用上面检索到的 getter:
TestEntity testEntity = new TestEntity(1L, "Test");
System.out.println(getter.get(testEntity));
但是,一旦我这样做:
Long value = getter.get(testEntity);
我得到以下异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at de.cyclonit.exercise.Main.main(Main.java:26)
有没有办法早点抓住这个?
TestEntity 类:
public class TestEntity {
private Long id;
private String name;
public TestEntity(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
}
问题是你的方法
public <T, R> IGetter<T, R> getGetter(Class<T> clazz, Field field) {
Class<R> fieldType = null;
try {
fieldType = (Class<R>) field.getType();
} catch(ClassCastException e) {
error("Attempted to create a mistyped getter for the field " + field + "!");
}
return getGetter(clazz, field.getName(), fieldType);
}
实际上并未执行检查。您基本上是在将Class
实例转换为Class
类型,这没有任何效果。将泛型类型Class<?>
更改为Class<R>
是纯粹的编译时问题,这就是为什么此时应该收到"未选中"警告的原因,至少,如果启用了所有警告。
在运行时进行真正检查的唯一方法是从调用方获取预期的类型:
public <T, R> IGetter<T, R> getGetter(Class<T> clazz, Class<R> fieldType, Field field) {
if(fieldType != field.getType()) {
error("Attempted to create a mistyped getter for the field " + field + "!");
}
return getGetter(clazz, field.getName(), fieldType);
}
当然,这使得接受Field
的方法有点毫无意义。实际getGetter
已经执行了需要与 getter 的返回类型完全匹配的查找,并且要求字段的类型和 getter 的返回类型必须匹配,您将没有任何好处。实际上,它对内部细节产生了不必要的依赖。
为什么不简单地注释getter而不是字段...