blob: 86bc880eaaa3a5f8099703d91efe708872afbf97 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package cbt.reflect;
import java.security.*;
import java.lang.reflect.InvocationTargetException;
public abstract class TrapSystemExit<T> {
public static SecurityManager createSecurityManager(SecurityManager delegateTo) {
return new TrapSecurityManager(delegateTo);
}
public static <T> T run(TrapSystemExit<T> runnable) throws Throwable {
boolean trapExitCodeBefore = TrapSecurityManager.trapExitCode().get();
try {
TrapSecurityManager.trapExitCode().set(true);
return runnable.run();
} catch (InvocationTargetException exception) {
Throwable cause = exception.getCause();
if (TrapSecurityManager.isTrappedExit(cause)) {
return runnable.wrap(TrapSecurityManager.exitCode(cause));
}
throw exception;
} catch (Exception exception) {
if (TrapSecurityManager.isTrappedExit(exception)) {
return runnable.wrap(TrapSecurityManager.exitCode(exception));
}
throw exception;
} finally {
TrapSecurityManager.trapExitCode().set(trapExitCodeBefore);
}
}
public abstract T run() throws Throwable;
public abstract T wrap(int exitCode);
}
|