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

Subclassing of java.util.prefs.AbstractPreferences

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta2
    • generic, x86
    • generic, windows_nt
    • Verified



      Name: bsC130419 Date: 07/24/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)


      My task is to subclass the class java.util.prefs.AbstractPreferences

      1. Create a subclass which is not located in the java.util.prefs package of
      java.util.prefs.AbstractPreferences and implement the
      interface PreferencesFactory.

      2. Set the property java.util.prefs.PreferencesFactory to the new factory.

      3. Call Preferences p = Preferences.userRoot() or similar.

      A listing which reproduces the bug is below. It contains three java files.

      // start:BugTest.java
      package test;
      import java.util.prefs.*;
      public class BugTest {
          static {
              System.setProperty("java.util.prefs.PreferencesFactory",
                  "test.BugPreferencesFactory");
          }
          public static void main(String[] args) {
              Preferences p = Preferences.userRoot();
          }
      }
      // end:BugTest.java
      // start: BugPreferencesFactory.java
      package test;
      import java.util.prefs.*;
      public class BugPreferencesFactory implements PreferencesFactory {
          public Preferences userRoot() {
              return BugPreferences.root;
          }
          public Preferences systemRoot() {
              return BugPreferences.root;
          }
      }
      // end: BugPreferencesFactory.java
      // start: BugPreferences.java
      package test;
      import java.util.prefs.*;
      public class BugPreferences extends AbstractPreferences {
          static BugPreferences root = new BugPreferences(null, "");
          public BugPreferences(AbstractPreferences parent, String name) {
              super(parent,name);
          }
          protected void putSpi(String key, String value) {
          }
          protected String getSpi(String key) {
              return null;
          }
          protected void syncSpi() throws BackingStoreException {
          }
          protected void removeNodeSpi() throws BackingStoreException {
          }
          protected AbstractPreferences childSpi(String name) {
              return new BugPreferences(this,name);
          }
          protected void flushSpi() throws BackingStoreException {
          }
          protected void removeSpi(String key) {
          }
          protected String[] childrenNamesSpi() throws BackingStoreException {
              return new String[0];
          }
          protected String[] keysSpi() throws BackingStoreException {
              return new String[0];
          }
      }
      // end:BugPreferences.java

      Running the follwing commandline:

      java -cp . test.BugTest

      leads to:

      Exception in thread "main" java.lang.InternalError: Can't instantiate Preference
      s factory java.lang.ClassNotFoundException: test/BugPreferencesFactory
              at java.util.prefs.Preferences.<clinit>(Preferences.java:191)
              at test.BugTest.main(BugTest.java:9)

      As a hint the error is class loader related.
      (Review ID: 128738)
      ======================================================================

            jjb Josh Bloch (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: