use of com.sun.jna.Callback.UncaughtExceptionHandler in project jna by java-native-access.
the class CallbacksTest method testCallbackExceptionHandler.
// Most Callbacks are wrapped in DefaultCallbackProxy, which catches their
// exceptions.
public void testCallbackExceptionHandler() {
final RuntimeException ERROR = new RuntimeException(getName());
final Throwable[] CAUGHT = { null };
final Callback[] CALLBACK = { null };
UncaughtExceptionHandler old = Native.getCallbackExceptionHandler();
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Callback cb, Throwable e) {
CALLBACK[0] = cb;
CAUGHT[0] = e;
}
};
Native.setCallbackExceptionHandler(handler);
try {
TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {
@Override
public CbCallback callback(CbCallback arg) {
throw ERROR;
}
};
TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
assertNotNull("Exception handler not called", CALLBACK[0]);
assertEquals("Wrong callback argument to handler", cb, CALLBACK[0]);
assertEquals("Wrong exception passed to handler", ERROR, CAUGHT[0]);
} finally {
Native.setCallbackExceptionHandler(old);
}
}
use of com.sun.jna.Callback.UncaughtExceptionHandler in project jna by java-native-access.
the class CallbacksTest method testCallbackExceptionHandlerWithCallbackProxy.
// CallbackProxy is called directly from native.
public void testCallbackExceptionHandlerWithCallbackProxy() throws Throwable {
final RuntimeException ERROR = new RuntimeException(getName());
final Throwable[] CAUGHT = { null };
final Callback[] CALLBACK = { null };
UncaughtExceptionHandler old = Native.getCallbackExceptionHandler();
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Callback cb, Throwable e) {
CALLBACK[0] = cb;
CAUGHT[0] = e;
}
};
Native.setCallbackExceptionHandler(handler);
try {
class TestProxy implements CallbackProxy, TestLibrary.CbCallback {
@Override
public CbCallback callback(CbCallback arg) {
throw new Error("Should never be called");
}
@Override
public Object callback(Object[] args) {
throw ERROR;
}
@Override
public Class<?>[] getParameterTypes() {
return new Class[] { CbCallback.class };
}
@Override
public Class<?> getReturnType() {
return CbCallback.class;
}
}
;
TestLibrary.CbCallback cb = new TestProxy();
TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
assertNotNull("Exception handler not called", CALLBACK[0]);
assertEquals("Wrong callback argument to handler", cb, CALLBACK[0]);
assertEquals("Wrong exception passed to handler", ERROR, CAUGHT[0]);
} finally {
Native.setCallbackExceptionHandler(old);
}
}
Aggregations