First, I should say that I am able to get the generic type of instances or anonymous inner classes.
Here is the code example, look for the catch block:
public static interface MyInterface<E extends Throwable>{
void call() throws E;
}
public static <E extends Throwable> void method(MyInterface<E> lambda) throws E {
try {
lambda.call();
} catch(Throwable ex) {
// Pseudo code
Class<?> T = ((ParameterizedType)ex.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
if ( T.isInstance(ex) ) {
throw ex;
} else {
...
}
}
}
public static void throwsException() throws Exception {
throw new Exception();
}
This doesn't work:
method(() -> {
throwsException();
});
But this does:
method(new MyInterface<Exception>() {
@Override
public void call() throws Exception {
throwsException();
}
});
But with the introduction of Lambdas, i can no longer enforce this!
It should also be noted that this now potentially breaks backwards compatibility with Libraries older than < 8, and that reflected out this information.
I have researched this topic, and there only seems to be possible workarounds for getting the method parameters, but this is regarding the throws part so that won't work.
Note: I have seen this thread already: Reflection type inference on Java 8 Lambdas