File tree Expand file tree Collapse file tree 5 files changed +109
-0
lines changed
java/ql/test/query-tests/CallsToSystemExit Expand file tree Collapse file tree 5 files changed +109
-0
lines changed Original file line number Diff line number Diff line change 1+ | ExampleRuntimeExit.java:22:17:22:44 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
2+ | ExampleRuntimeExit.java:25:17:25:44 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
3+ | ExampleRuntimeExit.java:35:9:35:43 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
4+ | ExampleRuntimeHalt.java:18:17:18:44 | halt(...) | Avoid calls to Runtime.halt() as this makes code harder to reuse. |
5+ | ExampleRuntimeHalt.java:21:17:21:44 | halt(...) | Avoid calls to Runtime.halt() as this makes code harder to reuse. |
6+ | ExampleSystemExit.java:22:17:22:30 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
7+ | ExampleSystemExit.java:25:17:25:30 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
8+ | ExampleSystemExit.java:35:9:35:29 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
Original file line number Diff line number Diff line change 1+ query: Violations of Best Practice/Undesirable Calls/CallsToSystemExit.ql
2+ postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change 1+ import java .io .FileOutputStream ;
2+ import java .io .IOException ;
3+
4+ public class ExampleRuntimeExit {
5+
6+ public static void main (String [] args ) {
7+ Action action = new Action ();
8+ try {
9+ action .run ();
10+ } catch (Exception e ) {
11+ printUsageAndExit (e .getMessage (), 1 );
12+ }
13+ Runtime .getRuntime ().exit (0 ); // COMPLIANT
14+ }
15+
16+ static class Action {
17+ public void run () {
18+ try {
19+ FileOutputStream fos = new FileOutputStream ("output.txt" );
20+ fos .write ("Hello, World!" .getBytes ());
21+ fos .close ();
22+ Runtime .getRuntime ().exit (0 ); // $ Alert
23+ } catch (IOException e ) {
24+ e .printStackTrace ();
25+ Runtime .getRuntime ().exit (1 ); // $ Alert
26+ } catch (Exception e ) {
27+ // re-throw the exception
28+ throw e ;
29+ }
30+ }
31+ }
32+
33+ protected static void printUsageAndExit (final String message , final int exitCode ) {
34+ System .err .println ("Usage: <example_cmd> <example_args> : " + message );
35+ Runtime .getRuntime ().exit (exitCode ); // $ SPURIOUS: Alert
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ import java .io .FileOutputStream ;
2+ import java .io .IOException ;
3+
4+ public class ExampleRuntimeHalt {
5+
6+ public static void main (String [] args ) {
7+ Action action = new Action ();
8+ action .run ();
9+ Runtime .getRuntime ().halt (0 ); // COMPLIANT
10+ }
11+
12+ static class Action {
13+ public void run () {
14+ try {
15+ FileOutputStream fos = new FileOutputStream ("output.txt" );
16+ fos .write ("Hello, World!" .getBytes ());
17+ fos .close ();
18+ Runtime .getRuntime ().halt (0 ); // $ Alert
19+ } catch (IOException e ) {
20+ e .printStackTrace ();
21+ Runtime .getRuntime ().halt (1 ); // $ Alert
22+ }
23+ }
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ import java .io .FileOutputStream ;
2+ import java .io .IOException ;
3+
4+ public class ExampleSystemExit {
5+
6+ public static void main (String [] args ) {
7+ Action action = new Action ();
8+ try {
9+ action .run ();
10+ } catch (Exception e ) {
11+ printUsageAndExit (e .getMessage (), 1 );
12+ }
13+ System .exit (0 ); // COMPLIANT
14+ }
15+
16+ static class Action {
17+ public void run () {
18+ try {
19+ FileOutputStream fos = new FileOutputStream ("output.txt" );
20+ fos .write ("Hello, World!" .getBytes ());
21+ fos .close ();
22+ System .exit (0 ); // $ Alert
23+ } catch (IOException e ) {
24+ e .printStackTrace ();
25+ System .exit (1 ); // $ Alert
26+ } catch (Exception e ) {
27+ // re-throw the exception
28+ throw e ;
29+ }
30+ }
31+ }
32+
33+ protected static void printUsageAndExit (final String message , final int exitCode ) {
34+ System .err .println ("Usage: <example_cmd> <example_args> : " + message );
35+ System .exit (exitCode ); // $ SPURIOUS: Alert
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments