JDK-8073613 : Here documents: how to avoid string interpolation?
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2015-02-23
  • Updated: 2016-01-14
  • Resolved: 2015-08-26
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 8 JDK 9
8u72Fixed 9 b80Fixed
Related Reports
Relates :  
Description
This was reported via nashorn-dev openjdk alias. http://mail.openjdk.java.net/pipermail/nashorn-dev/2015-February/004204.html

Email content here for reference:

Hi

How can I avoid string interpolation in here documents with Nashorn? In 
other words: what if the here document contains the literal string 
"${a}"? The user guide section on here documents [1] doesn't tell how to 
do this.
If this is not available yet, shall I create a bug report for this?

Kind regards, Anthony

[1] 
http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html#sthref26
Comments
The issue is strictly about heredocs, and the problematic behaviour can be reproduced. var str = "hello" print(<<END) ${str} world $\{str} world END This script will output the following in both 9-dev and 8u51: hello world $\{str} world The poster of the original question clarifies his intention in a follow-up e-mail: http://mail.openjdk.java.net/pipermail/nashorn-dev/2015-February/004212.html (Quote) var a = 4; var b = 2; print(<<EOD) ${a}${b} EOD would print 42, but using print(<<"EOD") or print(<<'EOD') would print ${a}${b} (End of quote) To address the issue, delimiter string quoting should be implemented.
24-08-2015

Behaviour is also correct in 8u51.
24-08-2015

In a recent 9-dev build, this is no longer the case: jjs> var str = "hello" jjs> var s = "$\{str} world" jjs> print(s) ${str} world jjs> var s1 = "${str} world" jjs> print(s1) hello world
24-08-2015

My last comment is wrong. Escape does not work as mentioned in the comment. When using $\{foo}, I get $\{foo} in the output and not ${foo} as expected.
23-02-2015

Based on code reading and testing, escaping by \ after $ seems to work as shown by example below: var str = "hello"; var s = "$\{str} world"; print(s); // prints ${str} world var s1 = "${str} world"; print(s1) // prints "hello world" Two things: 1) clarify that it is indeed the expected/supported way to escape 2) document the expected/supported behaviour to escape interpolation 1)
23-02-2015