Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Apache Groovy
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Prototype extension=== Groovy offers support for [[Prototype-based programming|prototype extension]] through <code>ExpandoMetaClass</code>, Extension Modules (only in Groovy 2), Objective-C-like [[Objective-C#Categories|Categories]] and <code>DelegatingMetaClass</code>.<ref>{{cite web |url=http://groovy.codehaus.org/JN3525-MetaClasses |title=JN3525-MetaClasses |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121001122409/http://groovy.codehaus.org/JN3525-MetaClasses |archive-date=2012-10-01 |url-status=dead }}</ref> <code>ExpandoMetaClass</code> offers a [[domain-specific language]] (DSL) to express the changes in the class easily, similar to [[Ruby (programming language)|Ruby's]] open class concept: <syntaxhighlight lang="groovy"> Number.metaClass { sqrt = { Math.sqrt(delegate) } } assert 9.sqrt() == 3 assert 4.sqrt() == 2 </syntaxhighlight> Groovy's changes in code through prototyping are not visible in Java, since each attribute/method invocation in Groovy goes through the metaclass registry. The changed code can only be accessed from Java by going to the metaclass registry. Groovy also allows overriding methods as <code>getProperty()</code>, <code>propertyMissing()</code> among others, enabling the developer to intercept calls to an object and specify an action for them, in a simplified [[Aspect-oriented programming|aspect-oriented]] way. The following code enables the class <code>java.lang.String</code> to respond to the <code>hex</code> property: <syntaxhighlight lang="groovy"> enum Color { BLACK('#000000'), WHITE('#FFFFFF'), RED('#FF0000'), BLUE('#0000FF') String hex Color(String hex) { this.hex = hex } } String.metaClass.getProperty = { String property -> def stringColor = delegate if (property == 'hex') { Color.values().find { it.name().equalsIgnoreCase stringColor }?.hex } } assert "WHITE".hex == "#FFFFFF" assert "BLUE".hex == "#0000FF" assert "BLACK".hex == "#000000" assert "GREEN".hex == null </syntaxhighlight> The Grails framework uses metaprogramming extensively to enable [[Grails (framework)#Persistence|GORM]] dynamic finders, like <code>User.findByName('Josh')</code> and others.<ref>{{cite web |url=http://www.slideshare.net/zenMonkey/metaprogramming-techniques-in-groovy-and-grails |title=Metaprogramming Techniques in Groovy and Grails |date=11 Jun 2009}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)