Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Become a polyglot with language injection

DZone's Guide to

Become a polyglot with language injection

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Today we'll take a tour through a unique feature found in IntelliJ IDEA called Language Injection.
Working on a project these days frequently makes you combine various languages and embed them into one another. Think of all the SQL or EJBQL queries scattered across your Java or XML files. Maybe your Java code returns snippets of HTML, CSS or XML and you use XPath a couple of times here and there.
Language Injection
has been designed to help you live in such an environment.

What is it?

 Look at the piece of code below:

This is how a String variable holding a piece of html code looks like in most IDEs. Now check this one out:

The same variable, but this time annotated with @Language. Annotating the variable made IntelliJ IDEA start-up the html support for the string value, inspect the code and offer us html-specific code assistance.

Now the IDE puts all its html editing power into our String Java variable and helps us code fast and possibly avoid mistakes.

How about all the other languages out there?

Consistently you can get this type of support for all the supported languages. Use the Alt + Enter key shortcut while positioned over a String value and select Inject language from the pop-up menu.

You get a pretty long list of languages you can inject.

 So you can edit, for example, CSS

or SQL

obviously in all the supported different dialects

How does it all integrate with my project?

The code inside strings with injected languages is attached to your project. When looking at a column inside an embedded SQL statement, for example, press Control + Q to get the column definition.

Or hit Control + B to jump right to its definition in an SQL script.

Anything else to show?

So far, we haven't left the Java editor. The last important thing to mention here - since String values are frequently used also inside XML, you can use language injection in XML, too. Look at the nice piece of JavaScript:

As for the @Language annotation itself, it follows the rules set by JSR-305 and so may one day become something other IDEs will recognize or even support. Until then, keep the annotations.jar file, that comes with IntelliJ IDEA, on your classpath for other IDEs to recognize them.

Feeling more polyglot already?

 

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}