JDK-6457659 : sun.beans.editors.StringEditor does not correctly encode \n
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-08-07
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b07Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0-rc-fastdebug"
Java(TM) SE Runtime Environment (build 1.6.0-rc-fastdebug-b93)
Java HotSpot(TM) Client VM (build 1.6.0-rc-fastdebug-b93-debug, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
sun.beans.editors.StringEditor does not handle the \n character correctly.  It should replace it with the string "\\n", but instead passes it through as the carriage return character.  This causes strings containing carriage returns to generate incorrect code.


REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :

The below class is a good workaround.  t must be registered with the PropertyEditorManager so that the bean manager can find it.

public class StringPropertyEditorSupport extends sun.beans.editors.StringEditor
{
    
    /** Creates a new instance of StringPropertyEditorSupport */
    public StringPropertyEditorSupport()
    {
    }
    
    public String getJavaInitializationString()
    {
        String value = super.getJavaInitializationString();
        
        value = value.replaceAll("\\n", "\\\\n\" +\n    \"");
        
        return value;
    }
}

Comments
EVALUATION Suggested workaround could not be applied because we should support other escape sequences. See Java Language Specification (2nd edition): 3.10.6 Escape Sequences for Character and String Literals
08-08-2006