Advertisement
Elotin

Groovy annotation dynamic

Jan 24th, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.97 KB | None | 0 0
  1.  
  2. import javax.xml.bind.annotation.XmlElement
  3.  
  4. @GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
  5. class WebserviceAnnotationModifier implements ASTTransformation {
  6.     @Override
  7.     void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
  8.  
  9.         if (!astNodes) return
  10.         if (!astNodes[0] || !astNodes[1]) return
  11.         if (!(astNodes[0] instanceof AnnotationNode)) return
  12.         if (!(astNodes[1] instanceof ClassNode)) return
  13.         ClassNode node = (ClassNode)astNodes[1]
  14.         List fields = node.getFields()
  15.         fields.each {FieldNode field ->
  16.             field.addAnnotation(ClassHelper.make(new XmlElement.DEFAULT()));
  17.         }
  18.     }
  19. }
  20.  
  21. @Retention(RetentionPolicy.SOURCE)
  22. @Target([ElementType.TYPE])
  23. @GroovyASTTransformationClass(classes =[WebserviceAnnotationModifier])
  24. public @interface WebresourceAnnotation{}
  25.  
  26.  @WebresourceAnnotation
  27.    class TestPerson{
  28.         String name;
  29.         String lastName;
  30.         int Age
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement