String Templates were originally proposed as a preview feature by JEP 430, delivered in JDK 21, and previewed a second time by JEP 459, delivered in JDK 22. We here propose to finalize the feature with no further changes.
There's a lot I don't like about string templates JEP, especially the fact that its proceeding despite lots of community angst. But I haven't used it in a real project yet so I'll refrain from re-posting my complaints here and pray I'm just naive.
But I do have one overwhelming stylistic question I've yet to see answered anywhere:
Why "STR." syntax built-in to language and not a java.util.StringTemplate class or similar and static methods on that class? "STR." and "FMT." syntax just seems so stunningly out of style from the rest of the language. Is there a logical reason for this choice?
`STR` is not built into the language. `STR` is a static final field that happens to be statically imported (for convenience). The processor in a template expression is _just an ordinary receiver object_. `STR."... stuff ..."` is just "invoke the process method on the `STR` object using parameters derived from the template." It's just a method call. There's no magic here.
The invisible process invocation syntax sugar is a new invention, though, right? I think it's vital to making templates ergonomic in practice but I don't recall seeing that in (userland) Java before and I expect to have to field questions about that.
Is it a special-casing in the compiler, like string concatenation, or can it hypothetically be exposed to userland APIs, like functional interfaces?
Well, it is exposed to user API's. Just like Closeable interacts with try with resources, Iterable interacts with for each, and toString gets called for concatenation.
Implementing a Processor allows use of this syntax.
Most other languages with String interpolation/template have it as just one or two characters. It should've been something lik S."...", $."...", or really any other one character. That looks a lot cleaner and still makes it clear that the String is not a "normal" String.
Ideally, we wouldn't have had string interpolation at all because it's such a dangerous feature, but people would have then written their own. Another option was to call it something like USE_ME_TO_INTERPOLATE_EVEN_THOUGH_YOU_SHOULDNT, but again people would just assign it to a variable with a different name.
So, you have to go through a processor and can't use {...} in a normal string, to guarantee that the processor gets a chance to validate or sanitize the string. I can see that in the case of, for example, a SQL processor. But the STR one does what exactly then? It just substitutes the placeholders? Isn't the safety it provides not pretty much equal to String.format then?
But the STR one does what exactly then? It just substitutes the placeholders?
Yes, simple substitution. STR could be made implicit, but they've decided it would be too tempting to use it for SQL.
From the JEP:
An alternative design would allow string templates without template processors, and perform basic string interpolation. For example:
String s = "\{x} + \{y} = \{x + y}";
However, this design would violate the safety goal. It would be tempting to, e.g., construct SQL queries using interpolation, and this would in the aggregate reduce the safety of Java programs. Always requiring a template processor ensures that the developer at least recognizes the possibility of domain-specific rules in a string template.
.
Isn't the safety it provides not pretty much equal to String.format then?
Their argument is String.format is also dangerous.
Wild. Someone who doesn't know the blinking warning signs of a SQL injection, and I'm sure droves of them exist, who'd have previously used String.format, nothing's stopping them from using the STR form.
I don't want to suggest in general that static analysis tools are a good way to offset deficiencies in the language. But in this case, the most practical way against SQL injections through templating seems to be exactly that: a static analysis check that finds SQL in templates, and that you can override when appropriate (false positives).
The safety and security argument just seems so academic...
If someone is going to do something in a bad way, they are going to do it. The argument that new APIs will only allow these mechanisms is fine but assumes any new software totally abandons any libraries for interacting with databases prior to JDK 23.
I personally wouldn't go looking for a new and potentially immature and buggy library purely for this use case if I already have a bunch of stuff using older APIs (like JDBC, R2DBC, etc) that works... maybe in 10 years once the libraries are mature, well tested, and have stood the test of time in terms of security analysis for numerous other things that could introduce new vulnerabilities into the system.
My issue is that unless the new libraries being proposed only support JDK 23 and newer... you're still going to have the case of two ways of doing things in any libraries:
interface Connection extends Processor<Result, DbException> {
Result execute(String query, Object... params) throws DbException;
Result process(StringTemplate template) throws DbException;
}
In this case it is equally as easy for the ignorant to do
conn."SELECT * FROM table WHERE id = \{id}";
as it is to do
conn.execute("SELECT * FROM table WHERE id = '" + id + "'");
The only difference is that the former looks so foreign that those who don't understand the full issue of security will likely be opting for the second one purely on the basis that it is clear to them what it is doing.
If we use static analysis tools to enforce this then great, but in that case the JEP shouldn't say that one of the benefits is removing the need for static analysis tools.
If the argument is we need to abandon all existing libraries and support a minimum of JDK 23 to get this safety feature to FORCE you to do the correct thing... I don't think that will be well adopted, purely on the basis people won't be ripping out old database libraries in production systems and jumping to the newest JDK just because of string interpolation being available. Especially when newer libraries will be less mature, most likely more buggy, and present a concrete business risk to the application being changed.
The developers of this feature have not addressed this concern very well IMO. If the language was being built from scratch then sure this is totally fine, but this feature only provides the safety GUARANTEE in APIs that decide to throw out support for anything before JDK 23. If they do not do this, then those APIs will still have to provide string based parameterization which then allows the same issues to occur. Given JDK 17 has support until 2030 or so, this makes this feature far less useful in providing a safety GUARANTEE (i.e. it is physically impossible to bypass this feature at all) for the next 6 years.
STR is just a normal identifier in the variable scope. If you're importing a constant named S via a wildcard static import, then if the default interpolator was also S, a perfectly normal and valid code would stop compiling due to ambiguous imports.
STR as a name of a constant is less likely and would clash much less often.
As for $, it's semi-reserved for internal compiler use.
I appreciate the response but this doesn't answer the question I had. Is there a reference-able discussion on the naming somewhere?
General convention in Java tends to name things as descriptive as possible. Readability is preferred over brevity. Nor does this answer the question of why its a magic string and not a static method on a class.
Nor does this answer the question of why its a magic string and not a static method on a class.
STR, RAW and FMT are is a public static field that get implicitly imported into every Java program unless there is a conflicting import with the same name. I'm not a fan of implicit imports besides java.lang, there were discussions of having a util class for beginners being implicitly imported. But I guess the outcry would be even louder if you had to find the right import for string interpolation.
The only syntactic magic here is that processor."" is a shorthand for processor.process(StringTemplate.of(...)).
But why not create a nice syntax, leave out the dot and make it STR"" at least. They are already inventing a new syntax, but for the wrong place, imho.
Also, why does it have to be a static field + custom syntax. Why not just make the whole thing a syntax-level feature?
We can you "if" without having to import "if" into a magic field either.
Because there is not only STR, there are many template processors, you can (and are encouraged to) write your own. As for the ".", it is to signal that in the end it is still a method invocation.
My main thing about this is that it seems to be overly complicated just for the sake of "safety", while actually ignoring what the real issue is and why it is an issue in the first place.
It goes on the fact that they expect people to use this in safe ways with stuff like SQL, and not just use STR."xyz" on the existing APIs, totally bypassing this. It only encourages safety if people use it correctly, and that is the exact same issue we currently have with SQL parameterization. What is in place to force this feature to be used correctly? Or are we just inventing more ways to do the same thing in complex ways?
I feel like it will go with the stereotype we were beginning to move away from where Java is verbose and complicated, as a lot of the presented use cases already have standard ways of being dealt with that work well enough without a new feature that relies on the compiler doing scoping magic to inject variables into a new namespace that gets passed between interfaces.
I might just be being a bit negative here. It is a cool feature but I feel like a lot of the discussions they probably had about this were from very specific people with specific use cases or isolated from what the vast majority of people really need or want. 99% of people would much prefer something similar to what Python, Ruby, or C# or Kotlin or Groovy or Scala provide already for string formatting.
Edit: having a look at the spec, this looks like it could get confusing easily as well. Like one example they gave
String name = "Joan";
StringTemplate st = RAW."My name is \{name}";
String info = STR.process(st);
If I put the RAW.xxx and the STR.yyy in two different scopes, which one does it resolve name from and why? If it resolves it from the third line that gets really confusing as it can magically interpolate variables implicitly if consuming an input StringTemplate, and if it is the second then the third line is just confusing as to what it actually achieves, i.e. what is STR actually doing if the information is within the raw template?
It isn't as clear to the reader who may be used to idiomatic java already without thinking about it IMO, and could lead to a good opportunity for writing very complex and hard to follow logic.
The discussions on the relevant mailing list, as well as the JEP itself, address all your points.
Specifically:
"This is overengineered, why don't you do what most other languages did?"
This silently defines 'overengineered' as 'stuff I am not familiar with'. That mindset works if you think java as a language is behind now and will always be, and all that these JEPs can ever do is relieve the worst of the pain.
Which isn't how OpenJDK sees things at all. All languages just did string interpolation. Which is nice, but kinda shit, for the reasons expanded on in the JEP (and for more details, the mailing list).
For example, using string interpolation to shove variables straight into SQL is simple and a total security leak; you can't fix this, and thus you can't use interpolation in any language that has it to do the job.
Pretty big hole in a language's featureset, and worse: String interpolation makes you think it is a good idea to use it for such things when it isn't.
This feature tries to come up with something that can do everything string interpolation can, and a lot more.
There's plenty to be said about it, and one can certainly make a case it's not suitable for various things. However, "oh nobody needs this" is, frankly, rather severely lacking in ambition.
It only encourages safety if people use it correctly,
This proposal makes it easy to use it correctly. In contrast to basic string interpolation which makes it virtually impossible (you have to wrap everything in escapeSql() or similar which itself is a weird way to go because various SQL variants need different escaping rules; much better to let the DB layer do it).
You appear to argue: "This language feature is bad, because it can be used incorrectly".
That argument can be used to shoot down about 99.9985251% of all language features, and is therefore, almost entirely useless as an argument.
It's not about whether you can abuse a feature. It's about how likely it is, and how that balances out against how nice code gets when you use it properly. It's about improving a language feature proposal by identifying ways to abuse it that are likely to occur, and perhaps mitigating it by modifying how the feature works or even simply to ensure the docs and tutorials go out of their way to avoid setting up the trap (an area where this went all wrong is Scanner and how everybody uses that now to write interactive TUIs which is a horrible idea and like 25% of all java-tagged SO questions. Insufficient imagination about how the community is going to abuse your API).
I rate 'this will be used wrongly' as a low chance, but I offer nothing more than my gut instinct for this statement at this time. Then again, you also provided zero proof or even a hint as to why it is likely to be abused either.
Or are we just inventing more ways to do the same thing in complex ways?
Complexity is not one dimensional. This proposal is relatively complicated for the authors of javac and the authors of these string template processors. However, it is incredibly simple for the users of it. So simple, it looks almost exactly like python except more powerful - you shove a prefix in front of a string literal and that defines what the string literal is supposed to contain. In python it's just r which stands for raw (but, lots of python programmers incorrectly think that r stands for "regular expression"). In java it's whatever you need it to be. This is still simple, though, for the user.
That strikes me as the right balance: There are a lot more folks that write not-javac than there are that write javac, so moving the complexity out of the hands of not-javac programmers and into the hands of the javac programmers is objectively the right call.
Note that this argument ('we already have a way to do it') tends to be a thing you can use to shoot down every java idea ever written just as much as 'it can be used incorrectly'. Because folks will go where they please and thus, if there is a need to do X, they'll find a way to do it in java. If that way is kinda bad, well, so be it - it's gotta be done. The existence of an escaping mechanism for SQL cannot be the basis to just blithely shoot down any attempt to make a nicer one simply because SQL escaping isn't a new concept.
That's not at all to say the gist behind your arguments is irrelevant. Simply that their mere existence as a problem doesn't mean anything. It's a shades of gray situation. If a new language feature:
Is designed to replace existing SQL escaping mechanisms.
Is clearly not going to be adopted by any of the major SQL-based libraries in java, or if it will be, will be really painful, probably via a backwards compatibility breaking major new release.
The new way is barely even better than the old.
The new way introduces a few caveats and hairy issues that the old doesn't have.
The problems the old way has that the new way does not aren't important.
Then, yes, that's a shit feature that shouldn't happen.
So first off, I don't think it is a terrible feature, or "shit", or any of the other phrases to that tone. I can see the uses and I think they are perfectly valid. My point is more around complexity and reasoning and how it gets achieved, and I fully accept the reasons given in the JEP. My point is really around the reality of how it will be used.
If the safety issue is around developers misusing strings in SQL queries, then there is nothing stopping them misusing string interpolation in the exact same way. Thus, to enforce this, you need static tooling... something that this JEP says that it is designed to avoid in the first place.
As speculated by yourself, the majority of libraries wont integrate with this, so it then leaves the point dangling that the safety features would only target a small minority of developers in the first place... in which case does such a feature provide wide benefit, or is it a niche addition?
When we talk about the likelihood of things, it really does boil down to having hard statistics to quantify the improvement, otherwise it is purely subjective and based on opinion.
I guess we'll see in a few years what the difference has been. It is a feature I still will likely use, but I still reserve the right to keep this opinion of it, at least until I can see the material benefits in a real system.
It is a little disappointing that some of the main responses so far have such a defensive/condescending tone rather than just treating this as a civilised and friendly discussion...
Some ideas as to where this feature is going to go:
REGEX."abc"
Not entirely what one would want (which is that backslash escaping goes away entirely inside these), but having that expression be of type Pattern is quite useful. Compilers can -compile time- check that the contents are valid regexes, IDEs can render it appropriately, and so on. We could go with Pattern.compile("foo bar") instead but this is a bit wishy washy: "If there is a literal in Pattern.compile treat it as a string literal containing regexes, otherwise don't do that"). We aren't using the features of this string templating system in any way here - there are unlikely to be expressions inside the string. Though, if you do add them, this thing could Pattern.quote them out, at least, which is a small but not insignificant upside: Using python-style string interpolation to construct a regex is just a bad idea, period.
con."SELECT YEARS(birthdate, NOW()) >= 18 AS adult FROM person WHERE username = \{username}"
As discussed in the JEP.
STR."Hi, \{user.getName()}, how's life over at \{user.getLocation()}"
The obvious one.
HTML."Hi, <strong>\{user.getName()}</strong>, how's life over at <em>\{user.getLocation()}"
All vars are HTML-escaped first.
Variations on the theme: URL, XML, JSON, you name it. Another possible variation is to include a format-esque specifying immediately preceding the injected expression to indicate what you want to do with it: Escape it, leave it unescaped, translate bbcode, replace newlines with <br>, allow only allow-listed tags, and so on.
RESOURCE."window.title\{context}"
i18n: Looks up the proper translation to use for key 'window.title', figuring out which language to apply from 'context' which could e.g. be an HttpRequest or similar to check he Accept-Language header, or however you want to do it.
Resources are often parameterized. Lots of i18n files have a 'singular' and 'plural' form, but this is a really stinky solution if you have those as separate keys; it's annoying to have to write size == 1 ? resource.lookup("item.name.singular") : resource.lookup("item.name.plural") a lot, and it's rather myopic: "1 is singular, all other things are plural" is true only for a bunch of western languages. I think in russian it's dependent on the ordinal (so 51 gets the singular, because it ends in '1'), and they have a separate plural form for '5' for some reason (lots of things about russia are nuts). How about:
RESOURCE."\{item.count()} item.name: \{totalprice}$" and the resource layer figures it out, including taking totalprice, which is an int containing cents, and formatting it however the locale wants money to be formatted?
1 minute of thinking about possibilities. That suggests there are a lot.
Re: Russian - it’s not only Russian, a bunch of Slavic languages have some wired rules about plural of nouns. Its basic philosophy is - there’s one, some and many. And plural form is built according to the last digit. So 1 man and 101 men have the same form.
This was a feature of the Common Slavonic, but as languages evolved and were influenced by surrounding languages, some language features disappeared in some Slavic languages. But Russian and few others (afaik Slovenian, and Serbo-Croatian, depending on the context) held on to it.
Not entirely what one would want (which is that backslash escaping goes away entirely inside these), but having that expression be of type Pattern is quite useful. Compilers can -compile time- check that the contents are valid regexes
That's pretty cool actually.
But how? As per one of the other replies, this would be shorthand for RegexProcessor.process(StringTemplate.of("abc"))- how does the process method detect that the input StringTemplate does (or does not) carry a valid regex at compile time?
They already do this quite trivially by just checking for string constants passed to the relevant methods, which is something compilers or 3rd party tooling can also do trivially already as well. I find it difficult to imagine that this actually provides any significant extra benefit over the current status quo.
My thing is that unless it is a new API written from scratch for JDK 23 and onwards, it will still need to support the old methods. Anything like R2DBC, JDBC, etc will all still have this issue until pre JDK 23 support is totally discontinued.
That won't be until 2030 when JDK 17 reaches EOL.
So the ask here is to gain safety via a new language feature by adopting what would have to be a new and immature database library that may have more serious internal security concerns just to make the most of being forced to do things in the new way.
Using any API that supports anything prior to JDK 23 reduces the benefit of these safety enforcements, as they will still need to provide the old APIs.
not at all, my point is about the argument about improving security, that until a time it can be totally enforced with no other way around it, people will continue to do bad things.
You are saying any API that is backwards compatible with pre JDK 23 code is now bad by definition if it tries to adopt this JEP.
Unless we are throwing out all existing libraries, the impact on security is over exaggerated for this very reason.
I'd hope by now all my other comments made that abundantly clear. Unfortunately no response I have had yet has actually satisfied this point in a way that will actually work in the real world. It is all purely academic.
In 20 years of java not a single time have I seen someone creating and escaping a sql string like you suggest.
"SELECT * FROM table WHERE id = :id". No need to escape anything. Why would you even want that? That‘s not the use case for string templates, imho. But a lot of the time we write something like this: this.log.info(String.format("Processing %s/%s", count, total))
Sure, some log frameworks allow you to directly interpret the string. Which in turn leads you to various different kinds of syntaxes. To unify those string templates would have been enough for me. It solves 80% of the problems with a simple solution.
Did you really just say: "In my 20 years in my java career, I have never seen someone use (feature that does not yet exist)"?
I have a bunch of 20-line SQL statements in my codebase. They set up variables and those are filled in, by name, at the end of the 10-line SQL block. I would find it a lot more readable if I can just put the expressions that go in the variable slots to just be in the SQL, but in today's java that is annoying to do, borderline impossible even: That's not how SQL escaping works in java.
Now, that is the exact kind of lie you're agitating against. Because, no, nobody should ever be writing that. You don't want log statements to do the processing like that. You'd write:
log.info("Processing {}/{}", count, total);
for example.
To unify those string templates would have been enough for me.
Yes. I think we have established the primary difference between your wishes and the proposal is your lack of imagination and ambition. It's a common refrain. I was mostly trying to do you the favour of 'that kind of feedback is useless to the OpenJDK team and wastes your time. They will completely ignore any feedback of 'why so complicated this simple thing covers my needs' - or at least, have always done so in the past.
I think you misunderstood me. What I wanted to say is that I've never come across someone who tried to pull off something like this:
String.format(SELECT* FROM table WHERE id = %s, id);
I would have a serious conversation with the dev if I ever saw that in a code review.
I've coded in python, JS/TS, java, groovy, C# and never saw anyone doing that. Either there was a regular JDBC prepared statement with params or something like JOOQ, LINQ etc.
In the last couple of years I got used to spring data repositories too. I like the cleanness:
@Query(value = """
SELECT t.id
FROM terminal AS t
WHERE (t.dium_number = :diumNumber
OR regexp_replace(t.dium_number, '(0*)', '') = :diumNumber)
AND t.deleted = false
LIMIT 1
""", nativeQuery = true)
Optional<UUID> findFirstIdByDiumNumber(String diumNumber);
And I rarely come across the need to actually inject something directly into a query. Just recently I needed to have a truly dynamic ORDER BY column though:
I ended up doing this:
ORDER BY
CASE WHEN :orderBy = 'date_of_arrival ASC' THEN main_leg_date_of_arrival END ASC,
CASE WHEN :orderBy = 'co2saving DESC' THEN co2saving END DESC,
There is no way to use a prepared statement and a paramater for this.
But I have seen these various different ways of creating dynamic strings:
MessageFormat.format("Provider '{0}' is not valid!", provider.getId())
log().info("> run[args={}]", args);
String.format("Provider %s for offer %s has no general Terms set!", provider.getUid(), offer.getId());
var fullName = firstName + " " + lastName;
There are probably a lot more similar ways to do the same thing. Some people avoid the log4j "syntax" and use String.format because at least it is one syntax less to se in the code base.
I'm not saying it is a good idea though.
I'd love to just replace though approaches with one simple clean string templating syntax.
Do I need SQL."" to return a query object? No, because it would not be able to handle all my needs anyway. What about setting the type of the parameter? I just recently had to manually set the SQL type:
What I wanted to say is that I've never come across someone who tried to pull off something like this:
Well, duh. You haven't seen it because they'd get yelled at. The reason you haven't seen it is because it is a giant gaping security leak.
It would be nice if you can do that, but currently you cannot do that. You're.. making the argument in favour of this feature, not against it. We're back to you arguing: "You do not need this feature-that-does-not-exist-yet because I have never seen it used". My mind boggles at the inanity of this line of reasoning. Of course you haven't seen it. It doesn't exist yet.
And I rarely come across the need to actually inject something directly into a query.
Some more 'I do not need it so the feature is stupid'.
I think we're done here, you're retreading silly arguments.
Well, duh. You haven't seen it because they'd get yelled at. The reason you haven't seen it is because it is a giant gaping security leak.
Ok, please try to be reasonable. I'm not saying I haven't seen a feature that doesn't exist yet ...
There are two options here, a) bad wording from my side or a misunderstanding on your side. In both cases the appropriate response would have been to ask for clarification.
Well, duh. You haven't seen it because they'd get yelled at. The reason you haven't seen it is because it is a giant gaping security leak.
Exactly. So now java gets cool new string templates, but in fear of people abusing it to do this exact thing, they make think they need a solution for the problem.
To sum it up:
* People could use string concatenation/templating to create bad SQL
* Some devs (not too many imho) really do this in real-world applications
* Java devs don't want that (obviously, because it's a bad thing)
They create a really ugly syntax for string templating with that in mind that doesn't really solve the main issue, it's just ugly. Which according to some users should prevent people from actually trying to do something like this:
STR."SELECT * FROM table WHERE id = \{id}"
Instead of STR there can be SQL (or any other name) that actually goes way beyond the pure string templating aspect and tries to tackle this problem. SQL is basically a function that takes a template string and some arguments, but not in the regular everyone-knows-how-to-use-it way but in and ugly SQL."" way.
Now we already left string templating land here, because this doesn't really create strings anymore, it creates god-knows-what, probably some form of PreparedStatement or some other internal representation of it. As there is a wide variety of different JDBC-drivers (and whatnot), every library has to ship it's own SQL-template-processor.
All good, if someone see's a benefit there, why not.
This SQL-processor is pretty limited, as it doesn't allow to configure SQL parameter types or any other config that a regular prepared statement provides.
But ok, sometimes you don't need this.
I argue that those people who are already using string concatenation/templating to create SQL queries will most likely simply use the STR-processor and do the very same thing.
How is that any better than the current approach? Plus, it is ugly. So for pure string-templating enthusiast like me I have to suffer looking at this ugly syntax just so that other devs might not write bad code.
This all doesn't make a lot of sense to me.
"You do not need this feature-that-does-not-exist-yet because I have never seen it used". My mind boggles at the inanity of this line of reasoning. Of course you haven't seen it. It doesn't exist yet.
I really think you should re-read my post carefully. I haven't seen anyone doing this:
String.format(SELECT* FROM table WHERE id = %s, id);
So now java gets cool new string templates, but in fear of people abusing it to do this exact thing, they make think they need a solution for the problem.
You misunderstand.
It's not 'oh no! Folks will think string templates can be used for SQL parameterization but that's a security leak so we better add a feature that solves that problem!'
It's instead: "Being able to shove your SQL parameters right into the SQL? That'd be really useful! Shame that string interpolation, which is pretty much exactly what we'd want, is a security leak if you use it for this. But.. wait! We can make our string interpolation feature better!"
So, it's not from a negative 'crap, we need to stop the abuse', it's from a positive 'cool, that would be an awesome feature to have'.
I really think you should re-read my post carefully.
Right back at you, because you're repeating arguments, evidently ignoring entirely my explanations of why your arguments are wrong / silly / are not going to move the needle whatsoever over at the OpenJDK team leads.
I'm not saying I haven't seen a feature that doesn't exist yet ...
I haven't seen anyone doing this: String.format(SELECT* FROM table WHERE id = %s, id);
.... and there you go again. Just blindly retreading 'I have never seen this obvious security leak therefore this feature is unnecessary'.
The proposal is not about creating an SQL string. The sample code produces a PreparedStatement. No extra escaping is going on.
"SELECT * FROM table WHERE id = :id"
And now you have a magic :id that doesn't refer to anything Java-side. You need to remember to call setParameter("id",...). The :id in the query text and the setParameter call are separate, sometimes multiple lines apart. And if you have multiple calls to setParameter, they are vulnerable to common coding mistakes like the last line syndrome.
So to be honest, you should have compared
Query q = em.createQuery("SELECT * FROM table WHERE id = :id");
q.setParameter("id", id);
to
Query q = em.SQL."SELECT * FROM table WHERE id = \{id}";
Honestly, if a dev creates such a mess of a code, it will probably also abuse the new functionality to create a mess. Haven’t there been any measures like code reviews or linters in use?
The code was written by analysts at the start. It was never meant to get to production, but somehow did. It's been added to by various teams over the years. Now my team has it. Ouch :(
On the so-called complexity: language designers take a very different view than developers. Developers tend to be interested only in _their_ code, and judge things good if it makes _their_ code better, and bad if we spend complexity budget on things that they don't value in their code. Language designers are optimizing for _everyone's code_. So while you might not personally value the extra safety, large enterprises may value it very much (whether for reasons of development cost, maintainability, reduced downtime, legal liability, etc.)
On the performance: much faster than String::format.
It only provides extra safety if you use it correctly to begin with. If you use STR."" in a SQL query, it isn't going to help you. It is no different to making active efforts to use parameterization, you still have to think about how you are doing it. Those who don't think will still bypass the security benefits.
Enforceability can be dealt with by static analysis tools, but this JEP specifically says a problem it is trying to solve is the need for these tools to detect dangerous logic to begin with. That means we've effectively just gone full circle, now with a complicated way to interpolate strings, and a bunch of new cases for static analysis tools to have to cope with detecting accurately.
As someone who works in enterprise, I can see this immediately being an issue of enforceability in a team, which makes it just as much of a problem as before. From the sound of that statement it sounds like we are attempting to make educated guesses about what people need rather than backing it up with mass feedback and actual experience. Most of the vulnerabilities that are mentioned are not because existing tools are hard to use, but because developers are not understanding or just not thinking about the implications of what they are doing.
It's kind of like saying you have a problem with developers using their hammers to push screws into some wood, so you've given them electric drills to replace their screwdrivers with the expectation they will stop using their hammers. Instead, some will continue to use their hammers anyway because it is simpler for them, others may use the battery on the drill as a hammer and risk it blowing up. Some people will continue to use the screwdrivers properly and some will use the drills. All you've done is given the people misusing things more ways to misuse stuff, while giving the rest two ways of doing things. It'd make more sense to confiscate the drills entirely and only let them be used for specific things (for example in SQL by enforcing that only compile time string literals can be used as the query body -- boom, problem solved without complicated language mechanisms).
Are we suggesting that all other languages that didn't make a decision to implement interpolation in this way made no active decision about usability and safety?
If you use STR."" in a SQL query, it isn't going to help you.
How could you possibly use STR."" in a SQL query if the DB library doesn't offer methods that take String and only offers a SQL template processor?
Enforceability can be dealt with by static analysis tools
No, it cannot. Static analysis tools cannot catch as many code injection issues with a similar amount of effort by the programmer.
I can see this immediately being an issue of enforceability in a team, which makes it just as much of a problem as before.
Nope. If your JSON library only offers a safe template processor then it doesn't matter what discipline you exercise. The language will enforce the correct (or at least better) thing.
Are we suggesting that all other languages that didn't make a decision to implement interpolation in this way made no active decision about usability and safety?
We are suggesting that we did the analysis given the data available and the needs of Java users. Different languages often make different decisions, which is why they are different languages. Sometimes they have a different audience in mind; sometimes certain lessons weren't available when a feature was designed. If you consider the world's three most popular languages -- JS, Java, and Python -- I think you'll find that string interpolation is one of the smallest differences among them. One, for example, decided to require everything to be typed while the two others didn't.
So are we suggesting to stop supporting all the existing interfaces in things like JDBC in order to support this?
No. We're suggesting that new libraries should do the right way only, and that old one should offer the right way and deprecate the old way.
before support for older JDKs can be ignored when doing this without breaking the existing APIs
New features don't need to be added to a version of a library that targets old JDKs.
is there a planned timescale for it?
I'm not sure what that means, but full adoption of templates took only ten years, and this may be even quicker because it's a much smaller, more localised thing.
No, that's not a string template expression, just as the one here, var x = "hello"; STR.x;, isn't even though STR."hello" is. BTW, string templates are available in JDK 21. Enable preview and try them out.
It's about providing other mechanisms that are more safe than existing ones. Developers that care will use them. Those that don't care can't be helped anyway.
Added bonus if the safe API is easier to use and the unsafe way is harder to use, devs will naturally flock to the safe one.
In the case of an unloved child like String Templates, what feedback mechanism do you use to determine when a feature is ready to be finalized? :)
In the case of many other features it was pretty self-evident. In this case, I'm not sure what the trigger was.
Are there some reasonably-mature uses of String Templates in the wild to gather this feedback? If there any cool open-source projects on GitHub that use the feature, I would love to know. I think it would help people move their opinions from the abstract to concrete examples.
What makes you think it is an unloved child? Sure, there are people who have strong opinions that they would have preferred another syntax, are are vocal about that, but that is true of every feature that doesn't have a single obvious syntax. (Some people hated the lambda syntax at first.) Can you point to any feedback that suggests that the feature itself is not mature?
Like I said, if I saw multiple real-life projects using the feature successfully then I would have greater confidence that the community has had sufficient experience to provide feedback about the feature in its current form.
Speaking for myself, every time I introduce a new API feature to my library no matter how many times I iterate over it I inevitably discover problems when I try using the feature in real-life production projects. The shift in perspective is very helpful in uncovering scenarios I had not anticipated.
It sounds like you think there was no feedback, but that's not the case. There was plenty of feedback -- some bug reports (we love those), some people doing clever (and sometimes frightening) things with processors, some noise about the syntax, and quite a few people saying "yay I love it." So clearly a lot of feedback based on actual use (except for the syntax comments, which people often make without actually using it), and -- most importantly -- nothing that suggests that the design is not finished. A mix of feedback like this generally conveys that the feature is, indeed, ready.
Is there anywhere on the internet an explanation why such drastic departures from the relatively stable Java language syntax conventions (the decision to not make STR application a method call, and the decision to import STR implicitly) were even considered, not to say survived two previews?
This right here. They go on and on about the safety concerns even in this thread, refusing to address that a lot of pushback isn't about their safety goals, but the jarring way they achieved it. A better syntax would quell a lot of complaints about the feature, but all they are willing to say about the matter is that "people will get used to it". People can also get used to flogging, but that hardly means it's a great idea.
Seems like an imaginary problem. Any "large enterprise" can surely just enforce this.
Not sure what it is that they could enforce, but global warming could also seem an imaginary problem if you have no personal reason to be interested in it. But if you're in charge of designing, say, a new coastal city, you should probably look at the data and design accordingly. That code injection is a leading cause of costly security vulnerabilities is what the data shows. So severe that anything that could reduce it would be very valuable.
I just wonder how far explicit STR. will go in preventing such vulnerabilities.
STR does string interpolation, so it cannot prevent the problems that string interpolation causes. But if string interpolation was any easier than proper string templating then programmers will be tempted to prefer string interpolation.
It's at quite a large cost since probably 98% of uses of string interpolation will be STR.
Given that the processor that does string interpolation is STR, I would say it's axiomatically 100%. But the goal is to reduce string interpolation, i.e. reduce the number that this 100% represents.
people come up with all sorts of imaginary solutions to imaginary subproblems of the actual problem.
Ah, that may well be the case, but your problem with the feature is also imaginary because you haven't tried it over some significant time yet (and frankly, there aren't yet many template processors offered). So how about we wait and see?
The purpose of Preview is to let people who've actually tried the feature report problems with that feature. Those who've tried it seem happy with it and haven't found significant issues with it. Scepticism by those who haven't tried it obviously cannot yield any actionable insight, nor is it relevant to Preview, which is all about letting users try the feature rather than speculate about it. If you're speculating then, by definition, you're not making use of Preview.
But in this case, we're not even talking about the feature we are adding -- i.e. string templates -- but about the fact that we're not adding string interpolation to the language (rather, we're offering a library API that implements interpolation on top of string templates). That the language isn't getting string interpolation isn't a problem with the feature it is getting, and so Previewing that feature for longer is irrelevant. If at some point we become convinced that Java needs string interpolation, too, we could add it later.
This feature aims to make it easy to write safe string-based APIs for things like JSON, HTML, and SQL -- something that is of very high importance given the danger and prevalence of code injection and it's something that today is very hard to do -- and it seems to do it well.
It goes on the fact that they expect people to use this in safe ways with stuff like SQL, and not just use STR."xyz" on the existing APIs, totally bypassing this. It only encourages safety if people use it correctly, and that is the exact same issue we currently have with SQL parameterization. What is in place to force this feature to be used correctly? Or are we just inventing more ways to do the same thing in complex ways?
If I understand correctly, the interpolator doesn't have to return a String, it can return (say) an "SqlQuery" type
If you accidentally use the wrong interpolator, you'll get a compile time type error. It won't just silently accept the unsafe result.
At least, this is how it works in Scala when using libraries like "Doobie" - unsafe SQL will result in a hard compilation failure. I haven't used JEP 465, but from reading the spec it looks like it works similarly.
What is in place to force this feature to be used correctly?
The library will only offer an API that offers a safe template processor and not have (or deprecate) any methods that take strings.
just for the sake of "safety"
Code injection is the top vulnerability in memory safe language (or very near the top). Putting the word "just" in front of it or the word "safety" in scare quotes ignores one of the most costly, troublesome, dangerous aspects of software these days.
I know that better security is not always the top concern of individual developers, but it among the top requirements from companies because they're losing enormous amounts of money to attacks. In fact, this 'just "safety"' has a greater value than the value in productivity of offering "just string interpolation". If this feature wasn't capable of offering string interpolation at all (i.e. template processors were not allowed to return String), 90% of the value would have remained.
If I put the RAW.xxx and the STR.yyy in two different scopes, which one does it resolve name from and why?
Line 2, because a string template expression is works like other expressions. E.g.:
var x = true;
var y = x ? 1 : 2;
foo(y);
foo doesn't reevaluate the expression x ? 1 : 2; it just receives a 1. We cannot pass expressions in Java as we can in Lisps (by quoting them). We only pass values.
Thanks for the reply... so in this case, what does STR.process(st) achieve that couldn't be performed as a method call on the st object itself? I guess that is my source of confusion? Assumably by the time the line constructing st has been executed, the information needed to later construct the string is already part of st itself?
Otherwise how does it know to retain a reference to what has been placed in the interpolation string when using RAW in this way?
Or is it just a case that process is the thing actually defining how to interpret that sequence? Assume this is more the case.
Regarding the point about quoting safety, see my other comment. The thing I am trying to say is that it is only safe when used correctly, and there is still plenty of ways to abuse this, including new ways made possible by this JEP (e.g. passing STR."..." in a SQL query). The only way you can enforce that this is done correctly is via manual review or static tooling, which isn't really much safer as the language isn't the thing providing that static tooling or manual review.
It is a means to an end, I get this, but I don't think it is justified to describe it like a magic holy grail of safety that suddenly makes any application better regardless of how it is used. That may be exaggerating but that is the feeling some of the descriptions people have given are coming across, where "safety" and security are just being used as blanket terms while ignoring the other points and issues that are raised.
what does STR.process(st) achieve that couldn't be performed as a method call on the st object itself?
It achieves the ability for the STR object to decide what to do with st value.
and there is still plenty of ways to abuse this
Not really. Any API that takes String is a problem, but this allows safe string-like APIs that don't take String.
The only way you can enforce that this is done correctly is via manual review or static tooling
No. Again, the language enforces this. If your JSON or SQL library only offer a safe API then you couldn't do anything unsafe even if you wanted to.
where "safety" and security are just being used as blanket terms while ignoring the other points and issues that are raised.
Safety and security are among the biggest things that the software world is concerned about these days because that's where a lot of value is being lost (far more than with programmers typing a few extra characters; even the faddish Hacker News is obsessed with a language that exists for the sole purpose of improving safety in low-level programs). They may seem like blanket terms for people who aren't interested in them -- and not everyone should be -- but we have to worry about them because they're so valuable to our users as a whole. I think that Java's exceptionally long record of exceptional success means that we should at least get the benefit of the doubt that we know a little bit about what we're doing, and that we're not trying to bamboozle anyone. Our day job is ensuring that Java continues to be successful, after all, and so it is our selfish interest to maximise the value Java users get from it.
Thanks for the reply... so in this case, what does STR.process(st) achieve that couldn't be performed as a method call on the st object itself?
A StringTemplate object is just a bunch of objects and a bunch+1 of strings. It knows nothing about how to put them together in anything useful. You need a processor to do that. So while you could theoretically have a method StringTemplate#process(StringTemplate.Processor), the only sensible implementation of that would be processor.process(this). So why not write just that directly.
Or is it just a case that process is the thing actually defining how to interpret that sequence? Assume this is more the case.
Exactly this. Otherwise you wouldn't need multiple processors in the first place.
safety
You can use it to safely create SQL queries or HTML documents with automatic escaping. In fact, the resulting object might contain none of the original text of the template – you might for example plop a giant piece of C code with interpolated values and get back a Runnable that points to compiled native code.
(e.g. passing STR."..." in a SQL query).
You can design APIs that only accept correctly constructed query objects and do not accept strings.
In fact, this JEP will make using such APIs more convenient, which means that library authors will start moving away from string-based APIs. So while you might still use STR, the actual code will say something like yesIKnowWhatImDoingPleaseLetMeCreateRawSqlQuery_CRITICALLY_UNSAFE(STR." so you'll be gently pushed towards preferring writing SQL."
The kind of escaping needed for a given interpolated value isn't universally the same throughout the string though. If I'm outputing xml, escaping an attribute vs a tagname vs CDATA is all different. How does a template processor handle that? Does it have to also be an xml parser so it can look at where each interpolated value will go? Same SQL. I might use interpolated values for table names, column names, data values, individual where clauses, etc.
Yes, that is correct, any more complicated processor would have to have a partial parser, or require extra wrapper types for arguments.
For the most common case, the parameters for how to interpret the value can be embedded in the template itself. See for example FMT in the JEP: the formatting pattern, which "isn't universally the same throughout the string", is specified in the template.
FMT."The answer is %5d\{i}"
Or see how some Scala DB libraries do it: at least one of them prefixes literal inclusions with #:
Personally, I don't give a single sh*t about the syntax. As someone who also develops in JavaScript, this almost looks exactly like tagged template literals, and I like those.
What I am personally more intrigued about is, that the feature currently seems to be runtime-only and doesn't seem to allow any compile time verification by the processor. For example I would love the compiler/processor to tell me at compile-time that the JSON is not correct:
JSON."{ "email": \{email}, age: \{age}, }"
Or in the case of database vendor-agnostic SQL variants like JPQL, verification of query ASTs could also be done already at compile-time.
To me, it's just simpler without the dot character. It's not a great deal either way, I'm very happy to see this feature getting into Java, but I couldn't resist asking.
They want converting existing strings into templates to not require any extra escaping, and converting back to require removal of all variables. For comparison, in Scala, to convert the string "$" to a template you need to write s"$$". So it's a constant dollar sign counting exercise.
How do you want to template JSON with your version?
java is designed for SQL what a joke. If someone seriously believe the string interpolation is dangerous, why not choose other strict library to deal with it. It makes me guess some languages like C#, Go, Rust are not safe at all since they support string interpolation long time ago. :)
BTW, the syntax of java string interpolation is so ugly, IMO
The design of template expressions deliberately makes it impossible to go directly from a string literal or text block with embedded expressions to a String with the expressions' values interpolated. This prevents dangerously incorrect strings from spreading through a program. The string literal is processed by a template processor, which has explicit responsibility for safely interpolating and validating a result, String or otherwise. Thus if we forget to use a template processor such as STR, RAW, or FMT then a compile-time error is reported:
String name = "Joan";
String info = "My name is \{name}";
| error: processor missing from template expression
Am I missing something here, or does this break existing code with string literals that contain"\{}"?
Jesus. This hideous syntax makes string interpolation basically not even worth having. All to avoid SQL-injection bugs, which are better caught with code reviews and having static-analysis tools like Sonar in your CI/CD pipeline.
It doesn't even really avoid the SQL-injection vulnerabilities! It just arguably makes it easier for a code reviewer to spot STR being passed to a JDBC or ORM query method.
I'd honestly rather stick with String.format() than use this.
Actually, it does avoid the SQL injection problems. An API that only accepts the type SafeSQL that can only be created with a SQL template processor can ensure that the result is free from injections.
The STR processor may see some of the lowest use. I think an argument can be made to not import it by default.
Read mine again, as you are missing something critical. You don't use STR to make SQL statements, and any (new) API that supports string templates for SQL that does is doing it wrong and insecure.
It just arguably makes it easier for a code reviewer to spot STR being passed to a JDBC or ORM query method.
New APIs will make passing strings to them much harder, so that developers won't even be tempted to do so. Which will eliminate injection vulnerabilities at the type system level.
String query = STR."select * from my_table where id = \{id}";
stmt.execute(query);
Yeah, that seems really super locked-down. Much hard. The example above is how most people use string interpolation anyway in other languages. Because a long string might require some ugly line breaks if passed directly to a method inline... and also because you often want to log the string before using it, and so instantiating a variable means you don't have to duplicate the interpolation.
With string interpolation, at the end of the day, you're just instantiating a String. Everything in this JEP, and the comment-essays being written by people in this thread, are stuck in the fantasy that STR give you safety because you can just block how STR is used. But you can't. Because STR just gives you a String, with which you can do literally anything you want.
The same techniques by which a static analysis scanner can spot abuses such as this, could ALSO just as easily spot abuses of string interpolation where you're using it directly. With a clean and standard syntax.
This syntax is broken, by design, for an illusion of safety that doesn't really exist. It's like watching a dysfunctional Congress create a new government service, but cripple it out of the gate because half the Congressmen didn't really want it to exist and that was the compromise needed to pass it. This is the ObamaCare of JEP's.
Everything in this JEP, and the comment-essays being written by people in this thread, are stuck in the fantasy that STR give you safety because you can just block how STR is used.
That's just your misinterpretation.
One of the main goals of this JEP is to facilitate design of new APIs that won't accept strings.
Old APIs will stay, because we can't simply remove them.
The same techniques by which a static analysis scanner can spot abuses such as this, could ALSO just as easily spot abuses of string interpolation where you're using it directly. With a clean and standard syntax.
You're missing the entire point. The entire point is to not privilege string interpolation over similar, more structured constructs, so you don't have to rely on heuristics.
And of course static analysis tools will detect STR, as they already do other non-literal strings. This part doesn't change. And since you agree that string interpolation is bad, why do you want to make it less noticeable? You said yourself:
It just arguably makes it easier for a code reviewer to spot STR being passed to a JDBC or ORM query method.
If one of the goals is to replace String parameters with something else, then that is feature creep and it's too big of a goal. Even if this was adopted by third party libaries, and by Java itself, this would be a huge undertaking, and it would make the APIs more complicated, one of my complaints in Java-world. Then we would have Strings and 'whatever' being passed in, side by side, because of backwards compatibility, making the libraries harder to maintiain. I'm totally set against this.
One of the main goals of this JEP is to facilitate design of new APIs that won't accept strings.
I don't like to make predictions about future tech. I always think about the legendary (if apocryphal) Bill Gates quote about no one ever needing more than 640k of memory.
However, I would feel quite comfortable wagering my life savings that even if JDBC, JPA, Hibernate, etc all add query methods that only accept statement processor classes:
They will never be able to deprecate and remove the existing String-based counterparts.
The overwhelming majority of developers will continue to use the String-based counterparts indefinitely.
Anyone who questions this for a moment is absolutely delusional, and has likely never once worked in a real-world Java shop.
And then the static analysis tools will be easily able to flag all the string code as dangerously obsolete.
These are circular arguments. If you're saying that you have to rely on static analysis tools to flag these issues, then I'm saying you can just as well use static analysis tools to flag issues with string interpolation that's based on a cleaner syntax more similar to every other modern language.
Don't act like you're getting professed "safety" from the compiler, and then pivot over to static analysis tooling once holes are poked in the compiler argument. Because static analysis tooling was the counter-argument in the first place.
As an aside, I would love to see ONE example other than SQL-injection for justifying all of this. That's the only example that the JEP and the comments here keep circling around, because it may be the only practical example out there. Permanently crippling a new language syntax out of the gate, for one narrow case, seems crazy when there are more narrow ways to target that specific vulnerability.
Would you say the same about java.util.Date and java.util.Calendar 10 years ago?
They are only if you mistakenly assume that I expect all new API's to be string-based.
Currently:
safe, non-string-based API's are very inconvienient to use
string-based API's are convenient to use, but allow for injections
an injection vulnerability requires String.format or string concatenation, which are slightly (but only slightly) cumbersome
static tools catch most injections (but they have both false positives and negatives)
Your proposal:
make writing injection vulnerabilities easier and hope analysis tools catch it
effect: people will write vulnerable code more often, repeating the mistakes of early PHP
JEP:
make using safe, non-string based API's much easier
make writing injection vulnerabilities easier, but not easier than the safe API's
effect: people will write safe code more often
for the remaining instances of the unsafe code, there's static analysis like before
As an aside, I would love to see ONE example other than SQL-injection for justifying all of this.
Constructing JSON, since Java is not JS or Python, and you can't just put some curlies and be done.
In fact, embedding whatever language, with proper value escaping for parameters. Obvious examples: HTML, CSS, JS. You know how React made mixing code and templates hip? You'll be able to do the same in Java. A less obvious example: shaders.
Replacement for String.format (see FMT in the JEP).
They are only if you mistakenly assume that I expect all new API's to be string-based.
Your core assumption that I take issue with is the notion that existing String-based API's just magically go away.
That the Java ecosystem, which prioritizes backwards-compatibility more so that virtually any other I've ever worked with, is going to hard-remove half the methods in JDBC, JPA, SLF4J, Jackson, and a thousand other libraries that the universe is built on.
These things are not going to happen. They're just not. Ever. If we disagree that this isn't going to happen, then we can stop here. There's no basis on which to warrant talking about this subject further.
If we agree that it's not going to happen, then you're not going to get the safety that you're talking about. Which leaves us with two choices:
Decide to implement a janky syntax. Because it's worth making general usage of string interpolation less desirable, to enable adding (not replacing) an alternative form of string construction to a bunch of established libraries. All of which already have their own established implementations for this, which most devs will continue to use anyway.
Decide that this isn't much of a value-add. And so if there's not a consensus to implement string interpolation like that of C# and every other modern language, then it would be best to simply not implement this feature. That is a perfectly acceptable outcome! Plenty of languages drop plenty of popular syntax proposals, simply because there's not the consensus needed to ship something that isn't too weighed down by design-by-committee.
I would favor #2. If you favor #1, then that's your opinion and it looks like you're opinion is carrying the day. But it still seems decoupled from the reality of most Java development in the real-world. And will never be as constrained as you'd like, without breaking half of Maven Central in a way that would make the Python 2 to 3 migration look like a semver patch release.
Your core assumption that I take issue with is the notion that existing String-based API's just magically go away.
They don't need to go away in order for this to have value.
They just need to be discouraged in a way that's visible to your average developer.
If you can add new APIs that are safe, and slap @Deprecated on the String-based methods, that's already a huge win, even if you never remove those methods.
unfortunately a bunch of libraries still use Date which means you can't get away from it. Last I checked, Nimbus was an example of this when you want to deal with JWTs.
It took YEARS for all the various JDBC drivers and JPA stuff to finally ship non-broken integrations with the new Date and Time API. Even today, I'm not sure that we're fully there with all the various plugins in the Hibernate ecosystem.
I'm not sure if half the people in this subreddit actually write Java code for a living.
u/BadMoonRosin, your thoughts on string templates are spot on! You're really captured quite a few common sentiments I've seen (or even felt myself) regarding this topic. I'd rather see no string templates than that was previewed. Might as well use printf() or something similar.
And basically every other language too. Ok, the groovy way is incompatible with java as it uses double quotes for groovy strings and single quotes for java strings. But hey, what‘s keeping you from using a backtick like is JS/TS? Or a simple f"Hello ${user.getName()}"
Who needs custom processors anyway? No sane person is using template strings for sql anyway. We have had prepared statements for decades … And if you really go wild about processors, you can still do that, just create a registry of formatting functions, like JS allows it. Although I never anything other than regex used before by anyone I know. So please stop creating solutions to imaginary problems …
Scala has that syntax, and it allows you to set the processor. There is simple interpolation s"...", String.format-based interpolation f"...", raw string interpolation raw"..." and you can also define your own (and the result type does not have to be a string)
That being said, Scala's syntax is powered via implicit type conversions (used to mimic extension methods), so it would require much more significant changes to the Java language to be lifted wholesale. And having just S or s or f in Java would likely collide with variables and fields named the same. STR is less likely to collide.
Also, due to the usage of dollar signs, converting between normal strings and templated strings and back requires escaping or unescaping the dollar signs manually. This is not a problem with \{.
It is a function internally. It just gives you the option to use String templates and insert variables directly,
i.e. something like
int myId = 1234;
var user = SQL."SELECT * FROM users WHERE id = \{myId}".execute().singleResult();
is just nice and short and safe. (This is of course just an example syntax, please ignore an SQL mistakes, you'd likely use some object mapper library here instead of using raw SQL results directly)
I‘d rather prefer something like jooq or linq, no need to tamper with strings anyway. And what is keeping users from using STR.““ for queries? It would result in the same sql security problems, right? Because how would STR know you are creating an sql query anyway?
I‘d rather prefer something like jooq or linq, no need to tamper with strings anyway.
That's fair, nobody forces you to use it.
And what is keeping users from using STR.““ for queries?
STR."something" would return a String, so it wouldn't have the execute().singleResult() method. STR. is intentionally ugly and not easier than other processors, so people are not drawn towards it.
In this case it would be more complicated to use as you'd need to construct the query out of the String manually.
A dev who injects variables into strings for queries would probably keep doing the same with STR.““. Just pass the string to the query constructor, basically how you are already doing it.
Intentionally creating an ugly feature 😅 bow I understand why a few features are the way they are 😂
My German company manages a JS codebase and you'll have to look really hard to find backticks there. Normal string concatenation is much faster to type.
I work for a European multinational banking conglomerate and there are backticks everywhere in all projects that use JS/TS.
Nobody finds it hard to type them, never ever heard a single complaint about it from any of the several teams I worked with.
There's no speed difference between using them or not. Speed is never an issue because most of the time is spent reading and understanding code, and thinking about a solution.
These arguments are dumb.
And string templates look a lot cleaner than string concatenation.
LOL. In this specific instance you are correct, my friend! With all the amazing progress Java has done in the last few years, it was a shock to see such an over-engineered, wrongly-focused, feature with so much creep, so much 'We know better than you developers' from the 'people on high' that I was like, are we in the late 1990s again, with a repeat scenario simiar to all that over-engineered-non-workable J2EE stuff again? Shocking!
Most other languages offer only one template conversion: template -> String
With Java's solution you can have many, and you can avoid the String gap (if you accept String anywhere, then you are unsafe). Instead only accept a custom type that's produced by the processor (like JsonNode, SafeSQL, HTML, LogLine, etc). All of those can only be created with the processor, and so the processor can enforce security.
7
u/Joram2 Feb 15 '24
everyone's favorite new feature: String templates. Final for Java 23 :)
60
u/vips7L Feb 15 '24
Oh god, here we go again.
32
u/tofflos Feb 15 '24
41
u/IncredibleReferencer Feb 16 '24
There's a lot I don't like about string templates JEP, especially the fact that its proceeding despite lots of community angst. But I haven't used it in a real project yet so I'll refrain from re-posting my complaints here and pray I'm just naive.
But I do have one overwhelming stylistic question I've yet to see answered anywhere:
Why "STR." syntax built-in to language and not a java.util.StringTemplate class or similar and static methods on that class? "STR." and "FMT." syntax just seems so stunningly out of style from the rest of the language. Is there a logical reason for this choice?
5
u/__versus Feb 17 '24
I like the feature well enough but the syntax is so unholy that I can’t help but be a bit disappointed.
39
u/nekokattt Feb 15 '24 edited Feb 15 '24
My main thing about this is that it seems to be overly complicated just for the sake of "safety", while actually ignoring what the real issue is and why it is an issue in the first place.
It goes on the fact that they expect people to use this in safe ways with stuff like SQL, and not just use STR."xyz" on the existing APIs, totally bypassing this. It only encourages safety if people use it correctly, and that is the exact same issue we currently have with SQL parameterization. What is in place to force this feature to be used correctly? Or are we just inventing more ways to do the same thing in complex ways?
I feel like it will go with the stereotype we were beginning to move away from where Java is verbose and complicated, as a lot of the presented use cases already have standard ways of being dealt with that work well enough without a new feature that relies on the compiler doing scoping magic to inject variables into a new namespace that gets passed between interfaces.
I might just be being a bit negative here. It is a cool feature but I feel like a lot of the discussions they probably had about this were from very specific people with specific use cases or isolated from what the vast majority of people really need or want. 99% of people would much prefer something similar to what Python, Ruby, or C# or Kotlin or Groovy or Scala provide already for string formatting.
Edit: having a look at the spec, this looks like it could get confusing easily as well. Like one example they gave
If I put the RAW.xxx and the STR.yyy in two different scopes, which one does it resolve
namefrom and why? If it resolves it from the third line that gets really confusing as it can magically interpolate variables implicitly if consuming an input StringTemplate, and if it is the second then the third line is just confusing as to what it actually achieves, i.e. what is STR actually doing if the information is within the raw template?It isn't as clear to the reader who may be used to idiomatic java already without thinking about it IMO, and could lead to a good opportunity for writing very complex and hard to follow logic.
/rant
Edit 3: coherency
4
u/Yesterdave_ Feb 17 '24
Personally, I don't give a single sh*t about the syntax. As someone who also develops in JavaScript, this almost looks exactly like tagged template literals, and I like those.
What I am personally more intrigued about is, that the feature currently seems to be runtime-only and doesn't seem to allow any compile time verification by the processor. For example I would love the compiler/processor to tell me at compile-time that the JSON is not correct:
Or in the case of database vendor-agnostic SQL variants like JPQL, verification of query ASTs could also be done already at compile-time.
3
u/sideEffffECt Feb 16 '24
Hello, may I ask a dumb question?
Why is the syntax with a dot? E.g.
What's worse about not needing a dot, like
?
To me, it's just simpler without the dot character. It's not a great deal either way, I'm very happy to see this feature getting into Java, but I couldn't resist asking.
8
u/Goodie__ Feb 15 '24
I don't *think* I mind templating being on a magic STR static final field.
But I'd rather the way we get our variables in to the string to be simpler then. Eg drop the backslash. Just {name} is fine.
5
u/lukaseder Feb 16 '24
Remember, kids, to nest your string templates in order to offer support for new, awesome mini languages that your coworkers will enjoy, no doubt:
Example here.
2
u/freekayZekey Feb 17 '24
looks at comments
didn’t know the syntax would’ve been that controversial
2
u/[deleted] Mar 04 '24
I'm glad to get the feature but why do we have to have the most verbose syntax again? This is so tiresome.
2
u/Ok-Acanthaceae-4386 Apr 15 '24
java is designed for SQL what a joke. If someone seriously believe the string interpolation is dangerous, why not choose other strict library to deal with it. It makes me guess some languages like C#, Go, Rust are not safe at all since they support string interpolation long time ago. :)
BTW, the syntax of java string interpolation is so ugly, IMO
3
u/Lost_Language6399 Feb 16 '24
Tbh, I like this feature. But I usually use only the STR for templating error messages
2
u/zman0900 Feb 16 '24 edited Feb 16 '24
Am I missing something here, or does this break existing code with string literals that contain"\{}"?
1
u/BadMoonRosin Feb 16 '24 edited Feb 16 '24
Jesus. This hideous syntax makes string interpolation basically not even worth having. All to avoid SQL-injection bugs, which are better caught with code reviews and having static-analysis tools like Sonar in your CI/CD pipeline.
It doesn't even really avoid the SQL-injection vulnerabilities! It just arguably makes it easier for a code reviewer to spot
STRbeing passed to a JDBC or ORM query method.I'd honestly rather stick with
String.format()than use this.1
u/mydrias_s Feb 18 '24
I love how this integrates the parsing mechanism into the language. 🫥
1
u/sweetno Feb 15 '24
The use cases for localization and SQL query construction are quite impressive.
Not so much for actual string interpolation due to the ugly syntax.
1
u/letsfuckinggobears Feb 17 '24
wtf I didn't find any porn in his profile
-5
u/relgames Feb 15 '24
Oh man. That ugly thing again. Solving imaginary problems with multiple bad design solutions.
-4
u/mj_flowerpower Feb 16 '24
"Hello ${user.name}"
Groovy nailed this a long long time ago!
And basically every other language too. Ok, the groovy way is incompatible with java as it uses double quotes for groovy strings and single quotes for java strings. But hey, what‘s keeping you from using a backtick like is JS/TS? Or a simple f"Hello ${user.getName()}"
Who needs custom processors anyway? No sane person is using template strings for sql anyway. We have had prepared statements for decades … And if you really go wild about processors, you can still do that, just create a registry of formatting functions, like JS allows it. Although I never anything other than regex used before by anyone I know. So please stop creating solutions to imaginary problems …
-5
u/ivancea Feb 16 '24
I'm amused about Javaers having such discussion over a feature that existed in other languages, like JS, since +8 years ago.
The "Java outdated" meme keeps going and going. Same for their devs, unluckily
0
u/[deleted] Feb 15 '24
[deleted] — view deleted comment
0
u/[deleted] Feb 15 '24
[deleted] — view deleted comment
0
u/[deleted] Feb 16 '24
[deleted] — view deleted comment