-
Notifications
You must be signed in to change notification settings - Fork 668
/
System.java
2346 lines (2134 loc) · 104 KB
/
System.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang;
import jdk.internal.HotSpotIntrinsicCandidate;
import jdk.internal.logger.LazyLoggers;
import jdk.internal.logger.LocalizedLoggerWrapper;
import jdk.internal.logger.LoggerFinderLoader;
import jdk.internal.misc.JavaLangAccess;
import jdk.internal.misc.SharedSecrets;
import jdk.internal.misc.VM;
import jdk.internal.module.ModuleBootstrap;
import jdk.internal.module.ServicesCatalog;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import jdk.internal.util.StaticProperty;
import sun.nio.ch.Interruptible;
import sun.reflect.annotation.AnnotationType;
import sun.security.util.SecurityConstants;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Console;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.module.ModuleDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.nio.channels.Channel;
import java.nio.channels.spi.SelectorProvider;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.PropertyPermission;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* The {@code System} class contains several useful class fields
* and methods. It cannot be instantiated.
*
* Among the facilities provided by the {@code System} class
* are standard input, standard output, and error output streams;
* access to externally defined properties and environment
* variables; a means of loading files and libraries; and a utility
* method for quickly copying a portion of an array.
*
* @since 1.0
*/
// 系统工具类
public final class System {
/** @see #initPhase2() */
static ModuleLayer bootLayer;
/** The security manager for the system. */
// 当前使用的安全管理器,默认为null
private static volatile SecurityManager security;
// 当前系统关联的控制台,在IDE中通常为null
private static volatile Console cons;
/**
* System properties. The following properties are guaranteed to be defined:
* <dl>
* <dt>java.version <dd>Java version number
* <dt>java.version.date <dd>Java version date
* <dt>java.vendor <dd>Java vendor specific string
* <dt>java.vendor.url <dd>Java vendor URL
* <dt>java.vendor.version <dd>Java vendor version
* <dt>java.home <dd>Java installation directory
* <dt>java.class.version <dd>Java class version number
* <dt>java.class.path <dd>Java classpath
* <dt>os.name <dd>Operating System Name
* <dt>os.arch <dd>Operating System Architecture
* <dt>os.version <dd>Operating System Version
* <dt>file.separator <dd>File separator ("/" on Unix)
* <dt>path.separator <dd>Path separator (":" on Unix)
* <dt>line.separator <dd>Line separator ("\n" on Unix)
* <dt>user.name <dd>User account name
* <dt>user.home <dd>User home directory
* <dt>user.dir <dd>User's current working directory
* </dl>
*/
/*
* 环境变量属性集,可通过-D运行参数向其添加自定义条目。
* 该属性集是删减过的,完整的初始加载的环境变量保存在VM的字段savedProps中。
*/
private static Properties props;
// 行分隔符,在windows上是'\r\n'
private static String lineSeparator;
/**
* The "standard" input stream. This stream is already
* open and ready to supply input data. Typically this stream
* corresponds to keyboard input or another input source specified by
* the host environment or user.
*/
public static final InputStream in = null; // 标准输入流,会关联到某个默认的输入设备
/**
* The "standard" output stream. This stream is already
* open and ready to accept output data. Typically this stream
* corresponds to display output or another output destination
* specified by the host environment or user.
* <p>
* For simple stand-alone Java applications, a typical way to write
* a line of output data is:
* <blockquote><pre>
* System.out.println(data)
* </pre></blockquote>
* <p>
* See the {@code println} methods in class {@code PrintStream}.
*
* @see java.io.PrintStream#println()
* @see java.io.PrintStream#println(boolean)
* @see java.io.PrintStream#println(char)
* @see java.io.PrintStream#println(char[])
* @see java.io.PrintStream#println(double)
* @see java.io.PrintStream#println(float)
* @see java.io.PrintStream#println(int)
* @see java.io.PrintStream#println(long)
* @see java.io.PrintStream#println(java.lang.Object)
* @see java.io.PrintStream#println(java.lang.String)
*/
public static final PrintStream out = null; // 标准输出流,会关联到某个默认的输出设备
/**
* The "standard" error output stream. This stream is already
* open and ready to accept output data.
* <p>
* Typically this stream corresponds to display output or another
* output destination specified by the host environment or user. By
* convention, this output stream is used to display error messages
* or other information that should come to the immediate attention
* of a user even if the principal output stream, the value of the
* variable {@code out}, has been redirected to a file or other
* destination that is typically not continuously monitored.
*/
public static final PrintStream err = null; // 标准错误流,会关联到某个默认的输出设备
static {
registerNatives();
}
/** Don't let anyone instantiate this class */
private System() {
}
/*▼ 标准流 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Reassigns the "standard" input stream.
*
* First, if there is a security manager, its {@code checkPermission}
* method is called with a {@code RuntimePermission("setIO")} permission
* to see if it's ok to reassign the "standard" input stream.
*
* @param in the new standard input stream.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPermission} method doesn't allow
* reassigning of the standard input stream.
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
* @since 1.1
*/
// 重定向标准输入流:使得"标准输入流"变为in,即从in中读取数据
public static void setIn(InputStream in) {
checkIO();
setIn0(in);
}
/**
* Reassigns the "standard" output stream.
*
* First, if there is a security manager, its {@code checkPermission}
* method is called with a {@code RuntimePermission("setIO")} permission
* to see if it's ok to reassign the "standard" output stream.
*
* @param out the new standard output stream
*
* @throws SecurityException if a security manager exists and its
* {@code checkPermission} method doesn't allow
* reassigning of the standard output stream.
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
* @since 1.1
*/
// 重定向标准输出流:使得"标准输出流"变为out,即向out中写入数据
public static void setOut(PrintStream out) {
checkIO();
setOut0(out);
}
/**
* Reassigns the "standard" error output stream.
*
* First, if there is a security manager, its {@code checkPermission}
* method is called with a {@code RuntimePermission("setIO")} permission
* to see if it's ok to reassign the "standard" error output stream.
*
* @param err the new standard error output stream.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPermission} method doesn't allow
* reassigning of the standard error output stream.
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
* @since 1.1
*/
// 重定向标准错误流:使得"标准错误流"变为err,即向err中写入数据
public static void setErr(PrintStream err) {
checkIO();
setErr0(err);
}
// 为字段System.in关联(初始化)标准输入流
private static native void setIn0(InputStream in);
// 为字段System.out关联(初始化)标准输出流
private static native void setOut0(PrintStream out);
// 为字段System.err关联(初始化)标准错误流
private static native void setErr0(PrintStream err);
/*▲ 标准流 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 复制 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Copies an array from the specified source array, beginning at the
* specified position, to the specified position of the destination array.
* A subsequence of array components are copied from the source
* array referenced by {@code src} to the destination array
* referenced by {@code dest}. The number of components copied is
* equal to the {@code length} argument. The components at
* positions {@code srcPos} through
* {@code srcPos+length-1} in the source array are copied into
* positions {@code destPos} through
* {@code destPos+length-1}, respectively, of the destination
* array.
* <p>
* If the {@code src} and {@code dest} arguments refer to the
* same array object, then the copying is performed as if the
* components at positions {@code srcPos} through
* {@code srcPos+length-1} were first copied to a temporary
* array with {@code length} components and then the contents of
* the temporary array were copied into positions
* {@code destPos} through {@code destPos+length-1} of the
* destination array.
* <p>
* If {@code dest} is {@code null}, then a
* {@code NullPointerException} is thrown.
* <p>
* If {@code src} is {@code null}, then a
* {@code NullPointerException} is thrown and the destination
* array is not modified.
* <p>
* Otherwise, if any of the following is true, an
* {@code ArrayStoreException} is thrown and the destination is
* not modified:
* <ul>
* <li>The {@code src} argument refers to an object that is not an
* array.
* <li>The {@code dest} argument refers to an object that is not an
* array.
* <li>The {@code src} argument and {@code dest} argument refer
* to arrays whose component types are different primitive types.
* <li>The {@code src} argument refers to an array with a primitive
* component type and the {@code dest} argument refers to an array
* with a reference component type.
* <li>The {@code src} argument refers to an array with a reference
* component type and the {@code dest} argument refers to an array
* with a primitive component type.
* </ul>
* <p>
* Otherwise, if any of the following is true, an
* {@code IndexOutOfBoundsException} is
* thrown and the destination is not modified:
* <ul>
* <li>The {@code srcPos} argument is negative.
* <li>The {@code destPos} argument is negative.
* <li>The {@code length} argument is negative.
* <li>{@code srcPos+length} is greater than
* {@code src.length}, the length of the source array.
* <li>{@code destPos+length} is greater than
* {@code dest.length}, the length of the destination array.
* </ul>
* <p>
* Otherwise, if any actual component of the source array from
* position {@code srcPos} through
* {@code srcPos+length-1} cannot be converted to the component
* type of the destination array by assignment conversion, an
* {@code ArrayStoreException} is thrown. In this case, let
* <b><i>k</i></b> be the smallest nonnegative integer less than
* length such that {@code src[srcPos+}<i>k</i>{@code ]}
* cannot be converted to the component type of the destination
* array; when the exception is thrown, source array components from
* positions {@code srcPos} through
* {@code srcPos+}<i>k</i>{@code -1}
* will already have been copied to destination array positions
* {@code destPos} through
* {@code destPos+}<i>k</I>{@code -1} and no other
* positions of the destination array will have been modified.
* (Because of the restrictions already itemized, this
* paragraph effectively applies only to the situation where both
* arrays have component types that are reference types.)
*
* @param src the source array.
* @param srcPos starting position in the source array.
* @param dest the destination array.
* @param destPos starting position in the destination data.
* @param length the number of array elements to be copied.
*
* @throws IndexOutOfBoundsException if copying would cause
* access of data outside array bounds.
* @throws ArrayStoreException if an element in the {@code src}
* array could not be stored into the {@code dest} array
* because of a type mismatch.
* @throws NullPointerException if either {@code src} or
* {@code dest} is {@code null}.
*/
// 数组复制,从src的srcPos索引处复制length个元素放入dest的destPos索引处
@HotSpotIntrinsicCandidate
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
/*▲ 复制 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 系统属性 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Sets the system property indicated by the specified key.
*
* First, if a security manager exists, its
* {@code SecurityManager.checkPermission} method
* is called with a {@code PropertyPermission(key, "write")}
* permission. This may result in a SecurityException being thrown.
* If no exception is thrown, the specified property is set to the given
* value.
*
* @param key the name of the system property.
* @param value the value of the system property.
*
* @return the previous value of the system property,
* or {@code null} if it did not have one.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPermission} method doesn't allow
* setting of the specified property.
* @throws NullPointerException if {@code key} or
* {@code value} is {@code null}.
* @throws IllegalArgumentException if {@code key} is empty.
* @apiNote <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
* @see #getProperty
* @see java.lang.System#getProperty(java.lang.String)
* @see java.lang.System#getProperty(java.lang.String, java.lang.String)
* @see java.util.PropertyPermission
* @see SecurityManager#checkPermission
* @since 1.2
*/
// 添加一条系统属性:键值对<key, value>
public static String setProperty(String key, String value) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if(sm != null) {
sm.checkPermission(new PropertyPermission(key, SecurityConstants.PROPERTY_WRITE_ACTION));
}
return (String) props.setProperty(key, value);
}
/**
* Removes the system property indicated by the specified key.
*
* First, if a security manager exists, its
* {@code SecurityManager.checkPermission} method
* is called with a {@code PropertyPermission(key, "write")}
* permission. This may result in a SecurityException being thrown.
* If no exception is thrown, the specified property is removed.
*
* @param key the name of the system property to be removed.
*
* @return the previous string value of the system property,
* or {@code null} if there was no property with that key.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPropertyAccess} method doesn't allow
* access to the specified system property.
* @throws NullPointerException if {@code key} is {@code null}.
* @throws IllegalArgumentException if {@code key} is empty.
* @apiNote <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} method for details.
* @see #getProperty
* @see #setProperty
* @see java.util.Properties
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkPropertiesAccess()
* @since 1.5
*/
// 移除指定key对应的系统属性
public static String clearProperty(String key) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if(sm != null) {
sm.checkPermission(new PropertyPermission(key, "write"));
}
return (String) props.remove(key);
}
/**
* Gets the system property indicated by the specified key.
*
* First, if there is a security manager, its
* {@code checkPropertyAccess} method is called with the key as
* its argument. This may result in a SecurityException.
* <p>
* If there is no current set of system properties, a set of system
* properties is first created and initialized in the same manner as
* for the {@code getProperties} method.
*
* @param key the name of the system property.
*
* @return the string value of the system property,
* or {@code null} if there is no property with that key.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPropertyAccess} method doesn't allow
* access to the specified system property.
* @throws NullPointerException if {@code key} is {@code null}.
* @throws IllegalArgumentException if {@code key} is empty.
* @apiNote <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
* @see #setProperty
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
* @see java.lang.System#getProperties()
*/
// 返回指定key对应的系统属性的值。如果key不存在,返回null
public static String getProperty(String key) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if(sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key);
}
/**
* Gets the system property indicated by the specified key.
*
* First, if there is a security manager, its
* {@code checkPropertyAccess} method is called with the
* {@code key} as its argument.
* <p>
* If there is no current set of system properties, a set of system
* properties is first created and initialized in the same manner as
* for the {@code getProperties} method.
*
* @param key the name of the system property.
* @param def a default value.
*
* @return the string value of the system property,
* or the default value if there is no property with that key.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPropertyAccess} method doesn't allow
* access to the specified system property.
* @throws NullPointerException if {@code key} is {@code null}.
* @throws IllegalArgumentException if {@code key} is empty.
* @see #setProperty
* @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
* @see java.lang.System#getProperties()
*/
// 返回指定key对应的系统属性的值。如果key不存在,返回默认值def
public static String getProperty(String key, String def) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if(sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key, def);
}
/**
* Sets the system properties to the {@code Properties} argument.
*
* First, if there is a security manager, its
* {@code checkPropertiesAccess} method is called with no
* arguments. This may result in a security exception.
* <p>
* The argument becomes the current set of system properties for use
* by the {@link #getProperty(String)} method. If the argument is
* {@code null}, then the current set of system properties is
* forgotten.
*
* @param props the new system properties.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPropertiesAccess} method doesn't allow access
* to the system properties.
* @apiNote <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
* @see #getProperties
* @see java.util.Properties
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkPropertiesAccess()
*/
// 设置系统属性集(会整体替换掉上次设置的属性值,初始时默认为系统属性集)
public static void setProperties(Properties props) {
SecurityManager sm = getSecurityManager();
if(sm != null) {
sm.checkPropertiesAccess();
}
if(props == null) {
props = new Properties();
// 加载环境变量以填充props
initProperties(props);
}
System.props = props;
}
/**
* Determines the current system properties.
*
* First, if there is a security manager, its
* {@code checkPropertiesAccess} method is called with no
* arguments. This may result in a security exception.
* <p>
* The current set of system properties for use by the
* {@link #getProperty(String)} method is returned as a
* {@code Properties} object. If there is no current set of
* system properties, a set of system properties is first created and
* initialized. This set of system properties always includes values
* for the following keys:
* <table class="striped" style="text-align:left">
* <caption style="display:none">Shows property keys and associated values</caption>
* <thead>
* <tr><th scope="col">Key</th>
* <th scope="col">Description of Associated Value</th></tr>
* </thead>
* <tbody>
* <tr><th scope="row">{@code java.version}</th>
* <td>Java Runtime Environment version, which may be interpreted
* as a {@link Runtime.Version}</td></tr>
* <tr><th scope="row">{@code java.version.date}</th>
* <td>Java Runtime Environment version date, in ISO-8601 YYYY-MM-DD
* format, which may be interpreted as a {@link
* java.time.LocalDate}</td></tr>
* <tr><th scope="row">{@code java.vendor}</th>
* <td>Java Runtime Environment vendor</td></tr>
* <tr><th scope="row">{@code java.vendor.url}</th>
* <td>Java vendor URL</td></tr>
* <tr><th scope="row">{@code java.vendor.version}</th>
* <td>Java vendor version</td></tr>
* <tr><th scope="row">{@code java.home}</th>
* <td>Java installation directory</td></tr>
* <tr><th scope="row">{@code java.vm.specification.version}</th>
* <td>Java Virtual Machine specification version, whose value is the
* {@linkplain Runtime.Version#feature feature} element of the
* {@linkplain Runtime#version() runtime version}</td></tr>
* <tr><th scope="row">{@code java.vm.specification.vendor}</th>
* <td>Java Virtual Machine specification vendor</td></tr>
* <tr><th scope="row">{@code java.vm.specification.name}</th>
* <td>Java Virtual Machine specification name</td></tr>
* <tr><th scope="row">{@code java.vm.version}</th>
* <td>Java Virtual Machine implementation version which may be
* interpreted as a {@link Runtime.Version}</td></tr>
* <tr><th scope="row">{@code java.vm.vendor}</th>
* <td>Java Virtual Machine implementation vendor</td></tr>
* <tr><th scope="row">{@code java.vm.name}</th>
* <td>Java Virtual Machine implementation name</td></tr>
* <tr><th scope="row">{@code java.specification.version}</th>
* <td>Java Runtime Environment specification version, whose value is
* the {@linkplain Runtime.Version#feature feature} element of the
* {@linkplain Runtime#version() runtime version}</td></tr>
* <tr><th scope="row">{@code java.specification.vendor}</th>
* <td>Java Runtime Environment specification vendor</td></tr>
* <tr><th scope="row">{@code java.specification.name}</th>
* <td>Java Runtime Environment specification name</td></tr>
* <tr><th scope="row">{@code java.class.version}</th>
* <td>Java class format version number</td></tr>
* <tr><th scope="row">{@code java.class.path}</th>
* <td>Java class path (refer to
* {@link ClassLoader#getSystemClassLoader()} for details)</td></tr>
* <tr><th scope="row">{@code java.library.path}</th>
* <td>List of paths to search when loading libraries</td></tr>
* <tr><th scope="row">{@code java.io.tmpdir}</th>
* <td>Default temp file path</td></tr>
* <tr><th scope="row">{@code java.compiler}</th>
* <td>Name of JIT compiler to use</td></tr>
* <tr><th scope="row">{@code os.name}</th>
* <td>Operating system name</td></tr>
* <tr><th scope="row">{@code os.arch}</th>
* <td>Operating system architecture</td></tr>
* <tr><th scope="row">{@code os.version}</th>
* <td>Operating system version</td></tr>
* <tr><th scope="row">{@code file.separator}</th>
* <td>File separator ("/" on UNIX)</td></tr>
* <tr><th scope="row">{@code path.separator}</th>
* <td>Path separator (":" on UNIX)</td></tr>
* <tr><th scope="row">{@code line.separator}</th>
* <td>Line separator ("\n" on UNIX)</td></tr>
* <tr><th scope="row">{@code user.name}</th>
* <td>User's account name</td></tr>
* <tr><th scope="row">{@code user.home}</th>
* <td>User's home directory</td></tr>
* <tr><th scope="row">{@code user.dir}</th>
* <td>User's current working directory</td></tr>
* </tbody>
* </table>
* <p>
* Multiple paths in a system property value are separated by the path
* separator character of the platform.
* <p>
* Note that even if the security manager does not permit the
* {@code getProperties} operation, it may choose to permit the
* {@link #getProperty(String)} operation.
*
* @return the system properties
*
* @throws SecurityException if a security manager exists and its
* {@code checkPropertiesAccess} method doesn't allow access
* to the system properties.
* @apiNote <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified.</strong>
* Property values may be cached during initialization or on first use.
* Setting a standard property after initialization using {@link #getProperties()},
* {@link #setProperties(Properties)}, {@link #setProperty(String, String)}, or
* {@link #clearProperty(String)} may not have the desired effect.
* @implNote In addition to the standard system properties, the system
* properties may include the following keys:
* <table class="striped">
* <caption style="display:none">Shows property keys and associated values</caption>
* <thead>
* <tr><th scope="col">Key</th>
* <th scope="col">Description of Associated Value</th></tr>
* </thead>
* <tbody>
* <tr><th scope="row">{@code jdk.module.path}</th>
* <td>The application module path</td></tr>
* <tr><th scope="row">{@code jdk.module.upgrade.path}</th>
* <td>The upgrade module path</td></tr>
* <tr><th scope="row">{@code jdk.module.main}</th>
* <td>The module name of the initial/main module</td></tr>
* <tr><th scope="row">{@code jdk.module.main.class}</th>
* <td>The main class name of the initial module</td></tr>
* </tbody>
* </table>
* @see #setProperties
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkPropertiesAccess()
* @see java.util.Properties
*/
// 获取系统属性集
public static Properties getProperties() {
SecurityManager sm = getSecurityManager();
if(sm != null) {
sm.checkPropertiesAccess();
}
return props;
}
/*▲ 系统属性 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 环境变量 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns an unmodifiable string map view of the current system environment.
* The environment is a system-dependent mapping from names to
* values which is passed from parent to child processes.
*
* <p>If the system does not support environment variables, an
* empty map is returned.
*
* <p>The returned map will never contain null keys or values.
* Attempting to query the presence of a null key or value will
* throw a {@link NullPointerException}. Attempting to query
* the presence of a key or value which is not of type
* {@link String} will throw a {@link ClassCastException}.
*
* <p>The returned map and its collection views may not obey the
* general contract of the {@link Object#equals} and
* {@link Object#hashCode} methods.
*
* <p>The returned map is typically case-sensitive on all platforms.
*
* <p>If a security manager exists, its
* {@link SecurityManager#checkPermission checkPermission}
* method is called with a
* {@code {@link RuntimePermission}("getenv.*")} permission.
* This may result in a {@link SecurityException} being thrown.
*
* <p>When passing information to a Java subprocess,
* <a href=#EnvironmentVSSystemProperties>system properties</a>
* are generally preferred over environment variables.
*
* @return the environment as a map of variable names to values
*
* @throws SecurityException if a security manager exists and its
* {@link SecurityManager#checkPermission checkPermission}
* method doesn't allow access to the process environment
* @see #getenv(String)
* @see ProcessBuilder#environment()
* @since 1.5
*/
// 返回所有环境变量
public static Map<String, String> getenv() {
SecurityManager sm = getSecurityManager();
if(sm != null) {
sm.checkPermission(new RuntimePermission("getenv.*"));
}
return ProcessEnvironment.getenv();
}
/**
* Gets the value of the specified environment variable. An
* environment variable is a system-dependent external named
* value.
*
* <p>If a security manager exists, its
* {@link SecurityManager#checkPermission checkPermission}
* method is called with a
* {@code {@link RuntimePermission}("getenv."+name)}
* permission. This may result in a {@link SecurityException}
* being thrown. If no exception is thrown the value of the
* variable {@code name} is returned.
*
* <p><a id="EnvironmentVSSystemProperties"><i>System
* properties</i> and <i>environment variables</i></a> are both
* conceptually mappings between names and values. Both
* mechanisms can be used to pass user-defined information to a
* Java process. Environment variables have a more global effect,
* because they are visible to all descendants of the process
* which defines them, not just the immediate Java subprocess.
* They can have subtly different semantics, such as case
* insensitivity, on different operating systems. For these
* reasons, environment variables are more likely to have
* unintended side effects. It is best to use system properties
* where possible. Environment variables should be used when a
* global effect is desired, or when an external system interface
* requires an environment variable (such as {@code PATH}).
*
* <p>On UNIX systems the alphabetic case of {@code name} is
* typically significant, while on Microsoft Windows systems it is
* typically not. For example, the expression
* {@code System.getenv("FOO").equals(System.getenv("foo"))}
* is likely to be true on Microsoft Windows.
*
* @param name the name of the environment variable
*
* @return the string value of the variable, or {@code null}
* if the variable is not defined in the system environment
*
* @throws NullPointerException if {@code name} is {@code null}
* @throws SecurityException if a security manager exists and its
* {@link SecurityManager#checkPermission checkPermission}
* method doesn't allow access to the environment variable
* {@code name}
* @see #getenv()
* @see ProcessBuilder#environment()
*/
// 返回指定名称的环境变量
public static String getenv(String name) {
SecurityManager sm = getSecurityManager();
if(sm != null) {
sm.checkPermission(new RuntimePermission("getenv." + name));
}
return ProcessEnvironment.getenv(name);
}
/*▲ 环境变量 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 时间 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns the current time in milliseconds. Note that
* while the unit of time of the return value is a millisecond,
* the granularity of the value depends on the underlying
* operating system and may be larger. For example, many
* operating systems measure time in units of tens of
* milliseconds.
*
* <p> See the description of the class {@code Date} for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @return the difference, measured in milliseconds, between
* the current time and midnight, January 1, 1970 UTC.
*
* @see java.util.Date
*/
/*
* 返回当前时间点与新纪元时间点之间的毫秒差值(具体粒度由底层操作系统决定)
*
* 新纪元时间点:UTC/GMT时间1970年1月1号0时0分0秒
*/
@HotSpotIntrinsicCandidate
public static native long currentTimeMillis();
/**
* Returns the current value of the running Java Virtual Machine's
* high-resolution time source, in nanoseconds.
*
* This method can only be used to measure elapsed time and is
* not related to any other notion of system or wall-clock time.
* The value returned represents nanoseconds since some fixed but
* arbitrary <i>origin</i> time (perhaps in the future, so values
* may be negative). The same origin is used by all invocations of
* this method in an instance of a Java virtual machine; other
* virtual machine instances are likely to use a different origin.
*
* <p>This method provides nanosecond precision, but not necessarily
* nanosecond resolution (that is, how frequently the value changes)
* - no guarantees are made except that the resolution is at least as
* good as that of {@link #currentTimeMillis()}.
*
* <p>Differences in successive calls that span greater than
* approximately 292 years (2<sup>63</sup> nanoseconds) will not
* correctly compute elapsed time due to numerical overflow.
*
* <p>The values returned by this method become meaningful only when
* the difference between two such values, obtained within the same
* instance of a Java virtual machine, is computed.
*
* <p>For example, to measure how long some code takes to execute:
* <pre> {@code
* long startTime = System.nanoTime();
* // ... the code being measured ...
* long elapsedNanos = System.nanoTime() - startTime;}</pre>
*
* <p>To compare elapsed time against a timeout, use <pre> {@code
* if (System.nanoTime() - startTime >= timeoutNanos) ...}</pre>
* instead of <pre> {@code
* if (System.nanoTime() >= startTime + timeoutNanos) ...}</pre>
* because of the possibility of numerical overflow.
*
* @return the current value of the running Java Virtual Machine's
* high-resolution time source, in nanoseconds
*
* @since 1.5
*/
// 返回一个纳秒级的时间,不与具体的日期挂钩,只反应某段流逝的时间,可用来计数
@HotSpotIntrinsicCandidate
public static native long nanoTime();
/*▲ 时间 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 日志 ████████████████████████████████████████████████████████████████████████████████┓ */
/* 默认实现:LoggingProviderImpl$JULWrapper*/
/**
* Returns an instance of {@link Logger Logger} for the caller's
* use.
*
* @param name the name of the logger.
*
* @return an instance of {@link Logger} that can be used by the calling
* class.
*
* @throws NullPointerException if {@code name} is {@code null}.
* @throws IllegalCallerException if there is no Java caller frame on the
* stack.
* @implSpec Instances returned by this method route messages to loggers
* obtained by calling {@link LoggerFinder#getLogger(java.lang.String,
* java.lang.Module) LoggerFinder.getLogger(name, module)}, where
* {@code module} is the caller's module.
* In cases where {@code System.getLogger} is called from a context where
* there is no caller frame on the stack (e.g when called directly
* from a JNI attached thread), {@code IllegalCallerException} is thrown.
* To obtain a logger in such a context, use an auxiliary class that will
* implicitly be identified as the caller, or use the system {@link
* LoggerFinder#getLoggerFinder() LoggerFinder} to obtain a logger instead.
* Note that doing the latter may eagerly initialize the underlying
* logging system.
* @apiNote This method may defer calling the {@link
* LoggerFinder#getLogger(java.lang.String, java.lang.Module)
* LoggerFinder.getLogger} method to create an actual logger supplied by
* the logging backend, for instance, to allow loggers to be obtained during
* the system initialization time.
* @since 9
*/
// 获取一个名为name的Logger实例
@CallerSensitive
public static Logger getLogger(String name) {
Objects.requireNonNull(name);
// 获取getLogger()方法的调用者所处的类
final Class<?> caller = Reflection.getCallerClass();
if(caller == null) {
throw new IllegalCallerException("no caller frame");
}
return LazyLoggers.getLogger(name, caller.getModule());
}
/**
* Returns a localizable instance of {@link Logger Logger} for the caller's use.
* The returned logger will use the provided resource bundle for message localization.
*
* @param name the name of the logger.
* @param bundle a resource bundle.
*
* @return an instance of {@link Logger} which will use the provided
* resource bundle for message localization.
*
* @throws NullPointerException if {@code name} is {@code null} or
* {@code bundle} is {@code null}.
* @throws IllegalCallerException if there is no Java caller frame on the
* stack.
* @implSpec The returned logger will perform message localization as specified
* by {@link LoggerFinder#getLocalizedLogger(java.lang.String,
* java.util.ResourceBundle, java.lang.Module)
* LoggerFinder.getLocalizedLogger(name, bundle, module)}, where
* {@code module} is the caller's module.
* In cases where {@code System.getLogger} is called from a context where
* there is no caller frame on the stack (e.g when called directly
* from a JNI attached thread), {@code IllegalCallerException} is thrown.
* To obtain a logger in such a context, use an auxiliary class that
* will implicitly be identified as the caller, or use the system {@link
* LoggerFinder#getLoggerFinder() LoggerFinder} to obtain a logger instead.
* Note that doing the latter may eagerly initialize the underlying
* logging system.
* @apiNote This method is intended to be used after the system is fully initialized.
* This method may trigger the immediate loading and initialization
* of the {@link LoggerFinder} service, which may cause issues if the