Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 319900

Summary: StringLiteral#setLiteralValue needlessly escapes apostrophes (')
Product: [Eclipse Project] JDT Reporter: Chris West (Faux) <eclipse>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: minor    
Priority: P3 CC: amj87.iitr, satyam.kandula, srikanth_sankaran
Version: 3.6Flags: srikanth_sankaran: review+
Target Milestone: 3.6.1   
Hardware: PC   
OS: Windows Vista   
Whiteboard:
Attachments:
Description Flags
Remove the "case '" clause
Olivier_Thomann: iplog+
Proposed fix + regression tests none

Description Chris West (Faux) CLA 2010-07-14 14:51:12 EDT
Build Identifier: N20100703-2000

Calling setLiteralValue() with a String containing a single-quote will result in the quote being escaped in the rewritten output.

i.e. sl.setLiteralValue(sl.getLiteralValue() + " loves horses' manes") results in ...horses\' manes", which is correct, but ugly.

Easy enough to work around by copy-pastaing the method and removing the offending case, as patch.

Reproducible: Always

Steps to Reproduce:
1. Execute:
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.StringLiteral;

class JDTScratch {
  public static void main(String[] args) {
    StringLiteral sl = ast().newStringLiteral();

    sl.setEscapedValue("\"'\"");
    System.out.println(sl.getEscapedValue()); // "'"

    sl.setLiteralValue("'");
    System.out.println(sl.getEscapedValue()); // "\'"
  }

  static AST ast() {
    final ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(new char[0]);
    return parser.createAST(null).getAST();
  }
}


Observe output, as comments.
Comment 1 Chris West (Faux) CLA 2010-07-14 14:55:07 EDT
Created attachment 174331 [details]
Remove the "case '" clause
Comment 2 Olivier Thomann CLA 2010-07-15 10:47:03 EDT
The quote must be escaped only within a character literal.
Comment 3 Olivier Thomann CLA 2010-07-15 10:55:35 EDT
Srikanth, we should fix this for 3.6.1 as we corrupt the string literal.

Released for 3.7M1.
Comment 4 Olivier Thomann CLA 2010-07-15 10:56:03 EDT
Created attachment 174405 [details]
Proposed fix + regression tests
Comment 5 Olivier Thomann CLA 2010-07-15 10:56:16 EDT
Same patch with regression tests.
Comment 6 Srikanth Sankaran CLA 2010-07-18 23:56:24 EDT
Patch looks good.
Comment 7 Olivier Thomann CLA 2010-07-19 13:01:35 EDT
Released for 3.6.1 with updated copyright for the test.
Comment 8 Satyam Kandula CLA 2010-08-03 05:23:14 EDT
Verified for 3.7M1 using build I20100802-1800
Comment 9 Satyam Kandula CLA 2010-08-05 04:40:47 EDT
Change status to RESOLVED as this needs to be reverified for 3.6.1.
Comment 10 Satyam Kandula CLA 2010-08-26 05:49:43 EDT
Verified for 3.6.1 RC2 using Build id: M20100825-0800