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 > Custom Groovy DSL support

Custom Groovy DSL support

Vaclav Pech user avatar by
Vaclav Pech
·
Nov. 11, 09 · Java Zone · Interview
Like (0)
Save
Tweet
18.54K Views

Join the DZone community and get the full member experience.

Join For Free

This time I have a little surprise up in my sleeve. An experimental feature which we would like to get some feedback on from you. Look carefully at the IntelliJ IDEA Community Edition screenshot below.

 

What you see is me editing Groovy code with some sort of currency DSL in it. The code completion dialog is visible, showing me the options ... WAIT a moment! The dialog is proposing currencies as valid properties to numbers! Currencies like eur or usd are being suggested! And once typed, valid currencies are no longer underlined as unresolvable, while invalid currency symbols will still get their underline.

What? Have we taught IntelliJ IDEA the world's currencies? No way, it's just me leveraging one of the many new Groovy features available in Maia, the next version of IntelliJ IDEA, available currently under EAP.
IntelliJ IDEA 9 will allow you to describe your custom DSLs with a Groovy script. The script having the .gdsl extension then needs to be put on project's classpath (by you or a library) so that IDEA can see it. The following simple script did the whole magic for the currencies.

def ctx1 = context(ctype: "java.lang.Number")

contributor(ctx1) {
property name: "eur", type: "test.Money"
property name: "usd", type: "test.Money"
property name: "chf", type: "test.Money"
property name: "rur", type: "test.Money"
}

IDEA picks up the file and starts recognizing the mentioned properties in your code right after you save the gdsl file.

Methods

It's all not only about properties. Methods can be added as well. For example, a quite handy enhancement of the ReentrantLock class with a withLock() method to safely lock and unlock the reentrant lock before and after use, which can be defined like this:

ReentrantLock.metaClass.withLock = {nestedCode ->
delegate.lock()
try {
nestedCode()
} finally { delegate.unlock() }
}

will be recognized by IDEA, if you add an extra .gdsl script file to your project.

def ctx2 = context(ctype: "java.util.concurrent.locks.ReentrantLock")

contributor(ctx2) {
method name: 'withLock', type: 'void', params: [closure: { } ]
}

Now the withLock() method is recognized and properly auto-completed for you.

Generic methods

Since the .gdsl files are normal Groovy scripts, using iterations you can quickly build up whole families of dynamic methods to feed IDEA with.

def ctx3 = context(ctype: "demo.introduction.domain.Company")

contributor(ctx3) {
['Senior', 'Expert', 'Junior'].each {
method name: "findAll${it}Employees", type: 'java.util.Collection'
}
}

Now you get support for dynamic methods with quite generic names as well. And you can easily spot a typo, for example. Can you see the misspelled method name?



Libraries may also come with their .gdsl files bundled and so you get DSL code-assistance out-of-the-box.

Conclusion

This whole feature is still experimental and may change before the final IntelliJ IDEA 9 release, but we wanted to let you know to stimulate feedback early enough. Please, read more at the JetBrains wiki or on mrhaki's blog. Whatever ideas you have about the feature, please, let us know, so we could tune it exactly for your needs.

Enjoy Groovy coding.

 

Domain-Specific Language intellij Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • Composable Architecture
  • Which JVM Version Is the Fastest?
  • 12 Kick-Ass Software Prototyping and Mockup Tools

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