JDK-4061282 : Properties class does not 'force' String objects
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 1.1.1,1.2.0
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_95
  • CPU: x86
  • Submitted: 1997-06-25
  • Updated: 2016-05-18
  • Resolved: 1998-08-26
Related Reports
Relates :  
Relates :  
Description

Name: sgC58550			Date: 06/25/97


This is in the context of an application
Take an instance of java.utils.Properties class, insert an 
Object other than java.lang.String.  After this has been 
accomplihed attempt to use 
java.utils.Properties.save(OutputStream,String) a 
ClassCastError is given with out much reason.  The output only
has the header and property name followed by an =.

public static void main(String args[]) {
   try {
      Properties myProps = new Properties();
      myProps.put("One",new Integer(1));
      FileOutputStream os = new FileOutputStream("outprops");
      myProps.save(os,"My Props");
      os.flush();
      os.close();
   }catch(Exception ex) {
      ex.printStackTrace();
      Sytem.exit(1);
   }
   System.exit(0);
}

company -  , email - ###@###.###
======================================================================

Comments
WORK AROUND Name: sgC58550 Date: 06/25/97 I think that the Properties class should over-ride the put method by either a) Changing the parameter of put from Object,Object to String, String or b) Over-riding the put method to verify that both parameters are String. Option (a) seems more preferrable as the compiler will catch the error. ======================================================================
11-06-2004

EVALUATION We should really catch this when the bogus key-value pair is inserted into the Properties object. joshua.bloch@Eng 1997-07-08 Properties will now enforce that keys and values be instances of java.lang.String michael.mccloskey@eng 1998-01-21 This fix is being backed out for compatibility reasons. The new setProperty() method will still take only Strings but the inherited put() method will not be overridden because to do so breaks existing code. michael.mccloskey@eng 1998-06-05
05-06-1998