Skip to content
This repository was archived by the owner on Apr 24, 2023. It is now read-only.

Commit d49851a

Browse files
committed
8299689: Make use of JLine for Console as "opt-in"
Reviewed-by: jpai, alanb
1 parent 1f141bd commit d49851a

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

src/java.base/share/classes/java/io/Console.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -608,7 +608,13 @@ public Console console() {
608608
@SuppressWarnings("removal")
609609
private static Console instantiateConsole(boolean istty) {
610610
try {
611-
// Try loading providers
611+
/*
612+
* The JdkConsole provider used for Console instantiation can be specified
613+
* with the system property "jdk.console", whose value designates the module
614+
* name of the implementation, and which defaults to "java.base". If no
615+
* providers are available, or instantiation failed, java.base built-in
616+
* Console implementation is used.
617+
*/
612618
PrivilegedAction<Console> pa = () -> {
613619
var consModName = System.getProperty("jdk.console",
614620
JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);

src/java.base/share/classes/jdk/internal/io/JdkConsoleProvider.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,18 +28,12 @@
2828

2929
/**
3030
* Service provider interface for JdkConsole implementations.
31-
* The provider used for instantiating JdkConsole instance can be
32-
* specified with the system property "jdk.console", whose value
33-
* designates the module name of the implementation, and which defaults
34-
* to "jdk.internal.le" (jline). If no providers is available,
35-
* or instantiation failed, java.base built-in Console implementation
36-
* is used.
3731
*/
3832
public interface JdkConsoleProvider {
3933
/**
4034
* The module name of the JdkConsole default provider.
4135
*/
42-
String DEFAULT_PROVIDER_MODULE_NAME = "jdk.internal.le";
36+
String DEFAULT_PROVIDER_MODULE_NAME = "java.base";
4337

4438
/**
4539
* {@return the Console instance, or {@code null} if not available}

test/jdk/java/io/Console/ModuleSelectionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,11 +23,11 @@
2323

2424
/**
2525
* @test
26-
* @bug 8295803
26+
* @bug 8295803 8299689
2727
* @summary Tests System.console() returns correct Console (or null) from the expected
2828
* module.
2929
* @modules java.base/java.io:+open
30-
* @run main/othervm ModuleSelectionTest jdk.internal.le
30+
* @run main/othervm ModuleSelectionTest java.base
3131
* @run main/othervm -Djdk.console=jdk.internal.le ModuleSelectionTest jdk.internal.le
3232
* @run main/othervm -Djdk.console=java.base ModuleSelectionTest java.base
3333
* @run main/othervm --limit-modules java.base ModuleSelectionTest java.base

test/jdk/java/io/Console/RedirectTest.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,33 +28,50 @@
2828

2929
/**
3030
* @test
31-
* @bug 8295803
31+
* @bug 8295803 8299689
3232
* @summary Tests System.console() works with standard input redirection.
3333
* @library /test/lib
34+
* @run main RedirectTest
35+
* @run main/othervm -Djdk.console=jdk.internal.le RedirectTest
3436
*/
3537
public class RedirectTest {
38+
private static final String SYSPROP = "jdk.console";
39+
3640
public static void main(String... args) throws Throwable {
3741
if (args.length == 0) {
3842
// no arg will launch the child process that actually perform tests
39-
var pb = ProcessTools.createTestJvm("RedirectTest", "dummy");
43+
var pb = ProcessTools.createTestJvm(
44+
"-D" + SYSPROP + "=" + System.getProperty(SYSPROP, ""),
45+
"RedirectTest", "dummy");
4046
var input = new File(System.getProperty("test.src", "."), "input.txt");
4147
pb.redirectInput(input);
4248
var oa = ProcessTools.executeProcess(pb);
43-
var output = oa.asLines();
44-
var expected = Files.readAllLines(input.toPath());
45-
if (!output.equals(expected)) {
46-
throw new RuntimeException("""
49+
if (oa.getExitValue() == 1) {
50+
System.out.println("System.console() returns null. Ignoring the test.");
51+
} else {
52+
var output = oa.asLines();
53+
var expected = Files.readAllLines(input.toPath());
54+
if (!output.equals(expected)) {
55+
throw new RuntimeException("""
4756
Standard out had unexpected strings:
4857
Actual output: %s
4958
Expected output: %s
5059
""".formatted(output, expected));
60+
} else {
61+
oa.shouldHaveExitValue(0);
62+
System.out.println("Redirect succeeded.");
63+
}
5164
}
52-
oa.shouldHaveExitValue(0);
5365
} else {
5466
var con = System.console();
55-
String line;
56-
while ((line = con.readLine()) != null) {
57-
System.out.println(line);
67+
if (con != null) {
68+
String line;
69+
while ((line = con.readLine()) != null) {
70+
System.out.println(line);
71+
}
72+
} else {
73+
// Exit with 1
74+
System.exit(1);
5875
}
5976
}
6077
}

0 commit comments

Comments
 (0)