[英]Java 8: Reflection Field.get() try-catch error
I am receiving an error in Eclipse stating that I have an unhandled exception with my call to Field.get(Object)
. 我在Eclipse中收到一条错误消息,指出我对
Field.get(Object)
调用Field.get(Object)
未处理的异常。 When I wrap the code in the recommended try-catch, the error persists. 当我将代码包装在推荐的try-catch中时,错误仍然存在。 I assume that I have to wrap the parameter, in the
get()
method, with a try-catch. 我假设我必须用try-catch将参数包装在
get()
方法中。 Is this possible? 这可能吗?
Unhandled exception type IllegalAccessException
Line in question: 有问题的行:
return list.stream().map(e -> fieldType.cast(f.get(e))).collect(Collectors.toList());
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Entity> entities = Arrays.asList(
new Item(0, "Apple"),
new Item(1, "Banana"),
new Item(2, "Grape"),
new Item(3, "Orange")
);
List<Long> ids = null;
try {
ids = pluck("id", Long.class, entities, Entity.class, true);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(ids);
}
public static <E extends Entity> List<Long> pluckIds(List<E> list) {
return list.stream().map(e -> e.getId()).collect(Collectors.toList());
}
public static <T,F> List<F> pluck(String fieldName, Class<F> fieldType,
List<T> list, Class<T> listType, boolean useLambda)
throws NoSuchFieldException, IllegalAccessException,
IllegalArgumentException {
Field f = listType.getDeclaredField(fieldName);
f.setAccessible(true);
if (useLambda) {
return list.stream().map(e -> fieldType.cast(f.get(e))).collect(Collectors.toList());
} else {
List<F> result = new ArrayList<F>();
for (T element : list) {
result.add(fieldType.cast(f.get(element)));
}
return result;
}
}
}
public class Item extends Entity {
private String name;
public Item(long id, String name) {
super(id);
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public abstract class Entity {
private long id;
public Entity(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Eclipse is kind of stupid right now (there's probably a bug open). Eclipse现在有点愚蠢(可能有一个打开的错误)。
You have a lambda expression in here 你在这里有一个lambda表达式
return list.stream().map(e -> fieldType.cast(f.get(e))).collect(Collectors.toList());
Field#get(Object)
is declared as throwing a checked exception. Field#get(Object)
被声明为引发已检查的异常。 But Function#apply(Object)
, which is the functional interface method you're implementing, does not. 但是
Function#apply(Object)
并不是您要实现的功能接口方法。 Eclipse is stupid in that it recommends that you wrap the statement in a try block, but it adds the try block around the whole thing Eclipse是愚蠢的,因为它建议您将语句包装在try块中,但它会在整个过程中添加try块
try {
return list.stream().map(e -> fieldType.cast(f.get(e))).collect(Collectors.toList());
} catch (Exception e) {
}
when in reality it should be adding it to the body of the lambda 实际上,应该将其添加到lambda的主体中
return list.stream().map(e -> {
try {
return fieldType.cast(f.get(e));
} catch (Exception e) {
return /* something else */ null; // or throw an unchecked exception
}
}).collect(Collectors.toList());
This seems to happen when your lambda body is a simple expression rather than a block. 当您的lambda主体是一个简单的表达式而不是一个块时,似乎会发生这种情况。
You need to handle IllegalAccessException
which can be thrown as a result of fieldType.cast(f.get(e))
. 您需要处理
IllegalAccessException
,它可能是fieldType.cast(f.get(e))
。 You can surround the code within try/catch
您可以将代码包含在
try/catch
if (useLambda) {
return list.stream().map(e -> {
try {
return fieldType.cast(f.get(e));
} catch (IllegalAccessException e1) {
e1.printStackTrace();
return null;
}
}).collect(Collectors.toList());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.