From 235ccec802395d22f50703600166cd22e75da1e8 Mon Sep 17 00:00:00 2001 From: "Doroszlai, Attila" Date: Mon, 29 Jul 2024 19:50:18 +0200 Subject: [PATCH 1/6] let signal() return default handler, too --- src/main/java/jnr/posix/util/SunMiscSignal.java | 7 +++++++ src/test/java/jnr/posix/SignalTest.java | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/jnr/posix/util/SunMiscSignal.java b/src/main/java/jnr/posix/util/SunMiscSignal.java index 93de45e9..97680485 100644 --- a/src/main/java/jnr/posix/util/SunMiscSignal.java +++ b/src/main/java/jnr/posix/util/SunMiscSignal.java @@ -11,6 +11,13 @@ public static SignalHandler signal(jnr.constants.platform.Signal sig, final Sign if (oldHandler instanceof SunMiscSignalHandler) { return ((SunMiscSignalHandler)oldHandler).handler; + } else if (oldHandler instanceof sun.misc.SignalHandler) { + return new SignalHandler() { + @Override + public void handle(int signal) { + oldHandler.handle(s); + } + }; } else { return null; } diff --git a/src/test/java/jnr/posix/SignalTest.java b/src/test/java/jnr/posix/SignalTest.java index 7077cdda..01a85735 100644 --- a/src/test/java/jnr/posix/SignalTest.java +++ b/src/test/java/jnr/posix/SignalTest.java @@ -51,12 +51,14 @@ public void testJavaSignal() { if (!Platform.IS_WINDOWS) { Signal s = Signal.SIGHUP; final AtomicBoolean fired = new AtomicBoolean(false); - javaPosix.signal(s, new SignalHandler() { + SignalHandler previousHandler = javaPosix.signal(s, new SignalHandler() { public void handle(int signal) { fired.set(true); } }); + Assert.assertNotNull(previousHandler); + // have to use native here; no abstraction for kill in pure Java // TODO: sun.misc.Signal.raise can be used to kill current pid posix.kill(posix.getpid(), s.intValue()); From 0f79c9ff044a4e3b0ea5091b0729f8d4affbc4e2 Mon Sep 17 00:00:00 2001 From: "Doroszlai, Attila" Date: Tue, 30 Jul 2024 10:11:22 +0200 Subject: [PATCH 2/6] replace instanceof with null check --- src/main/java/jnr/posix/util/SunMiscSignal.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/jnr/posix/util/SunMiscSignal.java b/src/main/java/jnr/posix/util/SunMiscSignal.java index 97680485..164a52ec 100644 --- a/src/main/java/jnr/posix/util/SunMiscSignal.java +++ b/src/main/java/jnr/posix/util/SunMiscSignal.java @@ -11,7 +11,7 @@ public static SignalHandler signal(jnr.constants.platform.Signal sig, final Sign if (oldHandler instanceof SunMiscSignalHandler) { return ((SunMiscSignalHandler)oldHandler).handler; - } else if (oldHandler instanceof sun.misc.SignalHandler) { + } else if (oldHandler != null) { return new SignalHandler() { @Override public void handle(int signal) { From 8e70fa03eed59e06a2258f5a933977f98055ab35 Mon Sep 17 00:00:00 2001 From: "Doroszlai, Attila" Date: Tue, 30 Jul 2024 10:12:14 +0200 Subject: [PATCH 3/6] replace with lambda --- src/main/java/jnr/posix/util/SunMiscSignal.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/jnr/posix/util/SunMiscSignal.java b/src/main/java/jnr/posix/util/SunMiscSignal.java index 164a52ec..e29c65c7 100644 --- a/src/main/java/jnr/posix/util/SunMiscSignal.java +++ b/src/main/java/jnr/posix/util/SunMiscSignal.java @@ -12,12 +12,7 @@ public static SignalHandler signal(jnr.constants.platform.Signal sig, final Sign if (oldHandler instanceof SunMiscSignalHandler) { return ((SunMiscSignalHandler)oldHandler).handler; } else if (oldHandler != null) { - return new SignalHandler() { - @Override - public void handle(int signal) { - oldHandler.handle(s); - } - }; + return any -> oldHandler.handle(s); } else { return null; } From 4b420d36cc628d98fd8edd999bfcc95cdec36469 Mon Sep 17 00:00:00 2001 From: "Doroszlai, Attila" Date: Tue, 30 Jul 2024 10:13:42 +0200 Subject: [PATCH 4/6] add comments --- src/main/java/jnr/posix/util/SunMiscSignal.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/jnr/posix/util/SunMiscSignal.java b/src/main/java/jnr/posix/util/SunMiscSignal.java index e29c65c7..4c550d92 100644 --- a/src/main/java/jnr/posix/util/SunMiscSignal.java +++ b/src/main/java/jnr/posix/util/SunMiscSignal.java @@ -10,8 +10,10 @@ public static SignalHandler signal(jnr.constants.platform.Signal sig, final Sign sun.misc.SignalHandler oldHandler = Signal.handle(s, new SunMiscSignalHandler(handler)); if (oldHandler instanceof SunMiscSignalHandler) { + // previous custom handler return ((SunMiscSignalHandler)oldHandler).handler; } else if (oldHandler != null) { + // default handler return any -> oldHandler.handle(s); } else { return null; From 90870bdb2c72ea09918252093cc9985b224288c5 Mon Sep 17 00:00:00 2001 From: "Doroszlai, Attila" Date: Tue, 30 Jul 2024 10:15:32 +0200 Subject: [PATCH 5/6] Revert "add comments" This reverts commit 4b420d36cc628d98fd8edd999bfcc95cdec36469. --- src/main/java/jnr/posix/util/SunMiscSignal.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/jnr/posix/util/SunMiscSignal.java b/src/main/java/jnr/posix/util/SunMiscSignal.java index 4c550d92..e29c65c7 100644 --- a/src/main/java/jnr/posix/util/SunMiscSignal.java +++ b/src/main/java/jnr/posix/util/SunMiscSignal.java @@ -10,10 +10,8 @@ public static SignalHandler signal(jnr.constants.platform.Signal sig, final Sign sun.misc.SignalHandler oldHandler = Signal.handle(s, new SunMiscSignalHandler(handler)); if (oldHandler instanceof SunMiscSignalHandler) { - // previous custom handler return ((SunMiscSignalHandler)oldHandler).handler; } else if (oldHandler != null) { - // default handler return any -> oldHandler.handle(s); } else { return null; From cfd6f102dcd6fb3b91ab1ed0c61852bdacf24677 Mon Sep 17 00:00:00 2001 From: "Doroszlai, Attila" Date: Tue, 30 Jul 2024 10:15:32 +0200 Subject: [PATCH 6/6] Revert "replace with lambda" This reverts commit 8e70fa03eed59e06a2258f5a933977f98055ab35. --- src/main/java/jnr/posix/util/SunMiscSignal.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/jnr/posix/util/SunMiscSignal.java b/src/main/java/jnr/posix/util/SunMiscSignal.java index e29c65c7..164a52ec 100644 --- a/src/main/java/jnr/posix/util/SunMiscSignal.java +++ b/src/main/java/jnr/posix/util/SunMiscSignal.java @@ -12,7 +12,12 @@ public static SignalHandler signal(jnr.constants.platform.Signal sig, final Sign if (oldHandler instanceof SunMiscSignalHandler) { return ((SunMiscSignalHandler)oldHandler).handler; } else if (oldHandler != null) { - return any -> oldHandler.handle(s); + return new SignalHandler() { + @Override + public void handle(int signal) { + oldHandler.handle(s); + } + }; } else { return null; }