Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4693380

(prefs) AbstractPreferences.isUserNode can cause NPE when customizing preference

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • x86
    • windows_2000

      Name: gm110360 Date: 05/29/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      See RFE 4689858 for some background information.

      When customizing Preferences in the proper way, and using
      the platform backing preferences to do the persistence etc,
      the Preferences.userRoot() may not be the same as the nodes
      root even when the node is a user node.

      The AbstractPreferences isUserNode is making this check,
      which is not always correct. In my case it is attempting to
      ask if the WindowsPreferences userRoot == TestFactory
      userRoot which would return false even though it is a user
      node (it actually throws an Exception due to the bug).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Expected: to work
      Actual: throws NullPointerException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.NullPointerException
      at java.util.prefs.Preferences.userRoot(Preferences.java:341)
      at java.util.prefs.AbstractPreferences$1.run(AbstractPreferences.java:1006)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.util.prefs.AbstractPreferences.isUserNode(AbstractPreferences.java:1003)
      at java.util.prefs.WindowsPreferences.rootNativeHandle(WindowsPreferences.java:1021)
      at java.util.prefs.WindowsPreferences.openKey(WindowsPreferences.java:467)
      at java.util.prefs.WindowsPreferences.openKey(WindowsPreferences.java:449)
      at java.util.prefs.WindowsPreferences.<init>(WindowsPreferences.java:372)
      at java.util.prefs.WindowsPreferences.childSpi(WindowsPreferences.java:789)
      at java.util.prefs.AbstractPreferences.node(AbstractPreferences.java:816)
      at java.util.prefs.AbstractPreferences.node(AbstractPreferences.java:796)
      at test.TestFactory.<init>(TestFactory.java:15)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at
      sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at
      sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
      at java.lang.Class.newInstance0(Class.java:296)
      at java.lang.Class.newInstance(Class.java:249)
      at java.util.prefs.Preferences.<clinit>(Preferences.java:190)
      at TestFactory.main(TestFactory.java:36)



      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.reflect.*;
      import java.util.prefs.*;

      public class TestFactory implements PreferencesFactory {
        private Preferences systemPreferences;
        private Preferences userPreferences;

        public TestFactory() {
          try {
            Constructor constructor =
      Class.forName(System.getProperty("java.util.prefs.DefaultPreferencesFactory")).getDeclaredConstructor(null);
            constructor.setAccessible(true);
            PreferencesFactory defaultPreferencesFactory =
      (PreferencesFactory)constructor.newInstance(null);
            systemPreferences = defaultPreferencesFactory.systemRoot().node("test");
            userPreferences = defaultPreferencesFactory.userRoot().node("test");
          }
          catch (Exception ex) {
            System.out.println("Exception: " + ex);
          }
        }

        public Preferences systemRoot() {
          return systemPreferences;
        }

        public Preferences userRoot() {
          return userPreferences;
        }

        public static void main(String[] args) {
          System.setProperty("java.util.prefs.DefaultPreferencesFactory",
      System.getProperty("java.util.prefs.PreferencesFactory"));
          System.setProperty("java.util.prefs.PreferencesFactory", "TestFactory");

          Preferences.systemRoot();
        }
      }
      ---------- END SOURCE ----------
      (Review ID: 147179)
      ======================================================================
      ###@###.### 11/3/04 20:34 GMT

            sherman Xueming Shen
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: