Please, Java. Do Finally Support Multiline String Literals
I understand the idea of Java-the-language being rather hard to maintain in a backwards-compatible way. I understand the idea of JDK API, such as the collections, to be rather tough not to break. Yes.
I don’t understand why Java still doesn’t have multiline string literals.
How often do you write JDBC code (or whatever other external language or markup, say, JSON or XML you want to embed in Java) like this?
try (PreparedStatement s = connection.prepareStatement( "SELECT * " + "FROM my_table " + "WHERE a = b " )) { ... }
What’s the issue?
- Syntax correctness, i.e. don’t forget to add a whitespace at the end of each line
- Style in host language vs style in external language, sure the above code looks “nicely” formatted in Java, but it’s not formatted for the consuming server side
- SQL injection, didn’t we teach our juniors not to perform this kind of string concatenation in SQL, to prevent SQL injection? Sure, the above is still safe, but what keeps a less experienced maintainer from embedding, accidentally, user input?
Today, I was working with some code written in Xtend, a very interesting language that compiles into Java source code. Xtend is exceptionally useful for templating (e.g. for generating jOOQ’s Record1 – Record22 API). I noticed another very nice feature of multi line strings:
The lack of need for escaping!
Multi line strings in Xtend are terminated by triple-apostrophes. E.g.
// Xtend val regex = '''import java\.lang\.AutoCloseable;'''
Yes, the above is a valid Java regular expression. I’m escaping the dots when matching imports of the AutoCloseable
type. I don’t have to do this tedious double-escaping that I have to do in ordinary strings to tell the Java compiler that the backslash is really a backslash, not Java escaping of the following character:
// Java String regex = "import java\\.lang\\.AutoCloseable;";
So… Translated to our original SQL example, I would really like to write this, instead:
try (PreparedStatement s = connection.prepareStatement( '''SELECT * FROM my_table WHERE a = b''' )) { ... }
With a big nice-to-have plus: String interpolation (even PHP has it)!
String tableName = "my_table"; int b = 1; try (PreparedStatement s = connection.prepareStatement( '''SELECT * FROM ${tableName} WHERE a = ${b}''' )) { ... }
Small but very effective improvement
This would be a very small (in terms of language complexity budget: Just one new token) but very effective improvement for all of us out there who are embedding an external language (SQL, XML, XPath, Regex, you name it) in Java. We do that a lot. And we hate it.
It doesn’t have to be as powerful as Xtend’s multiline string literals (which really rock with their whitespace management for formatting, and templating expressions). But it would be a start.
Please, make this a New Year’s resolution! :)
Reference: | Please, Java. Do Finally Support Multiline String Literals from our JCG partner Lukas Eder at the JAVA, SQL, AND JOOQ blog. |
With a big nice-to-have plus: String interpolation (even PHP has it)!
>>what about string format or \s in java
Well, I can also write a parser in ANTLR and read external files. There’s always a way to achieve a goal. But this article is about convenience, which leads to developer productivity. In other words, let Java be a bit less of a “Blub language” (http://www.paulgraham.com/avg.html)
swift is open source and will be cross platform
so i want to know who is responsible to add languages like groovy to work in jvm ?
Why don’t YOU do it?
is there any tutorial to add languages to jvm ?
i programming in swift language
in swift and objective-c there is a great feature called extensions
is there is something similar in java?
extension example:
String{
class func isEmailFormat -Bool{
//some code
}
}
var foo = “name@domain”
var isEmail = foo.isEmailFormat()
No, Java doesn’t have extension methods.
Well… someone could actually make an Eclipse plugin (for example) to “translate” these multiline literals whilst building the workspace. :-)
They certainly could, but before you do that, why not just use another language that supports them. Like… virtually any other language (Scala, Groovy, Xtend, Kotlin, etc.)
Totally agree with Lukas.
It would be a real plus to have such feature. I myself have stumbled upon few of the scenarios to have json string in as formatted way directly inside the java code, which becomes tedious enough when the json string is pretty lengthy. Hence forth got to put that in another file and load or minify in a one liner but with tedious escaping of the spl characters.
https://github.com/benelog/multiline/wiki/Non-Maven-Java-project-with-Eclipse
import org.adrianwalker.multilinestring.Multiline;
public class MailTemplate {
/**
Hello,
You are receiving this email because
you have subscribed to our newsletter.
….
*/
@Multiline private static String msg;
public static String getTemplate() {
return msg;
}
}
I want to know why finalize method is declared protected not public in Object class?
What is use of marker interface and user defined marker interface is possible?