Skip to content

Commit 32c7681

Browse files
author
Alan Bateman
committed
8332029: Provide access to initial value of stderr via JavaLangAccess
Reviewed-by: jpai, bpb, mcimadamore
1 parent f9a1d33 commit 32c7681

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/java.base/share/classes/java/lang/System.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ private System() {
191191
*/
192192
public static final PrintStream err = null;
193193

194-
// Holder for the initial value of `in`, set within `initPhase1()`.
195-
private static InputStream initialIn;
194+
// Initial values of System.in and System.err, set in initPhase1().
195+
private static @Stable InputStream initialIn;
196+
private static @Stable PrintStream initialErr;
196197

197198
// indicates if a security manager is possible
198199
private static final int NEVER = 1;
@@ -355,9 +356,6 @@ private static class CallersHolder {
355356
= Collections.synchronizedMap(new WeakHashMap<>());
356357
}
357358

358-
// Remember initial System.err. setSecurityManager() warning goes here
359-
private static volatile @Stable PrintStream initialErrStream;
360-
361359
private static URL codeSource(Class<?> clazz) {
362360
PrivilegedAction<ProtectionDomain> pa = clazz::getProtectionDomain;
363361
@SuppressWarnings("removal")
@@ -417,7 +415,7 @@ public static void setSecurityManager(@SuppressWarnings("removal") SecurityManag
417415
} else {
418416
source = callerClass.getName() + " (" + url + ")";
419417
}
420-
initialErrStream.printf("""
418+
initialErr.printf("""
421419
WARNING: A terminally deprecated method in java.lang.System has been called
422420
WARNING: System::setSecurityManager has been called by %s
423421
WARNING: Please consider reporting this to the maintainers of %s
@@ -2200,7 +2198,8 @@ private static void initPhase1() {
22002198
// thus they are equivalent to Console.charset(), otherwise the encodings
22012199
// of those properties default to native.encoding
22022200
setOut0(newPrintStream(fdOut, props.getProperty("stdout.encoding")));
2203-
setErr0(newPrintStream(fdErr, props.getProperty("stderr.encoding")));
2201+
initialErr = newPrintStream(fdErr, props.getProperty("stderr.encoding"));
2202+
setErr0(initialErr);
22042203

22052204
// Setup Java signal handlers for HUP, TERM, and INT (where available).
22062205
Terminator.setup();
@@ -2406,8 +2405,6 @@ private static void initPhase3() {
24062405
notSupportedJnuEncoding);
24072406
}
24082407

2409-
initialErrStream = System.err;
2410-
24112408
// initializing the system class loader
24122409
VM.initLevel(3);
24132410

@@ -2598,6 +2595,10 @@ public InputStream initialSystemIn() {
25982595
return initialIn;
25992596
}
26002597

2598+
public PrintStream initialSystemErr() {
2599+
return initialErr;
2600+
}
2601+
26012602
public void setCause(Throwable t, Throwable cause) {
26022603
t.setCause(cause);
26032604
}

src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package jdk.internal.access;
2727

2828
import java.io.InputStream;
29+
import java.io.PrintStream;
2930
import java.lang.annotation.Annotation;
3031
import java.lang.foreign.MemorySegment;
3132
import java.lang.invoke.MethodHandle;
@@ -401,6 +402,11 @@ public interface JavaLangAccess {
401402
*/
402403
InputStream initialSystemIn();
403404

405+
/**
406+
* Returns the initial value of System.err.
407+
*/
408+
PrintStream initialSystemErr();
409+
404410
/**
405411
* Encodes ASCII codepoints as possible from the source array into
406412
* the destination byte array, assuming that the encoding is ASCII

src/java.base/share/classes/jdk/internal/misc/VM.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2024, 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
@@ -27,6 +27,7 @@
2727

2828
import static java.lang.Thread.State.*;
2929

30+
import java.io.PrintStream;
3031
import java.util.ArrayList;
3132
import java.util.Collections;
3233
import java.util.List;
@@ -496,4 +497,11 @@ private static class BufferPoolsHolder {
496497
public static List<BufferPool> getBufferPools() {
497498
return BufferPoolsHolder.BUFFER_POOLS;
498499
}
500+
501+
/**
502+
* Return the initial value of System.err that was set during VM initialization.
503+
*/
504+
public static PrintStream initialErr() {
505+
return SharedSecrets.getJavaLangAccess().initialSystemErr();
506+
}
499507
}

0 commit comments

Comments
 (0)