[prev in list] [next in list] [prev in thread] [next in thread]
List: groovy-user
Subject: Re: [groovy-user] Using 'any' as a method name in a DSL
From: Paolo Di Tommaso <paolo.ditommaso () gmail ! com>
Date: 2014-09-17 7:17:42
Message-ID: CADgKzdwxDcWYFzSdn00xiQBxZ5WF2Y5QAbd3=UDT8_NDr9Noyg () mail ! gmail ! com
[Download RAW message or body]
I not sure, but the problem could be that "any" is added as a extension
method.
You may need to use an AST transformation to handle it, though it would
require some extra coding
http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations
p
On Wed, Sep 17, 2014 at 4:15 AM, Keith Suderman <suderman@anc.org> wrote:
> I am implementing a very simple DSL and I want to allow the following:
>
> container {
> …
> any {
> …
> }
> }
>
> The problem is that ‘any' is a method added by Groovy to java.lang.Object
> and I can not figure out how to override that method in my DSL. I have
> implemented ‘any' methods in every delegate and/or metaClass that I can
> think of. I have tried catching calls to ‘any' in invokeMethod and nothing
> works. I am hoping that I am missing something obvious.
>
> I am using:
> Groovy Version: 2.2.2 JVM: 1.7.0_25 Vendor: Oracle Corporation OS: Mac OS X
>
> Here is a Groovy script that illustrates my problem:
>
> /*
> * AnyProblems.groovy
> */
> String source = """
> container {
> // Replace 'term' with 'any' to see the problem
> term {
> uri 'http://www.example.com/term'
> description 'the first term'
> }
> }
> """
>
> GroovyShell shell = new GroovyShell()
> Script script = shell.parse(source)
> ExpandoMetaClass meta = new ExpandoMetaClass(script.class, false)
> meta.container = { Closure cl ->
> cl.resolveStrategy = Closure.DELEGATE_FIRST
> cl.delegate = new ContainerDelegate()
> cl()
> }
> meta.initialize()
> script.metaClass = meta
> script.run()
> return
>
> class ContainerDelegate {
> void methodMissing(String name, args) {
> println "container.$name"
> Closure cl = args[0]
> cl.resolveStrategy = Closure.DELEGATE_FIRST
> cl.delegate = new TermDelegate()
> cl()
> }
> def invokeMethod(String name, args) {
> if ('any' != name) {
> println "Invoking super method $name"
> return super.invokeMethod(name, args)
> }
> println "WOOHOO invoking any"
> }
> // I have tried every variation of this method signature
> // that I can think of.
> def any(Closure cl) {
> println "WOOHO any method."
> }
> }
> class TermDelegate {
> void uri(String uri) { println uri }
> void description(String text) { println text }
> }
>
> ------------------------------
> Research Associate
> Department of Computer Science
> Vassar College
> Poughkeepsie, NY
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>
[Attachment #3 (text/html)]
<div dir="ltr">I not sure, but the problem could be that "any" is added as \
a extension method. <div><br></div><div>You may need to use an AST transformation to \
handle it, though it would require some extra coding </div><div><a \
href="http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations">h \
ttp://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations</a><br></div><div><br></div><div>p</div></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 17, 2014 at 4:15 AM, \
Keith Suderman <span dir="ltr"><<a href="mailto:suderman@anc.org" \
target="_blank">suderman@anc.org</a>></span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">I am implementing a very simple DSL and I want to allow the \
following:<br> <br>
container {<br>
…<br>
any {<br>
…<br>
}<br>
}<br>
<br>
The problem is that ‘any' is a method added by Groovy to java.lang.Object and I can \
not figure out how to override that method in my DSL. I have implemented ‘any' \
methods in every delegate and/or metaClass that I can think of. I have tried \
catching calls to ‘any' in invokeMethod and nothing works. I am hoping that I am \
missing something obvious.<br> <br>
I am using:<br>
Groovy Version: 2.2.2 JVM: 1.7.0_25 Vendor: Oracle Corporation OS: Mac OS X<br>
<br>
Here is a Groovy script that illustrates my problem:<br>
<br>
/*<br>
* AnyProblems.groovy<br>
*/<br>
String source = """<br>
container {<br>
// Replace 'term' with 'any' to see the problem<br>
term {<br>
uri '<a href="http://www.example.com/term" \
target="_blank">http://www.example.com/term</a>'<br> description 'the first \
term'<br> }<br>
}<br>
"""<br>
<br>
GroovyShell shell = new GroovyShell()<br>
Script script = shell.parse(source)<br>
ExpandoMetaClass meta = new ExpandoMetaClass(script.class, false)<br>
meta.container = { Closure cl -><br>
cl.resolveStrategy = Closure.DELEGATE_FIRST<br>
cl.delegate = new ContainerDelegate()<br>
cl()<br>
}<br>
meta.initialize()<br>
script.metaClass = meta<br>
script.run()<br>
return<br>
<br>
class ContainerDelegate {<br>
void methodMissing(String name, args) {<br>
println "container.$name"<br>
Closure cl = args[0]<br>
cl.resolveStrategy = Closure.DELEGATE_FIRST<br>
cl.delegate = new TermDelegate()<br>
cl()<br>
}<br>
def invokeMethod(String name, args) {<br>
if ('any' != name) {<br>
println "Invoking super method \
$name"<br>
return super.invokeMethod(name, args)<br>
}<br>
println "WOOHOO invoking any"<br>
}<br>
// I have tried every variation of this method signature<br>
// that I can think of.<br>
def any(Closure cl) {<br>
println "WOOHO any method."<br>
}<br>
}<br>
class TermDelegate {<br>
void uri(String uri) { println uri }<br>
void description(String text) { println text }<br>
}<br>
<br>
------------------------------<br>
Research Associate<br>
Department of Computer Science<br>
Vassar College<br>
Poughkeepsie, NY<br>
<br>
<br>
<br>
---------------------------------------------------------------------<br>
To unsubscribe from this list, please visit:<br>
<br>
<a href="http://xircles.codehaus.org/manage_email" \
target="_blank">http://xircles.codehaus.org/manage_email</a><br> <br>
<br>
</blockquote></div><br></div>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic