[ 
https://issues.apache.org/jira/browse/GROOVY-10713?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17576767#comment-17576767
 ] 

Yih Tsern commented on GROOVY-10713:
------------------------------------

{quote}Afaik this transformer does also not visit inner classes.
{quote}
[~blackdrag] I don't think this will be a problem (at least for my project) 
since the inner class will be treated just like any other top-level classes (my 
global AST transformation class processes all classes).

 
{quote}Because ClassCodeExpressionTransformer is thought of more as "internal" 
API.
{quote}
[~blackdrag] That may be your intention when you created that class, but [it 
has since been documented as a public 
API|https://github.com/apache/groovy/commit/9910b6d409d6eb946a7b86de37819d0ce63b3190].
  😅

 
{quote}It does state "Transformed expressions are usually not visited." which 
is maybe not that informative but does hint that transformation and (complete) 
visitation are separate.
{quote}
[~emilles] I understand that javadoc to mean after an expression is 
transformed, it won't be re-visited e.g. the code does NOT do something like 
this:

 
{code:java}
public Expression transform(Expression exp) {
    ...
    def transformedExp = exp.transformExpression(this);
    transformedExp.visit(this); <-- I understand the Javadoc to mean it doesn't 
do something like this

    return transformedExp;
} {code}
Only [~blackdrag] can confirm whether I'm misunderstanding that, since [he was 
the one who wrote the 
javadoc|https://github.com/apache/groovy/commit/ef77a9384472b479eb8e6c67434fdf46f9a2708b#diff-ca42000c35d6b49b7738351b49858794f1d9287369731edd72e7331d9aed0b76].

 

 
h3. The wish

I was hoping for something like this in the javadoc and/or [the public 
doc|https://groovy-lang.org/metaprogramming.html#_classcodeexpressiontransformer]:

 
{panel:title=💡 Expressions in ClosureExpression will not be visited, for 
historical reason}


If you want to transform Expressions in ClosureExpressions, you need to do this:
{code:java}
class MyTransformer extends ClassCodeExpressionTransformer {
  ...
  @Override
  public Expression transform(Expression exp) {
     // your customer instanceof checks and transformations
     // ...

     if (exp instanceof ClosureExpression) {
        exp.visit(this);

        return exp;
     }

     // ...
  }
}
{code}
{panel}
To be honest, first-timers likely won't understand the significance of the note 
above, but hopefully it will serve as a hint if and when a related bug is 
logged for their implementation.

But I'm OK even if there's no such note, since the existence of this ticket (& 
the replies) should be enough to serve as a guide.

 

> ClassCodeExpressionTransformer ignoring Expressions within 
> ClosureExpression.code
> ---------------------------------------------------------------------------------
>
>                 Key: GROOVY-10713
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10713
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Yih Tsern
>            Assignee: Eric Milles
>            Priority: Minor
>
> E.g.
> {code:java}
> class SomeClass {
>   def someClosureField = {
>       // Expressions are NOT visited by 
> ClassCodeExpressionTransformer.transform(Expression)
>   }
>   
>   def someMethod() {
>     // Expressions are visited by 
> ClassCodeExpressionTransformer.transform(Expression)
>     3.times {
>       // Expressions are NOT visited by 
> ClassCodeExpressionTransformer.transform(Expression)
>     }
>     def someClosureVar = {
>       // Expressions are NOT visited by 
> ClassCodeExpressionTransformer.transform(Expression)
>     }
>   }
> }
> {code}
> Is this current behaviour by design or is it a bug?  If it is by design, it 
> seems to be quite a gotcha (also caused GROOVY-6932 & GROOVY-6434) - probably 
> should be noted in [the 
> doc|https://groovy-lang.org/metaprogramming.html#_classcodeexpressiontransformer]?
>  
> Below is my current workaround(??) - is this the expected way to work with 
> ClassCodeExpressionTransformer?
> {code:java}
> class MyAstTransformer extends ClassCodeExpressionTransformer {
>   Expression transform(Expression expression) {
>     // Some code to deal with specific expression classes...
>     ...
>     
>     if (expression instanceof ClosureExpression) {
>       expression.visit(this)
>     }
>     super.transform(expression);
>   }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to