DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Become a polyglot with language injection

Become a polyglot with language injection

Vaclav Pech user avatar by
Vaclav Pech
·
Sep. 01, 09 · Java Zone · Interview
Like (0)
Save
Tweet
10.72K Views

Join the DZone community and get the full member experience.

Join For Free

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?

 

intellij Injection Polyglot (computing) sql Strings Data Types Java (programming language) XML Embedded SQL

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Migrating From Heroku To Render
  • How to Test JavaScript Code in a Browser
  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java
  • The Engineer’s Guide to Creating a Technical Debt Proposal

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo