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 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
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Groovy: Resolve Strategies

Groovy: Resolve Strategies

Victor Savkin user avatar by
Victor Savkin
·
Mar. 28, 11 · Interview
Like (0)
Save
Tweet
Share
12.31K Views

Join the DZone community and get the full member experience.

Join For Free

A few weeks ago I had to deal with an interesting bug in one of my DSLs. I had a simple builder overriding propertyMissing and saving all missed properties to map inside the builder:

class SampleBuilder {

private savedObjects = [:]

void propertyMissing(String name, value){
savedObjects[name] = value
}

void printSavedObjects(){
println 'Saved Objects:'
savedObjects.each {
println "${it.key} = ${it.value}"
}
}

static build(Closure c){
def builder = new SampleBuilder()
c.delegate = builder
c()
builder
}
}

I always used that builder inside another class:

class Application {
static void main(String[] args){
def res = SampleBuilder.build {
name1 = 'value1'
name2 = 'value2'
}
res.printSavedObjects()
}
}

The output of the this chunk of code is:

Saved Objects:
name1 = value1
name2 = value2

But what will happen if I use my SampleBuilder in a groovy script?

def res = SampleBuilder.build {
name1 = 'value1'
name2 = 'value2'
}
res.printSavedObjects()

No objects will be saved in that map:

Saved Objects:

It may look weird but if you think about it a bit more it kind of makes sense. To understand why does it happen we need to know how Groovy resolves all missing methods and properties.

Groovy tries to find it by looking in the delegate of the closure or the owner of the closure or using both. You can also specify to use none of them and use Closure’s meta class to resolve missing methods and properties.

These are descriptions of all strategies from http://groovy.codehaus.org/api/groovy/lang/Closure.html:

  • DELEGATE_FIRST. With this resolveStrategy set the closure will attempt to resolve property references to the delegate first.

  • DELEGATE_ONLY. With this resolveStrategy set the closure will attempt to resolve property references to the delegate first.

  • OWNER_FIRST.With this resolveStrategy set the closure will attempt to resolve property references to the owner first

  • OWNER_ONLY. With this resolveStrategy set the closure will resolve property references to the owner only and not call the delegate at all.

  • TO_SELF. With this resolveStrategy set the closure will resolve property references to itself and go through the usual MetaClass look-up process.

OWNER_FIRST is a default strategy used by Groovy.

Let’s look one more time at our SampleBuilder. Why does it behave differently inside a class and inside a groovy script? First of all, let’s look at the way it works inside a class. As the default strategy is OWNER_FIRST it tries to find a missed property in Application class. It can’t find it and that is why it uses delegate property to resolve it. But in a groovy script you can always define global properties using this syntax:

key = ‘value’

Because all our properties are resolved by owner our delegate won’t be invoked. To make it work we just need to change the resolve strategy of our closure to DELEGATE_FIRST:

def builder = new SampleBuilder()
c.delegate = builder
c.resolveStrategy = Closure.DELEGATE_FIRST
c()
builder

If you run that groovy script again you will see what you would have expected to see in the first place:

Saved Objects:
name1 = value1
name2 = value2

From http://victorsavkin.com/post/4135607663/groovy-resolve-strategies

Groovy (programming language) Property (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Beyond Coding: The 5 Must-Have Skills to Have If You Want to Become a Senior Programmer
  • How To Perform Local Website Testing Using Selenium And Java
  • Navigating Progressive Delivery: Feature Flag Debugging Common Challenges and Effective Resolution
  • When Should We Move to Microservices?

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

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

Let's be friends: