shekhargulati / 52-technologies-in-2016

Let's learn a new technology every week. A new technology blog every Sunday in 2016.
https://shekhargulati.com/
MIT License
7.19k stars 598 forks source link

Week 32: Groovy AST Transformations By Example #45

Open shekhargulati opened 8 years ago

shekhargulati commented 8 years ago

Please provide your feedback by posting a comment against this issue.

sdp0et commented 7 years ago

This was from a while ago, so I'll post some code snippets to refresh your memory. I've been wrestling with a problem trying to get a Transform to utilize the default value for the annotation parameters when empty.

`import playground.Hash

    @Hash
    class Foo{
        String msg`

where in Hash: String algorithm() default "SHA1"; the AnnotationNode will have null for .getMember("algorithm") In ToHashAdderAstTransformation you get around this by checking for null and providing the default value again: List<ASTNode> astNodes = new AstBuilder() .buildFromString(String.format(hashString, classNode.getName(), hashProvider != null ? hashProvider.getValue() : "SHA1"));

Is there a way to use the actual default value set in the Annotation class? What is the point of supplying it if it is not available to the Transform class? Thanks for any help you can provide.