google / google/error-prone

SystemConsoleNull should consider the case where `System.console() == null`

Open
#4,355 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

https://errorprone.info/bugpattern/SystemConsoleNull says:

> System.console() no longer returns null in JDK 22 and newer versions

However, it may still return `null`, both from the [specification](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/System.html#console()) and [actual implementation](https://github.com/openjdk/jdk/blob/20cb6e786fbf6d924c509e28d6fded86d61a5f84/src/java.base/share/classes/java/io/Console.java#L431).

So I propose to:

(a) rephrase the above sentence to

> System.console() returns null in fewer cases in JDK 22 and newer versions

(b) update the code example to handle the case where `System.console() == null`:

```java
@SuppressWarnings("SystemConsoleNull") // https://errorprone.info/bugpattern/SystemConsoleNull
private static boolean systemConsoleIsTerminal() {
Console systemConsole = System.console();
if (Runtime.version().feature() < 22) {
return systemConsole != null;
}
try {
return (systemConsole != null) && (Boolean) Console.class.getMethod("isTerminal").invoke(systemConsole);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.