title: CGLib - create a bean with some fields and place annotations on them?
body: <p>is it possible to generate a bean class that have a field annotated with specified annotations? I know that a bean can be created but what about annotations...? I cannot find anything about it so I have strong doubts about it and the only way to make sure is to ask here...</p>
  
  <p>// I found something which may be helpful... please verify this code. It uses javassist capabilities.</p>
  
  <pre><code>    // pool creation
      ClassPool pool = ClassPool.getDefault();
      // extracting the class
      CtClass cc = pool.getCtClass(clazz.getName());
      // looking for the method to apply the annotation on
      CtField fieldDescriptor = cc.getDeclaredField(fieldName);
      // create the annotation
      ClassFile ccFile = cc.getClassFile();
      ConstPool constpool = ccFile.getConstPool();
      AnnotationsAttribute attr = new AnnotationsAttribute(constpool,
              AnnotationsAttribute.visibleTag);
  
      Annotation annot = new Annotation("sample.PersonneName", constpool);
      annot.addMemberValue("name",
              new StringMemberValue("World!! (dynamic annotation)", ccFile.getConstPool()));
      attr.addAnnotation(annot);
  
      // add the annotation to the method descriptor
      fieldDescriptor.getFieldInfo().addAttribute(attr);
  </code></pre>
  
  <p>The problem with it is that I don't know a way to apply existing annotation on the newly created type... is there a way to do that?</p>