src/share/classes/sun/misc/VM.java

Print this page
rev 9156 : 8022854: System.setProperties(null) causes implementation private properties to reappear
Reviewed-by: duke

@@ -187,11 +187,11 @@
     //
     private static long directMemory = 64 * 1024 * 1024;
 
     // Returns the maximum amount of allocatable direct buffer memory.
     // The directMemory variable is initialized during system initialization
-    // in the saveAndRemoveProperties method.
+    // in the saveVMSystemProperties method.
     //
     public static long maxDirectMemory() {
         return directMemory;
     }
 

@@ -199,33 +199,33 @@
     // aligned. The "-XX:+PageAlignDirectMemory" option can be used to force
     // buffers, allocated by ByteBuffer.allocateDirect, to be page aligned.
     private static boolean pageAlignDirectMemory;
 
     // Returns {@code true} if the direct buffers should be page aligned. This
-    // variable is initialized by saveAndRemoveProperties.
+    // variable is initialized by saveVMSystemProperties.
     public static boolean isDirectMemoryPageAligned() {
         return pageAlignDirectMemory;
     }
 
     // A user-settable boolean to determine whether ClassLoader.loadClass should
     // accept array syntax.  This value may be changed during VM initialization
     // via the system property "sun.lang.ClassLoader.allowArraySyntax".
     //
-    // The default for 1.5 is "true", array syntax is allowed.  In 1.6, the
-    // default will be "false".  The presence of this system property to
+    // The default for 1.5 is "true", array syntax is allowed.  In 1.6+, the
+    // default is "false".  The presence of this system property to
     // control array syntax allows applications the ability to preview this new
     // behaviour.
     //
     private static boolean defaultAllowArraySyntax = false;
     private static boolean allowArraySyntax = defaultAllowArraySyntax;
 
     // The allowArraySyntax boolean is initialized during system initialization
-    // in the saveAndRemoveProperties method.
+    // in the saveVMSystemProperties method.
     //
     // It is initialized based on the value of the system property
     // "sun.lang.ClassLoader.allowArraySyntax".  If the system property is not
-    // provided, the default for 1.5 is "true".  In 1.6, the default will be
+    // provided, the default for 1.5 is "true".  In 1.6+, the default is
     // "false".  If the system property is provided, then the value of
     // allowArraySyntax will be equal to "true" if Boolean.parseBoolean()
     // returns "true".   Otherwise, the field will be set to "false".
     //
     public static boolean allowArraySyntax() {

@@ -262,26 +262,27 @@
     // TODO: the Property Management needs to be refactored and
     // the appropriate prop keys need to be accessible to the
     // calling classes to avoid duplication of keys.
     private static final Properties savedProps = new Properties();
 
-    // Save a private copy of the system properties and remove
-    // the system properties that are not intended for public access.
-    //
-    // This method can only be invoked during system initialization.
-    public static void saveAndRemoveProperties(Properties props) {
+    /**
+     * Save a private copy of the system properties.
+     *
+     * @implNote This method can only be invoked during system initialization.
+     */
+    public static void saveVMSystemProperties(Properties props) {
         if (booted)
             throw new IllegalStateException("System initialization has completed");
 
         savedProps.putAll(props);
 
         // Set the maximum amount of direct memory.  This value is controlled
         // by the vm option -XX:MaxDirectMemorySize=<size>.
         // The maximum amount of allocatable direct buffer memory (in bytes)
         // from the system property sun.nio.MaxDirectMemorySize set by the VM.
         // The system property will be removed.
-        String s = (String)props.remove("sun.nio.MaxDirectMemorySize");
+        String s = savedProps.getProperty("sun.nio.MaxDirectMemorySize");
         if (s != null) {
             if (s.equals("-1")) {
                 // -XX:MaxDirectMemorySize not given, take default
                 directMemory = Runtime.getRuntime().maxMemory();
             } else {

@@ -290,31 +291,49 @@
                     directMemory = l;
             }
         }
 
         // Check if direct buffers should be page aligned
-        s = (String)props.remove("sun.nio.PageAlignDirectMemory");
-        if ("true".equals(s))
-            pageAlignDirectMemory = true;
+        s = savedProps.getProperty("sun.nio.PageAlignDirectMemory");
+        pageAlignDirectMemory = Boolean.parseBoolean(s);
 
         // Set a boolean to determine whether ClassLoader.loadClass accepts
         // array syntax.  This value is controlled by the system property
         // "sun.lang.ClassLoader.allowArraySyntax".
-        s = props.getProperty("sun.lang.ClassLoader.allowArraySyntax");
-        allowArraySyntax = (s == null
-                               ? defaultAllowArraySyntax
-                               : Boolean.parseBoolean(s));
+        s = savedProps.getProperty("sun.lang.ClassLoader.allowArraySyntax");
+        allowArraySyntax = Boolean.parseBoolean(s);
+   }
+
+    /**
+     * Returns a copy of the saved system properties with private implementation
+     * properties removed.
+     *
+     * @return a newly created Properties instance containing only sanitized
+     */
+    public static Properties getSanitizedSavedProperties() {
+        Properties sanitized = new Properties();
+
+        sanitized.putAll(savedProps);
+
+        // Remove implementation private system properties
+
+        sanitized.remove("sun.nio.MaxDirectMemorySize");
+
+        sanitized.remove("sun.nio.PageAlignDirectMemory");
+
+        sanitized.remove("sun.lang.ClassLoader.allowArraySyntax");
 
-        // Remove other private system properties
         // used by java.lang.Integer.IntegerCache
-        props.remove("java.lang.Integer.IntegerCache.high");
+        sanitized.remove("java.lang.Integer.IntegerCache.high");
 
         // used by java.util.zip.ZipFile
-        props.remove("sun.zip.disableMemoryMapping");
+        sanitized.remove("sun.zip.disableMemoryMapping");
 
         // used by sun.launcher.LauncherHelper
-        props.remove("sun.java.launcher.diag");
+        sanitized.remove("sun.java.launcher.diag");
+
+        return sanitized;
     }
 
     // Initialize any miscellenous operating system settings that need to be
     // set for the class libraries.
     //