Skip to content

ExitException when calling System.exit(0) from Method#invoke #115

@pcworld

Description

@pcworld

Try to run the following class e.g. on final2.subtests.ValidCommandLineArgumentsTest#orderArgumentTest:

package reflectiontest;

import java.lang.reflect.InvocationTargetException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        try {
            getClass().getMethod("test").invoke(this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        // if using the following instead of invoke, everything works fine:
        // test();
    }

    public void test() {
        System.exit(0);
    }

}

leads to the following exception (stacktrace printed on System.out):

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at reflectiontest.Test.<init>(Test.java:13)
    at reflectiontest.Test.main(Test.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at test.TestObject.run(TestObject.java:651)
    at test.TestObject.runStaticVoid(TestObject.java:468)
    at test.TestObject.runStaticVoid(TestObject.java:481)
    at test.InteractiveConsoleTest.noOutputTest(InteractiveConsoleTest.java:450)
    at test.InteractiveConsoleTest.noOutputTest(InteractiveConsoleTest.java:433)
    at final2.subtests.ValidCommandLineArgumentsTest.orderArgumentTest(ValidCommandLineArgumentsTest.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)
Caused by: test.framework.ExitException: The method called System.exit(0)
    at test.framework.NoExitSecurityManager.checkExit(NoExitSecurityManager.java:33)
    at java.lang.Runtime.exit(Runtime.java:107)
    at java.lang.System.exit(System.java:962)
    at reflectiontest.Test.test(Test.java:30)
    ... 26 more
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at reflectiontest.Test.<init>(Test.java:13)
    at reflectiontest.Test.main(Test.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at test.TestObject.run(TestObject.java:651)
    at test.TestObject.runStaticVoid(TestObject.java:468)
    at test.TestObject.runStaticVoid(TestObject.java:481)
    at test.InteractiveConsoleTest.noOutputTest(InteractiveConsoleTest.java:450)
    at test.InteractiveConsoleTest.noOutputTest(InteractiveConsoleTest.java:433)
    at final2.subtests.ValidCommandLineArgumentsTest.orderArgumentTest(ValidCommandLineArgumentsTest.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)
Caused by: test.framework.ExitException: The method called System.exit(0)
    at test.framework.NoExitSecurityManager.checkExit(NoExitSecurityManager.java:33)
    at java.lang.Runtime.exit(Runtime.java:107)
    at java.lang.System.exit(System.java:962)
    at reflectiontest.Test.test(Test.java:30)
    ... 26 more

even though ValidCommandLineArgumentsTest calls setAllowedSystemExitStatus(SystemExitStatus.WITH_0) in its constructor.
When calling System.exit(0) directly without Method#invoke, the test succeeds without exceptions (as expected).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions