java lang的system_java.lang.system 类源码解读

这段代码展示了Java中获取和设置系统属性的方法。通过`getProperties()`获取系统属性集合,包括Java版本、操作系统信息、用户目录等。`getProperty()`用于按指定键获取属性值,而`setProperty()`则用于设置系统属性。同时,安全检查贯穿整个过程,确保操作的合法性。
摘要由CSDN通过智能技术生成

/*** System properties. The following properties are guaranteed to be defined:

*

*

java.version Java version number

*

java.vendor Java vendor specific string

*

java.vendor.url Java vendor URL

*

java.home Java installation directory

*

java.class.version Java class version number

*

java.class.path Java classpath

*

os.name Operating System Name

*

os.arch Operating System Architecture

*

os.version Operating System Version

*

file.separator File separator ("/" on Unix)

*

path.separator Path separator (":" on Unix)

*

line.separator Line separator ("\n" on Unix)

*

user.name User account name

*

user.home User home directory

*

user.dir User's current working directory

*

*/

private staticProperties props;private static nativeProperties initProperties(Properties props);/*** Determines the current system properties.

*

* First, if there is a security manager, its

* checkPropertiesAccess method is called with no

* arguments. This may result in a security exception.

*

* The current set of system properties for use by the

* {@link#getProperty(String)} method is returned as a

* 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:

*

*

Key

*

Description of Associated Value

*

java.version

*

Java Runtime Environment version

*

java.vendor

*

Java Runtime Environment vendor

*

java.vendor.url

*

Java vendor URL

*

java.home

*

Java installation directory

*

java.vm.specification.version

*

Java Virtual Machine specification version

*

java.vm.specification.vendor

*

Java Virtual Machine specification vendor

*

java.vm.specification.name

*

Java Virtual Machine specification name

*

java.vm.version

*

Java Virtual Machine implementation version

*

java.vm.vendor

*

Java Virtual Machine implementation vendor

*

java.vm.name

*

Java Virtual Machine implementation name

*

java.specification.version

*

Java Runtime Environment specification version

*

java.specification.vendor

*

Java Runtime Environment specification vendor

*

java.specification.name

*

Java Runtime Environment specification name

*

java.class.version

*

Java class format version number

*

java.class.path

*

Java class path

*

java.library.path

*

List of paths to search when loading libraries

*

java.io.tmpdir

*

Default temp file path

*

java.compiler

*

Name of JIT compiler to use

*

java.ext.dirs

*

Path of extension directory or directories

* Deprecated. This property, and the mechanism

* which implements it, may be removed in a future

* release.

*

os.name

*

Operating system name

*

os.arch

*

Operating system architecture

*

os.version

*

Operating system version

*

file.separator

*

File separator ("/" on UNIX)

*

path.separator

*

Path separator (":" on UNIX)

*

line.separator

*

Line separator ("\n" on UNIX)

*

user.name

*

User's account name

*

user.home

*

User's home directory

*

user.dir

*

User's current working directory

*

*

* Multiple paths in a system property value are separated by the path

* separator character of the platform.

*

* Note that even if the security manager does not permit the

* getProperties operation, it may choose to permit the

* {@link#getProperty(String)} operation.

*

*@returnthe system properties

*@exceptionSecurityException if a security manager exists and its

* checkPropertiesAccess method doesn't allow access

* to the system properties.

*@see#setProperties

*@seejava.lang.SecurityException

*@seejava.lang.SecurityManager#checkPropertiesAccess()

*@seejava.util.Properties*/

public staticProperties getProperties() {

SecurityManager sm=getSecurityManager();if (sm != null) {

sm.checkPropertiesAccess();

}returnprops;

}/*** Returns the system-dependent line separator string. It always

* returns the same value - the initial value of the {@linkplain* #getProperty(String) system property} {@codeline.separator}.

*

*

On UNIX systems, it returns {@code"\n"}; on Microsoft

* Windows systems it returns {@code"\r\n"}.

*

*@returnthe system-dependent line separator string

*@since1.7*/

public staticString lineSeparator() {returnlineSeparator;

}private staticString lineSeparator;/*** Sets the system properties to the Properties

* argument.

*

* First, if there is a security manager, its

* checkPropertiesAccess method is called with no

* arguments. This may result in a security exception.

*

* The argument becomes the current set of system properties for use

* by the {@link#getProperty(String)} method. If the argument is

* null, then the current set of system properties is

* forgotten.

*

*@paramprops the new system properties.

*@exceptionSecurityException if a security manager exists and its

* checkPropertiesAccess method doesn't allow access

* to the system properties.

*@see#getProperties

*@seejava.util.Properties

*@seejava.lang.SecurityException

*@seejava.lang.SecurityManager#checkPropertiesAccess()*/

public static voidsetProperties(Properties props) {

SecurityManager sm=getSecurityManager();if (sm != null) {

sm.checkPropertiesAccess();

}if (props == null) {

props= newProperties();

initProperties(props);

}

System.props=props;

}/*** Gets the system property indicated by the specified key.

*

* First, if there is a security manager, its

* checkPropertyAccess method is called with the key as

* its argument. This may result in a SecurityException.

*

* 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 getProperties method.

*

*@paramkey the name of the system property.

*@returnthe string value of the system property,

* or null if there is no property with that key.

*

*@exceptionSecurityException if a security manager exists and its

* checkPropertyAccess method doesn't allow

* access to the specified system property.

*@exceptionNullPointerException if key is

* null.

*@exceptionIllegalArgumentException if key is empty.

*@see#setProperty

*@seejava.lang.SecurityException

*@seejava.lang.SecurityManager#checkPropertyAccess(java.lang.String)

*@seejava.lang.System#getProperties()*/

public staticString getProperty(String key) {

checkKey(key);

SecurityManager sm=getSecurityManager();if (sm != null) {

sm.checkPropertyAccess(key);

}returnprops.getProperty(key);

}/*** Gets the system property indicated by the specified key.

*

* First, if there is a security manager, its

* checkPropertyAccess method is called with the

* key as its argument.

*

* 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 getProperties method.

*

*@paramkey the name of the system property.

*@paramdef a default value.

*@returnthe string value of the system property,

* or the default value if there is no property with that key.

*

*@exceptionSecurityException if a security manager exists and its

* checkPropertyAccess method doesn't allow

* access to the specified system property.

*@exceptionNullPointerException if key is

* null.

*@exceptionIllegalArgumentException if key is empty.

*@see#setProperty

*@seejava.lang.SecurityManager#checkPropertyAccess(java.lang.String)

*@seejava.lang.System#getProperties()*/

public staticString getProperty(String key, String def) {

checkKey(key);

SecurityManager sm=getSecurityManager();if (sm != null) {

sm.checkPropertyAccess(key);

}returnprops.getProperty(key, def);

}/*** Sets the system property indicated by the specified key.

*

* First, if a security manager exists, its

* SecurityManager.checkPermission method

* is called with a 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.

*

*

*@paramkey the name of the system property.

*@paramvalue the value of the system property.

*@returnthe previous value of the system property,

* or null if it did not have one.

*

*@exceptionSecurityException if a security manager exists and its

* checkPermission method doesn't allow

* setting of the specified property.

*@exceptionNullPointerException if key or

* value is null.

*@exceptionIllegalArgumentException if key is empty.

*@see#getProperty

*@seejava.lang.System#getProperty(java.lang.String)

*@seejava.lang.System#getProperty(java.lang.String, java.lang.String)

*@seejava.util.PropertyPermission

*@seeSecurityManager#checkPermission

*@since1.2*/

public staticString setProperty(String key, String value) {

checkKey(key);

SecurityManager sm=getSecurityManager();if (sm != null) {

sm.checkPermission(newPropertyPermission(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

* SecurityManager.checkPermission method

* is called with a PropertyPermission(key, "write")

* permission. This may result in a SecurityException being thrown.

* If no exception is thrown, the specified property is removed.

*

*

*@paramkey the name of the system property to be removed.

*@returnthe previous string value of the system property,

* or null if there was no property with that key.

*

*@exceptionSecurityException if a security manager exists and its

* checkPropertyAccess method doesn't allow

* access to the specified system property.

*@exceptionNullPointerException if key is

* null.

*@exceptionIllegalArgumentException if key is empty.

*@see#getProperty

*@see#setProperty

*@seejava.util.Properties

*@seejava.lang.SecurityException

*@seejava.lang.SecurityManager#checkPropertiesAccess()

*@since1.5*/

public staticString clearProperty(String key) {

checkKey(key);

SecurityManager sm=getSecurityManager();if (sm != null) {

sm.checkPermission(new PropertyPermission(key, "write"));

}return(String) props.remove(key);

}private static voidcheckKey(String key) {if (key == null) {throw new NullPointerException("key can't be null");

}if (key.equals("")) {throw new IllegalArgumentException("key can't be empty");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值