I am using Eclipse Luna Service Release 2 (4.4.2), Java 8 u51.
I'm trying to create a method which will create instances of a passed object based on another method parameter. The prototype is simplified to
public <T> T test(Object param, T instance) {
Constructor<?> constructor = instance.getClass().getConstructors()[0]; // I actually choose a proper constructor
// eclipse reports "Unhandled exception type InvocationTargetException"
Function<Object, Object> createFun = constructor::newInstance;
T result = (T) createFun.apply(param);
return result;
}
On line with Function
declaration eclipse reports Unhandled exception type InvocationTargetException
compiler error. I need the Function
to later use in a stream.
I tried to add various try/catch blocks, throws declarations, but nothing fixes this compiler error.
How to make this code work?