[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Groovy 2.5 @Macro ?

I'll try to add in some basic information. I didn't realise that wasn't covered at all.

On Sun, May 27, 2018 at 9:55 AM, mg <mgbiz@xxxxxxxxxxxx> wrote:
Hi Guillaume,

yes, there seems to be nothing on @Macro.


-------- Ursprüngliche Nachricht --------
Von: Guillaume Laforge <glaforge@xxxxxxxxx>
Datum: 26.05.18 21:15 (GMT+01:00)
Betreff: Re: Groovy 2.5 @Macro ?

On Sat, May 26, 2018 at 7:04 PM mg <mgbiz@xxxxxxxxxxxx> wrote:
Hi Cedric,

thank you for replying. I did try having the @Macro annotated in a (static) method in a seperate class first, then moved it closer to the test. I posted that  for brevity, and since I was banking on the author of the feature to set me straight...

The release notes ( currently have the following to say on the topic:

Macro support

With Groovy 2.5, you can write macros in Groovy!

Expressions and statements


Macro classes


AST matching


-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <cedric.champeau@xxxxxxxxx>
Datum: 26.05.18 18:12 (GMT+01:00)
Betreff: Re: Groovy 2.5 @Macro ?

I didn't check, but I _think_ you can't define a macro and use it in the same file.

Le sam. 26 mai 2018 à 17:15, MG <mgbiz@xxxxxxxxxxxx> a écrit :
I would have expected a quick "you can't use it like that / you just have to / here is some documentation" reply...
Then let me rephrase my question: Why are these Groovy 2.5 tests green:

On 26.05.2018 00:00, MG wrote:
Hi guys,

giving the new Groovy 2.5 macro functionality a spin, and would have expected the code below to replace the "call" to nv(x) with the AST _expression_ created in the method, i.e. returning the name of the "passed" variable. Instead no macro magic happens, and the compilation accordingly fails with "groovy.lang.MissingMethodException: No signature of method: groovy.GroovyMacroSpike.nv() is applicable for argument types: (Integer) values: [123]":
import org.codehaus.groovy.ast.expr._expression_
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.macro.runtime.Macro
import org.codehaus.groovy.macro.runtime.MacroContext
import org.junit.Ignore
import org.junit.Test

import static

class GroovyMacroSpike {
  void nvTest() {
    final x = 123
    assert x == 123
    assert nv(x) == "x"

  _expression_ nv(MacroContext ctx, VariableExpression variable) {
    return constX(variable.getName());
What is missing to make this work ?

Guillaume Laforge
Apache Groovy committer & PMC Vice-President
Developer Advocate @ Google Cloud Platform