Search in sources :

Example 1 with CbCallback

use of com.sun.jna.CallbacksTest.TestLibrary.CbCallback 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);
    }
}
Also used : CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) UncaughtExceptionHandler(com.sun.jna.Callback.UncaughtExceptionHandler)

Example 2 with CbCallback

use of com.sun.jna.CallbacksTest.TestLibrary.CbCallback in project jna by java-native-access.

the class CallbacksTest method testCallCallbackWithCallbackArgumentAndResult.

public void testCallCallbackWithCallbackArgumentAndResult() {
    TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {

        @Override
        public CbCallback callback(CbCallback arg) {
            return arg;
        }
    };
    TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
    assertEquals("Callback reference should be reused", cb, cb2);
}
Also used : CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback)

Example 3 with CbCallback

use of com.sun.jna.CallbacksTest.TestLibrary.CbCallback 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);
    }
}
Also used : CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) UncaughtExceptionHandler(com.sun.jna.Callback.UncaughtExceptionHandler)

Example 4 with CbCallback

use of com.sun.jna.CallbacksTest.TestLibrary.CbCallback in project jna by java-native-access.

the class CallbacksTest method testDefaultCallbackExceptionHandler.

public void testDefaultCallbackExceptionHandler() {
    final RuntimeException ERROR = new RuntimeException(getName());
    PrintStream ps = System.err;
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    System.setErr(new PrintStream(s));
    try {
        TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {

            @Override
            public CbCallback callback(CbCallback arg) {
                throw ERROR;
            }
        };
        TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
        String output = s.toString();
        assertTrue("Default handler not called", output.length() > 0);
    } finally {
        System.setErr(ps);
    }
}
Also used : PrintStream(java.io.PrintStream) CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

CbCallback (com.sun.jna.CallbacksTest.TestLibrary.CbCallback)4 UncaughtExceptionHandler (com.sun.jna.Callback.UncaughtExceptionHandler)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1