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

ResourceBundle.getBundle() does not check for subclass of ResourceBundle

    XMLWordPrintable

Details

    • beta
    • generic, x86
    • generic, windows_nt

    Description



      Name: joT67522 Date: 10/01/97


      /*
       * filename: test.java
       *
       * Cut out this code, compile and run it.
       *
       * OUTPUT description: The constructor gets called repeatedly.
       *
       * The reason for this lies in ResourceBundle.getBundle().
       * In ResourceBundle class, getBundle() uses findBundle()
       * first to look for a class with the given argument string
       * and it tries to instanstiate it. It does not check whether
       * it is a subclass of ResourceBundle before instantiating it.
       * Since the constructor for class test is public, there is
       * no problem with trying to instantiate it, and it will,
       * however ResourceBundle.getBundle() will be called
       * repeatedly.
       */

      import java.util.ResourceBundle;

      public class test
      {
          String message = null;
          ResourceBundle rb;
          
          // remove the public access modifier for the constructor
          // and this code will work fine.
          public test()
          {
              System.out.println( "constructor called" );

              try
              {
                  rb = ResourceBundle.getBundle( "test" );
                  System.out.println( "read properties file" );
              }
              catch( Exception e )
              {
                  System.err.println( "test.properties not found" );
                  e.printStackTrace();
                  System.exit( 1 );
              }

              try
              {
                  message = rb.getString( "message" );
                  System.out.println( message );
              }
              catch( Exception e )
              {
                  e.printStackTrace();
              }
          }

          public static void main( String args[] )
          {
              test t = new test();
          }
      }

      //------------------------------------------
      // Cut below the next line and remove the comments and save as test.properties
      //------------------------------------------
      message=Hello!
      ======================================================================


      The bug actually covers two problems, only one of which is corrected by this fix:

      Problem 1: ResourceBundle.getBundle does not find a .properties file. This occurs if you try to load a .properties file that has the same name as a class (and the class file does not descend from ResourceBundle). Ex. you have a class named MyClass (which does not descend from ResourceBundle) and a properties file named MyClass.properties. Resource bundle will try to return an object of type MyClass, fail when casting it to a ResourceBundle, and throw a ClassCastException. The .properties file is never looked for.

      Problem 2: ResourceBundle.getBundle can cause an infinite recursion. This occurs when you meet the conditions of problem 1 AND you try to load the properties file from the class constructor or initialization code. ex. MyClass tries to load MyClass.properties from its constructor.

      Problem 1 is corrected by this fix. Problem 2 is not.

      johnfz@eng 1999-03-16

      Attachments

        Issue Links

          Activity

            People

              duke J. Duke
              johsunw Joon Oh (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: