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 org.jspecify.annotations.Nullable;
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
029public enum StandardSystemProperty {
030
031  /** Java Runtime Environment version. */
032  JAVA_VERSION("java.version"),
033
034  /** Java Runtime Environment vendor. */
035  JAVA_VENDOR("java.vendor"),
036
037  /** Java vendor URL. */
038  JAVA_VENDOR_URL("java.vendor.url"),
039
040  /** Java installation directory. */
041  JAVA_HOME("java.home"),
042
043  /** Java Virtual Machine specification version. */
044  JAVA_VM_SPECIFICATION_VERSION("java.vm.specification.version"),
045
046  /** Java Virtual Machine specification vendor. */
047  JAVA_VM_SPECIFICATION_VENDOR("java.vm.specification.vendor"),
048
049  /** Java Virtual Machine specification name. */
050  JAVA_VM_SPECIFICATION_NAME("java.vm.specification.name"),
051
052  /** Java Virtual Machine implementation version. */
053  JAVA_VM_VERSION("java.vm.version"),
054
055  /** Java Virtual Machine implementation vendor. */
056  JAVA_VM_VENDOR("java.vm.vendor"),
057
058  /** Java Virtual Machine implementation name. */
059  JAVA_VM_NAME("java.vm.name"),
060
061  /** Java Runtime Environment specification version. */
062  JAVA_SPECIFICATION_VERSION("java.specification.version"),
063
064  /** Java Runtime Environment specification vendor. */
065  JAVA_SPECIFICATION_VENDOR("java.specification.vendor"),
066
067  /** Java Runtime Environment specification name. */
068  JAVA_SPECIFICATION_NAME("java.specification.name"),
069
070  /** Java class format version number. */
071  JAVA_CLASS_VERSION("java.class.version"),
072
073  /** Java class path. */
074  JAVA_CLASS_PATH("java.class.path"),
075
076  /** List of paths to search when loading libraries. */
077  JAVA_LIBRARY_PATH("java.library.path"),
078
079  /** Default temp file path. */
080  JAVA_IO_TMPDIR("java.io.tmpdir"),
081
082  /** Name of JIT compiler to use. */
083  JAVA_COMPILER("java.compiler"),
084
085  /**
086   * Path of extension directory or directories.
087   *
088   * @deprecated This property was <a
089   *     href="https://openjdk.java.net/jeps/220#Removed:-The-extension-mechanism">deprecated</a> in
090   *     Java 8 and removed in Java 9. We do not plan to remove this API from Guava, but if you are
091   *     using it, it is probably not doing what you want.
092   */
093  @Deprecated
094  JAVA_EXT_DIRS("java.ext.dirs"),
095
096  /** Operating system name. */
097  OS_NAME("os.name"),
098
099  /** Operating system architecture. */
100  OS_ARCH("os.arch"),
101
102  /** Operating system version. */
103  OS_VERSION("os.version"),
104
105  /** File separator ("/" on UNIX). */
106  FILE_SEPARATOR("file.separator"),
107
108  /** Path separator (":" on UNIX). */
109  PATH_SEPARATOR("path.separator"),
110
111  /** Line separator ("\n" on UNIX). */
112  LINE_SEPARATOR("line.separator"),
113
114  /** User's account name. */
115  USER_NAME("user.name"),
116
117  /** User's home directory. */
118  USER_HOME("user.home"),
119
120  /** User's current working directory. */
121  USER_DIR("user.dir");
122
123  private final String key;
124
125  StandardSystemProperty(String key) {
126    this.key = key;
127  }
128
129  /** Returns the key used to look up this system property. */
130  public String key() {
131    return key;
132  }
133
134  /**
135   * Returns the current value for this system property by delegating to {@link
136   * System#getProperty(String)}.
137   *
138   * <p>The value returned by this method is non-null except in rare circumstances:
139   *
140   * <ul>
141   *   <li>{@link #JAVA_EXT_DIRS} was deprecated in Java 8 and removed in Java 9. We have not
142   *       confirmed whether it is available under older versions.
143   *   <li>{@link #JAVA_COMPILER}, while still listed as required as of Java 15, is typically not
144   *       available even under older version.
145   *   <li>Any property may be cleared through APIs like {@link System#clearProperty}.
146   *   <li>Unusual environments like GWT may have their own special handling of system properties.
147   * </ul>
148   *
149   * <p>Note that {@code StandardSystemProperty} does not provide constants for more recently added
150   * properties, including:
151   *
152   * <ul>
153   *   <li>{@code java.vendor.version} (added in Java 11, listed as optional as of Java 13)
154   *   <li>{@code jdk.module.*} (added in Java 9, optional)
155   * </ul>
156   */
157  public @Nullable String value() {
158    return System.getProperty(key);
159  }
160
161  /** Returns a string representation of this system property. */
162  @Override
163  public String toString() {
164    return key() + "=" + value();
165  }
166}