Debian Codegraph @

native/jpype_module/src/main/java/org/jpype/JPypeSignal.java

raw symbols SCIP
1 /* ****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 **************************************************************************** */
16 package org.jpype;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.Proxy;
21
22 /**
23 * Java wants to make this action nearly impossible.
24 *
25 * Thus the have warnings against it that cannot be disabled. So we will skin
26 * this cat another way.
27 */
28 public class JPypeSignal
29 {
30
31 static Thread main;
32
33 static Object getSignalHandler(Class signalHandlerClazz, int signal) throws ClassNotFoundException
34 {
35 return Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]
36 {
37 signalHandlerClazz
38 }, (proxy, method, args) ->
39 {
40 main.interrupt();
41 interruptPy(signal);
42 return null;
43 });
44 }
45
46 static void installHandlers()
47 {
48 try
49 {
50 Class Signal = Class.forName("sun.misc.Signal");
51 Class SignalHandler = Class.forName("sun.misc.SignalHandler");
52 main = Thread.currentThread();
53 Method method = Signal.getMethod("handle", Signal, SignalHandler);
54 Object intr = Signal.getDeclaredConstructor(String.class).newInstance("INT");
55 method.invoke(null, intr, getSignalHandler(SignalHandler, 2));
56 Object intrTerm = Signal.getDeclaredConstructor(String.class).newInstance("TERM");
57 method.invoke(null, intrTerm, getSignalHandler(SignalHandler, 15));
58 } catch (InvocationTargetException | IllegalArgumentException | IllegalAccessException | InstantiationException | ClassNotFoundException | NoSuchMethodException | SecurityException ex)
59 {
60 // If we don't get the signal handler run without it. (ANDROID)
61 }
62 }
63
64 native static void interruptPy(int signal);
65
66 native static void acknowledgePy();
67 }