001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.base;
016
017import com.google.common.annotations.GwtIncompatible;
018import com.google.common.annotations.J2ktIncompatible;
019import javax.annotation.CheckForNull;
020
021/**
022 * Represents a {@linkplain System#getProperties() standard system property}.
023 *
024 * @author Kurt Alfred Kluever
025 * @since 15.0
026 */
027@J2ktIncompatible
028@GwtIncompatible // java.lang.System#getProperty
029@ElementTypesAreNonnullByDefault
030public enum StandardSystemProperty {
031
032  /** Java Runtime Environment version. */
033  JAVA_VERSION("java.version"),
034
035  /** Java Runtime Environment vendor. */
036  JAVA_VENDOR("java.vendor"),
037
038  /** Java vendor URL. */
039  JAVA_VENDOR_URL("java.vendor.url"),
040
041  /** Java installation directory. */
042  JAVA_HOME("java.home"),
043
044  /** Java Virtual Machine specification version. */
045  JAVA_VM_SPECIFICATION_VERSION("java.vm.specification.version"),
046
047  /** Java Virtual Machine specification vendor. */
048  JAVA_VM_SPECIFICATION_VENDOR("java.vm.specification.vendor"),
049
050  /** Java Virtual Machine specification name. */
051  JAVA_VM_SPECIFICATION_NAME("java.vm.specification.name"),
052
053  /** Java Virtual Machine implementation version. */
054  JAVA_VM_VERSION("java.vm.version"),
055
056  /** Java Virtual Machine implementation vendor. */
057  JAVA_VM_VENDOR("java.vm.vendor"),
058
059  /** Java Virtual Machine implementation name. */
060  JAVA_VM_NAME("java.vm.name"),
061
062  /** Java Runtime Environment specification version. */
063  JAVA_SPECIFICATION_VERSION("java.specification.version"),
064
065  /** Java Runtime Environment specification vendor. */
066  JAVA_SPECIFICATION_VENDOR("java.specification.vendor"),
067
068  /** Java Runtime Environment specification name. */
069  JAVA_SPECIFICATION_NAME("java.specification.name"),
070
071  /** Java class format version number. */
072  JAVA_CLASS_VERSION("java.class.version"),
073
074  /** Java class path. */
075  JAVA_CLASS_PATH("java.class.path"),
076
077  /** List of paths to search when loading libraries. */
078  JAVA_LIBRARY_PATH("java.library.path"),
079
080  /** Default temp file path. */
081  JAVA_IO_TMPDIR("java.io.tmpdir"),
082
083  /** Name of JIT compiler to use. */
084  JAVA_COMPILER("java.compiler"),
085
086  /**
087   * Path of extension directory or directories.
088   *
089   * @deprecated This property was <a
090   *     href="https://openjdk.java.net/jeps/220#Removed:-The-extension-mechanism">deprecated</a> in
091   *     Java 8 and removed in Java 9. We do not plan to remove this API from Guava, but if you are
092   *     using it, it is probably not doing what you want.
093   */
094  @Deprecated
095  JAVA_EXT_DIRS("java.ext.dirs"),
096
097  /** Operating system name. */
098  OS_NAME("os.name"),
099
100  /** Operating system architecture. */
101  OS_ARCH("os.arch"),
102
103  /** Operating system version. */
104  OS_VERSION("os.version"),
105
106  /** File separator ("/" on UNIX). */
107  FILE_SEPARATOR("file.separator"),
108
109  /** Path separator (":" on UNIX). */
110  PATH_SEPARATOR("path.separator"),
111
112  /** Line separator ("\n" on UNIX). */
113  LINE_SEPARATOR("line.separator"),
114
115  /** User's account name. */
116  USER_NAME("user.name"),
117
118  /** User's home directory. */
119  USER_HOME("user.home"),
120
121  /** User's current working directory. */
122  USER_DIR("user.dir");
123
124  private final String key;
125
126  StandardSystemProperty(String key) {
127    this.key = key;
128  }
129
130  /** Returns the key used to look up this system property. */
131  public String key() {
132    return key;
133  }
134
135  /**
136   * Returns the current value for this system property by delegating to {@link
137   * System#getProperty(String)}.
138   *
139   * <p>The value returned by this method is non-null except in rare circumstances:
140   *
141   * <ul>
142   *   <li>{@link #JAVA_EXT_DIRS} was deprecated in Java 8 and removed in Java 9. We have not
143   *       confirmed whether it is available under older versions.
144   *   <li>{@link #JAVA_COMPILER}, while still listed as required as of Java 15, is typically not
145   *       available even under older version.
146   *   <li>Any property may be cleared through APIs like {@link System#clearProperty}.
147   *   <li>Unusual environments like GWT may have their own special handling of system properties.
148   * </ul>
149   *
150   * <p>Note that {@code StandardSystemProperty} does not provide constants for more recently added
151   * properties, including:
152   *
153   * <ul>
154   *   <li>{@code java.vendor.version} (added in Java 11, listed as optional as of Java 13)
155   *   <li>{@code jdk.module.*} (added in Java 9, optional)
156   * </ul>
157   */
158  @CheckForNull
159  public String value() {
160    return System.getProperty(key);
161  }
162
163  /** Returns a string representation of this system property. */
164  @Override
165  public String toString() {
166    return key() + "=" + value();
167  }
168}