Github user mariogarcia commented on a diff in the pull request: https://github.com/apache/groovy/pull/439#discussion_r82523189 --- Diff: src/spec/doc/core-metaprogramming.adoc --- @@ -2868,6 +3015,90 @@ The difference is that when you use `assertScript`, the code in the `assertScrip unit test is executed*. That is to say that this time, the `Subject` class will be compiled with debugging active, and the breakpoint is going to be hit. +===== ASTMatcher + +Sometimes you may want to make assertions over AST nodes whether to filter those nodes, or to make sure a given +transformation has built the expected AST node. + +**Filtering nodes** + +For instance if you would like to apply a given transformation only to a specific set of AST nodes, you could +use **ASTMatcher** to filter these nodes. The following example shows how to transform a given expression to +another. Using **ASTMatcher** it looks for a specific expression `1 + 1` and it transforms it to `3`. That's why +I called it the `@Joking` example. + +First we create the `@Joking` annotation that only can be applied to methods: + +[source,groovy] +---- +include::{projectdir}/src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy[tags=jokingannotation,indent=0] +---- + +Then the transformation, that only applies an instance of `org.codehaus.groovy.ast.ClassCodeExpressionTransformer` +to all the expressions within the method code block. + +[source,groovy] +---- +include::{projectdir}/src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy[tags=jokingtransformation,indent=0] +---- +<1> Get the method's code statement and apply the expression transformer + +And then is when the **ASTMatcher** is used to apply the transformation only to those expressions matching --- End diff -- :+1:
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---