Merge pull request #20190 from Napalys/java/jvm-exit-query-promotion

Java: Enhance `java/jvm-exit` query and add to quality
This commit is contained in:
Napalys Klicius
2025-08-27 13:23:02 +02:00
committed by GitHub
11 changed files with 246 additions and 33 deletions

View File

@@ -0,0 +1,6 @@
| ExampleRuntimeExit.java:22:17:22:44 | exit(...) | Avoid calls to Runtime.exit() as this prevents runtime cleanup and makes code harder to reuse. |
| ExampleRuntimeExit.java:25:17:25:44 | exit(...) | Avoid calls to Runtime.exit() as this prevents runtime cleanup and makes code harder to reuse. |
| ExampleRuntimeHalt.java:18:17:18:44 | halt(...) | Avoid calls to Runtime.halt() as this prevents runtime cleanup and makes code harder to reuse. |
| ExampleRuntimeHalt.java:21:17:21:44 | halt(...) | Avoid calls to Runtime.halt() as this prevents runtime cleanup and makes code harder to reuse. |
| ExampleSystemExit.java:22:17:22:30 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
| ExampleSystemExit.java:25:17:25:30 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |

View File

@@ -0,0 +1,2 @@
query: Violations of Best Practice/Undesirable Calls/CallsToSystemExit.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql

View File

@@ -0,0 +1,37 @@
import java.io.FileOutputStream;
import java.io.IOException;
public class ExampleRuntimeExit {
public static void main(String[] args) {
Action action = new Action();
try {
action.run();
} catch (Exception e) {
printUsageAndExit(e.getMessage(), 1);
}
Runtime.getRuntime().exit(0); // COMPLIANT
}
static class Action {
public void run() {
try {
FileOutputStream fos = new FileOutputStream("output.txt");
fos.write("Hello, World!".getBytes());
fos.close();
Runtime.getRuntime().exit(0); // $ Alert
} catch (IOException e) {
e.printStackTrace();
Runtime.getRuntime().exit(1); // $ Alert
} catch (Exception e) {
// re-throw the exception
throw e;
}
}
}
protected static void printUsageAndExit(final String message, final int exitCode) {
System.err.println("Usage: <example_cmd> <example_args> : " + message);
Runtime.getRuntime().exit(exitCode); // COMPLIANT
}
}

View File

@@ -0,0 +1,25 @@
import java.io.FileOutputStream;
import java.io.IOException;
public class ExampleRuntimeHalt {
public static void main(String[] args) {
Action action = new Action();
action.run();
Runtime.getRuntime().halt(0); // COMPLIANT
}
static class Action {
public void run() {
try {
FileOutputStream fos = new FileOutputStream("output.txt");
fos.write("Hello, World!".getBytes());
fos.close();
Runtime.getRuntime().halt(0); // $ Alert
} catch (IOException e) {
e.printStackTrace();
Runtime.getRuntime().halt(1); // $ Alert
}
}
}
}

View File

@@ -0,0 +1,37 @@
import java.io.FileOutputStream;
import java.io.IOException;
public class ExampleSystemExit {
public static void main(String[] args) {
Action action = new Action();
try {
action.run();
} catch (Exception e) {
printUsageAndExit(e.getMessage(), 1);
}
System.exit(0); // COMPLIANT
}
static class Action {
public void run() {
try {
FileOutputStream fos = new FileOutputStream("output.txt");
fos.write("Hello, World!".getBytes());
fos.close();
System.exit(0); // $ Alert
} catch (IOException e) {
e.printStackTrace();
System.exit(1); // $ Alert
} catch (Exception e) {
// re-throw the exception
throw e;
}
}
}
protected static void printUsageAndExit(final String message, final int exitCode) {
System.err.println("Usage: <example_cmd> <example_args> : " + message);
System.exit(exitCode); // COMPLIANT
}
}

View File

@@ -0,0 +1,26 @@
public class LocalClassInTestMethod {
public void testNestedCase() {
class OuterLocalClass {
void func() {
class NestedLocalClass {
void nestedMethod() {
System.exit(4);
Runtime.getRuntime().halt(5);
}
}
}
}
OuterLocalClass outer = new OuterLocalClass();
outer.func();
}
public void testNestedCase2() {
class OuterLocalClass {
class NestedLocalClass {
void nestedMethod() {
System.exit(4);
Runtime.getRuntime().halt(5);
}
}
}
}
}