Groovy - AST Transformations, a practical example - groovy

AST Transformations are implemented in Groovy. What's a practical example of using an AST Transformation?

This page has practical examples of how to use:
#Singleton, #Lazy, #Immutable, #Delegate, #Newify, #Category, #Mixin, #PackageScope

Scenarios like:
Authorization Checking - Security by checking role from context
Print Parameter values with which the method is called
Asserts Parameters are not null or any validation
Check various entry-conditions/Pre-Conditions of the method
Generic AOP style BeforeMethod() implementation
Create a method and mark it to run as runnable or main method
Take a look at my blog post at AST AOP and AST Param not null
Hope this helps!

Most of the practical examples of using the AST transformations are provided on that page. I've often used #Delegate to delegate to another class or #Lazy for lazy loading. #Grab is great for pulling in dependencies from a Maven/ivy repository. All of those are based on AST transformations and are part of the core language.
You can use transformations directly too but most of the stuff you would want them for is already built. You can do things that you might want to do with AOP in other languages.

Related

Groovy static analysis for finding variable modifications

I have a simple task which requires finding the modification of variables in a given code. This will be a static analysis. For instance, given a variable (e.g., age), I would like to create a list or tree (a data structure) that gives me what modifies this variable and preferably the function name that makes the modification (as a return) or any other auxiliary information. I start writing my script, yet I see that it's very error-prone as I need to consider many cases such as nested loops, etc.
Would you suggest me where to start?
If the code to be analyzed happens to be Groovy code then you could write an AST transformation (probably a global one) that walks the code and obtains the information you seek.
The Groovy documentation site has a section on AST Transformations, have a look at http://groovy-lang.org/metaprogramming.html#_compile_time_metaprogramming
This page describes existing AST xforms and how you can develop your own. I'd recommend browsing the code that implements the standard AST xforms such as #Immutable, #Cannonical, and others.
CodeNarc (http://codenarc.sourceforge.net/) is a static code analyzer for Groovy code, inspired by PMD. It also relies on AST xforms.
GContracts (https://github.com/andresteingress/gcontracts) is another tool implemented using AST xforms. These two can serve as a basis for understanding more about AST transformations.
OTOH if the analyzed code happens to be Java then AST transformations will not help you.

How to write junits for anonymous inner class?

I have searched for writing junits for anonymous inner class, but cannot find any solution. Is there a simple example for writing junits for anonymous inner class using Mockito?
An anonymous inner class is an implementation detail, don't try and "write a test for it".
Instead write a test that describes the behaviour of the class that uses the inner class. It should be possible to fully exercise the code of the inner class from the parent's public interface, otherwise why is the code there?
Should ever you wish to change the implementation of this functionality the test will now support you in making that change. If you somehow "tested the inner class" you would need to also rewrite the test and it would make your job harder rather than easier.

Source code of Groovy ASTNode

I implemented some AST transformations that are applied at compile time and for logging purposes I would like to emit "source" code that reflects injected AST nodes. The toString()/getText() methods return quite unreadable structure that quickly becomes complicated for long expressions.
The closest thing I can think of is the AstNodeToScriptAdapter which can be found here

What programming languages will let me manipulate the sequence of instructions in a method?

I have an upcoming project in which a core requirement will be to mutate the way a method works at runtime. Note that I'm not talking about a higher level OO concept like "shadow one method with another", although the practical effect would be similar.
The key properties I'm after are:
I must be able to modify the method in such a way that I can add new expressions, remove existing expressions, or modify any of the expressions that take place in it.
After modifying the method, subsequent calls to that method would invoke the new sequence of operations. (Or, if the language binds methods rather than evaluating every single time, provide me a way to unbind/rebind the new method.)
Ideally, I would like to manipulate the atomic units of the language (e.g., "invoke method foo on object bar") and not the assembly directly (e.g. "pop these three parameters onto the stack"). In other words, I'd like to be able to have high confidence that the operations I construct are semantically meaningful in the language. But I'll take what I can get.
If you're not sure if a candidate language meets these criteria, here's a simple litmus test:
Can you write another method called clean which:
accepts a method m as input
returns another method m2 that performs the same operations as m
such that m2 is identical to m, but doesn't contain any calls to the print-to-standard-out method in your language (puts, System.Console.WriteLn, println, etc.)?
I'd like to do some preliminary research now and figure out what the strongest candidates are. Having a large, active community is as important to me as the practicality of implementing what I want to do. I am aware that there may be some unforged territory here, since manipulating bytecode directly is not typically an operation that needs to be exposed.
What are the choices available to me? If possible, can you provide a toy example in one or more of the languages that you recommend, or point me to a recent example?
Update: The reason I'm after this is that I'd like to write a program which is capable of modifying itself at runtime in response to new information. This modification goes beyond mere parameters or configurable data, but full-fledged, evolved changes in behavior. (No, I'm not writing a virus. ;) )
Well, you could always use .NET and the Expression libraries to build up expressions. That I think is really your best bet as you can build up representations of commands in memory and there is good library support for manipulating, traversing, etc.
Well, those languages with really strong macro support (in particular Lisps) could qualify.
But are you sure you actually need to go this deeply? I don't know what you're trying to do, but I suppose you could emulate it without actually getting too deeply into metaprogramming. Say, instead of using a method and manipulating it, use a collection of functions (with some way of sharing state, e.g. an object holding state passed to each).
I would say Groovy can do this.
For example
class Foo {
void bar() {
println "foobar"
}
}
Foo.metaClass.bar = {->
prinltn "barfoo"
}
Or a specific instance of foo without effecting other instances
fooInstance.metaClass.bar = {->
println "instance barfoo"
}
Using this approach I can modify, remove or add expression from the method and Subsequent calls will use the new method. You can do quite a lot with the Groovy metaClass.
In java, many professional framework do so using the open source ASM framework.
Here is a list of all famous java apps and libs including ASM.
A few years ago BCEL was also very much used.
There are languages/environments that allows a real runtime modification - for example, Common Lisp, Smalltalk, Forth. Use one of them if you really know what you're doing. Otherwise you can simply employ an interpreter pattern for an evolving part of your code, it is possible (and trivial) with any OO or functional language.

During Groovy AST Transformation, some FieldNodes wrongly say they have 0 annotations

Some FieldNodes wrongly say they have 0 annotations during an AST Transformation. My AST Transformation is during the CLASS_GENERATION phase. Why does it do this, and how can I get the missing annotations to show up?
EDIT: The problem mainly seems to happen on super classes of the class that the AST Transformation is running on.
Why CLASS_GENERATION? Are you just analyzing the code? Nevertheless, I haven't heard anyone using this phase for a transform.
A general guideline is to use phase CONVERSION for transforms that don't need much semantic (e.g type) information, and phase CANONICALIZATION for the rest.
GroovyConsole's AST browser (open with CTRL+T) is a handy tool to get an idea of what the AST looks like after each phase. Maybe it will help you find the problem.

Resources