I am generating a class field in my JvmModelinferrer in xtend:
val exp = NodeModelUtils::getNode(rule.expression);
members+=rule.toField("text",rule.newTypeRef('java.lang.String'))[
^static = true
val Procedure1<ITreeAppendable> b = [
append('''"«exp.text.replace('"','').replaceAll("\n"," \\n ")»"''')
]
initializer = b
setFinal(true)
setVisibility(JvmVisibility::PUBLIC)
]
given that the initial exp.text was
'a
b'
I would like the generated field to look like this:
String text = "a \n b";
but instead it look like this:
String text = "a n b";
where as if I dont replace the newline characters at all then I get:
String text = "a
b";
which of course doesnt compile. The problem seems to be that xtend is evaluating the java string, so even though “\n” gets compiled to “\n” in java, in xtend it evaluates to “n”
How can I get xtend to not evaluate the \n so that it is preserved in the generated java string?
Turns out you need to replace with “\\\\n”, as the source code you’re writing will get through the lexer too and the backslash needs one more escape.
courtesy of Vlad Dumitrescu