Skip to content

Commit

Permalink
Adapt new behavior of System.console() since JDK22
Browse files Browse the repository at this point in the history
  • Loading branch information
Eng-Fouad authored and stalep committed May 30, 2024
1 parent d171b39 commit 79cd22d
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.aesh.terminal.utils.OSUtils;
import org.aesh.readline.util.LoggerUtil;

import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -143,8 +145,8 @@ else if (OSUtils.IS_WINDOWS) {

private Terminal createWindowsTerminal(String name) throws IOException {
try {
//if console != null its a native terminal, not redirects etc
if(System.console() != null)
Console console = System.console();
if (console != null && isTerminal(console)) // a native terminal, not redirects etc
return new WinSysTerminal(name, nativeSignals);
else {
return new WinExternalTerminal(name, type, (in == null) ? System.in : in,
Expand All @@ -156,4 +158,19 @@ private Terminal createWindowsTerminal(String name) throws IOException {
(out == null) ? System.out : out);
}
}

// Console.isTerminal() was introduced in Java 22
private static boolean isTerminal(Console console) {
try {
Method isTerminal = Console.class.getMethod("isTerminal");
try {
return (boolean) isTerminal.invoke(console);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to invoke System.console().isTerminal() via Reflection API", e);
return false;
}
} catch (NoSuchMethodException e) {
return true; // for Java <= 21
}
}
}

0 comments on commit 79cd22d

Please sign in to comment.