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 > Tips And Resources For Creating DSLs in Groovy

Tips And Resources For Creating DSLs in Groovy

Jakub Holý user avatar by
Jakub Holý
·
Nov. 16, 11 · Java Zone · Interview
Like (0)
Save
Tweet
6.66K Views

Join the DZone community and get the full member experience.

Join For Free

Paul King had a very good presentation (last year’s slides) at JavaZone about why to use Domain-Specific Languages and how to create internal DSLs in Groovy. I’d like to list here few tips that he has mentioned but before we get to that, why would you want to create a DSL? Martin Fowler answers that in his Domain-Specific Languages book (2010). Some of the reasons are to have a higher-level, more focused and conscise representation that also domain experts can read and perhaps even write. You  have certainly already used a DSL such as regular expressions, CSS, SQL, Spock‘s BDD tests, build instructions in Gradle – these are rather technical but sometimes DSLs are also created to be used by business users, f.ex. for anti-malaria drug resistance simulation. (Want more DSLs in Groovy?).

Paul mentions one important thing – you can always make your DSL better, i.e. more fail-proof (case insensitive, support plural endings, …) and secure and more like a natural language but it all comes at a cost and you must evaluate when the cost overweights the benefit (beware the 80:20 rule).

Some of the Groovy DSL implementation tips:

  • Add methods to any object/class via metaprogramming: <class|instance>.metaclass.<method name or getProperty> = {MyType param -> … } – you can use ‘delegate’ to refer to the instance
    • notice that getProperty is called for anything that isn’t a method as in map['key'] == map.key – useful to implement e.g. 1.kg
    • Call ExpandoMetaClass.enableGlobally() to propagate methods added to Number also to Integer etc.
  • Use a GroovyShell and a custom Binding subclass overriding getVariable(String symbol) so as to return/create an object for unknown “variables” (ex.: “newOrder” -> new Order(), creation of vars for symbols like “h”, “km” etc. => 24.km/h)
  • Operator overloading: methods of specific names are invoked for operators (/ -> div, * -> multiply etc.)
  • Use a closure setting its delegate and configuring it the cosure’s resolve strategy to delegate unknown methods further to the delegate
  • Create a custom Closure subclass and use that for a closure instead of the default one
  • Use Groovy’s Categories (st. like a temporary mixin, activated via the use(category) { .. } form) to add localy methods to classes
  • Use Groovy Builders for tree-like DSLs (builder <-> method calls that take a Closure as argument)
  • Use map attributes to get st. resembling named atributes
  • Use GroovyShell with a custom classloader and compilation phase something to prevent calls of other static and instance methods than allowed

Resources

  • Book Groovy for Domain-Specific Languages (a review) – I hope I’ll get a chance to read this one
  • M. Fowler’s Domain-Specific Languages book
  • Groovy wiki: Writing Domain-Specific Languages
  • DZone: Unit-specific DSL using JScience, using Groovy
  • M. Fowler’s DSL Patterns Catalog

PS: The latest release of Groovy in Action should also contain a chapter on DSLs.

 

From http://theholyjava.wordpress.com/2011/11/13/tips-and-resources-for-creating-dlss-in-groovy/

Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Autowiring in Spring
  • 5 Steps to Strengthen API Security
  • 12 Kick-Ass Software Prototyping and Mockup Tools
  • ETL, ELT, and Reverse ETL

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