DZone
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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • Evolving Domain-Specific Languages
  • Integrating Java and npm Builds Using Gradle - Groovy or Kotlin DSL
  • How To Approach Dependency Management in Java [Video]

Trending

  • Continuous Integration vs. Continuous Deployment
  • New Free Tool From Contrast Security Makes API Security Testing Fast and Easy
  • Agile Estimation: Techniques and Tips for Success
  • Hugging Face Is the New GitHub for LLMs
  1. DZone
  2. Coding
  3. Languages
  4. Custom Groovy DSL support

Custom Groovy DSL support

Vaclav Pech user avatar by
Vaclav Pech
·
Nov. 11, 09 · Interview
Like (0)
Save
Tweet
Share
19.24K 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.

Related

  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • Evolving Domain-Specific Languages
  • Integrating Java and npm Builds Using Gradle - Groovy or Kotlin DSL
  • How To Approach Dependency Management in Java [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: